前綴式計算(前綴表達式)


前綴式計算

時間限制: 1000 ms  |  內存限制:65535 KB
難度: 3
 
描述

先說明一下什么是中綴式:

如2+(3+4)*5這種我們最常見的式子就是中綴式。

而把中綴式按運算順序加上括號就是:(2+((3+4)*5))

然后把運算符寫到括號前面就是+(2 *( +(3 4) 5) )

把括號去掉就是:+ 2 * + 3 4 5

最后這個式子就是該表達式的前綴表示。

給你一個前綴表達式,請你計算出該前綴式的值。

比如:

+ 2 * + 3 4 5的值就是 37

 
輸入
有多組測試數據,每組測試數據占一行,任意兩個操作符之間,任意兩個操作數之間,操作數與操作符之間都有一個空格。輸入的兩個操作數可能是小數,數據保證輸入的數都是正數,並且都小於10,操作數數目不超過500。 以EOF為輸入結束的標志。
輸出
對每組數據,輸出該前綴表達式的值。輸出結果保留兩位小數。
樣例輸入
+ 2 * + 3 4 5
+ 5.1 / 3 7
樣例輸出
37.00
5.53
題解:自己寫了一大堆代碼結果wa了,至今沒發現錯誤,大神就幾行就A了;論代碼的靈活性;
借助大神寫的AC:
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<cstring>
 6 #include<queue>
 7 #include<stack>
 8 using namespace std;
 9 double f(){
10     char s[10];
11     if(!~scanf("%s",s))exit(0);
12     switch(*s){
13         case '+':return f()+f();break;
14         case '-':return f()-f();break;
15         case '/':return f()/f();break;
16         case '*':return f()*f();break;
17         default:return atof(s);
18     }
19 }
20 int main(){
21     while(1)printf("%.2lf\n",f());
22     return 0;
23 }

自己的WA:

  1 #include<cstdio>
  2 #include<iostream>
  3 #include<cmath>
  4 #include<algorithm>
  5 #include<cstring>
  6 #include<queue>
  7 #include<stack>
  8 using namespace std;
  9 const int INF=0x3f3f3f3f;
 10 #define mem(x,y) memset(x,y,sizeof(x))
 11 #define SI(x) scanf("%d",&x)
 12 #define PI(x) printf("%d",x)
 13 #define ll root<<1
 14 #define rr root<<1|1
 15 typedef long long LL;
 16 const int MAXN=10010;
 17 char s[MAXN];
 18 char inout(char a,char b){
 19     if(a=='#')return '>';
 20     if(b=='=')return '=';
 21     if(a=='+'||a=='-'){
 22         if(b=='*'||b=='/')return '>';
 23         else return '<';
 24     }
 25     if(a=='*'||a=='/')return '<';
 26 }
 27 double work(double x,char o,double y){
 28     switch(o){
 29         case '+':return x+y;break;
 30         case '-':return x-y;break;
 31         case '*':return x*y;break;
 32         case '/':return x/y;break;
 33     }
 34 }
 35 void getnum(int& i,double& num){
 36     double p=10;
 37     while(isdigit(s[i])||s[i]=='.'){
 38         if(s[i]=='.'){
 39             i++;p=0.1;continue;
 40         }
 41         if(p==10)num=num*10+s[i]-'0';
 42         else num=num+(s[i]-'0')*p,p*=0.1;
 43         i++;
 44     }
 45 }
 46 int main(){
 47     while(gets(s)){
 48         stack<char>s1;
 49         stack<double>s2;
 50         s1.push('#');
 51         int len=strlen(s);
 52         s[len]='=';
 53         s[len+1]='\0';
 54         //puts(s);
 55         int temp=0;
 56         for(int i=0;s[i];){
 57             if(s[i]==' '){
 58                 i++;continue;
 59             }
 60             double num=0;
 61             if(isdigit(s[i])||s[i]=='.'){
 62                 getnum(i,num);
 63                 if(temp)temp++;
 64                 s2.push(num);
 65                 if(temp==3){
 66                     double a=s2.top();
 67                     s2.pop();
 68                     double b=s2.top();
 69                     s2.pop();
 70                     s2.push(work(b,s1.top(),a));
 71                     s1.pop();
 72                     temp=0;
 73                 }
 74             }
 75             else{
 76                 switch(inout(s1.top(),s[i])){
 77                     case '<':
 78                         s1.push(s[i]);
 79                         i++;
 80                         break;
 81                     case '>':
 82                         s1.push(s[i]);
 83                         i++;
 84                         temp=1;
 85                         break;
 86                     case '=':
 87                         double a=s2.top();
 88                         s2.pop();
 89                         double b=s2.top();
 90                         s2.pop();
 91                         s2.push(work(b,s1.top(),a));
 92                         s1.pop();
 93                         break;
 94                 }
 95             }
 96         }
 97         printf("%.2lf\n",s2.top());
 98     }
 99     return 0;
100 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM