简单计算器

题目描述:
    读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。
输入:
    测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。
输出:
    对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。
样例输入:
1 + 2
4 + 2 * 5 - 7 / 11
0
样例输出:
3.00
13.36

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstdlib>
  4 #include <cmath>
  5 #include <cctype>
  6 #include <cstring>
  7 
  8 #include <vector>
  9 #include <deque>
 10 #include <list>
 11 #include <map>
 12 #include <set>
 13 #include <stack>
 14 #include <queue>
 15 #include <algorithm>
 16 #include <string>
 17 
 18 
 19 #define MAXD 99999999
 20 using namespace std;
 21 
 22 
 23 
 24 stack<char>opch;
 25 
 26 
 27 
 28 stack<double>opn;
 29 
 30 
 31 double compute(char ch,double x,double y)
 32 {
 33     switch(ch)
 34     {
 35     case '+':return x+y;
 36     case '-':return x-y;
 37     case '*':return x*y;
 38     case '/':return x/y;
 39     }
 40 }
 41 
 42 
 43 
 44 
 45 int cmp(char op1,char op2)
 46 {
 47     if((op1=='*')||(op1=='/'))
 48         return 1;
 49     
 50     if((op1=='+')||(op1=='-'))
 51     {
 52         if((op2=='*')||(op2=='/'))
 53             return -1;
 54         else 
 55             return 1;
 56     }
 57     else if(op1=='#')
 58     {
 59         if(op2=='#')
 60             return 0;
 61 
 62         else
 63             return -1;
 64     }
 65 }
 66 
 67 
 68 
 69 
 70 
 71 int isop(char ch)
 72 {
 73     if(ch=='+')
 74         return 1;
 75     if(ch=='-')
 76         return 1;
 77     if(ch=='*')
 78         return 1;
 79     if(ch=='/')
 80         return 1;
 81     if(ch=='#')
 82         return 1;
 83 
 84     return 0;
 85 }
 86 
 87 
 88 
 89 
 90 
 91 int main()
 92 {
 93 
 94 
 95     char s[1000];
 96 
 97 
 98     int i,j,k;
 99     int n;
100 
101 
102     while(gets(s))
103     {
104         if(strcmp(s,"0")==0)
105             break;
106 
107         n=strlen(s);
108 
109         s[n]='#';
110         s[n+1]=0;
111 
112     
113 
114 
115         n=strlen(s);
116 
117 
118         while(opch.size())
119             opch.pop();
120 
121         while(opn.size())
122             opn.pop();
123 
124 
125 
126         opch.push('#');
127 
128     i=0;
129 
130 
131 
132     while(i<n)
133     {
134         if(s[i]==' ')
135         {i++;continue;}
136         else if(isdigit(s[i]))
137         {
138             double temp=0;
139 
140 
141             while((i<n)&&(isdigit(s[i])))
142             {
143                 temp*=10;
144                 temp+=s[i]-'0';
145                 i++;
146             }
147             opn.push(temp);
148         
149         }
150         else if(isop(s[i]))
151         {
152             char ch=s[i];
153             int tag;
154             tag=cmp(opch.top(),ch);
155             if(tag==0)
156             {opch.pop();i++;}
157             else if(tag>0)
158             {
159                 double temp;
160 
161                 temp=opn.top();
162                 opn.pop();
163 
164                 
165 
166 
167 
168                 temp=compute(opch.top(),opn.top(),temp);
169                 opch.pop();
170                 opn.pop();
171                 opn.push(temp);
172 
173             }
174             else
175             {
176                 opch.push(s[i]);
177                 i++;
178             }
179         }
180     }
181 
182 
183     printf("%.2lf\n",opn.top());
184 
185     opn.pop();
186     }
187 
188         
189 
190 
191 
192 
193     return 0;
194 }

 

posted @ 2012-05-30 20:23  cseriscser  阅读(375)  评论(0编辑  收藏  举报