In: Computer Science
#ifndef CCALC_HEADER
#define CCALC_HEADER
class CCalc
{
public:
// member functions
CCalc();
void Add(double value);
void Clear();
void Divide(double value);
double GetValue() const;
void Multiply(double value);
void SetValue(double newValue);
void Subtract(double value);
private:
// data members
double m_total;
};
#endif // CCALC_HEADER
int main()
{
CCalc calculator;
char choice;
// loop and let the user manipulate the calculator
do {
// display the menu and get the user selection
DisplayMenu();
cout << "Please enter a selection: ";
cin >> choice;
HandleChoice(calculator, choice);
cout << endl;
} while ('Q' != toupper(choice));
return 0;
} // end of "main"
// ==== DisplayMenu ===========================================================
//
// This function displays the menu of options to stdout.
//
// Input: nothing
//
// Output: nothing
//
// ============================================================================
void DisplayMenu()
{
cout << "Calculator Options:" << endl;
cout << " [C] Clear" << endl;
cout << " [T] Set Value" << endl;
cout << " [V] Display Value" << endl;
cout << " [A] Add" << endl;
cout << " [S] Subtract" << endl;
cout << " [D] Divide" << endl;
cout << " [M] Multiply" << endl;
cout << " [Q] Quit" << endl;
} // end of "DisplayMenu"
// ==== HandleChoice ==========================================================
//
// This function handles the menu selection by examining the input char
// parameter and using the CCalc parameter to call the appropriate CCalc member
// function.
//
// Input:
// calc [IN/OUT] -- a reference to an existing CCalc object
//
// item [IN] -- a char representing the current menu selection
//
// Output:
// Nothing
//
// ============================================================================
void HandleChoice(CCalc &calc, char item)
{
double temp;
// switch on the menu selection and call the corresponding CCalc member
// function
switch (toupper(item))
{
case 'C':
// clear the calculator so it has a value of zero
calc.Clear();
break;
???
} // end of "HandleChoice"
// CCalc.h
#ifndef CCALC_HEADER
#define CCALC_HEADER
class CCalc
{
public:
// member functions
CCalc();
void Add(double value);
void Clear();
void Divide(double value);
double GetValue() const;
void Multiply(double value);
void SetValue(double newValue);
void Subtract(double value);
private:
// data members
double m_total;
};
#endif // CCALC_HEADER
//end of CCalc.h
// CCalc.cpp
#include "CCalc.h"
// default constructor to set m_total to 0
CCalc::CCalc() : m_total(0)
{}
// add value to m_total
void CCalc:: Add(double value)
{
m_total += value;
}
// reset m_total to 0
void CCalc:: Clear()
{
m_total = 0;
}
// divide m_total by value
void CCalc:: Divide(double value)
{
m_total /= value;
}
// return m_total
double CCalc:: GetValue() const
{
return m_total;
}
// multiply m_total by value
void CCalc:: Multiply(double value)
{
m_total *= value;
}
// set value to m_total
void CCalc:: SetValue(double newValue)
{
m_total = newValue;
}
// subtract value from m_total
void CCalc:: Subtract(double value)
{
m_total -= value;
}
//end of CCalc.cpp
// main.cpp
#include <iostream>
#include <cctype>
#include "CCalc.h"
using namespace std;
void DisplayMenu();
void HandleChoice(CCalc &calc, char item);
int main()
{
CCalc calculator;
char choice;
// loop and let the user manipulate the calculator
do {
// display the menu and get the user selection
DisplayMenu();
cout << "Please enter a selection: ";
cin >> choice;
HandleChoice(calculator, choice);
cout << endl;
} while ('Q' != toupper(choice));
return 0;
}
// ==== DisplayMenu
===========================================================
//
// This function displays the menu of options to stdout.
//
// Input: nothing
//
// Output: nothing
//
//
============================================================================
void DisplayMenu()
{
cout << "Calculator Options:" << endl;
cout << " [C] Clear" << endl;
cout << " [T] Set Value" << endl;
cout << " [V] Display Value" << endl;
cout << " [A] Add" << endl;
cout << " [S] Subtract" << endl;
cout << " [D] Divide" << endl;
cout << " [M] Multiply" << endl;
cout << " [Q] Quit" << endl;
} // end of "DisplayMenu"
// ==== HandleChoice
==========================================================
//
// This function handles the menu selection by examining the input
char
// parameter and using the CCalc parameter to call the appropriate
CCalc member
// function.
//
// Input:
// calc [IN/OUT] -- a reference to an existing CCalc object
//
// item [IN] -- a char representing the current menu
selection
//
// Output:
// Nothing
//
//
============================================================================
void HandleChoice(CCalc &calc, char item)
{
double temp;
// switch on the menu selection and call the corresponding CCalc
member
// function
switch (toupper(item))
{
case 'C':
// clear the calculator so it has a value of zero
calc.Clear();
break;
case 'T':
// input a value
cout<<"Enter a value: ";
cin>>temp;
calc.SetValue(temp); // set calc to temp
break;
case 'V':
// display the current total
cout<<"Value: "<<calc.GetValue()<<endl;
break;
case 'A':
// input a value
cout<<"Enter a value: ";
cin>>temp;
calc.Add(temp); // add temp to calc
break;
case 'S':
// input a value
cout<<"Enter a value: ";
cin>>temp;
calc.Subtract(temp); // subtract temp from calc
break;
case 'D':
// input a value
cout<<"Enter a value: ";
cin>>temp;
// validate input is not 0 as it will cause divide by zero
error
// re-prompt until valid
while(temp == 0)
{
cout<<"Divisor cannot be 0. Re-enter: ";
cin>>temp;
}
calc.Divide(temp); // divide calc by temp
break;
case 'M':
// input a value
cout<<"Enter a value: ";
cin>>temp;
calc.Multiply(temp); // multiply calc by temp
break;
case 'Q': // exit
break;
default:
cout<<"Invalid choice"<<endl;
}
} // end of "HandleChoice"
//end of main.cpp
Output: