简易计算器(但得保证输入合法哟)

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <string>
#include <cstdlib>
#include <stdio.h>
#include<algorithm>
#include<stack>
using namespace std;

class calc{
private:
    char * expression;
    enum token {OPAREN,ADD,SUB,MULTI,DIV,EXP,CPAREN,VALUE,ENL};
    void Binaryop(token op, stack<int> &datastack);//
    token getop(int &value);//

public:
    calc(char* s);
    ~calc(){delete [] expression;}
    int result();

};

calc::calc(char*s)
{
    expression = new char[strlen(s)+1];
    strcpy(expression,s);
}

calc::token calc::getop(int &value)
{
    value = 0;
    while(*expression)
    {
        while(*expression && *expression == ' ')
            ++expression;
        if(*expression >= '0' && *expression <= '9')
        {
            while(*expression >= '0' && *expression <= '9')
            {
                value = value * 10 + (*expression)-'0';
                expression ++;
            }
            return VALUE;
        }
        switch (*expression)
        {
        case '(': expression++;return OPAREN;break;
        case ')': expression++;return CPAREN;break;
        case '+': expression++;return ADD;break;
        case '-': expression++;return SUB;break;
        case '*': expression++;return MULTI;break;
        case '/': expression++;return DIV;break;
        case '^': expression++;return EXP;break;
        default:
            break;
        }
        
    }
    return ENL;
}
void calc::Binaryop(token op, stack<int> & datastack)
{
    int num2 = datastack.top();
    datastack.pop();
    int num1 = datastack.top();
    datastack.pop();
    switch (op)
    {
    case ADD: datastack.push(num1+num2);break;
    case SUB: datastack.push(num1-num2);break;
    case MULTI: datastack.push(num1*num2);break;
    case DIV: datastack.push(num1/num2);break;
    case EXP: datastack.push(pow(num1,num2));break;
    default:
        break;
    }


}

int calc::result()
{
    token nextop,topop;
    stack<token> opstack;
    stack<int> datastack;
    int resultvalue;
    int currentvalue;
    while(1)
    {
        currentvalue = 0;
        nextop = getop(currentvalue);
        if (nextop==ENL) break;
        switch (nextop)
        {
        case VALUE:datastack.push(currentvalue);break;
        case OPAREN:opstack.push(OPAREN);break;
        case EXP: opstack.push(EXP);break;
        case ADD: case SUB:
            while(!(opstack.empty()) && (topop=opstack.top())!=OPAREN)
            {
                Binaryop(opstack.top(),datastack);
                opstack.pop();
            }
            opstack.push(nextop);break;
        case MULTI: case DIV:
            while(!(opstack.empty()) && (topop= opstack.top())!=OPAREN && topop>= MULTI)
            {
                Binaryop(opstack.top(),datastack);
                opstack.pop();
            }
            opstack.push(nextop);break;
        case CPAREN:
            while(!(opstack.empty()) && (topop = opstack.top())!=OPAREN)
            {
                Binaryop(topop,datastack);
                opstack.pop();
            }
            opstack.pop();break;

        default:
            break;
        }

    }
    while(!opstack.empty())
    {
        Binaryop(opstack.top(),datastack);
        opstack.pop();
    }
    return datastack.top();
}

char data[101000];
int main(){
    scanf("%s",data);
    calc tmp(data);
    cout<<tmp.result();



    getchar();
    getchar();
    return 0;
}
原文地址:https://www.cnblogs.com/Chips/p/5376348.html