首页 > 代码库 > 反转波兰计算器
反转波兰计算器
#include <iostream>
using namespace std;
typedef double stackEntry;
const int overflow = 1;
const int underflow = 2;
const int success = 0;
const int maxstack = 100;//栈的最大尺寸
class stack{
public:
stack();
int pop();
int push(const stackEntry &item);
int top(stackEntry &item) const;
bool empty() const;
private:
int count;
stackEntry data[maxstack];
};
int stack::push(const stackEntry &item)//如果栈未满,将元素item压入栈顶,否则报错
{
if(count>=maxstack){
cout<<"栈上溢,无法压入元素。";
return overflow;
}
else{
data[count++] = item;
return success;
}
}
int stack::pop()//如果栈未空,将栈顶元素删除,否则报错
{
if(count == 0){
cout<<"栈下溢,无法弹出元素。";
return underflow;
}
else{
--count;
return success;
}
}
int stack::top(stackEntry &item) const//如果栈未空,将栈顶元素取出放在item里,否则报错
{
if(count == 0){
cout<<"栈下溢,无法读取元素。";
return underflow;
}
else{
item = data[count - 1];
return success;
}
}
bool stack::empty() const //判断栈是否为空
{
if(count > 0)
return false;
return true;
}
stack::stack()//构建函数,初始化一个空栈
{
count = 0;
}
bool executeCommand(char cmd,stack &operands);
char getCommand();
//反转波兰计算器
int main()
{
stack operands;//定义栈operands,用来存放操作数
cout<<"本程序实现一个反转波兰计算器。用户按后缀形式输入算术表达试,"<<endl
<<"本计算器的合法操作命令如下:"<<endl
<<"[?]压栈"<<endl
<<"[=]打印栈顶元素"<<endl
<<"[+][-][*][/]算术操作符"<<endl
<<"[Q]退出程序."<<endl;
cout<<"请输入命令:"<<endl;
while(executeCommand(getCommand(),operands));
}
char getCommand()
{
char cmd;
bool waiting = true;
cout<<"选择命令然后按回车键:";
while(waiting){
cin>>cmd;
cmd = tolower(cmd);
if(cmd == ‘?‘||cmd == ‘=‘||cmd == ‘+‘||cmd == ‘-‘||cmd == ‘*‘||cmd == ‘/‘||cmd == ‘q‘)
waiting = false;
else{
cout<<"请输入一个合法命令:"<<endl
<<"[?]压栈"<<endl
<<"[=]打印栈顶元素"<<endl
<<"[+][-][*][/]算术操作符"<<endl
<<"[Q]退出程序."<<endl;
}
}
return cmd;
}
bool executeCommand(char cmd,stack &operands)
//将命令cmd应用到栈里的元素上,当cmd=‘q‘时程序结束
{
double p,q;
switch(cmd){
case‘?‘:
cout<<"请输入一个实数:"<<endl;
cin>>p;
if(operands.push(p) == overflow)
cout<<"警告:栈已满,数据丢失"<<endl;
break;
case‘=‘:
if(operands.top(p)==underflow)
cout<<"栈下溢,无法打印栈顶元素"<<endl;
else
cout<<p<<endl;
break;
case‘+‘:
if(operands.top(p) == underflow)
cout<<"空栈"<<endl;
else{
operands.pop();
if(operands.top(q) == underflow){
cout<<"栈里只有一个元素"<<endl;
operands.push(p);
}else{
operands.pop();
if(operands.push(q+p) == overflow)
cout<<"警告:栈上溢,结果丢失"<<endl;
}
}
break;
case‘-‘:
if(operands.top(p) == underflow)
cout<<"空栈"<<endl;
else{
operands.pop();
if(operands.top(q) == underflow){
cout<<"栈里只有一个元素"<<endl;
operands.push(p);
}else{
operands.pop();
if(operands.push(q-p) == overflow)
cout<<"警告:栈上溢,结果丢失"<<endl;
}
}
break;
case‘*‘:
if(operands.top(p) == underflow)
cout<<"空栈"<<endl;
else{
operands.pop();
if(operands.top(q) == underflow){
cout<<"栈里只有一个元素"<<endl;
operands.push(p);
}else{
operands.pop();
if(operands.push(q*p) == overflow)
cout<<"警告:栈上溢,结果丢失"<<endl;
}
}
break;
case‘/‘:
if(operands.top(p) == underflow)
cout<<"空栈"<<endl;
else{
operands.pop();
if(operands.top(q) == underflow){
cout<<"栈里只有一个元素"<<endl;
operands.push(p);
}else{
operands.pop();
if(operands.push(q/p) == overflow)
cout<<"警告:栈上溢,结果丢失"<<endl;
}
}
break;
case‘q‘:
cout<<"计算结束.\n";\
return false;
}
return true;
}
反转波兰计算器