nyoj 1272(表达式求值)

表达式求值

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 3
描述
假设表达式定义为: 1. 一个十进制的正整数 X 是一个表达式。 2. 如果 X 和 Y 是 表达式,则 X+Y, X*Y 也是表达式; *优先级高于+. 3. 如果 X 和 Y 是 表达式,则 函数 Smax(X,Y)也是表达式,其值为:先分别求出 X ,Y 值的各位数字之和,再从中选最大数。 4.如果 X 是 表达式,则 (X)也是表达式。 例如: 表达式 12*(2+3)+Smax(333,220+280) 的值为 69。 请你编程,对给定的表达式,输出其值。  
输入
【标准输入】 第一行: T 表示要计算的表达式个数 (1≤ T ≤ 10) 接下来有 T 行, 每行是一个字符串,表示待求的表达式,长度<=1000
输出
【标准输出】 对于每个表达式,输出一行,表示对应表达式的值。
样例输入
3
12+2*3
12*(2+3)
12*(2+3)+Smax(333,220+280)
样例输出
18
60
69
来源

河南省第九届省赛


这道题表达式求值题类似于nyoj35和nyoj305.这道题多了个Smax运算,其实思路都是一样的,就是把Smax的优先级定为最高的就行,同样有两种解题思路,一是先把中缀表达式转化为后缀表达式,再进行运算,第二种是在进行遍历表达式的时候就进行计算:

代码1:

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
using  namespace std;
const  int maxn= 1000+ 5;

string s1,s2;
stack< char> s;
stack< double> c;

void init()
{
     ///对两个栈进行初始化
     while(!s.empty())
        s.pop();
     while(!c.empty())
        c.pop();
}

int pro( char ch)
{
     ///规定运算符的优先级
     switch(ch)
    {
     case  '+':
     case  '-':
         return  1;
     case  '*':
     case  '/':
         return  2;
     case  'S':
         return  3;
     default :
         return  0;
    }
}

void deal()
{
    init();
     int i= 0,len=s1.length();
    s.push( '#');
     while(i<len)
    {
         if(s1[i]== '(')
            s.push(s1[i++]);
         else  if(s1[i]== 'S')
        {
            s.push(s1[i]);
            i=i+ 4;
        }
         else  if(s1[i]== ')')
        {
             while(s.top()!= '(')
            {
                s2+=s.top();
                s2+= ' ';
                s.pop();
            }
            s.pop();
            i++;
        }
         else  if(s1[i]== '+'||s1[i]== '-'||s1[i]== '*'||s1[i]== '/'||s1[i]== 'S')
        {
             while(pro(s.top())>=pro(s1[i]))
            {
                s2+=s.top();
                s2+= ' ';
                s.pop();
            }
            s.push(s1[i]);
            i++;
        }
         else  if(s1[i]<= '9'&&s1[i]>= '0'||s1[i]== '.')
        {
             while(s1[i]<= '9'&&s1[i]>= '0'||s1[i]== '.')
                s2+=s1[i++];
            s2+= ' ';
        }
         else  if(s1[i]== ','||s1[i]== 'x')
            i++;
    }
     while(s.top()!= '#')
    {
        s2+=s.top();
        s.pop();
        s2+= ' ';
    }
     //cout<<s2<<endl;
}

double countt()
{

     int len=s2.length(),i= 0;
     double y,x;
     while(i<len)
    {
         if(s2[i]== ' ')
            i++;
         else
        {
             switch(s2[i])
            {
             case  '+':
                x=c.top();
                c.pop();
                x+=c.top();
                c.pop();
                i++;
                 break;
             case  '*':
                x=c.top();
                c.pop();
                x*=c.top();
                c.pop();
                i++;
                 break;
             case  'S':
            {
                 int a=c.top();
                c.pop();
                 int b=c.top();
                c.pop();
                 int c,da =  0,db =  0;
                 while(b!= 0)
                {
                    db += b% 10;
                    b /=  10;
                }
                 while(a!= 0)
                {
                    da += a% 10;
                    a /=  10;
                }
                x = max(da,db);
                i++;
                 break;
            }
             default :
            {
                x= 0. 0;
                 while(s2[i]<= '9'&&s2[i]>= '0')
                    x=x* 10+(s2[i++]- '0');
            }
            }
            c.push(x);
        }
    }
     return c.top();
}

int main()
{
     int t;
    cin>>t;
     while(t--)
    {
        cin>>s1;
        s2= "";
        deal();
        printf( "%.0lf\n",countt());
    }
     return  0;
}

代码2:

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <stdio.h>  
#include <string.h>  
#include <algorithm>  
#include <stack>  
using  namespace std;  
stack< char> s1;  
stack< int> s2;  
char s[ 1010];  
int main()  
{  
     int T;  
    scanf( "%d",&T);  
     while(T--)  
    {  
         int i,j;  
        getchar();  
        scanf( "%s",&s[ 1]);  
         int len = strlen(&s[ 1]);  
        s[ 0] =  '(';  
        s[len+ 1] =  ')';  
        len++;  
         for(i= 0;i<=len;i++)  
        {  
             if(s[i]== '(')  
            {  
                s1.push(s[i]);  
            }  
             else  if(s[i]== 'S')  
            {  
                s1.push( '(');  
                s1.push( 'S');  
                i +=  4;  
            }  
             else  if(s[i]>= '0' && s[i]<= '9')  
            {  
                 int v =  0;  
                 while(s[i]>= '0' && s[i]<= '9')  
                {  
                    v = v* 10+(s[i++]- '0');  
                }  
                i--;  
                s2.push(v);  
            }  
             else  if(s[i]== '+' || s[i]== '-')  
            {  
                 while(s1.top()!= '(' && s1.top()!= 'S')  
                {  
                     int a = s2.top(); s2.pop();  
                     int b = s2.top(); s2.pop();  
                     int c;  
                     switch(s1.top())  
                    {  
                         case  '+': c = b + a;  break;  
                         case  '-': c = b - a;  break;  
                         case  '*': c = b * a;  break;  
                         case  '/': c = b / a;  break;  
                    }  
                    s2.push(c);  
                    s1.pop();  
                }  
                s1.push(s[i]);  
            }  
             else  if(s[i]== '*' || s[i]== '/')  
            {  
                 if(s1.top()== '*' )  
                {  
                     int a = s2.top(); s2.pop();  
                     int b = s2.top(); s2.pop();  
                    s2.push(b*a);  
                    s1.pop();  
                }  
                 else  if(s1.top()== '/')  
                {  
                     int a = s2.top();s2.pop();  
                     int b = s2.top();s2.pop();  
                    s2.push(b/a);  
                    s1.pop();  
                }  
                s1.push(s[i]);  
            }  
             else  if(s[i]== ')')  
            {  
                 while(s1.top()!= '(' &&  s1.top()!= 'S')  
                {  
                     int a = s2.top();s2.pop();  
                     int b = s2.top();s2.pop();  
                     int c,da =  0 ,db =  0;  
                     switch(s1.top())  
                    {  
                         case  '+': c = b + a;  break;  
                         case  '-': c = b - a;  break;  
                         case  '*': c = b * a;  break;  
                         case  '/': c = b / a;  break;  
                    }  
                    s2.push(c);  
                    s1.pop();  
                }  
                 if(s1.top()== 'S')     
                {  
                     int a=s2.top();s2.pop();  
                     int b=s2.top();s2.pop();  
                     int c,da =  0 ,db =  0;  
                     while(b!= 0)  
                    {  
                        db += b% 10;  
                        b /=  10;  
                    }  
                     while(a!= 0)  
                    {  
                        da += a% 10;  
                        a /=  10;  
                    }  
                    c = max(da,db);  
                    s2.push(c);  
                    s1.pop();  
                }  
                s1.pop();             
            }  
        }  
        printf( "%d\n",s2.top());  
        s2.pop();  
    }  
     return  0;  
}  



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值