Question

In: Computer Science

#ifndef CCALC_HEADER #define CCALC_HEADER    class   CCalc { public:     // member functions     CCalc();     void    Add(double...

#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"

Solutions

Expert Solution

// 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:


Related Solutions

Study the following class definition: class Car { public: Car(double speed); void start(); void accelerate(double speed);...
Study the following class definition: class Car { public: Car(double speed); void start(); void accelerate(double speed); void stop(); double get_speed() const; private: double speed; }; Which of the following options would make logical sense in the definition of the void accelerate(double speed)function? Group of answer choices this->speed = this->speed; this->speed = speed; this.speed = speed; speed1 = this->speed; Flag this Question Question 131 pts The Point class has a public function called display_point(). What is the correct way of calling...
For this program you will add and test 2 new member functions to the class in...
For this program you will add and test 2 new member functions to the class in //************************ intSLList.h ************************** // singly-linked list class to store integers #ifndef INT_LINKED_LIST #define INT_LINKED_LIST class IntSLLNode { public: IntSLLNode() { next = 0; } IntSLLNode(int el, IntSLLNode *ptr = 0) { info = el; next = ptr; } int info; IntSLLNode *next; }; class IntSLList { public: IntSLList() { head = tail = 0; } ~IntSLList(); int isEmpty() { return head == 0; }...
#ifndef PROJ7_MYVECTOR #define PROJ7_MYVECTOR #include "proj7-ContainerIfc.h" template <class T> class MyVector : public ContainerIfc<T> { public:...
#ifndef PROJ7_MYVECTOR #define PROJ7_MYVECTOR #include "proj7-ContainerIfc.h" template <class T> class MyVector : public ContainerIfc<T> { public: /** * MyVector * * This is the default constructor that sets size equal * to 0 and capacity to 10. * * Parameters: none * * Output: * return: none * reference parameters: none * stream: none */ MyVector(); /** * ~MyVector * * This is the destructor that deletes memory * * Parameters: none * * Output: * return: none * reference...
import java.util.Random; class Conversions { public static void main(String[] args) { public static double quartsToGallons(double quarts)...
import java.util.Random; class Conversions { public static void main(String[] args) { public static double quartsToGallons(double quarts) { return quarts * 0.25; } public static double milesToFeet(double miles) { return miles * 5280; } public static double milesToInches(double miles) { return miles * 63360; } public static double milesToYards(double miles) { return miles * 1760; } public static double milesToMeters(double miles) { return miles * 1609.34; } public static double milesToKilometer(double miles) { return milesToMeters(miles) / 1000.0; } public static double...
////Fixme(1) add a statement to import ArrayList class public class ListManipulation { public static void main(String[]...
////Fixme(1) add a statement to import ArrayList class public class ListManipulation { public static void main(String[] args) { //Fixme(2) create an ArrayList of integers and name the ArrayList list. //Fixme(3) add the following numbers to the list: 10, 15, 7, -5, 73, -11, 100, 20, 5, -1    displayList(list); System.out.println(); displayListBackwards(list);       }    public static void displayList(ArrayList<Integer> list) { for(Integer i: list) System.out.print(i + " ");    } //Fixme(4) define a method displayListBackwards, which takes an ArrayList as...
import java.util.Scanner; public class Lab9Q3 { public static void main (String [] atgs) { double mass;...
import java.util.Scanner; public class Lab9Q3 { public static void main (String [] atgs) { double mass; double velocity; double totalkineticEnergy;    Scanner keyboard = new Scanner (System.in); System.out.println ("Please enter the objects mass in kilograms"); mass = keyboard.nextDouble();    System.out.println ("Please enter the objects velocity in meters per second: "); velocity = keyboard.nextDouble();    double actualTotal = kineticEnergy (mass, velocity); System.out.println ("Objects Kinetic Energy: " + actualTotal); } public static double kineticEnergy (double mass, double velocity) { double totalkineticEnergy =...
class Point3d { public: Point3d(double x, double y, double z); Point3d(const point3d& p); void setX(double x);...
class Point3d { public: Point3d(double x, double y, double z); Point3d(const point3d& p); void setX(double x); void setY(double y); void setZ(double z); double getX() const; double getY() const; double getZ() const; point3d& operator=(const point3d& rhs); private: double x; double y; double z; }; Given the Point class above, complete the following: 1. Write just the signature for the overloaded addition operator that would add the respective x,y, and z from two point3d objects. 2. What is the output of the...
public class sales_receipt { public static void main(String[] args) { //declare varables final double tax_rate =...
public class sales_receipt { public static void main(String[] args) { //declare varables final double tax_rate = 0.05; //tax rate String cashier_name = "xxx"; //sales person String article1, article2; //item name for each purchased int quantity1, quantity2; //number of quantities for each item double unit_cost1, unit_cost2; //unit price for each item double price1, price2; //calculates amounts of money for each item. double sub_total; //total price of two items without tax double tax; //amount of sales tax double total; //total price of...
Suppose that Account class has private attributes double balance and two public methods void setBalance(double amount)...
Suppose that Account class has private attributes double balance and two public methods void setBalance(double amount) and double getBalance() const. The method names explain its purpose. Further suppose that child class Savings has one more attribute double interest_rate and a public method void addInterest() which will update the balance according the formula new balance = old balance * (1 + interest_rate/100.0); Implement addInterest method. c++
Create a class DogWalker member List <String>doggies method: void addDog(String name ) //add dog to the...
Create a class DogWalker member List <String>doggies method: void addDog(String name ) //add dog to the List method: void printDogList() //print all dogs in list /* You’ll need an index. Iterate over your list of dog names in a while loop. Use your index to “get” the dog at the current index When you ‘find’ your dog name in the list, print it out */ Method: void findDogUsingWhile(String dogName) /* You’ll need an index. Iterate over your list of dog...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT