#include <iostream>
#include <sstream>
usingnamespace std;
int getNumber(stringstream& parser,int& number){
char temp;
parser >> temp;
if(isdigit(temp)){
parser.putback(temp);
parser >> number;
return number;
}
return -1;
}
int parse(int x,string& y){
stringstream parser;
char temp;
int number;
int result;
parser << y;
cout << "parser value = " << parser.str() << endl;
result = getNumber(parser,number);
while(!parser.eof()){
parser >> temp;
switch(temp){
case'+':
getNumber(parser,number);
result+=number;
break;
case'-':
getNumber(parser,number);
result-=number;
break;
case'*':
getNumber(parser,number);
result*=number;
break;
case'/':
getNumber(parser,number);
result /= number;
break;
case'x':
result *= x;
break;
}
}
cout << "RESULT = " << result << endl;
return result;
}
int input(){
int x;
string y;
cout << "enter equation for y " << endl;
cout << "for example - y = 5x + 5" << endl;
getline(cin,y);
cout << "enter number for x" << endl;
cin >> x;
int result = parse(x,y);
}
int main()
{
input();
return 0;
}
ok so finished program above,I think i will also need to implement a format checking function to make sure the format is correct before proceeding with the parsing and calculation,
any tips on how I can improve it are more than welcome :)
enter equation for y
for example - y = 5x + 5
2x + 5x + 3
enter number for x
2
parser value = 2x + 5x + 3
Y Value = 21
now I get what you mean ^^,reason being the program doesn't know the rules of arithmetic(multiplication before addition) so what it is really doing is 2 by 2 = 4 + 5 = 9 * 2 = 18 + 3 = 21
also tried 10x - 5 - 1 subbed 0 in for x again and got 4 which is the correct answer
I'm not sure how you got 4 being the correct answer here. Even with a strange order of operations, you should still get -6. Did your program read '-' and interpret it as '+'? Or did you possibly mistype your example when you posted?
sorry Doug made a crucial typo there meant to say 10x + 5 - 1 gave me 4
I added a function to check if a value has an x after it but now my while seems to be stuck in an infinite loop,I added a debug statement that printed temp and '+' keeps on being printed so it is obviously not reading any more characters after '+' or keeps on reading the same '+'
So to answer my own question the reason why I was stuck in an infinite loop was because of my hasX function I was putting a value back into the empty stream the only strange thing is that temp was always the value 'x'
so here is the updated code (which works so far,does multiplication before addition and subtraction,still need to add in division before addition and subtraction)
only problem is this code is very buggy as my hasX function will check the stream to see if the next value is 'x' but when the stream is empty it is checking the memory address past the stream which contains a junk value first time it was '(' on the next execution it was '!',so does the stringstream have a hasNext() function to check if the stream has another character?
(if that junk value just by coincidence turned out to be 'x' it would essentially break my program)
enter equation for y
for example - y = 5x + 5
2x + 3 + 2x
enter number for x
2
parser value = 2x + 3 + 2x
ITERATION4
ITERATION7
ITERATION7
ITERATION7
ITERATION7
Y Value = 7
the reason being the hasX function reads the plus sign and since it is a '+' it does not put it back into the stringstream so yeah I need a way to return false if there is no next token in the stream