#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();
}