数据结构 课后作业03 整形计算器

#include <iostream>
#include <string>
using namespace std;

enum error_code1 {
    success, overflow, underflow
};

template<class T>
class stack {
public:
    stack();
    bool empty()const;
    bool full()const;
    error_code1 get_top(T& x)const;
    error_code1 push(const T x);
    error_code1 pop();
private:
    int count;
    T data[100];
};

template <class T>
stack<T>::stack()
{
    count = 0;
}

template <class T>
bool stack<T>::empty()const {
    return count == 0;
}

template <class T>
bool stack<T>::full()const {
    return count == 100;
}

template <class T>
error_code1 stack<T>::get_top(T& x)const {
    if (empty()) return underflow;
    x = data[count - 1];
    return success;
}

template <class T>
error_code1 stack<T>::push(const T x) {
    if (full()) return overflow;
    data[count] = x;
    count++;
    return success;
}

template <class T>
error_code1 stack<T>::pop() {
    if (empty()) return underflow;
    count--;
    return success;
}

template <class T>
bool ReferenceError(error_code1 a) {
    if (a == overflow) {
        cout << "overflow!" << endl;
        return false;
    }
    if (a == underflow) {
        cout << "underflow!" << endl;
        return false;
    }
    return true;
}


//优先级
int Priority(char n) {
    if (n == '+' || n == '-')
        return 1;
    else if (n == '*' || n == '/')
        return 2;
    else if (n == '(' || n == ')')
        return 0;
    else
        return -1;
}

//四则运算
int calculate(char x, stack<int>& s) {
    int a, b;
    int c = 0;
    s.get_top(b);
    s.pop();
    s.get_top(a);
    s.pop();
    switch (x) {
    case '+': c = a + b;
        break;
    case '-': c = a - b;
        break;
    case '*': c = a * b;
        break;
    case '/': c = a / b;
        break;
    }
    return c;
}

void calculator() {
    stack<int> s1;
    stack<char> s2;
    string s;
    cin >> s;
    cout << "计算得:" << s;
    s += '#';
    s2.push('#');
    for (int i = 0; i <= s.size(); i++) {
        if (s[i] > '0' && s[i] < '9') {
            int m = s[i] - 48;
            while (s[i + 1] > '0' && s[i + 1] < '9') {
                i++;
                m = m * 10 + s[i] - 48;
            }
            s1.push(m);
        }
        else {
            int k = 1;
            char x, y;
            s2.get_top(x);
            while (k) {
                if (Priority(s[i]) > Priority(x)) {
                    s2.push(s[i]);
                    break;
                }
                else if (s[i] == '(') {
                    s2.push(s[i]);
                    break;
                }
                else if (Priority(s[i]) == Priority(x) && Priority(s[i]) == 0) {
                    s2.pop();
                    break;
                }
                else if (Priority(s[i]) == Priority(x) && Priority(s[i]) == -1) {
                    s2.pop();
                    break;
                }
                else {
                    s1.push(calculate(x, s1));
                    s2.pop();
                    s2.get_top(y);
                    x = y;
                }
            }
        }
    }
    int k;
    s1.get_top(k);
    cout << k << endl;
}

int main() {
    calculator();
    return 0;
}

栈结构独立

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值