coder  •  7个月前


#include <iostream>
#include "strings.h"

using namespace std;
template<class T>
class ArrStack{
public:
    T *arr;
    int MAXSIZE;
    int top = -1;
public:
    ArrStack(int maxsize){
        this->MAXSIZE = maxsize;
        arr = new T[this->MAXSIZE];
    }
    bool isFull(){
        return top == MAXSIZE-1;
    }
    bool isEmpty(){
        return top == -1;
    }
    void push(int value){

        if(isFull()){
            cout<<"栈满"<<endl;
            return;
        }

        top++;
        arr[top] = value;
    }

    //出栈
    int pop(){
        if (isEmpty()){
            throw new runtime_error("栈空\n");
        }
        int t;
        t = arr[top];
        top--;
        return t;
    }
    void show(){
        for (int i = top; i >= 0 ; --i) {
            cout<<i<<": "<<arr[i]<<"  ";
        }
        cout<<"\n";
    }

    //获取栈顶元素,不出栈
    int getTop(){
        return arr[top];
    }


    //操作符优先级判断
    int priority(char oper){
        if (oper == '*'||oper=='/'){
            return 1;
        } else if (oper == '+'||oper=='-'){
            return 0;
        } else{
            return -1;
        }
    }
    //判断是否是一个运算符
    bool isOper(char op){
        return op=='+'||op=='-'||op=='*'||op=='/';
    }
    /**
     *
     * @param num1 操作数1
     * @param num2 操作数2
     * @param op 运算符
     * @return
     */
    int cal(int num1, int num2,char op){
        int res = 0;
        switch (op) {
            case '+':
                res = num1+num2;
                break;
            case '-':
                res = num2-num1;
                break;
            case '*':
                res = num1*num2;
                break;
            case '/':
                res = num2/num1;
                break;
            default:
                break;
        }

        return res;
    }
};
int main() {
    string exp
    ;
    cin>>exp;
    //创建两个栈   数字栈  符号栈
    ArrStack<int> numarr(exp.size());
    ArrStack<char> oparr(exp.size());
    int index = 0;//用于扫描
    int num1 = 0;
    int num2  = 0;
    char op = ' ';
    int res = 0;
    char ch = ' ';//每次扫描的
    while (true){
        ch = exp[index];
        if (oparr.isOper(ch)){//如果是运算符
            //判断符号栈是否为空
            if (!oparr.isEmpty()){
                //处理
                if (oparr.priority(ch)<=oparr.priority(oparr.getTop())){//符号栈中的元素优先级 大于等于 当前扫描到的元素
                    num1=numarr.pop();
                    num2  = numarr.pop();
                    op = oparr.pop();
                    res = numarr.cal(num1 , num2,op);
                    //运算结果入数栈
                    numarr.push(res%10000);
                    //当前操作符入栈
                    oparr.push(ch);
                } else{//符号栈中的元素小于单钱扫描到的元素
                    oparr.push(ch);
                }
            } else{//符号栈为空,直接入栈
                oparr.push(ch);
            }
        } else{
            int t = 0;
            while (!oparr.isOper(exp[index])&&index<exp.size()){
                t  =t*10+(exp[index]-48);
                index++;
            }
            numarr.push(t%10000);
            index--;
        }
        index++;
        if (index>=exp.size()){
            break;
        }
    }
    while (true){
        //如果符号栈为空。则计算结束 , 数栈中只有一个数值
        if(oparr.isEmpty()){
            break;
        }else{
            num1=numarr.pop();
            num2  = numarr.pop();
            op = oparr.pop();
            res = numarr.cal(num1 , num2,op);
            //运算结果入数栈
            numarr.push(res%10000);
        }
    }
//    printf("表达式结果为:%d",numarr.getTop());
        printf("%d",numarr.getTop()%10000);
    return 0;
}

评论:

请先登录,才能进行评论