#include "calculator.h"
#include <iostream>
using namespace std;
//construction
Calculator::Calculator(){
command=' ';
}
//get_command
void Calculator::get_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'||command=='c')
waiting=false;
else
cout<<"Please enter a valid command:"<<endl
<<"[?]push to stack [=]print top "<<endl
<<"[+][-][*][/] are arithmetic operations"<<endl
<<"[c]clean the stack"<<endl
<<"[Q] quit"<<endl;
}
}
//do_command
bool Calculator::do_command(){
get_command();
switch(command){
case '?':
PutInNum();
break;
case '+':
Add();
break;
case '-':
Sub();
break;
case '*':
Mul();
break;
case '/':
Div();
break;
case 'c':
Clear();
break;
case '=':
ShowResult();
break;
case 'q':
return false;
}
return true;
}
//Once enter '?': put the next number into stack
void Calculator::PutInNum(){
Stack_entry p;
cout<<"Enter a real number: "<<flush;
cin>>p;
if(numbers.push(p)==overflow)
cout<<"Warning:Stack full,lost number"<<endl;
}
//Once enter '+' : Add the numbers in the stack
void Calculator::Add(){
Stack_entry p,q;
if(numbers.top(p)==underflow)
cout<<"Stack empty"<<endl;
else{
numbers.top(p);
numbers.pop();
if(numbers.top(q)==underflow){
cout<<"Stack has just one entry"<<endl;
numbers.push(p);
}
else{
numbers.pop();
if(numbers.push(p+q)==overflow)
cout<<"Warning:Stack full,lost result"<<endl;
else
numbers.push(p+q);
}
}
}
//Once enter '-': Subtract the numbers in the stack
void Calculator::Sub(){
Stack_entry p,q;
if(numbers.top(p)==underflow)
cout<<"Stack empty"<<endl;
else{
numbers.top(p);
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<<"Warning:Stack full,lost result"<<endl;
else
numbers.push(q-p);
}
}
}
//Once enter'*': Multiply the numbers in the stack
void Calculator::Mul(){
Stack_entry p,q;
if(numbers.top(p)==underflow)
cout<<"Stack empty"<<endl;
else{
numbers.top(p);
numbers.pop();
if(numbers.top(q)==underflow){
cout<<"Stack has just one entry"<<endl;
numbers.push(p);
}
else{
numbers.pop();
if(numbers.push(p*q)==overflow)
cout<<"Warning:Stack full,lost result"<<endl;
else
numbers.push(p*q);
}
}
}
//Once enter '/': Divide the numbers in the stack
void Calculator::Div(){
Stack_entry p,q;
if(numbers.top(p)==underflow)
cout<<"Stack empty"<<endl;
else{
numbers.top(p);
numbers.pop();
if(p==0){
cout<<"Bed command! The second number is 0 and cannot finishi divide command!"<<endl;
}
else 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<<"Warning:Stack full,lost result"<<endl;
else
numbers.push(q/p);
}
}
}
//Once enter '=':Show the result of least command
void Calculator::ShowResult(){
Stack_entry p;
if(numbers.top(p)==underflow)
cout<<"Stack empty"<<endl;
else
cout<<p<<endl;
}
//Once enter 'q':Stop the calculation
void Calculator::Quit(){
cout<<"Calculation finished!"<<endl;
}
//Once enter 'c':Clear the stack
void Calculator::Clear(){
numbers.clear();
}
没有合适的资源?快使用搜索试试~ 我知道了~
顺序栈各种基本运算算法的实现
共24个文件
cpp:3个
obj:3个
h:2个
5星 · 超过95%的资源 需积分: 50 49 下载量 40 浏览量
2012-07-16
11:40:23
上传
评论
收藏 673KB ZIP 举报
温馨提示
栈中数据用数组储存,通过top(),push(),pop()基本的函数用以实现其功能。 参见博客:http://blog.csdn.net/xiaowei_cqu/article/details/7748152
资源推荐
资源详情
资源评论
收起资源包目录
StackCalculator.zip (24个子文件)
StackCalculator
StackCalculator.sln 911B
StackCalculator.suo 15KB
Debug
StackCalculator.exe 43KB
StackCalculator.pdb 603KB
StackCalculator.ilk 435KB
StackCalculator
stack.h 383B
calculator.h 601B
StackCalculator.vcproj.SEB32.student.user 1KB
StackCalculator.vcproj.PC2010031614HZV.Administrator.user 1KB
StackCalculator.vcproj 4KB
calculator.cpp 4KB
stack.cpp 804B
Debug
stack.obj 6KB
vc90.idb 307KB
vc90.pdb 212KB
mt.dep 69B
calculator.obj 55KB
StackCalculator.exe.intermediate.manifest 621B
StackCalculator.exe.embed.manifest 663B
StackCalculator.exe.embed.manifest.res 728B
BuildLog.htm 7KB
TryCalculatro.obj 23KB
TryCalculatro.cpp 201B
StackCalculator.ncb 1.1MB
共 24 条
- 1
资源评论
- Bin-Zhang2014-04-19真心佩服,正在向你学习。
- stevenashpanl12014-04-29确实很不错啊,像lz学习
- red_iris2016-09-26实用,可以参考学习
- sgs20142015-04-26还是很不错的,新手学习学习了
- fudeking2013-10-25要是多点注释就更好了!!
xiaowei_cqu
- 粉丝: 1w+
- 资源: 35
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功