利用栈实现简单计算器的例子(Calculator)(1)
/* Calculator.h */#ifndef __CALCULATOR_H__#define __CALCULATOR_H__
#include <iostream.h>#include "Stack.h"
class Calculator {private: //存放操作数的栈 Stack<double> s; //将一个double型操作数压入栈中 void Enter(double operand) { s.Push(operand); } //从栈顶读取两个操作数 int GetTwoOperands(double &operand1, double &operand2) { if (s.StackEmpty()) { cerr << "No operand to pop!" << endl; s.ClearStack(); return 0; } operand1 = s.Pop(); if (s.StackEmpty()) { cerr << "No operand to pop!" << endl; s.ClearStack(); return 0; } operand2 = s.Pop(); return 1; } //将调用GetTwoOperands读取的两个操作数做运算op void Compute(char op) { double operand1, operand2, result; if (!GetTwoOperands(operand1, operand2)) return; switch(op) { case '+': result = operand1 + operand2; break;