8.1.3 简单计算器

简单计算器

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 58 Accepted Submission(s): 44

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 //不是我的代码
  2 #include "iostream"
  3 #include "stdio.h"
  4 #include "math.h"
  5 #include "vector"
  6 #include "stack"
  7 #include "queue"
  8 #include "memory.h"
  9 #include "algorithm"
 10 #include "string"
 11 using namespace std;
 12 
 13 char e[250],post[250];
 14 stack<char>Op;
 15 
 16 bool Isnum(char c)
 17 {
 18     if(c>='0'&&c<='9')
 19         return true;
 20     return false;
 21 }
 22 
 23 int OPMode(char c)
 24 {
 25     if(c=='+')        return 1;
 26     if(c=='-')        return 2;
 27     if(c=='*')        return 3;
 28     if(c=='/')        return 4;
 29     return -1;
 30 }
 31 
 32 void SplitExp(char* s)
 33 {
 34     int i,j=0;
 35     memset(post,'',sizeof(post));
 36     for(i=0;i<strlen(s);i++)
 37     {
 38         if(s[i]==' ')
 39             continue;
 40         post[j++]=' ';
 41         while(Isnum(s[i]))
 42             post[j++]=s[i++];
 43         int curop=OPMode(s[i]);
 44         if(curop!=-1)
 45         {
 46             if(curop<=2)
 47                 while(!Op.empty())
 48                 {
 49                     post[j++]=Op.top();
 50                     Op.pop();
 51                 }
 52             else
 53             {
 54                 while(!Op.empty()&&OPMode(Op.top())>2)
 55                 {
 56                     post[j++]=Op.top();
 57                     Op.pop();
 58                 }
 59             }
 60             Op.push(s[i])    ;
 61         }
 62     }
 63     while(!Op.empty())
 64     {
 65         post[j++]=Op.top();
 66         Op.pop();
 67     }
 68 }
 69 
 70 stack<double>Num;
 71 double Cal()
 72 {
 73     while(!Num.empty())
 74         Num.pop();
 75     int i=0,j;
 76     int len=strlen(post);
 77     while (i++<len)
 78     {
 79         if(post[i]==' ')
 80             continue;
 81         double cur=0;
 82         bool hasnum=false;
 83         while (Isnum(post[i]))
 84         {
 85             cur*=10;
 86             cur+=post[i++]-'0';
 87             hasnum=true;
 88         }
 89         if(hasnum)
 90             Num.push(cur);
 91         if(OPMode(post[i])!=-1)
 92         {
 93             double num1=Num.top();
 94             Num.pop();
 95             double num2=Num.top();
 96             Num.pop();
 97             switch(post[i])
 98             {
 99             case '+':        Num.push(num2+num1);break;
100             case '-':        Num.push(num2-num1);break;
101             case '*':        Num.push(num2*num1);break;
102             case '/':        Num.push(num2/num1);break;
103             }
104         }
105     }
106     return Num.top();
107 }
108 
109 int main()
110 {
111     while(gets(e))
112     {
113         if(strcmp(e,"0")==0)
114             break;
115         SplitExp(e);
116         printf("%.2f
",Cal());
117     }
118 }
原文地址:https://www.cnblogs.com/cssystem/p/3212478.html