Postfix Eval
Postfix Eval
#include <sstream>
using namespace std;
struct Node;
struct popReturn;
Node* push(Node* top,int val);
void printStack(Node* top);
popReturn pop(Node* top);
struct Node{
int val;
Node* next;
};
struct popReturn{
Node* newtop;
int poppedValue;
};
if(s[i] == '+'){
int result = firstNum.poppedValue + secondNum.poppedValue;
operandStack = push(operandStack,result);
}
else if(s[i] == '-'){
int result = firstNum.poppedValue - secondNum.poppedValue;
operandStack = push(operandStack,result);
}
else if(s[i] == '*'){
int result = firstNum.poppedValue * secondNum.poppedValue;
operandStack = push(operandStack,result);
}
else if(s[i] == '/'){
int result = firstNum.poppedValue / secondNum.poppedValue;
operandStack = push(operandStack,result);
}
else if(s[i] == '^'){
int result = firstNum.poppedValue + secondNum.poppedValue;
operandStack = push(operandStack,result);
}
}
if(isdigit(s[i])){
int val;
stringstream ss;
ss<<s[i];
ss >> val;
cout<<"Number is "<<val;
operandStack = push(operandStack,val);
cout<<"operand stack is";
printStack(operandStack);
cout<<endl;
}
}
}
while(top != NULL){
cout<<top->val;
top = top->next;
}
int main(){
Node* operands = NULL;
evalString("54+38*+",operands);