输入描述:
输入包含多组数据,,每组数据包含两行。
第一行为正整数n(3≤n≤50),紧接着第二行包含n个由数值和运算符组成的列表。
“+-*/”分别为加减乘除四则运算,其中除法为整除,即“5/3=1”。
输出描述:
对应每一组数据,输出它们的运算结果。
输入例子:
3 + 2 3 5 * + 2 2 3 5 * 2 + 2 3
输出例子:
5 12 10
;bool Is_Op(char ch){return (ch == ‘+’ || ch == ‘-‘ || ch == ‘*’ || ch == ‘/’);}int Deal(int a, char op, int b){switch (op){case ‘+’:return a + b;break;case ‘-‘:return a – b;break;case ‘*’:return a*b;break;case ‘/’:if (b!=0)return a / b;break;default:break;}}int main(){int n;stack<string> NUM;while (cin >> n){char *inputStr = new char[n];cin.get();cin.get(inputStr,n*2);string inputSTR(inputStr);string SStr(inputSTR.rbegin(),inputSTR.rend());strcpy(inputStr,SStr.c_str());char *p = inputStr;while (*p != ‘\0’ || NUM.size()!=1){string s;if (*p == ‘ ‘){p++;continue;}if (Is_Op(*p)){string num1 = NUM.top();NUM.pop();string NUM1(num1.rbegin(), num1.rend());string num2 = NUM.top();string NUM2(num2.rbegin(), num2.rend());NUM.pop();int result = Deal(atoi(NUM1.c_str()),*p,atoi(NUM2.c_str()));char *buff = new char[50];sprintf(buff,”%d”,result);NUM.push(buff);p++;}else{while (*p != ‘ ‘ && *p != ‘\0’){s += *p;p++;}NUM.push(s);}}cout<<NUM.top().c_str() << endl;NUM.pop();}return 0;}
而是他们在同伴们都睡着的时候,一步步艰辛地向上攀爬的。