Stack questions
I need to finish "int evaluate(string exp)" What am I missing to return the proper results? For example I need (((1+2)/3)*(6-4)) to return 2.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
//The evaluate function takes a string containing an arithmetic expression,
//evaluates it,and returns its result
int evaluate(string exp)
{
stack<char> parStack;
stack<int> numStack;
stack<char> opStack;
int j = exp.size();
int i=0;
char x;
while (i<j)
{
if(exp[i] == '(')
{
parStack.push(exp[i]);
cout << exp[i] << endl;
}
if((exp[i]=='0') || (exp[i]=='1') || (exp[i]=='2') || (exp[i]=='3') || (exp[i]=='4') || (exp[i]=='5') || (exp[i]=='6') || (exp[i]=='7') || (exp[i]=='8') || (exp[i]=='9'))
{
numStack.push(exp[i]);
}
if((exp[i] == '+') || (exp[i] == '-') || (exp[i] == '*') || (exp[i] == '/') || (exp[i] == '%'))
{
opStack.push(exp[i]);
}
i++;
}
return 0;
}
|
Topic archived. No new replies allowed.