后缀表达式

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

// abstract data type for stack
 
template <class T> 					// 栈的元素类型为 T
class Stack { 
public: 	            			// 栈的运算集
    void clear();	   			   // 变为空栈
   	bool push(const T item); 		// item入栈,成功则返回真,否则返回假
 	bool pop(T* item);				// 返回栈顶内容并弹出,成功返回真,否则返回假,
 	bool getTop(T* item);	  			// 返回栈顶内容但不弹出成功返回真,否则返回假,
   	bool isEmpty(); 	   			// 若栈已空返回真
   	bool isFull();        			// 若栈已满返回真
};


//  array-based stack: definition and implementation for some methods
template <class T> 	
class arrStack : public Stack<T> {      
private: 	                    	// 栈的顺序存储
		int		mSize;			// 栈中最多可存放的元素个数
		T 		*st;	  		// 存放栈元素的数组
public:  
         int		top;					// 栈顶位置,应小于mSize 
public:  						// 栈的运算的顺序实现
	arrStack(int size){ 				// 创建一个顺序栈的实例
		mSize = size;
		top = -1;
		st = new T[mSize];
	}
	arrStack(){
        top = -1;
    }
	~arrStack() {					// 析构函数
		delete [] st;
	}
	void clear() {					// 清空栈内容
		top = -1;
    }
	bool push(const T item) { 	   		// 入栈操作的顺序实现	
		if (top == mSize-1) {  			// 栈已满 
			cout << "栈满溢出" << endl;
			return false;
		}
		else {					// 新元素入栈并修改栈顶指针
			st[++top] = item;
			return true;
		}
	}
	bool pop(T* item) { 	        	                  	// 出栈的顺序实现
		if (top == -1) {			                       // 栈为空
			cout << "栈为空,不能出栈操作"<< endl; 
			return false;
		}
		else {
			*item = st[top--];	         // 返回栈顶元素并修改栈顶指针
			return true;
		}
	}
	bool getTop(T* item) {				     // 返回栈顶内容,但不弹出
		if (top == -1) {			         // 栈空
			cout << " 栈为空,不能出栈操作"<< endl; 
			return false;
		}
		else { 						
			*item = st[top];
			return true;
		}
    }
    bool isEmpty() { 
         return (top == -1);
    }
    bool isFull() { 
         return (top == mSize-1);
    }
};



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.isEmpty()) {
    		cerr << "Missing operand!" <<endl;
    		return false;
   	}
     s.pop(&opd1); 							// 右操作数
   	if (s.isEmpty())  {
    		cerr << "Missing operand!" <<endl;
    		return false;
   	}
   	s.pop(&opd2)	;						// 左操作数
   	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);
                   	break;
    		}
  	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.isEmpty()) {
		s.pop(&res);
        cout << res << endl; 					// 印出求值的最后结果
   }		
}



int main(int argc, char *argv[])
{
    int s;
    cout << "begin" << endl;
    

     Calculator *cal = new Calculator();
     cal->Run();
         
     cout << "end" << endl;
      cin >> s;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值