表达式求值系列

题目信息

描述

ACM队的mdd想做一个计算器,但是,他要做的不仅仅是一计算一个A+B的计算器,他想实现随便输入一个表达式都能求出它的值的计算器,现在请你帮助他来实现这个计算器吧。

比如输入:“1+2/4=”,程序就输出1.50(结果保留两位小数)

输入

第一行输入一个整数n,共有n组测试数据(n<10)。
每组测试数据只有一行,是一个长度不超过1000的字符串,表示这个运算式,每个运算式都是以“=”结束。这个表达式里只包含+-*/与小括号这几种符号。其中小括号可以嵌套使用。数据保证输入的操作数中不会出现负数。
数据保证除数不会为0

输出

每组都输出该组运算式的运算结果,输出结果保留两位小数。

样例输入

2
1.000+2/4=
((1+2)*5+1)/4=
样例输出

1.50
4.00

#include <iostream>
#include <string>
#include <bits/stdc++.h>

using namespace std;
char ch[]={'=','(','+','-','*','/',')'};
int lpri[]={0,1,3,3,5,5,6};
int rpri[]={0,6,2,2,4,4,1};
map<char,int> Ml;
map<char,int> Mr;
void Init_op()//建立运算符的优先级,用map将左右运算符的优先级的映射成相应的整数
{
    for(int i=0;i<7;i++)
    {
        Ml[ch[i]]=lpri[i];
        Mr[ch[i]]=rpri[i];
    }
}
bool IsOp(char ch)//判断当前字符是否是运算符
{
    if(ch=='('||ch==')'||ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='=')
        return true;
    else
        return false;
}
string str;//用来存原始输入的字符串
int n;//表示有n组测试数据
stack<char> op;//表示操作符栈
stack<string> postexp;//后缀表达式栈
void post_exp()
{
    Init_op();
    int len=str.length();
    op.push('=');//将运算符栈压一个优先级最低的元素
    string str_num;
    for(int i=0;i<len;i++)
    {
        char temp = op.top();
        if(IsOp(str[i]))//如果当前字符是操作符
        {
            if(Mr[str[i]]>Ml[temp])//判断左右运算符的优先级如果 有大于左则将运算符右运算符入栈
                op.push(str[i]);
            else if(Mr[str[i]]<Ml[temp]){//如果右小于左,先判断当前字符是否是右括号,如果是则一直出栈,并压入后缀表达式栈,直到遇到左括号,然后将左括号出栈,不压入后缀表达式栈,如果否则就一直出栈并压入后最表达式栈,直到左运算符优先级小于右运算符,最后再将当前运算符插入运算符栈,如果优先级相等只有左括号和右括号的情况,直接将左运算符出栈
                char temp_op,temp_ch;
                temp_op = op.top();
                temp_ch = str[i];
                if(temp_ch ==')')
                {
                    while(op.top()!='(')
                    {
                        char t=op.top();
                        op.pop();
                        string t_str;
                        t_str+=t;
                        postexp.push(t_str);
                    }
                    op.pop();
                }
                else{
                    while(Ml[op.top()]>Mr[temp_ch])
                    {
                        char t=op.top();
                        op.pop();
                        string t_str;
                        t_str+=t;
                        postexp.push(t_str);
                    }
                    op.push(temp_ch);
                }
            }
            else
                op.pop();
        }
        else{//如果是操作数,将操作数流成字符串,存入后缀表达式
            str_num+=str[i];
            if(IsOp(str[i+1])&&!str_num.empty())
            {
              postexp.push(str_num);
              str_num.clear();
            }
        }
    }
    postexp.push(str_num);
    str_num.clear();
    while(!op.empty())//最后将操作符中的所有字符(除等号)到压入后缀表达式栈
    {
        char temp_op=op.top(); op.pop();
        if(temp_op!='=')
        {
            string str_t;
            str_t+=temp_op;
            postexp.push(str_t);
        }
    }
}
double Count(double a,char c,double b)///计算+ - * /
{
    if(c=='+')
        return a+b;
    if(c=='-')
        return a-b;
    if(c=='*')
        return a*b;
    if(c=='/')
        return a/b;
}
double compvalue()
{
    string str_post[1005];//因为出栈会导致后缀表达式倒着所以重新用一个字符串数组来存正着的后缀表达式
    int len=0;
    while(!postexp.empty())
    {
        str_post[len++]=postexp.top();
        postexp.pop();
    }
    stack<double> op_digit;//操作数栈
    double d;
    char c;
    for(int i=len-1;i>=0;i--)
    {stringstream ss;
        if(str_post[i]=="+"||str_post[i]=="-"||str_post[i]=="*"||str_post[i]=="/")//如果是运算符,操作数栈出栈两个数首先出栈的那个为第二个操作数,其次为第一个操作数,并在进行相应的运算后压栈
        {
            c=str_post[i][0];
            double a,b;
            b=op_digit.top();op_digit.pop();
            a=op_digit.top();op_digit.pop();
            op_digit.push(Count(a,c,b));
        }
        else//如果是操作数就流成相应的类型并压入操作数栈
        {
            ss<<str_post[i];ss>>d;
            op_digit.push(d);
        }
    }
    double sum;
    while(!op_digit.empty())//最后就操作数栈全部出栈,返回栈中最后的数
    {sum=op_digit.top();
        op_digit.pop();
    }
    return sum;
}
int main()
{
    cin>>n;
    while(n--)
    {
        cin>>str;
        post_exp();
        printf("%.2lf\n",compvalue());
    }
    return 0;
}

表达式求值
时间限制: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
解题思路:这个题和上个题几乎是一样的,只是多了一个Smax运算而已,而且他只会有一层不会嵌套,所以我们只要给Smax中的‘,’当做一个运算符,算出它的优先级是作为左运算符时大于‘(’,作为右运算符是大于‘+’,其余处理如上题

#include <iostream>
#include <string>
#include <bits/stdc++.h>

using namespace std;
char ch[]= {'=','(','+','-','*','/',')',','};
double lpri[]= {0,1,3,3,5,5,6,1.5};
double rpri[]= {0,6,2,2,4,4,1,2.5};
map<char,double> Ml;
map<char,double> Mr;
void Init_op()
{
    for(int i=0; i<8; i++)
    {
        Ml[ch[i]]=lpri[i];
        Mr[ch[i]]=rpri[i];
    }
}
bool IsOp(char ch)
{
    if(ch=='('||ch==')'||ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='='||ch==',')
        return true;
    else
        return false;
}
string str;
int n;
stack<char> op;
stack<string> postexp;
void post_exp()
{
    Init_op();
    int len=str.length();
    op.push('=');
    string str_num;
    for(int i=0; i<len; i++)
    {
        if(str[i]=='S'||str[i]=='m'||str[i]=='a'||str[i]=='x')
            continue;
        char temp = op.top();
        if(IsOp(str[i]))
        {
            if(Mr[str[i]]>Ml[temp])
                op.push(str[i]);
            else if(Mr[str[i]]<Ml[temp])
            {
                char temp_op,temp_ch;
                temp_op = op.top();
                temp_ch = str[i];
                if(temp_ch ==')')
                {
                    while(op.top()!='(')
                    {
                        char t=op.top();
                        op.pop();
                        string t_str;
                        t_str+=t;
                        postexp.push(t_str);
                    }
                    op.pop();
                }
                else
                {
                    while(Ml[op.top()]>Mr[temp_ch])
                    {
                        char t=op.top();
                        op.pop();
                        string t_str;
                        t_str+=t;
                        postexp.push(t_str);
                    }
                    op.push(temp_ch);
                }
            }
            else
                op.pop();
        }
        else
        {
            str_num+=str[i];
            if(IsOp(str[i+1])&&!str_num.empty())
            {
                postexp.push(str_num);
                str_num.clear();
            }
        }
    }
    postexp.push(str_num);
    str_num.clear();
    while(!op.empty())
    {
        char temp_op=op.top();
        op.pop();
        if(temp_op!='=')
        {
            string str_t;
            str_t+=temp_op;
            postexp.push(str_t);
        }
    }
}
int f(int a)
{
    int sum=0;
    while(a)
    {
        sum+=a%10;
        a=a/10;
    }
    return sum;
}
int Smax(int a,int b)
{
    int sum1=f(a),sum2=f(b);
    return max(sum1,sum2);
}
int Count(int a,char c,int b)
{
    if(c=='+')
        return a+b;
    if(c=='-')
        return a-b;
    if(c=='*')
        return a*b;
    if(c=='/')
        return a/b;
    if(c==',')
        return Smax(a,b);
}
int compvalue()
{
    string str_post[1005];
    int len=0;
    while(!postexp.empty())
    {
        str_post[len++]=postexp.top();
        postexp.pop();
    }
    stack<int> op_digit;
    int d;
    char c;
    for(int i=len-1; i>=0; i--)
    {
        stringstream ss;
        if(str_post[i]=="+"||str_post[i]=="-"||str_post[i]=="*"||str_post[i]=="/"||str_post[i]==",")
        {
            c=str_post[i][0];
            int a,b;
            b=op_digit.top();
            op_digit.pop();
            a=op_digit.top();
            op_digit.pop();
            op_digit.push(Count(a,c,b));
        }
        else
        {
            ss<<str_post[i];
            ss>>d;
            op_digit.push(d);
        }
    }
    int sum;
    while(!op_digit.empty())
    {
        sum=op_digit.top();
        op_digit.pop();
    }
    return sum;
}
int main()
{
    cin>>n;
    while(n--)
    {
        cin>>str;
        str+='=';
        post_exp();
        printf("%d\n",compvalue());
    }
    return 0;
}

表达式求值
时间限制:3000 ms | 内存限制:65535 KB
难度:3
描述
Dr.Kong设计的机器人卡多掌握了加减法运算以后,最近又学会了一些简单的函数求值,比如,它知道函数min(20,23)的值是20 ,add(10,98) 的值是108等等。经过训练,Dr.Kong设计的机器人卡多甚至会计算一种嵌套的更复杂的表达式。
假设表达式可以简单定义为:
1. 一个正的十进制数 x 是一个表达式。
2. 如果 x 和 y 是 表达式,则 函数min(x,y )也是表达式,其值为x,y 中的最小数。
3. 如果 x 和 y 是 表达式,则 函数max(x,y )也是表达式,其值为x,y 中的最大数。
4.如果 x 和 y 是 表达式,则 函数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 <bits/stdc++.h>

using namespace std;
int add(int a,int b){
    return a+b;
}
int compevalue(string str)
{
    int len=str.length();
    stack<int> op_num;
    int d=0;string str_num;
    for(int i=len-1;i>=0;i--)
    {
        if(isdigit(str[i]))
        {
            str_num+=str[i];
            if(!isdigit(str[i-1]))
            {
                for(int j=str_num.length()-1;j>=0;j--)
                    d=d*10+(str_num[j]-'0');
                op_num.push(d);
                str_num.clear();
                d=0;
            }
        }
        else if(str[i]=='d'&&str[i+1]=='d')
        {
            int a=op_num.top();op_num.pop();
            int b=op_num.top();op_num.pop();
            op_num.push(add(a,b));
        }
        else if(str[i]=='x'){
            int a=op_num.top();op_num.pop();
            int b=op_num.top();op_num.pop();
            op_num.push(max(a,b));
        }
        else if(str[i]=='n'){
            int a=op_num.top();op_num.pop();
            int b=op_num.top();op_num.pop();
            op_num.push(min(a,b));
        }
        else
            continue;
    }
    int sum;
    while(!op_num.empty())
    {
        sum=op_num.top();
        op_num.pop();
    }
    return sum;
}
int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        string str;
        cin>>str;
        cout<<compevalue(str)<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值