'Evaluating a postfix expression
This is a program to evaluate a post-fix expression using stack. Why in the 8th line we have pushed question[i]-'0' rather than just question[i]. I did not understand the role of 0.
stack<int> s;
int main(){
string question;
cin>>question;
for(int i = 0 ; i < question.length() ; i++){
if(isdigit(question[i]))
s.push(question[i] - '0');
else{
int a = s.top();
s.pop();
int b = s.top();
s.pop();
if(question[i]=='+') s.push(a+b);
else if(question[i]=='-') s.push(a-b);
else if(question[i]=='*') s.push(a*b);
else if(question[i]=='/') s.push(a/b);
}
}
cout<<"Converting to infix and solving we get : "<<s.top();
return 0;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
