describe
Writing a program can complete the basic four operations with brackets. Where division (/) is an integral division and is rounded to 0 when dividing by a negative number. (the default division of C/C++/Java is to round to 0, and the default division of python is to round to negative infinity. )
For example, calculate 100 * (2 + 12) - (20 / 3) * 2, and the result is 1388.
input
A string of no more than 100 in length, representing the calculation to be calculated. Contains the numbers 0-9 and + - * / ().
The input ensures that the calculation process will not exceed 32-bit signed integers, and the '-' in them are all minus signs without negative signs.
output
Calculated results.
100*(2+12)-(20/3)*2sample output
1388Idea: mainly using the idea of stack, first convert the infix expression to suffix expression, and then calculate according to the calculation rules of suffix expression.
I think this topic is quite difficult. I couldn't start at first. After a day of careful deliberation, I finally wrote the AC code. I'm too weak for chicken. The AC code is as follows:
#include<cstdio> #include<cmath> #include<iostream> #include<string> #include<stack> #include<cstring> #include<algorithm> using namespace std; string str;//Input string stack<char>st; stack<int>mp; char s[100000];//Character array to record suffix expression int cal(int a,int b,char op) { switch(op) { case '+':return a+b; case '-':return a-b; case '*':return a*b; case '/':return a/b; } } int main() { cin>>str; int n=str.size(); int k=1,t=0; while(!st.empty()) st.pop(); for(int i=0;i<100000;i++) s[i]=' '; for(int i=0;i<n;i++) { if(str[i]>='0' && str[i]<='9') s[k++]=str[i]; else if(st.size()==0 && (str[i]<'0' || str[i]>'9'))//Pay attention to the order of each else if below. I can't mess it up at will. I changed it many times before I got it right { k++; st.push(str[i]); k++; } else if(str[i]=='(') { k++; st.push(str[i]); k++; } else if(str[i]==')') { k++; while(st.top()!='(') { s[k++]=st.top(); k++; st.pop(); } st.pop(); } else if(st.top()=='(') { k++; st.push(str[i]); k++; } else if((st.top()=='+' || st.top()=='-') && (str[i]=='*' || str[i]=='/')) { k++; st.push(str[i]); k++; } else if((st.top()=='*' || st.top()=='/') && (str[i]=='+' || str[i]=='-')) { k++; s[k++]=st.top(); st.pop(); k++; if(st.size()>0 && st.top()=='-') { s[k++]=st.top(); st.pop(); } st.push(str[i]); k++; } else if((str[i]=='*' && (st.top()=='/' || st.top()=='*')) || (str[i]=='/' && (st.top()=='*' || st.top()=='/'))) { k++; s[k++]=st.top(); st.pop(); st.push(str[i]); k++; } else if((str[i]=='+' && (st.top()=='-' || st.top()=='+')) || (str[i]=='-' && (st.top()=='+' || st.top()=='-'))) { k++; s[k++]=st.top(); st.pop(); st.push(str[i]); k++; } } while(st.size()>0) { k++; s[k++]=st.top(); st.pop(); } while(!mp.empty()) mp.pop(); t=0; for(int i=0;i<=k;i++) { if(s[i]=='+' || s[i]=='-' || s[i]=='*' || s[i]=='/') { int b=mp.top(); mp.pop(); int a=mp.top(); mp.pop(); mp.push(cal(a,b,s[i])); } else if(s[i]>='0' && s[i]<='9') { t=t*10+(s[i]-'0'); if(s[i+1]<'0' || s[i+1]>'9') { mp.push(t); t=0; } } } cout<<mp.top()<<endl; return 0; } //((((2+12)*3)+4)-61)+85 12-9+8*6/2-7 (9+8*5-7-8+20*3)-65+100/2 (1)+(1+2)+2*(1+3)