-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
29c6bf5
commit 74b8eb8
Showing
4 changed files
with
97 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"files.associations": { | ||
"iosfwd": "cpp", | ||
"string": "cpp", | ||
"ostream": "cpp" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#include "parser.h" | ||
|
||
int ctoi(char a) { | ||
char *c = &a; | ||
int i = atoi(c); | ||
return i; | ||
} | ||
|
||
string Parser::parseFunction(string functionin, int x) { | ||
string function = functionin; | ||
for (int i = 0; i < function.length(); i++) { | ||
if (function[i] == '*') { | ||
int mult1 = 0; | ||
int mult2 = 0; | ||
if (function[i - 1] == 'x') mult1 = x; | ||
else mult1 = ctoi(function[i - 1]); | ||
if (function[i + 1] == 'x') mult2 = x; | ||
else mult2 = ctoi(function[i + 1]); | ||
function[i] = to_string(mult1 * mult2)[0]; | ||
function[i + 1] = ' '; | ||
function[i - 1] = ' '; | ||
i--; | ||
} | ||
else if (function[i] == '/') { | ||
int div1 = 0; | ||
int div2 = 0; | ||
if (function[i - 1] == 'x') div1 = x; | ||
else div1 = ctoi(function[i - 1]); | ||
if (function[i + 1] == 'x') div2 = x; | ||
else div2 = ctoi(function[i + 1]); | ||
function[i] = to_string(div1 / div2)[0]; | ||
function[i + 1] = ' '; | ||
function[i - 1] = ' '; | ||
i--; | ||
} | ||
remove_if(function.begin(), function.end(), isspace); | ||
} | ||
for (int i = 0; i < function.length(); i++) { | ||
if (function[i] == '+') { | ||
int add1 = 0; | ||
int add2 = 0; | ||
if (function[i - 1] == 'x') add1 = x; | ||
else add1 = ctoi(function[i - 1]); | ||
if (function[i + 1] == 'x') add2 = x; | ||
else add2 = ctoi(function[i + 1]); | ||
function[i] = to_string(add1 + add2)[0]; | ||
function[i + 1] = ' '; | ||
function[i - 1] = ' '; | ||
i--; | ||
} | ||
else if (function[i] == '-') { | ||
int sub1 = 0; | ||
int sub2 = 0; | ||
if (function[i - 1] == 'x') sub1 = x; | ||
else sub1 = ctoi(function[i - 1]); | ||
if (function[i + 1] == 'x') sub2 = x; | ||
else sub2 = ctoi(function[i + 1]); | ||
function[i] = to_string(sub1 - sub2)[0]; | ||
function[i + 1] = ' '; | ||
function[i - 1] = ' '; | ||
i--; | ||
} | ||
remove_if(function.begin(), function.end(), isspace); | ||
} | ||
return function; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#include <string> | ||
using namespace std; | ||
|
||
class Parser | ||
{ | ||
public: | ||
static string parseFunction(string functionin, int x); | ||
}; |