FOJ 1339 Calculator

 

http://acm.fzu.edu.cn/problem.php?pid=1339

 

解题思路:这题就是模拟计算器啦,不过这道题给你的都是非负数,所以你就不用考虑负数的情况,题目也好做多了。这道题主要就是用到栈,建两个栈,一个是数值栈,另一个则是运算符栈。当前要放入栈的运算符和栈顶元素比较,运算符高的直接入栈,低或者相等的话,取出栈顶元素,跟这个运算符配对的两个数值也拿出去做运算。运算出来的结果在入栈,一直运算到当前栈顶元素的优先级别小于当前运算符,所以就要用到while来做了。

 

#include <iostream> #include <stack> #include <string> using namespace std; char compare(char NewOper,char OldOper)//比较优先级,oldOper运算符栈顶元素,NewOper当前要放入栈的运算符 { switch(NewOper) { case '+': if(OldOper == '+'||OldOper == '-')return '='; if(OldOper == '*'||OldOper == '/')return '<'; if(OldOper == '(')return '>'; break; case '-': if(OldOper == '+'||OldOper == '-')return '='; if(OldOper == '*'||OldOper == '/')return '<'; if(OldOper == '(')return '>'; break; case '*': if(OldOper == '+'||OldOper == '-')return '>'; if(OldOper == '*'||OldOper == '/')return '='; if(OldOper == '(')return '>'; break; case '/': if(OldOper == '+'||OldOper == '-')return '>'; if(OldOper == '*'||OldOper == '/')return '='; if(OldOper == '(')return '>'; break; } } int main() { stack<int>num; stack<char>oper; char strings[201]; int NumOfTest; int Sum; int num1,num2; int i,j; int clen; char top; while (scanf("%d",&NumOfTest)!=EOF) { for (j=0;j<NumOfTest;j++) { scanf("%s",strings); clen=strlen(strings); Sum=0; /************************************************************************/ /* 接收数字 */ /************************************************************************/ for(i=0;i<clen;i++) { if (strings[i]>='0'&&strings[i]<='9') { if (strings[i+1]<'0') { Sum = Sum*10+strings[i]-48; num.push(Sum); Sum=0; } else Sum = Sum*10+strings[i]-48; } /************************************************************************/ /* 接收运算符 */ /************************************************************************/ else { if(oper.empty()&&strings[i]!='(') oper.push(strings[i]); else { if (strings[i]!='('&&strings[i]!=')') { top = oper.top(); if (compare(strings[i],top) == '<'||compare(strings[i],top) == '=') { while (compare(strings[i],top)!='>')//一直到栈顶元素优先级小于当前运算符 { if(oper.empty()) break; else { oper.pop(); num1 = num.top();num.pop(); num2 = num.top();num.pop(); switch (top) { case '+':num.push(num2+num1); break; case '-':num.push(num2-num1); break; case '/':num.push(num2/num1); break; case '*':num.push(num2*num1); } if(!oper.empty()) top = oper.top();//栈不空,就去运算符 } } oper.push(strings[i]);//压入当前运算符 continue; } else { oper.push(strings[i]);//优先级高,直接入栈 continue; } } } } if (strings[i]=='(') { oper.push(strings[i]); continue; } if (strings[i]==')') { top = oper.top(); while (top!='(')//计算出()中的值 { num1 = num.top();num.pop(); num2 = num.top();num.pop(); switch (top) { case '+':num.push(num2+num1); break; case '-':num.push(num2-num1); break; case '/':num.push(num2/num1); break; case '*':num.push(num2*num1); } oper.pop(); top = oper.top(); } oper.pop();//要记得将(移除出栈,否则会错 } if (i==clen-1)//到运算式尾巴时,取出所有运算符计算 { while (!oper.empty()) { top = oper.top(); num1 = num.top();num.pop(); num2 = num.top();num.pop(); switch (top) { case '+':num.push(num2+num1); break; case '-':num.push(num2-num1); break; case '/':num.push(num2/num1); break; case '*':num.push(num2*num1); } oper.pop(); if(!oper.empty()) top = oper.top(); else break; } } } printf("%d/n",num.top()); num.pop(); } } return 0; }

你可能感兴趣的:(FOJ 1339 Calculator)