一个用栈实现后缀表达式的程序帮帮看一 下有没有错误,谢谢
#include <iostream.h>
#include <math.h>
#include <stack.h>
#include <assert.h>
using namespace std;
enum Boolean{False,True};
template<class Type>class Stack{
public:
Stack (int=10);
~Stack(){delete[]elements;}
void Push(const Type&item);
Type Pop();
Type GetTop();
void MakeEmpty(){top=-1;}
int IsEmpty()const{return top==-1;}
int IsFull()const{return top==maxSize-1};
private:
int top;
Type *elements;
int maxSize;
};
template<class Type> Stack<Type>::Stack(int s):top(-1),maxSize(s){
elements=new Type[maxSize];
assert(elements!=0);
}
template<class Type>void Stack<Type>::Push(const Type&item){
assert(!IsFull());
elements[++top]=item;
}
template<class Type>Type Stack<Type>::Pop(){
assert(!IsEmpty());
return elements[top--];
}
template<class Type>Type Stack<Type>::GetTop(){
assert(!Is Empty());
return elements[top];
}
class Calculator{
public:
Calculator(int sz):s(sz){}
void Run();
void Clear();
private:
void AddOperand(double value);
Boolean Get2Operands(double&left,double&right);
void DoOperand(char op);
stack<double>s;
};
void Calculator::AddOperand(double value){
s.Push(value);
}
Boolean Calculator:: Get2OPerands(double&left,double&right){
if (s.IsEmpty()){
cerr<<"Missing Operand!"<<endl; return Flase;
}
right=s.Pop();
if(s.IsEmpty()){
cerr<<"Missing Operand!"<<endl; return Flase;
}
left=s.Pop();
reyurn True;
}
void Calculator::DoOperator(char op){
double left,right; Boolean result;
result=Get2Operands(left,right);
if(result==True)
switch(op){
case'+': s.Push(left+right);break;
case'-': s.Push(left-right);break;
case'*': s.Push(left*right);break;
case'/': if(right==0.0){
cerr<<"Divide by 0!"<<endl; Clear();
}
elses.Push(result=left/right);break;
case'^': s.Push(Power(left,right));break;
}
else Clear();
}
void Calculator::Run(){
char c[20];
while(cin>>d,*c!='q')
switch(*c)
{
case'c': S.ClearStack();
break;
case'-'
if(strlen(c)>1)
AddOperand(atoi));
else DoOperator (*c);
break;
case'+':
case'*':
case'/':
case'^':
Do Operator(*c);
break;
default:
AddOperand(atoi(c));
break;
}
}
void Calculator::Clear(void)
{S.ClearStack();}
//1.cpp
int main()
{
Calculator CALC;
CALC.Run();
}