¡¡¡¡//push()
¡¡¡¡Error_code Stack::push(const Stack_entry &item){
¡¡¡¡Error_code outcome=success;
¡¡¡¡if(count>=stackmax)
¡¡¡¡outcome=overflow;
¡¡¡¡else
¡¡¡¡entry[count++]=item;
¡¡¡¡return outcome;
¡¡¡¡}
¡¡¡¡//top()
¡¡¡¡Error_code Stack::top(Stack_entry &item){
¡¡¡¡Error_code outcome=success;
¡¡¡¡if(count==0)
¡¡¡¡outcome=underflow;
¡¡¡¡else
¡¡¡¡item=entry[count-1];
¡¡¡¡return outcome;
¡¡¡¡}
¡¡¡¡//clear()
¡¡¡¡Error_code Stack::clear(){
¡¡¡¡count=0;
¡¡¡¡return success;
¡¡¡¡}
¡¡¡¡//empty()
¡¡¡¡bool Stack::empty() const{
¡¡¡¡bool outcome=true;
¡¡¡¡if(count!=0)
¡¡¡¡outcome=false;
¡¡¡¡return outcome;
¡¡¡¡}
¡¡¡¡#include"stack.h"
¡¡¡¡//implemention
¡¡¡¡//Stack()
¡¡¡¡Stack::Stack(){
¡¡¡¡count=0;
¡¡¡¡}
¡¡¡¡//pop()
¡¡¡¡Error_code Stack::pop(){
¡¡¡¡Error_code outcome=success;
¡¡¡¡if(count==0)
¡¡¡¡outcome=underflow;
¡¡¡¡else
¡¡¡¡count--;
¡¡¡¡return outcome;
¡¡¡¡}
¡¡¡¡//push()
¡¡¡¡Error_code Stack::push(const Stack_entry &item){
¡¡¡¡Error_code outcome=success;
¡¡¡¡if(count>=stackmax)
¡¡¡¡outcome=overflow;
¡¡¡¡else
¡¡¡¡entry[count++]=item;
¡¡¡¡return outcome;
¡¡¡¡}
¡¡¡¡//top()
¡¡¡¡Error_code Stack::top(Stack_entry &item){
¡¡¡¡Error_code outcome=success;
¡¡¡¡if(count==0)
¡¡¡¡outcome=underflow;
¡¡¡¡else
¡¡¡¡item=entry[count-1];
¡¡¡¡return outcome;
¡¡¡¡}
¡¡¡¡//clear()
¡¡¡¡Error_code Stack::clear(){
¡¡¡¡count=0;
¡¡¡¡return success;
¡¡¡¡}
¡¡¡¡//empty()
¡¡¡¡bool Stack::empty() const{
¡¡¡¡bool outcome=true;
¡¡¡¡if(count!=0)
¡¡¡¡outcome=false;
¡¡¡¡return outcome;
¡¡¡¡}
¡¡¡¡caculator.h
¡¡¡¡[cpp] view plaincopyprint
¡¡¡¡#ifndef CALCULATOR_H
¡¡¡¡#define CALCULATOR_H
¡¡¡¡#include"stack.h"
¡¡¡¡class Calculator{
¡¡¡¡public:
¡¡¡¡//constructor
¡¡¡¡Calculator();
¡¡¡¡//public function to get command
¡¡¡¡void get_command();
¡¡¡¡//pubblic funtion to do the command
¡¡¡¡bool do_command();
¡¡¡¡protected:
¡¡¡¡//As these functions will not be visible to the client
¡¡¡¡//but should be derived classes to access so I declare it as protected functions
¡¡¡¡void PutInNum();
¡¡¡¡void Add();
¡¡¡¡void Sub();
¡¡¡¡void Mul();
¡¡¡¡void Div();
¡¡¡¡void ShowResult();
¡¡¡¡void Clear();
¡¡¡¡void Quit();
¡¡¡¡private:
¡¡¡¡//data members
¡¡¡¡char command;
¡¡¡¡Stack numbers;
¡¡¡¡};
¡¡¡¡#endif
¡¡¡¡#ifndef CALCULATOR_H
¡¡¡¡#define CALCULATOR_H
¡¡¡¡#include"stack.h"
¡¡¡¡class Calculator{
¡¡¡¡public:
¡¡¡¡//constructor
¡¡¡¡Calculator();
¡¡¡¡//public function to get command
¡¡¡¡void get_command();
¡¡¡¡//pubblic funtion to do the command
¡¡¡¡bool do_command();
¡¡¡¡protected:
¡¡¡¡//As these functions will not be visible to the client
¡¡¡¡//but should be derived classes to access so I declare it as protected functions
¡¡¡¡void PutInNum();
¡¡¡¡void Add();
¡¡¡¡void Sub();
¡¡¡¡void Mul();
¡¡¡¡void Div();
¡¡¡¡void ShowResult();
¡¡¡¡void Clear();
¡¡¡¡void Quit();
¡¡¡¡private:
¡¡¡¡//data members
¡¡¡¡char command;
¡¡¡¡Stack numbers;
¡¡¡¡};
¡¡¡¡#endif
¡¡¡¡caculator.cpp
¡¡¡¡[cpp] view plaincopyprint
¡¡¡¡#include "calculator.h"
¡¡¡¡#include
¡¡¡¡using namespace std;
¡¡¡¡//construction
¡¡¡¡Calculator::Calculator(){
¡¡¡¡command=' ';
¡¡¡¡}
¡¡¡¡//get_command
¡¡¡¡void Calculator::get_command(){
¡¡¡¡bool waiting =true;
¡¡¡¡cout<<"Select command and press
¡¡¡¡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:"< ¡¡¡¡<<"[ ]push to stack [=]print top "< ¡¡¡¡<<"[+][-][*][/] are arithmetic operations"< ¡¡¡¡<<"[c]clean the stack"< ¡¡¡¡<<"[Q] quit"< ¡¡¡¡} ¡¡¡¡} ¡¡¡¡//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: "< ¡¡¡¡cin>>p; ¡¡¡¡if(numbers.push(p)==overflow) ¡¡¡¡cout<<"Warning:Stack full,lost number"< ¡¡¡¡} ¡¡¡¡//Once enter '+' : Add the numbers in the stack ¡¡¡¡void Calculator::Add(){ ¡¡¡¡Stack_entry p,q; ¡¡¡¡if(numbers.top(p)==underflow) ¡¡¡¡cout<<"Stack empty"< ¡¡¡¡else{ ¡¡¡¡numbers.top(p); ¡¡¡¡numbers.pop(); ¡¡¡¡if(numbers.top(q)==underflow){ ¡¡¡¡cout<<"Stack has just one entry"< ¡¡¡¡numbers.push(p); ¡¡¡¡} ¡¡¡¡else{ ¡¡¡¡numbers.pop(); ¡¡¡¡if(numbers.push(p+q)==overflow) ¡¡¡¡cout<<"Warning:Stack full,lost result"< ¡¡¡¡} ¡¡¡¡} ¡¡¡¡} ¡¡¡¡//Once enter '-': Subtract the numbers in the stack ¡¡¡¡void Calculator::Sub(){ ¡¡¡¡Stack_entry p,q; ¡¡¡¡if(numbers.top(p)==underflow) ¡¡¡¡cout<<"Stack empty"< ¡¡¡¡else{ ¡¡¡¡numbers.top(p); ¡¡¡¡numbers.pop(); ¡¡¡¡if(numbers.top(q)==underflow){ ¡¡¡¡cout<<"Stack has just one entry"< ¡¡¡¡numbers.push(p); ¡¡¡¡} ¡¡¡¡else{ ¡¡¡¡numbers.pop(); ¡¡¡¡if(numbers.push(q-p)==overflow) ¡¡¡¡cout<<"Warning:Stack full,lost result"< ¡¡¡¡} ¡¡¡¡} ¡¡¡¡} ¡¡¡¡//Once enter'*': Multiply the numbers in the stack ¡¡¡¡void Calculator::Mul(){ ¡¡¡¡Stack_entry p,q; ¡¡¡¡if(numbers.top(p)==underflow) ¡¡¡¡cout<<"Stack empty"< ¡¡¡¡else{ ¡¡¡¡numbers.top(p); ¡¡¡¡numbers.pop(); ¡¡¡¡if(numbers.top(q)==underflow){ ¡¡¡¡cout<<"Stack has just one entry"< ¡¡¡¡numbers.push(p); ¡¡¡¡} ¡¡¡¡else{ ¡¡¡¡numbers.pop(); ¡¡¡¡if(numbers.push(p*q)==overflow) ¡¡¡¡cout<<"Warning:Stack full,lost result"< ¡¡¡¡} ¡¡¡¡} ¡¡¡¡} ¡¡¡¡//Once enter '/': Divide the numbers in the stack ¡¡¡¡void Calculator::Div(){ ¡¡¡¡Stack_entry p,q; ¡¡¡¡if(numbers.top(p)==underflow) ¡¡¡¡cout<<"Stack empty"< ¡¡¡¡else{ ¡¡¡¡numbers.top(p); ¡¡¡¡numbers.pop(); ¡¡¡¡if(p==0){ ¡¡¡¡cout<<"Bed command! The second number is 0 and cannot finishi divide command!"< ¡¡¡¡} ¡¡¡¡else if(numbers.top(q)==underflow){ ¡¡¡¡cout<<"Stack has just one entry"< ¡¡¡¡numbers.push(p); ¡¡¡¡} ¡¡¡¡else{ ¡¡¡¡numbers.pop(); ¡¡¡¡if(numbers.push(q/p)==overflow) ¡¡¡¡cout<<"Warning:Stack full,lost result"< ¡¡¡¡} ¡¡¡¡} ¡¡¡¡} ¡¡¡¡//Once enter '=':Show the result of least command ¡¡¡¡void Calculator::ShowResult(){ ¡¡¡¡Stack_entry p; ¡¡¡¡if(numbers.top(p)==underflow) ¡¡¡¡cout<<"Stack empty"< ¡¡¡¡else ¡¡¡¡cout< ¡¡¡¡} ¡¡¡¡//Once enter 'q':Stop the calculation ¡¡¡¡void Calculator::Quit(){ ¡¡¡¡cout<<"Calculation finished!"< ¡¡¡¡} ¡¡¡¡//Once enter 'c':Clear the stack ¡¡¡¡void Calculator::Clear(){ ¡¡¡¡numbers.clear(); ¡¡¡¡} ¡¡¡¡#include "calculator.h" ¡¡¡¡#include ¡¡¡¡using namespace std; ¡¡¡¡//construction ¡¡¡¡Calculator::Calculator(){ ¡¡¡¡command=' '; ¡¡¡¡} ¡¡¡¡//get_command ¡¡¡¡void Calculator::get_command(){ ¡¡¡¡bool waiting =true; ¡¡¡¡cout<<"Select command and press ¡¡¡¡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:"< ¡¡¡¡<<"[ ]push to stack [=]print top "< ¡¡¡¡<<"[+][-][*][/] are arithmetic operations"< ¡¡¡¡<<"[c]clean the stack"< ¡¡¡¡<<"[Q] quit"< ¡¡¡¡} ¡¡¡¡} ¡¡¡¡//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: "< ¡¡¡¡cin>>p; ¡¡¡¡if(numbers.push(p)==overflow) ¡¡¡¡cout<<"Warning:Stack full,lost number"< ¡¡¡¡} ¡¡¡¡//Once enter '+' : Add the numbers in the stack ¡¡¡¡void Calculator::Add(){ ¡¡¡¡Stack_entry p,q; ¡¡¡¡if(numbers.top(p)==underflow) ¡¡¡¡cout<<"Stack empty"< ¡¡¡¡else{ ¡¡¡¡numbers.top(p); ¡¡¡¡numbers.pop(); ¡¡¡¡if(numbers.top(q)==underflow){ ¡¡¡¡cout<<"Stack has just one entry"< ¡¡¡¡numbers.push(p); ¡¡¡¡} ¡¡¡¡else{ ¡¡¡¡numbers.pop(); ¡¡¡¡if(numbers.push(p+q)==overflow) ¡¡¡¡cout<<"Warning:Stack full,lost result"< ¡¡¡¡} ¡¡¡¡} ¡¡¡¡} ¡¡¡¡//Once enter '-': Subtract the numbers in the stack ¡¡¡¡void Calculator::Sub(){ ¡¡¡¡Stack_entry p,q; ¡¡¡¡if(numbers.top(p)==underflow) ¡¡¡¡cout<<"Stack empty"< ¡¡¡¡else{ ¡¡¡¡numbers.top(p); ¡¡¡¡numbers.pop(); ¡¡¡¡if(numbers.top(q)==underflow){ ¡¡¡¡cout<<"Stack has just one entry"< ¡¡¡¡numbers.push(p); ¡¡¡¡} ¡¡¡¡else{ ¡¡¡¡numbers.pop(); ¡¡¡¡if(numbers.push(q-p)==overflow) ¡¡¡¡cout<<"Warning:Stack full,lost result"< ¡¡¡¡} ¡¡¡¡} ¡¡¡¡} ¡¡¡¡//Once enter'*': Multiply the numbers in the stack ¡¡¡¡void Calculator::Mul(){ ¡¡¡¡Stack_entry p,q; ¡¡¡¡if(numbers.top(p)==underflow) ¡¡¡¡cout<<"Stack empty"< ¡¡¡¡else{ ¡¡¡¡numbers.top(p); ¡¡¡¡numbers.pop(); ¡¡¡¡if(numbers.top(q)==underflow){ ¡¡¡¡cout<<"Stack has just one entry"< ¡¡¡¡numbers.push(p); ¡¡¡¡} ¡¡¡¡else{ ¡¡¡¡numbers.pop(); ¡¡¡¡if(numbers.push(p*q)==overflow) ¡¡¡¡cout<<"Warning:Stack full,lost result"< ¡¡¡¡} ¡¡¡¡} ¡¡¡¡} ¡¡¡¡//Once enter '/': Divide the numbers in the stack ¡¡¡¡void Calculator::Div(){ ¡¡¡¡Stack_entry p,q; ¡¡¡¡if(numbers.top(p)==underflow) ¡¡¡¡cout<<"Stack empty"< ¡¡¡¡else{ ¡¡¡¡numbers.top(p); ¡¡¡¡numbers.pop(); ¡¡¡¡if(p==0){ ¡¡¡¡cout<<"Bed command! The second number is 0 and cannot finishi divide command!"< ¡¡¡¡} ¡¡¡¡else if(numbers.top(q)==underflow){ ¡¡¡¡cout<<"Stack has just one entry"< ¡¡¡¡numbers.push(p); ¡¡¡¡} ¡¡¡¡else{ ¡¡¡¡numbers.pop(); ¡¡¡¡if(numbers.push(q/p)==overflow) ¡¡¡¡cout<<"Warning:Stack full,lost result"< ¡¡¡¡} ¡¡¡¡} ¡¡¡¡} ¡¡¡¡//Once enter '=':Show the result of least command ¡¡¡¡void Calculator::ShowResult(){ ¡¡¡¡Stack_entry p; ¡¡¡¡if(numbers.top(p)==underflow) ¡¡¡¡cout<<"Stack empty"< ¡¡¡¡else ¡¡¡¡cout< ¡¡¡¡} ¡¡¡¡//Once enter 'q':Stop the calculation ¡¡¡¡void Calculator::Quit(){ ¡¡¡¡cout<<"Calculation finished!"< ¡¡¡¡} ¡¡¡¡//Once enter 'c':Clear the stack ¡¡¡¡void Calculator::Clear(){ ¡¡¡¡numbers.clear(); ¡¡¡¡}