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


    #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;
    }
  • 相关阅读:
    解决一道leetcode算法题的曲折过程及引发的思考
    算法研究:一维多项式求值(秦九韶算法)
    通过HWND获得CWnd指针
    将标题空格替换为 '_' , 并自动复制到剪切板上
    稀疏矩阵操作算法
    微信好友分析之展示好友信息
    微信好友分析之获取好友信息
    爬取当当网的图书信息之结尾
    爬取当当网的图书信息之封装一个工具类
    爬取当当网的图书信息之实体设计
  • 原文地址:https://www.cnblogs.com/Chips/p/5376348.html
Copyright © 2020-2023  润新知