数组结构的栈实现

Stack.h

#pragma once

template <class T>
class Stack {
public:
	virtual void clear() = 0;			// clean the stack
	virtual void push(const T&) = 0;	// push T into stack
	virtual void pop() = 0;				// pop top item in stack
	virtual T& top() = 0;				// return the top item reference
	virtual bool empty() = 0;			// is stack empty?
	virtual bool full() = 0;			// is stack full?
};

ArrStack.h

#pragma once

#include <iostream>
#include <exception>

#include "Stack.h"

template <class T>
class ArrStack : Stack<T> {
	T* stack;
	size_t Maxsize;
	size_t stackTop = -1;
	size_t size;

	void checkUnderFlow() {
		if (empty()) {
			throw std::exception("Stack underflow");
		}
		return;
	}

	void checkOverFlow() {
		if (size + 1 > Maxsize) {
			throw std::exception("Stack overflow");
		}
		return;
	}

public:
	ArrStack(size_t size = 1024)
		:stack(new T[size]), Maxsize(size), stackTop(0), size(0) {}
	~ArrStack() {
		delete[] stack;
	}

	virtual void clear() override {
		stackTop = -1;
		size = 0;
		return;
	}

	virtual void push(const T& val) override {
		checkOverFlow();
		stack[size + 1] = val;
		++size;
		return;
	}

	virtual void pop() override {
		checkUnderFlow();
		--size;
	}

	virtual T& top() override {
		checkUnderFlow();
		return stack[size];
	}

	virtual bool empty() override {
		return size == 0;
	}

	virtual bool full() override {
		return size == Maxsize;
	}

	void showStack() {
		int tmp = size;
		while (tmp) {
			std::cout << "Stack current top value is " << stack[tmp] << std::endl;
			--tmp;
		}
		return;
	}
};

main.cpp

// ArrStack.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>

#include "ArrStack.h"

using namespace std;

// Class Declaration 类的说明
class Calculator {
private:
    ArrStack<double> s;		// 这个栈用于压入保存操作数
    bool GetTwoOperands(double& opd1, double& opd2); 	// 从栈顶弹出两个操作数opd1和opd2
    void Compute(char op);  	// 调用GetTwoOperands,并按op运算对两个操作数进行计算
public:
    //       Calculator(){} ;		// 创建计算器实例,开辟一个空栈
    void Run();		    // 后缀表达式的读入,在遇到符号"="时 ,启动求值计算 
    void Clear();   		// 计算器的清除,为随后的下一次计算做准备  
};

// 计算器类class Calculator中部分成员函数的程序实现
bool Calculator::GetTwoOperands(double& opd1, double& opd2) {
    if (s.empty()) {
        cerr << "Missing operand!" << endl;
        return false;
    }
    opd1 = s.top(); 							// 右操作数
    s.pop();
    if (s.empty()) {
        cerr << "Missing operand!" << endl;
        return false;
    }
    opd2 = s.top();						// 左操作数
    s.pop();
    return true;
}

void Calculator::Compute(char op) {
    bool result;
    double operand1, operand2;
    result = GetTwoOperands(operand1, operand2);
    if (result == true) {
        switch (op) {
        case '+': 	s.push(operand2 + operand1);
            break;
        case '-': 	s.push(operand2 - operand1);
            break;
        case '*': 	s.push(operand2 * operand1);
            break;
        case '/':	if (operand1 == 0.0) {
            cerr << "Divide by 0!" << endl;
            s.clear();
            } else {
                s.push(operand2 / operand1);
            }
        }
    }
    else {
        s.clear();
    }
}

void Calculator::Run(void) {
    char c;
    double newOperand, res;
    while (cin >> c, c != '=') {
        switch (c) {
        case '+':
        case '-':
        case '*':
        case '/':
            Compute(c);
            break;
        default:
            cin.putback(c);
            cin >> newOperand;
            s.push(newOperand);
            break;
        }
    }
    if (!s.empty()) {
        res = s.top();
        cout << res << endl; 					// 印出求值的最后结果
    }
}



int main()
{
    cout << "================Calculator begin================" << endl;

    cout << "Enter the number with operator, use '=' to end the operation" << endl;

    cout << "Example: '3 4 + 5 * =' means '(3+4)*5" << endl;

    Calculator* cal = new Calculator();
    cal->Run();

    cout << "================Calculator end================" << endl;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值