利用后缀表达式计算四则运算(codeup 1918)

问题 A: 简单计算器

时间限制 : 1.000 sec  内存限制 : 32 MB
 

题目描述

读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。

输入

测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。

输出

对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。

样例输入 Copy

30 / 90 - 26 + 97 - 5 - 6 - 13 / 88 * 6 + 51 / 29 + 79 * 87 + 57 * 92
0

样例输出 Copy

12178.21

 * 将中缀表达式转换为后缀表达式可以直接从左到右进行运算,不用考虑中间
 * 运算符的优先级。

/**
 * 将中缀表达式转换为后缀表达式可以直接从左到右进行运算,不用考虑中间
 * 运算符的优先级。
*/

#include <iostream>
#include <queue>
#include <stack>
#include <map>

using namespace std;

struct Node
{
    double num; //运算数
    char op;    //运算符
    bool flag;  //true表示操作数,false表示操作符
};

stack<Node> stk;    //操作符栈
queue<Node> q;      //后缀表达式
map<char,int> op;   //将运算符优先级映射成数字,数字越大,优先级越高

void get_beh(string &str)
{
    while(stk.size())   //清空栈的内容
        stk.pop();
        
    Node tm;
    for(int i=0;i<str.size();)
    {
        if(isdigit(str[i])) 
        {
            tm.flag=true;
            tm.num=str[i++]-'0';
            while(i<str.size() && isdigit(str[i]))//也许运算数并不止一位
                tm.num=tm.num*10+str[i++]-'0';
            q.push(tm);
        }
        else
        {
            tm.flag=false;
            while(!stk.empty() && op[stk.top().op] >= op[str[i]])
            {   //当栈顶的运算符优先级大于或等于该运算符
                q.push(stk.top());
                stk.pop();
            }
            tm.op=str[i++];
            stk.push(tm);
        }
    }
    
    while(stk.size())   //也许栈还有运算符,加入后缀表达式中
    {
        q.push(stk.top());
        stk.pop();
    }
}

double cal()
{
    while(q.size()) //只要后缀表达式中还有值
    {
        Node tm = q.front();    //取出队首元素
        q.pop();
        if(tm.flag == true)
            stk.push(tm);
        else
        {
            double temp2=stk.top().num;
            stk.pop();  //栈顶是第二个操作数
            double temp1=stk.top().num;
            stk.pop();  //栈的第二个元素才是第一个运算数
            
            if(tm.op == '+')
                tm.num=temp1+temp2;
            else if(tm.op == '-')
                tm.num=temp1-temp2;
            else if(tm.op == '*')
                tm.num=temp1*temp2;
            else
                tm.num=temp1/temp2;
            stk.push(tm);   //将得到的结果加入栈内
        }
    }
    
    return stk.top().num;   //只要是正确的运算式,那么栈此时有唯一一个元素,也是该算式的答案
}

int main()
{
    op['+'] = op['-'] =1;
    op['*'] = op['/'] =2;
    string str;
    while(getline(cin,str), str!="0")
    {
        string temp;
        for(int i=0;i<str.size();++i)
            if(!isspace(str[i]))
                temp+=str[i]; //将空格统统去掉
        str=temp;   
        
        get_beh(str);
        
        printf("%.2f\n",cal());
    }
    
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值