MIOJ #16 四则运算

描述

实现一个算法,可以进行任意非负整数的加减乘除组合四则运算。

请注意运算符的优先级。

输入

请输入一行算式,使用空格分隔数字与运算符。

数字为任意非负整数,运算符为+ – * /,不考虑括号。

输出

输出算式的运算结果。如果是小数,请向下取整(包含中间步骤结果)。 如果出现“除0异常”,输出err。

输入样例

3 + 5
12 + 45 / 9
1 / 2
1 / 0
12 + 34 * 56 - 78

输出样例

8
17
0
err
1838

小提示

可以使用栈来解决此类问题。

AC代码:

#include <bits/stdc++.h>
#include<vector>
#include<stack>
#include<sstream>
using namespace std;

int main()
{
    stack<string>a;
    string num;
    bool flag = false;
    bool err = false;
    string s;
    while(getline(cin,s))
    {
        vector<string>symbol;//用来存符号
        vector<string>number;//用来存数字
        num = "";
        for(int i = 0; i<=s.length(); i++)
        {
            if(s[i] =='+' || s[i] =='-' || s[i] =='*' || s[i] =='/' )
            {
                if(s[i] == '+')
                {
                    symbol.push_back("+");
                }
                else if(s[i] == '-')
                {
                    symbol.push_back("-");
                }
                else if(s[i] == '*')
                {
                    symbol.push_back("*");
                }
                else if(s[i] == '/')
                {
                    symbol.push_back("/");
                }
                continue;
            }
            while(s[i] >='0' && s[i] <= '9')
            {
                num = num + s[i];
                i++;
                flag = true;

            }
            if(flag == true)
            {
                number.push_back(num);
                num  = "";
                flag = false;
            }
        }
        vector<string>b; //存中缀表达式
        vector<string>c;//存后缀表达式
        stack<string>d;
        for(int i = 0; i < symbol.size(); i++)
        {
            b.push_back(number[i]);
            b.push_back(symbol[i]);
        }
        b.push_back(number[number.size()-1]);
        for(int i = 0; i<b.size(); i++)
        {
            if(b[i] == "+" || b[i] == "-" || b[i] == "*" || b[i] == "/")
            {
                if(d.empty())
                {
                    d.push(b[i]);
                }
                else if(d.top() == "+" || d.top() == "-")
                {
                    if(b[i] == "*" || b[i] == "/")
                    {
                        d.push(b[i]);
                    }
                    else
                    {
                        c.push_back(d.top());
                        d.pop();
                        d.push(b[i]);
                    }
                }
                else if(d.top() == "*" || d.top() == "/")
                {

                    if(b[i] == "*" || b[i] == "/")
                    {
                        c.push_back(d.top());
                        d.pop();
                        d.push(b[i]);
                    }
                    else
                    {

                        c.push_back(d.top());
                        d.pop();
                        if(d.empty())
                        {
                            d.push(b[i]);
                        }
                        else
                        {
                            c.push_back(d.top());
                            d.pop();
                            d.push(b[i]);
                        }

                    }

                }
            }
            else
            {
                c.push_back(b[i]);
            }
        }
        while(!d.empty())
        {
            c.push_back(d.top());
            d.pop();
        }
        stack<string>think;
        int num1 = -1;
        int num2 = -1;
        int res = 0;
        string resStr ;
        stringstream ss;
        for(int i = 0; i<c.size(); i++)
        {
            err = false;
            if(c[i] == "+")
            {
                ss << think.top();
                ss >>num2;
                ss.clear();
                think.pop();
                ss << think.top();
                ss >> num1;
                ss.clear();
                think.pop();
                res = num1 + num2;
                ss << res;
                ss >> resStr;
                ss.clear();
                think.push(resStr);
            }
            else if(c[i] == "-")
            {
                ss << think.top();
                ss >>num2;
                ss.clear();
                think.pop();
                ss << think.top();
                ss >> num1;
                ss.clear();
                think.pop();
                res = num1 - num2;
                ss << res;
                ss >> resStr;
                ss.clear();
                think.push(resStr);
            }
            else if(c[i] == "*")
            {
                ss << think.top();
                ss >>num2;
                ss.clear();
                think.pop();
                ss << think.top();
                ss >> num1;
                ss.clear();
                think.pop();
                res = num1 * num2;
                ss << res;
                ss >> resStr;
                ss.clear();
                think.push(resStr);
            }
            else if(c[i] == "/")
            {
                ss << think.top();
                ss >>num2;
                ss.clear();
                think.pop();
                ss << think.top();
                ss >> num1;
                ss.clear();
                think.pop();
                if(num2 == 0){
                    err = true;
                    break;
                }
                res = num1 / num2;
                ss << res;
                ss >> resStr;
                ss.clear();
                think.push(resStr);
            }
            else
            {
                think.push(c[i]);
            }
        }
        if(err){
            cout<<"err"<<endl;
        }
        else{
        cout<<think.top()<<endl;
        err = false;
        }
    }
}

 

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注