C++简易计算器

//简易计算器
//最简版(不含任何查错)
#include <iostream>
#include<stack>
#include<string>
using namespace std;

class calculator {
public:
    calculator() {
        infix_str = "";
        postfix_str = "";
        result = 0;
    };
    string transform(string);//中缀表达式转后缀表达式
    double calculation(string);//后缀表达式计算出结果
    void get_infix() {
        cin >> infix_str;
    };

    string infix_str;//中缀表达式
    string postfix_str;//后缀表达式
    double result;//运算结果
};

string calculator::transform(string infix)
{
    string postfix = "";//最终要返回的后缀表达式
    stack<char> C_stack;
    int i = 0, j = 0;// i->infix下标,j->postfix下标
    char c;
    while (i<infix.size())
    {
        while (infix[i] <= '9' && infix[i] >= '0' || infix[i] == '.')//如果是数字部分,直接放到后缀表达式中
        {
            postfix.push_back(infix[i]);//中缀转后缀
            i++;
            if ((infix[i] > '9' || infix[i] < '0' )&& infix[i] != '.')//如果数字部分结束,加' '分隔
                postfix.push_back(' ');
        }
        switch (infix[i])         //对非数字进行处理
        {
            case ')':
                c = C_stack.top();
                C_stack.pop();  //取出栈顶元素
                while (c != '(')      //出栈运算符,直到找到'( '
                    {
                        postfix.push_back(c);//把运算符加入后缀表达式
                        postfix.push_back(' ');
                        c = C_stack.top();
                        C_stack.pop();
                    }
                break;
            case '+':
            case '-':
                if (!C_stack.empty())//如果栈不为空
                    {
                        c = C_stack.top();//取出栈顶元素
                        C_stack.pop();
                    }
                else {
                    C_stack.push(infix[i]);
                    break;
                }
                while (!C_stack.empty() && c != '(')//当栈非空且栈顶元素不是'('就出栈栈顶元素
                    {
                        postfix.push_back(c);//运算符加入后缀表达式
                        postfix.push_back(' ');
                        if (!C_stack.empty())//如果栈内非空,出栈栈顶元素
                            {
                                c = C_stack.top();
                                C_stack.pop();
                            }
                    }//whlie结束,此时栈为空或者已经出栈的栈顶元素为'(';
                if (c == '(')//放回栈中
                    C_stack.push(c);
                else {
                    postfix.push_back(c);//运算符加入后缀表达式
                    postfix.push_back(' ');
                }
                C_stack.push(infix[i]);//+,- 进栈
                break;
            case '*':
            case '/':      //优先级最高,如果是这些就直接入栈
            case '(':
                C_stack.push(infix[i]);
                break;
        }//switch结束
        i++;//进行下一个部分
    }//while结束,i==infix.size()                       
    while (!C_stack.empty())//如果栈内非空,接下来出栈所有元素,加入后缀表达式
        {
            c = C_stack.top();
            C_stack.pop();
            postfix.push_back(c);
            postfix.push_back(' ');
        }
    cout << "后缀表达式为:" << endl;
    cout << postfix << endl;
    return postfix;
}

double calculator::calculation(string postfix)
{
    stack<double> D_stack;
    double a_num, b_num;
    int i = 0;
    while (i<postfix.size())
    {
        if (postfix[i] <= '9' && postfix[i] >= '0' || postfix[i] == '.')//数字处理部分
        {
            int next_pos = postfix.find(' ',i);
            string str = postfix.substr(i, next_pos - i);//获取子串
            a_num = stod(str);//string转为double
            D_stack.push(a_num);
            i = 1 + next_pos;
            if (i >= postfix.size())
                break;
            continue;
        }
        switch (postfix[i])
            {
                case '+':
                    a_num = D_stack.top();
                    D_stack.pop();
                    if (D_stack.empty())//单目运算::+1
                        {
                            D_stack.push(a_num);
                            break;
                        }
                    b_num = D_stack.top();
                    D_stack.pop();
                    D_stack.push(a_num + b_num);
                    break;
                case '-':
                    a_num = D_stack.top();
                    D_stack.pop();
                    if (D_stack.empty())//负数运算
                    {
                        D_stack.push(a_num*(-1));
                        break;
                    }
                    b_num = D_stack.top();
                    D_stack.pop();
                    D_stack.push(b_num - a_num);
                    break;
                case '*':
                    a_num = D_stack.top();
                    D_stack.pop();;
                    b_num = D_stack.top();
                    D_stack.pop();
                    D_stack.push(a_num * b_num);
                    break;
                case '/':
                    a_num = D_stack.top();
                    D_stack.pop();
                    b_num = D_stack.top();
                    D_stack.pop();
                    D_stack.push(b_num / a_num);
                    break;
                default://' '
                    break;
            }//switch结束
        i++;
    }//    while结束
    return D_stack.top();
}

int main()
{
    calculator cal;
    while (1) {
        cal.get_infix();
        cal.postfix_str=cal.transform(cal.infix_str);
        cal.result = cal.calculation(cal.postfix_str);
        cout << cal.result << endl;
    }
    return 0;
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值