#include "stack.h"
#include <iostream>
using namespace std;
char get_command() //输入运算符
{
char command;
bool waiting=true;
cout<<"Select command and press <Enter>:";
while(waiting)
{
cin>>command;
command = tolower(command); //变小写
if(command=='?'||command=='='||command=='+'||command=='-'||command=='*'||command=='/'||command=='q')
waiting = false;
else
{
cout<<"Please enter a valid command:"<<endl
<<"[?]push to stack [=]print top"<<endl
<<"[+][-][*][/] are arithmetic operations"<<endl
<<"[Q]uit."<<endl;
}
}
return command;
}
bool do_command(char command,Stack &numbers)
{
double p,q;
switch(command)
{
case '?':
cout<<"Enter a real number:"<<flush;
cin>>p;
if(numbers.push(p)==overflow)
cout<<"Waring:Stack full,lost number"<<endl;
break;
case '=':
if(numbers.top(p)==underflow)
cout<<"Stack empty"<<endl;
else
cout<<p<<endl;
break;
case '+':
if(numbers.top(p)==underflow)
cout<<"Stack empty"<<endl;
else
{
numbers.pop();
if(numbers.top(q)==underflow)
{
cout<<"Stack has just one entry"<<endl;
numbers.push(p);
}
else
{
numbers.pop();
if(numbers.push(q+p)==overflow)
cout<<"Waring:Stack full,lost result"<<endl;
}
}
break;
case '-':
if(numbers.top(p)==underflow)
cout<<"Stack empty"<<endl;
else
{
numbers.pop();
if(numbers.top(q)==underflow)
{
cout<<"Stack has just one entry"<<endl;
numbers.push(p);
}
else
{
numbers.pop();
if(numbers.push(q-p)==overflow)
cout<<"Waring:Stack full,lost result"<<endl;
}
}
break;
case '*':
if(numbers.top(p)==underflow)
cout<<"Stack empty"<<endl;
else
{
numbers.pop();
if(numbers.top(q)==underflow)
{
cout<<"Stack has just one entry"<<endl;
numbers.push(p);
}
else
{
numbers.pop();
if(numbers.push(q*p)==overflow)
cout<<"Waring:Stack full,lost result"<<endl;
}
}
break;
case '/':
if(numbers.top(p)==underflow)
cout<<"Stack empty"<<endl;
else
{
numbers.pop();
if(numbers.top(q)==underflow)
{
cout<<"Stack has just one entry"<<endl;
numbers.push(p);
}
else
{
numbers.pop();
if(numbers.push(q/p)==overflow)
cout<<"Waring:Stack full,lost result"<<endl;
}
}
break;
case 'q':
cout<<"Calculation finished.\n";
return false;
}
return true;
}
int main()
{
Stack stored_numbers;
while(do_command(get_command(),stored_numbers));
}
逆波兰计算器C++源码
4星 · 超过85%的资源 需积分: 12 164 浏览量
2010-04-12
12:11:05
上传
评论
收藏 134KB RAR 举报
zhu_546629
- 粉丝: 0
- 资源: 5
最新资源
- 基于Matlab人脸肤色定理的教师人数统计+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab霍夫曼变换的表盘读数识别+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab火灾烟雾检测源码带GUI界面+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab的恶劣天气交通标志识别系统+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB的霍夫曼变换的表盘示数识别+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab的车道线识别系统 +源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB的教室人数统计系统带Gui界面+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB的教室人数统计系统带Gui界面+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于MATLAB 的霍夫曼变换答题卡识别源码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
- 基于Matlab+bp神经网络的神经网络汉字识别系统+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈