华为机试-四则运算

题目描述

输入一个表达式(用字符串表示),求这个表达式的值。

保证字符串中的有效字符包括[‘0’-‘9’],‘+’,‘-’, ‘*’,‘/’ ,‘(’, ‘)’,‘[’, ‘]’,‘{’ ,‘}’。且表达式一定合法。

 

输入描述:

输入一个算术表达式

输出描述:

得到计算结果

 

示例1

输入

3+2*{1+2*[-4/(8-6)+7]}

输出

25

利用两个stack,一个存储计算结果的值,一个存储符号,留意的地方是计算结果的值需要考虑正负号

#include <iostream>
#include <stack>
#include <cstring>
using namespace std;

//将所有的括号转换为()
void convertStr(string& str){
    for(int i=0;i<str.size();i++){
        if(str[i]=='['||str[i]=='{'){
            str[i]='(';
        }else if(str[i]==']'||str[i]=='}'){
            str[i]=')';
        }
    }
}

int operation(int v1,int v2,char operate){
    int res=0;
    switch(operate){
        case '+':res=v1+v2;break;
        case '-':res=v1-v2;break;
        case '*':res=v1*v2;break;
        case '/':res=v1/v2;break;
    }
    return res;
}

int computing(string& str){
    int flag=0;//标记正负号,0表示无符号,1表示正号,2表示负号
    stack<int> nums;
    stack<char> operas;
    for(int i=0;i<str.size();i++){
        if(isdigit(str[i])){
            int j=i;
            while(i+1<str.size()&&isdigit(str[i+1])) ++i;
            string str_num=str.substr(j,i-j+1);
            int num=stoi(str_num);//转换为数字
            if(flag==2) num=0-num;
            flag=0;//使用完毕数字的正负号归0
            nums.push(num);
        }else if(str[i]=='*'||str[i]=='/'||str[i]=='('){
            operas.push(str[i]);//直接入栈
        }else if(str[i]=='+'||str[i]=='-'){
            if(i==0||str[i-1]=='('){//先判断是不是数字的正负号
                if(str[i]=='+') flag=1;
                else flag=2;
            }
            //遇到加减号,先进行计算
            while(flag==0&&!operas.empty()&&operas.top()!='('){
                //非空,符号弹出
                int v1=nums.top();nums.pop();
                int v2=nums.top();nums.pop();
                char opera=operas.top();operas.pop();
                nums.push(operation(v2,v1,opera));
            }
            if(flag==0) operas.push(str[i]);//证明通过operation得到的
        }else if(str[i]==')'){
            while(operas.top()!='('){
                int v1=nums.top();nums.pop();
                int v2=nums.top();nums.pop();
                char opera=operas.top();operas.pop();
                nums.push(operation(v2,v1,opera));
            }
            operas.pop();//弹出左括号
        }
    }
    //最后计算栈中的数字
    while(!operas.empty()){
        //非空,符号弹出
        int v1=nums.top();nums.pop();
        int v2=nums.top();nums.pop();
        char opera=operas.top();operas.pop();
        nums.push(operation(v2,v1,opera));
    }
    return nums.top();
}
int main(int argc,char* argv[]){
    string str;
    while(cin>>str){
        convertStr(str);
        cout<<computing(str)<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路上的追梦人

您的鼓励就是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值