c++:stack

#ifndef  STACK_CLASS
#define STACK_CLASS
#include<iostream>
#include<cstdlib>
using namespace std;
const int MaxStackSize = 50;
template<class T>
class Stack{
private:
	T stacklist[MaxStackSize];
	int top;
public:
	Stack(void);
	void Push(const T& item);
	T Pop(void);
	void ClearStack(void);
	T Peek(void) const;
	int StackEmpty(void) const;
	int StackFull(void) const;
};
template<class T>
Stack<T>::Stack(void) :top(-1){}
template<class T>
void Stack<T>::Push(const T& item){
	if (top == MaxStackSize - 1){
		std::cerr << "Stack overflow" << endl;
		exit(1);
	}
	top++;
	stacklist[top] = item;
}
template<class T>
T Stack<T>::Pop(void){
	T temp;
	if (top == -1){
		std::cerr << "attempt to pop an empty stack!" << endl;
		exit(1);
	}
	temp = stacklist[top];
	top--;
	return temp;
}
template<class T>
T Stack<T>::Peek(void)  const{
	if (top == -1){
		std::cerr << "attempt to peek at an empty stack" << endl;
		exit(1);
	}
	return stacklist[top];
}
template<class T>
int Stack<T>::StackEmpty(void) const{
	return top == -1;
}
template<class T>
int Stack<T>::StackFull(void) const{
	return top == MaxStackSize - 1;
}
template<class T>
void Stack<T>::ClearStack(void){
	top = -1;
}
#endif
<pre class="cpp" name="code">#include<iostream>
#include<cmath>
#include<cstdlib>
#include<cstring>
using namespace std;
enum Boolean{False,True};
#include"Stack.h"
class Calculator{
private:
	Stack<int> S;
	void Enter(int num);
	Boolean GetTwoOperands(int &opnd1, int &opnd2);
	void Compute(char op);
public:
	Calculator(void);
	void Run(void);
	void Clear(void);
};
void Calculator::Enter(int num){
	S.Push(num);
}
Boolean Calculator::GetTwoOperands(int &opnd1, int &opnd2){
	if (S.StackEmpty()){
		cerr << "Missing operand!" << endl;
		return False;
	}
	opnd1 = S.Pop();
	if (S.StackEmpty()){
		cerr << "Missing operand;;" << endl;
		return False;
	}
	opnd2 = S.Pop();
	return True;
}
void Calculator::Compute(char op){
	Boolean result;
	int operand1, operand2;
	result = GetTwoOperands(operand1, operand2);
	if (result == True){
		switch (op){
		case '+':S.Push(operand1 + operand2);
			break;
		case '-':S.Push(operand1 - operand2);
			break;
		case '*':S.Push(operand1 * operand2);
			break;
		case '/':if (operand1 == 0){
					 cerr << "divide by 0" << endl;
					 S.ClearStack();
		}
else
				 S.Push(operand2 / operand1);
			break;
		case '^':S.Push(pow(operand2, operand1));
			break;
		}
		cout << '=' << S.Peek() << ' ';
	}
else
	S.ClearStack();
}
Calculator::Calculator(void){  }
void Calculator::Run(void){
	char c[20];
	while (cin >> c, *c != 'q')
		switch (*c){
		case 'c':S.ClearStack();
			break;
		case '-':if (strlen(c) > 1)
			Enter(atoi(c));
				 else
					 Compute(*c);
			break;
		case '+':
		case '*':
		case '/':
		case '^':
			Compute(*c);
			break;
		default:
			Enter(atoi(c));
			break;
	}
}
void Calculator::Clear(void){
	S.ClearStack();
}
#include"Calculator.h"
int main(){
	Calculator Calculator;
	Calculator.Run();
}




posted on 2014-08-20 21:05  _noname  阅读(132)  评论(0编辑  收藏  举报