HDU 1237 简单计算器


 

                 简单计算器

 

Problem Description
读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。
 

 

Input
测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。
 

 

Output
对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。
 

 

Sample Input
1 + 2
4 + 2 * 5 - 7 / 11
0
 

 

Sample Output
3.00
13.36  
 
 
 
代码:
  1 #include<cstdio>
  2 #include<iostream>
  3 #include<stack>
  4 #include<queue>
  5 #include<cstring>
  6 #include<algorithm>
  7 using namespace std;
  8 
  9 int judge(string s)
 10 {
 11     if(s==")"||s=="(")
 12     return 3;
 13     else if(s=="/"||s=="*")
 14     return 2;
 15     else if(s=="+"||s=="-")
 16     return 1;
 17     else
 18     return 0;
 19 }
 20 
 21 int main()
 22 {
 23     //freopen("in.txt","r",stdin);
 24     double num1,num2,ans;
 25     int len;
 26     int i,j,key,flag;
 27     char str[205],temp[10];;
 28     while(gets(str))
 29     {
 30         string tmp2,tmp1,top;
 31         queue<string>q;
 32         stack<string>s;
 33         stack<double>cal;
 34         len=strlen(str);
 35         for(i=0;i<len;i++)
 36         if(str[i]!='0')break;
 37         if(i==len)break;
 38         str[len]=' ';
 39         str[++len]='\0';
 40         for(j=0,i=0;i<len;i++)
 41         {
 42             if(str[i]==' ')
 43             {
 44                 temp[j]='\0';
 45                 j=0;
 46                 tmp1=temp;
 47                 flag=judge(tmp1);
 48                 if(flag)
 49                 {
 50                     if(flag==3)
 51                     {
 52                         if(tmp1=="(")
 53                         s.push(tmp1);
 54                         else
 55                         {
 56                             while(s.top()!="(")
 57                             {
 58                                 q.push(s.top());
 59                                 s.pop();
 60                             }
 61                             s.pop();
 62                         }
 63                     }
 64                     else if(flag==2)
 65                     {
 66                         while(!s.empty()&&judge(s.top())==2)
 67                         {
 68                             q.push(s.top());
 69                             s.pop();
 70                         }
 71                         s.push(tmp1);
 72                     }
 73                     else
 74                     {
 75                         while(!s.empty()&&s.top()!="(")
 76                         {
 77                             q.push(s.top());
 78                             s.pop();
 79                         }
 80                         s.push(tmp1);
 81                     }
 82                 }
 83                 else
 84                 q.push(tmp1);
 85                 continue;
 86             }
 87             temp[j++]=str[i];
 88         }
 89         while(!s.empty())
 90         {
 91             q.push(s.top());
 92             s.pop();
 93         }
 94         string temp;
 95         ans=0.0;
 96         while(!q.empty())
 97         {
 98             while(1)
 99             {
100                 temp=q.front();
101                 if(temp!="+"&&temp!="-"&&temp!="*"&&temp!="/")
102                 {
103                     cal.push((double)(atoi(temp.c_str())));
104                     q.pop();
105                 }
106                 else
107                 break;
108             }
109             num1=cal.top();
110             cal.pop();
111             num2=cal.top();
112             cal.pop();
113             if(temp=="+")key=1;
114             else if(temp=="-")key=2;
115             else if(temp=="*")key=3;
116             else key=4;
117             switch(key)
118             {
119                 case 1:ans=num2+num1;cal.push(ans);break;
120                 case 2:ans=num2-num1;cal.push(ans);break;
121                 case 3:ans=num2*num1;cal.push(ans);break;
122                 case 4:ans=num2/num1;cal.push(ans);break;
123             }
124             q.pop();
125         }
126         printf("%.2lf\n",ans);
127     }
128     return 0;
129 }

 

 

posted on 2015-07-28 12:14    阅读(130)  评论(0编辑  收藏  举报

导航