NYOJ--128--前缀式计算(表达式求值)

前缀式计算

时间限制: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

知识点:
stringstream可以把string转换成double,int,long long等
unget()可以把读出的字符放回字符串流中
atof在stdlib.h中的把str字符串转换成double
atoi在stdlib.h中的把str字符串转换成int;

 1 /*
 2     Name: NYOJ--128--前缀式计算
 3     Copyright: 
 4     Author: 日天大帝 
 5     Date: 01/05/17 18:09
 6     Description: 学C++这么久了,还是只会C的部分,看了别人代码,惭愧了 
 7 */
 8 #include<iostream>
 9 #include<iomanip>
10 #include<string>
11 #include<sstream>
12 using namespace std;
13 stringstream ss; 
14 double ans;
15 double f(){
16     char ch;
17     ss>>ch;
18     if(ch == '+')return f()+f();
19     else if(ch == '-')return f()-f();
20     else if(ch == '*')return f()*f();
21     else if(ch == '/')return f()/f();
22     ss.unget();
23     ss>>ans;
24     return ans;
25 }
26 int main(){
27     ios::sync_with_stdio(false);
28     
29     string str;
30     while(getline(cin,str)){
31         ss.clear();
32         ss<<str;
33         cout<<setiosflags(ios::fixed)
34             <<setprecision(2)
35             <<f()<<endl;
36     }
37     return 0;
38 }
View Code
 1 #include<iostream>
 2 #include<string>
 3 #include<stdio.h>
 4 using namespace std;
 5 int pos;string str;
 6 double fun()
 7 {
 8     ++pos;
 9     if(str[pos]==' ')++pos;
10     if(str[pos]>='0'&&str[pos]<='9')
11     {
12         string s;
13         while(pos!=str.size()&&str[pos]!=' ')
14             s+=str[pos++];
15         double tp;
16         sscanf(s.c_str(),"%lf",&tp);
17         return tp;
18     }
19     if(str[pos]=='+')
20         return fun()+fun();
21     if(str[pos]=='-')
22         return fun()-fun();
23     if(str[pos]=='*')
24         return fun()*fun();
25     if(str[pos]=='/')
26         return fun()/fun();
27 }
28 int main()
29 {
30     while(getline(cin,str))
31     {
32         pos=-1;
33         printf("%.2lf
",fun());
34 
35     }
36 }
View Code
 1 #include<iostream>
 2 #include<string>
 3 #include<stdio.h>
 4 #include<sstream>
 5 #include<stdlib.h>
 6 using namespace std;
 7 stringstream ss;//字符串流
 8 double fun()
 9 {
10     char ch;
11     double tp;
12     ss>>ch;
13     if(ch=='+')
14         return fun()+fun();
15     if(ch=='-')
16         return fun()-fun();
17     if(ch=='*')
18         return fun()*fun();
19     if(ch=='/')
20         return fun()/fun();
21     ss.unget();//把读出的ch字符放回ss字符串流中
22     ss>>tp;    //从字符串流中读出double型数
23     return tp;
24 }
25 int main()
26 {
27     string str;
28     while(getline(cin,str))
29     {
30         ss.clear();
31         ss<<str;
32         printf("%.2lf
",fun());
33     }
34 }
View Code
 1 #include<iostream>
 2 #include<string>
 3 #include<stdio.h>
 4 #include<sstream>
 5 #include<stdlib.h>
 6 using namespace std;
 7 stringstream ss;//字符串流
 8 double fun()
 9 {
10     string str;
11     ss>>str;
12     if(str[0]=='+')
13         return fun()+fun();
14     if(str[0]=='-')
15         return fun()-fun();
16     if(str[0]=='*')
17         return fun()*fun();
18     if(str[0]=='/')
19         return fun()/fun();
20     return atof(str.c_str());//atof在stdlib.h中 把str字符串转换成double;
21                     //atoi在stdlib.h中 把str字符串转换成int;
22 }
23 int main()
24 {
25     string str;
26     while(getline(cin,str))
27     {
28         ss.clear();
29         ss<<str;
30         printf("%.2lf
",fun());
31 
32     }
33 }
View Code
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 char str[20];
 4 double fun()
 5 {
 6     if(scanf("%s",str)==EOF)
 7         return 1<<30;
 8     if(str[0]=='+')
 9         return fun()+fun();
10     if(str[0]=='-')
11         return fun()-fun();
12     if(str[0]=='*')
13         return fun()*fun();
14     if(str[0]=='/')
15         return fun()/fun();
16     return atof(str);//atof在stdlib.h中的把str字符串转换成double;
17                 //atoi在stdlib.h中的把str字符串转换成int;
18 }
19 int main()
20 {
21     while(1)
22     {
23         double tp=fun();
24         if(tp<1<<30)
25             printf("%.2lf
",tp);
26         else
27             break;
28     }
29 }
View Code
原文地址:https://www.cnblogs.com/langyao/p/6792447.html