用栈做逆波兰式表达式的计算,为什么没结果输出?
#include <iostream>
#include <cassert>
#include <cmath>
using namespace std;
class Stack;
class StackNode
{
friend class Stack;
private:
double data;
StackNode* link;
StackNode(double d=0.0, StackNode* l=NULL):data(d),link(l){}
};
class Stack
{
private:
StackNode* top;
public:
Stack():top(NULL){}
~Stack();
void Push(const double& item);
double Pop();
double GetTop();
void MakeEmpty();
bool IsEmpty() const {return top == NULL;}
};
Stack::~Stack()
{
StackNode* p;
while (top != NULL)
{
p = top;
top = top-> link;
delete p;
}
}
void Stack::MakeEmpty()
{
StackNode* p;
while (top != NULL)
{
p = top;
top = top-> link;
delete p;
}
}
void Stack::Push(const double& item)
{
top = new StackNode(item,top);
}
double Stack::Pop()
{
assert(!IsEmpty());
StackNode* p = top;
double retvalue = p-> data;
top = top-> link;
delete p;
return retvalue;
}
double Stack::GetTop()
{
assert(!IsEmpty());
return top-> data;
}
class Calculator
{
private:
void AddOperand(double value); //进操作数栈
bool Get2Operands(double& left, double& right);
void DoOperator(char op);
Stack s;
public:
CalCulator();
void Run();
void Clear();
};
int main()
{
Calculator a;
a.Run();
return EXIT_SUCCESS;
}
void Calculator::AddOperand(double value)
{
s.Push(value);
}
bool Calculator::Get2Operands(double& left, double& right)
{
if (s.IsEmpty())
{
cerr < < "操作数不存在! " < <endl;
Clear();
return false;
}
right = s.Pop();
if (s.IsEmpty())
{
cerr < < "操作数不存在! " < <endl;
Clear();
return false;
}
left = s.Pop();
return true;
}
void Calculator::DoOperator(char op)
{
double left = 0,
right = 0;
bool result = Get2Operands(left,right);
if (result == true)
{
switch(op)
{
case '+ ':
s.Push(left+right);
break;
case '- ':
s.Push(left-right);
break;
case '/ ':
if (right == 0.0)
{
cerr < < "除数不能为○! " < <endl;
Clear();
}
else
s.Push(left/right);
break;
case '* ':
s.Push(left*right);
break;
case '^ ':
s.Push(pow(left,right));
break;
}
}
else
Clear();
}
void Calculator::Run()
{
char ch;
double newoperand;
while (cin> > ch,ch!= '# ')
{
switch(ch)
{
case '+ ':
case '- ':
case '* ':
case '/ ':
DoOperator(ch);
break;
default:
cin.putback(ch);
cin> > newoperand;
AddOperand(newoperand);
break;
}
}
}
void Calculator::Clear()
{
s.MakeEmpty();
}
程序编译通过了.可没结果出来..
[解决办法]
调试呀,单步跟踪。
[解决办法]
你一个输出都没有,怎么能要求有显示