
Stacks C++
Code:
#include <iostream>#include <stack>
#include <string>
using namespace std;
void printMenu();
int evaluate(string exp);
int main()
{
char choice;
int result;
string line;
printMenu();
choice = 'Z';
do
{
cout << "What action would you like to perform?\n";
cin.get(choice);
cin.ignore(20, '\n'); //to flush '\n'
choice = toupper(choice);
switch(choice)
{
case 'E':
cout << "Enter an arithmetic expression:" << endl;
getline(cin, line);
result = evaluate(line);
cout << "The evaluated value is: " << result << endl;
break;
case 'Q': //Quit
break;
case '?': //Display Menu
printMenu();
break;
default:
cout << "Unknown action\n";
break;
}
} while (choice != 'Q');
return 0;
}
//Print the menu to a user
void printMenu()
{
cout << "Choice\t\tAction\n";
cout << "\t\t\n";
cout << "E\t\tEnter an arithmetic expression to evaluate\n";
cout << "Q\t\tQuit\n";
cout << "?\t\tDisplay Help\n\n";
return;
}
//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++;
}
I need to finish "int evaluate(string exp)" What am I missing to return the proper results? For example I need (((1+2)/3)*(64)) to return 2.

> if((exp[i]='0')
Well you got == in the first one, thereafter, you used = where you should be using ==
And also look up the isdigit() function in ctype.

Ok switched the = to == But still unsure of how to return my results.

Well the first thing to do is work out what to do with ')'
Shuntingyard algorithm  Wikipedia, the free encyclopedia

You must first understand why the code you have is pushing things onto stacks, because although what you need to write might involve doing that, you're not doing it in any kind of overly purposeful manner.
What I see so far is like someone wanting to make a car who thinks "hey cars have wheels, and wheels are round, I know I'll make a bunch of round things". I'm sorry, but that's truthfully just the feeling I get from looking at this code.
You also need to post more information about your requirements. Do you only ever need to deal with single digit numbers, for example?
Do you need to worry about operator precedence?
Plus about another 20 questions that I'm not going to bother writing out, because hopefully you will just post the description of what you are meant to do. One example does not constitute a complete set of requirements.
Have you looked up isdigit, and can you then see where it might be useful in place of something you have already?
It could help to do some research about parsing, so that you get a feeel for what you need to do, and learn any relevant terminology that you don't know of yet.