📄 postexp.h
字号:
#include "LinStack.h"
template <class T> class PostExp
{
private:
LinStack<T> s;
void Enter(T num);
int GetTwoOperands(T& operand1, T& operand2);
void Compute(char op);
public:
PostExp(void){};
void Run(void);
void Clear(void);
};
template <class T>
void PostExp<T>::Enter(T num)
{
s.Push(num);
}
template <class T>
int PostExp<T>::GetTwoOperands(T& operand1, T& operand2)
{
if(s.StackEmpty())
{
cerr << "缺少操作数!" << endl;
return 0;
}
operand1 = s.Pop();
if(s.StackEmpty())
{
cerr << "缺少操作数!" << endl;
return 0;
}
operand2 = s.Pop();
return 1;
}
template <class T>
void PostExp<T>::Compute(char op)
{
int result;
T operand1, operand2;
result = GetTwoOperands(operand1, operand2);
if(result = 1)
switch(op)
{
case '+': s.Push(operand2 + operand1);
break;
case '-': s.Push(operand2 - operand1);
break;
case '*': s.Push(operand2 * operand1);
break;
case '/': s.Push(operand2 / operand1);
break;
}
else
s.ClearStack();
}
template <class T>
void PostExp<T>::Run(void)
{
char c;
T newOperand;
cout << "输入后缀表达式, 以#号结束:" << endl;
while(cin >> c, c != '#')
{
switch(c)
{
case '+':
case '-':
case '*':
case '/': Compute(c);
break;
default: cin.putback(c);
cin >> newOperand;
Enter(newOperand);
break;
}
}
if(!s.StackEmpty())
cout << s.Peek() << endl;
}
template <class T>
void PostExp<T>::Clear(void)
{
s.ClearStack();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -