HDOJ1237

来源:互联网 发布:戏曲 知乎 编辑:程序博客网 时间:2024/06/05 08:29

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1237

题目描述:该题要求模拟实现计算器的加减乘除功能.

解题思路:运算符有优先级高低之分,借助栈将表达式(中缀表达式)转化为后缀表达式存于队列中,接着再次借助栈实现加减乘除功能.

  1. #include <iostream>
  2. #include <stack>
  3. #include <queue>
  4. #include <string>
  5. using namespace std;
  6. bool IsLowPriority(const string &s,const string &q)//the first parameter is in the stack to be compared
  7. {
  8.     if(s=="*" || s=="/")
  9.         return true;
  10.     if((s=="+" || s=="-") && (q=="+" || q=="-"))
  11.         return true;
  12.     return false;
  13. }
  14. double GetNum(const string &n)
  15. {
  16.     int len,cnt;
  17.     double num;
  18.     len=n.length();
  19.     cnt=1;
  20.     num=0;
  21.     for(int i=len-1;i>=0;i--)
  22.     {
  23.         num += (n[i]-'0')*cnt;
  24.         cnt *= 10;
  25.     }
  26.     return num;
  27. }
  28. int main()
  29. {   
  30.     int i;
  31.     int len;
  32.     double temp1,temp2,tempRes;
  33.     string temp;
  34.     char str[200];
  35.     stack<string> s;
  36.     stack<double> dStack;
  37.     queue<string> q;
  38.     while(gets(str))
  39.     {       
  40.         if(!strcmp(str,"0"))            
  41.             break;
  42.         while(!s.empty())
  43.             s.pop();
  44.         while(!q.empty())
  45.             q.pop();
  46.         while(!dStack.empty())
  47.             dStack.pop();
  48.         len=strlen(str);;
  49.         temp="";
  50.         /************infix to postfix conversion*******************/
  51.         for(i=0;i<len;i++)
  52.         {           
  53.             if(str[i] != ' ')
  54.             {
  55.                 temp += str[i];             
  56.             }
  57.             else
  58.             {
  59.                 if(temp=="+" || temp=="-" || temp=="*" || temp=="/")
  60.                 {
  61.                     if(s.empty())
  62.                         s.push(temp);
  63.                     else
  64.                     {
  65.                         while(!s.empty() && IsLowPriority(s.top(),temp))
  66.                         {
  67.                             q.push(s.top());
  68.                             s.pop();
  69.                         }
  70.                         s.push(temp);
  71.                     }
  72.                 }
  73.                 else
  74.                 {
  75.                     q.push(temp);
  76.                 }
  77.                 temp="";                
  78.             }           
  79.         }
  80.         if(temp != "")
  81.             q.push(temp);
  82.         while(!s.empty())
  83.         {
  84.             q.push(s.top());
  85.             s.pop();
  86.         }
  87.         /********************compute**********************/
  88.         while(!q.empty())
  89.         {
  90.             
  91.             if(q.front()=="+" || q.front()=="-" || q.front()=="*" || q.front()=="/")
  92.             {
  93.                 temp2=dStack.top();
  94.                 dStack.pop();
  95.                 temp1=dStack.top();
  96.                 dStack.pop();
  97.                 if(q.front()=="+")
  98.                     tempRes=temp1+temp2;
  99.                 if(q.front()=="-")
  100.                     tempRes=temp1-temp2;
  101.                 if(q.front()=="*")
  102.                     tempRes=temp1*temp2;
  103.                 if(q.front()=="/")
  104.                     tempRes=temp1/temp2;
  105.                 dStack.push(tempRes);
  106.                 q.pop();
  107.             }
  108.             else
  109.             {
  110.                 dStack.push(GetNum(q.front()));
  111.                 q.pop();
  112.             }
  113.         }
  114.         printf("%.2f/n",dStack.top());  
  115.     }
  116.     return 0;
原创粉丝点击