Prefix expression
Expression from right to left scanning, while a numeral, the number onto the stack, the face of the operator, the pop-up stack of two numbers, using operators calculated accordingly (the top element of the op-top element thereof ), and the result stack; repeat the process until the leftmost expression, is the value obtained by the last calculation result of the expression
From right to left, the number of pressure case 1, case 2 breaks shells, again calculated.
Postfix expression
Expression scan from left to right, when a numeral, the number onto the stack, the face of the operator, the pop-up stack of two numbers, using operators calculated accordingly (the top element of the op-top element thereof ), and the result stack; repeat the process until the leftmost expression, is the value obtained by the last calculation result of the expression
From left to right, the number of pressure case 1, case 2 breaks shells, again calculated.
example
https://www.luogu.org/problem/P1449
#include <iostream> #include <algorithm> #include <cstdio> using namespace std; int a[1005],p,top,t; char c; int main() { while(c=getchar()) { if(c=='@') break; if(isdigit(c)) t=t*10+c-'0'; if(c=='.') { a[++top]=t; t=0; } if(c=='+') { a[top-1]=a[top]+a[top-1]; top--; } if(c=='-') { a[top-1]=a[top-1]-a[top]; top--; } if(c=='*') { a[top-1]=a[top]*a[top-1]; top--; } if(c=='/') { a[top-1]=a[top-1]/a[top]; top--; } } cout<<a[top]; return 0; }
Precautions:
1. there is division, and to press the integer division in c ++
2.getchar () inside the cstdio