Stack (postfix evaluator)

#include <iostream>
#include <stack>
#include <string.h>
#include <stdlib.h>

using namespace std;

bool isOperator(char *);
int evaluate(int, int, char *);

int main() {
char myStr[3];
stack <int> symbolStack;
int i, op1, op2;

cin>> myStr;
while (strcmp(myStr,";")){
if (atoi(myStr)) {
i=atoi(myStr);
symbolStack.push(i);

}
else if (isOperator(myStr)) {
op1=symbolStack.top();
symbolStack.pop();

op2=symbolStack.top();
symbolStack.pop();

symbolStack.push(evaluate(op1,op2,myStr));

}
else {
cout << "Unknown symbol" <<myStr << "\n";
return 1;

}
cin >> myStr;

}
cout <<symbolStack.top();

return 0;
}

bool isOperator(char *tmp){
if(!(strcmp(tmp,"+")) || !(strcmp(tmp,"-")) || !(strcmp(tmp,"*")) || !(strcmp(tmp,"/")))
return true;

else return false;

}

int evaluate(int op1, int op2, char *oprtr ){

if (!(strcmp(oprtr,"+"))) return op1 + op2;
else if (!(strcmp(oprtr,"-"))) return op1 - op2;
else if (!(strcmp(oprtr,"*"))) return op1 * op2;
else if (!(strcmp(oprtr,"/"))) return op1 / op2;
else return -1;
}
Topic archived. No new replies allowed.