主题:表达式计算问题
enum Boolean{False,True};
#include<iostream.h>
#include<math.h>
#include<assert.h>
template<class Type> class Stack
{
public:
Stack(int s=100);
~Stack(){delete[] elements;}
void Push(const Type& item);
Type Pop();
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--];
}
class Calculator
{
public:
Calculator(int sz):s(sz){}
void Run();
void Clear();
private:
void AddOperand(double value);
Boolean Get2Operands(double &left,double &right);
void DoOperator(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 False;
}
right=s.Pop();
if(s.IsEmpty())
{
cerr<<"Missing Operand!"<<endl;return False;
}
left=s.Pop();
return True;
}
void Calculator::DoOperator(char op)
{
double left,right,value;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();
}
else
s.Push(value=left/right);break;
}
else
Clear();
}
void Calculator::Clear()
{
s.MakeEmpty();
}
void Calculator::Run()
{
char ch;double newoperand;
while(cin>>ch,ch!='=')
{
switch(ch)
{
case '+':
case '-':
case '*':
case '/':
DoOperator(ch);break;
default:
cin.putback(ch);
AddOperand(newoperand);
break;
}
}
}
int main()
{
Calculator CALC(100);
CALC.Clear();
CALC.Run();
return 0;
}
这个就是完整的程序,我用VC++6.0运行的,说是第27行(assert(!IsFull());)
的毛病,大家上机运一下就知道了,assert中断了程序,我是实在不知道怎么回事,高手帮帮忙吧
#include<iostream.h>
#include<math.h>
#include<assert.h>
template<class Type> class Stack
{
public:
Stack(int s=100);
~Stack(){delete[] elements;}
void Push(const Type& item);
Type Pop();
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--];
}
class Calculator
{
public:
Calculator(int sz):s(sz){}
void Run();
void Clear();
private:
void AddOperand(double value);
Boolean Get2Operands(double &left,double &right);
void DoOperator(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 False;
}
right=s.Pop();
if(s.IsEmpty())
{
cerr<<"Missing Operand!"<<endl;return False;
}
left=s.Pop();
return True;
}
void Calculator::DoOperator(char op)
{
double left,right,value;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();
}
else
s.Push(value=left/right);break;
}
else
Clear();
}
void Calculator::Clear()
{
s.MakeEmpty();
}
void Calculator::Run()
{
char ch;double newoperand;
while(cin>>ch,ch!='=')
{
switch(ch)
{
case '+':
case '-':
case '*':
case '/':
DoOperator(ch);break;
default:
cin.putback(ch);
AddOperand(newoperand);
break;
}
}
}
int main()
{
Calculator CALC(100);
CALC.Clear();
CALC.Run();
return 0;
}
这个就是完整的程序,我用VC++6.0运行的,说是第27行(assert(!IsFull());)
的毛病,大家上机运一下就知道了,assert中断了程序,我是实在不知道怎么回事,高手帮帮忙吧