nyist 305 表达式求值

表达式求值

时间限制: 3000 ms  |  内存限制: 65535 KB
难度: 3
描述

Dr.Kong设计的机器人卡多掌握了加减法运算以后,最近又学会了一些简单的函数求值,比如,它知道函数min(20,23)的值是20 ,add(10,98) 的值是108等等。经过训练,Dr.Kong设计的机器人卡多甚至会计算一种嵌套的更复杂的表达式。

假设表达式可以简单定义为:

1. 一个正的十进制数 x 是一个表达式。

2. 如果 和  表达式,则 函数min(x,y )也是表达式,其值为x,y 中的最小数。

3. 如果 和  表达式,则 函数max(x,y )也是表达式,其值为x,y 中的最大数。

4.如果 和  表达式,则 函数add(x,y )也是表达式,其值为x,y 之和。

例如, 表达式 max(add(1,2),7) 的值为 7。

请你编写程序,对于给定的一组表达式,帮助 Dr.Kong 算出正确答案,以便校对卡多计算的正误

输入
第一行: N 表示要计算的表达式个数 (1≤ N ≤ 10) 
接下来有N行, 每行是一个字符串,表示待求值的表达式
(表达式中不会有多余的空格,每行不超过300个字符,表达式中出现的十进制数都不
超过1000。)
输出
输出有N行,每一行对应一个表达式的值。
样例输入
3
add(1,2) 
max(1,999) 
add(min(1,1000),add(100,99)) 
样例输出
3
999
200
来源

第四届河南省程序设计大赛

#include<iostream>
#include<algorithm>
#include<stack>
#include<stdio.h>
#include<string.h>
using namespace std;
const int maxn=1005;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        char str[maxn];
        scanf("%s",str);
        int len=strlen(str);
        stack<int>num_result;
        for(int i=len-1;i>=0;i--)
        {
            if(str[i]>='0'&&str[i]<='9')
            {
                int num=0;
                int point_i=i;
                while(str[i]>='0'&&str[i]<='9')
                    i--;
                for(int j=i+1;j<=point_i;j++)
                {
                    num=num*10+str[j]-'0';
                }
                num_result.push(num);
                i++//之所以i又++是因为i减了到了非数字的位子 ,if这条语句执行完毕后i就会再减一           
            }
            else if(str[i]=='n')
            {
                int num1=num_result.top();
                 num_result.pop();
                int num2=num_result.top();
                 num_result.pop();
                 num1=min(num1,num2);
                 num_result.push(num1);

            }
             else if(str[i]=='x')
            {
                int num1=num_result.top();
                num_result.pop();
                int num2=num_result.top();
                 num_result.pop();
                 num1=max(num1,num2);
                 num_result.push(num1);

            }
            else if(str[i]=='d'&&str[i-1]=='d')
            {
               int num1=num_result.top();
               num_result.pop();
              int num2=num_result.top();
              num_result.pop();
              num1=num1+num2;
               num_result.push(num1);
            }
        }
        printf("%d\n",num_result.top());
    }
    return 0;
}



#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<stack>
#include<vector>
#include<cmath>
using namespace std;
char fun(char a, char b)
{
    if(a == 'a')
        return '+';
    else if(a == 'm' && b == 'a')
        return '>';
    else
        return '<';
}
int fun2(int a, int b, char c)
{
    switch (c)
    {
    case '+' : return a + b;
    case '<' : return min(a, b);
    case '>' : return max(a, b);
    }
}
int main()
{
    int ncase, len, tempnum, a, b, wei;
    char str[310], tempchar, cal;
    stack<char>ope;
    stack<int>num;
    scanf("%d", &ncase);
    getchar();
    while(ncase--)
    {
        while(!ope.empty())
            ope.pop();
        while(!num.empty())
            num.pop();
        gets(str);
        len = strlen(str);
        for(int i = 0; i < len; ++i)
        {
            if(str[i] == 'a' || str[i] == 'm')
            {
                tempchar = fun(str[i], str[i + 1]);
                ope.push(tempchar);
                i += 2;//min,max,add这三个函数的长度为3'a'和'm'都是函数的首字母,i+2后到第三个字母,然后i++
                 //i+2后到第三个字母,然后i++
            }
            else if(str[i] >= '0' && str[i] <= '9')
            {
                sscanf(&str[i], "%d%n", &tempnum, &wei);//%n统计共输入的字符数
                cout<<wei<<endl;
                i += wei - 1;//执行这条语句后,i又++
                num.push(tempnum);
            }
            else if(str[i] == ')')
            {
                b = num.top(); num.pop();
                a = num.top(); num.pop();
                cal = ope.top(); ope.pop();
                num.push(fun2(a, b, cal));
            }
        }
        printf("%d\n", num.top());
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值