蓝桥杯2013-省赛-C/C++-A组6题

题目

标题:逆波兰表达式

    正常的表达式称为中缀表达式,运算符在中间,主要是给人阅读的,机器求解并不方便。

    例如:3 + 5 * (2 + 6) - 1

    而且,常常需要用括号来改变运算次序。

    相反,如果使用逆波兰表达式(前缀表达式)表示,上面的算式则表示为:

    - + 3 * 5 + 2 6 1

    不再需要括号,机器可以用递归的方法很方便地求解。

    为了简便,我们假设:

    1. 只有 + - * 三种运算符
     2. 每个运算数都是一个小于10的非负整数
    
     下面的程序对一个逆波兰表示串进行求值。
     其返回值为一个结构:其中第一元素表示求值结果,第二个元素表示它已解析的字符数。

struct EV
{
     int result;  //计算结果
     int n;       //消耗掉的字符数
};

struct EV evaluate(char* x)
{
     struct EV ev = {0,0};
     struct EV v1;
     struct EV v2;

    if(*x==0) return ev;
    
     if(x[0]>='0' && x[0]<='9'){
         ev.result = x[0]-'0';
         ev.n = 1;
         return ev;
     }
     //- + 3 * 5 + 2 6 1
     v1 = evaluate(x+1);
     v2 = _____________________________;  //填空位置
    
     if(x[0]=='+') ev.result = v1.result + v2.result;
     if(x[0]=='*') ev.result = v1.result * v2.result;
     if(x[0]=='-') ev.result = v1.result - v2.result;
     ev.n = 1+v1.n+v2.n;

    return ev;
}


请分析代码逻辑,并推测划线处的代码,通过网页提交。
注意:仅把缺少的代码作为答案,千万不要填写多余的代码、符号或说明文字!!

代码

 1 #include<iostream>
 2 using namespace std;
 3 struct EV
 4 {
 5     int result;  //计算结果 
 6     int n;       //消耗掉的字符数 
 7 };
 8 
 9 struct EV evaluate(char* x)
10 {
11     struct EV ev = {0,0};
12     struct EV v1;
13     struct EV v2;
14 
15     //cout<<x[0]<<" "<<ev.result<<endl;
16     if(*x==0) return ev;
17     
18     if(x[0]>='0' && x[0]<='9'){
19         ev.result = x[0]-'0';
20         ev.n = 1;
21         return ev;
22     }
23     //-+3*5+261
24     v1 = evaluate(x+1);
25     v2 = evaluate(x+1+v1.n);  //填空位置
26     
27     
28     if(x[0]=='+') ev.result = v1.result + v2.result;
29     if(x[0]=='*') ev.result = v1.result * v2.result;
30     if(x[0]=='-') ev.result = v1.result - v2.result;
31     ev.n = 1+v1.n+v2.n;
32     //cout<<v1.result<<" "<<v2.result<<endl;
33     //cout<<x[0]<<"//"<<ev.result<<endl;
34     return ev;
35 }
36 int main(){
37     char str[15];
38     cin>>str;
39     EV ev=evaluate(str);
40     cout<<ev.result<<endl; 
41 } 

 

posted @ 2020-03-02 22:06  Mem_Ocean  阅读(184)  评论(0)    收藏  举报