表达式求值

表达式的三种记法:前缀表达式,中缀表达式,后缀表达式。

eg:

(1-2)*3+4/5:中缀表达式

12-3*45/+:后缀表达式

+*-123/45:前缀表达式

表达式求值(输入为int,除法为整除):

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstring>
#include<stack>
#include<map>
using std::string;
using std::map;
using std::stack;
using std::cin;
using std::cout;
using std::endl;
string TOPostfix(string infix)//中缀式 转 后缀式
{
    infix="("+infix+")";  

    map<char,int>priority;//优先级
    string postfix; //后缀式
    stack<char>mark;//符号栈

    priority['+']=0;
    priority['-']=0;
    priority['*']=1;
    priority['/']=1;

    for(string::const_iterator str=infix.begin();str!=infix.end();++str)
    {
        switch(*str)
        {
            case '0':case '1':case '2':case '3':case '4':
            case '5':case '6':case '7':case '8':case '9'://数字
            postfix.push_back(*str);
            break;

            case '-':case '+':case '*':case '/'://运算符
            if(*(str-1)!=')')
                postfix.push_back('|');
            while(!mark.empty()&&mark.top()!='('&&priority[mark.top()]>=priority[*str])//优先级大的先放
            {
                postfix.push_back(mark.top());
                mark.pop();
            }
            mark.push(*str);
            break;

            case '('://左括号
            mark.push(*str);
            break;

            case ')'://右括号
            if(*(str-1)!=')')
                postfix.push_back('|');
            while(mark.top()!='(')//清空符号栈
            {
                postfix.push_back(mark.top());
                mark.pop();
            }
            mark.pop();
        }
    }
   // cout<<postfix<<endl;
    return postfix;
}
int Clac(string postfix)
{
    string strNum;//字符数字
    stack<int>Res;//结果
    int temp;//运算时用到的缓存变量
    for(string::iterator str=postfix.begin();str!=postfix.end();++str)
    {
        switch(*str)
        {
            case '0':case '1':case '2':case '3':case '4':
            case '5':case '6':case '7':case '8':case '9':
            strNum.push_back(*str);//字符数字
            break;

            case '|'://str数字转int
            temp=atoi(strNum.c_str());//atoi():str->int string.c_str()返回一个指向正规C字符串的指针
            strNum.clear();
            Res.push(temp);
            break;

            case '+'://+运算
            temp=Res.top();
            Res.pop();
            Res.top()+=temp;
            break;

            case '-'://-运算
            temp=Res.top();
            Res.pop();
            Res.top()-=temp;
            break;
            
            case '*'://*运算
            temp=Res.top();
            Res.pop();
            Res.top()*=temp;
            break;
             
            case '/':// /运算
            temp=Res.top();
            Res.pop();
            Res.top()/=temp;
        }
    }
    return Res.top();
}
int main()//整数,整除
{
    string infix,postfix;
    cin>>infix;
    postfix=TOPostfix(infix);
    int res=Clac(postfix);
    cout<<res<<endl;

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值