Wednesday, August 23, 2017

Programming Challenge 13.8 - Circle Class

Example Files: UtilityCls.h
                          CircleClass.h
                          CircleClassDemo.cpp

CircleClass.h


#ifndef CIRCLE_H
#define CIRCLE_H

#include <cmath>

class Circle
{
    private:
        double radius;        // Radius of the circle
        double pi;  
     
    public:
        Circle()
        {
            radius = 0.0;
            pi = 3.14159;
        }

        // Sets the radius of the circle
        void setRadius(double r)       
        {
            radius = r;
        }

        // Gets the radius of the circle
        double getRadius() const       
        { return radius; }

        // Calculates the area of the circle
        double getArea() const           
        { return pi * pow(radius, 2.0); }

        // Calculates the circle's diameter
        double getDiameter() const               
        { return radius * 2.0; }

        // Calculates the circle's circumference
        double getCircumference() const       
        { return 2.0 * (pi * radius); }
};
#endif

CircleClassDemo.cpp


/* CircleClass.cpp - Circle Class demo. */

#include <iomanip>
#include <iostream>
#include <string>
#include "CircleClass.h"
#include "UtilityCls.h"

using std::cin;
using std::cout;
using std::fixed;
using std::showpoint;
using std::setprecision;
using std::setw;

void displayInstructions();
void displayMenu();
void getRadius(Circle &);
void menu(Circle &);

int main()
{
    Circle uCircle;
    menu(uCircle);

    cin.get();
    cin.ignore();

    return 0;
}
/* **********************************************************
    displayMenu (void)
    This function displays the menu options.
   ********************************************************** */

void displayMenu()
{
    cout << "\nCIRCLE CALCULATOR\n\n"
          << "V] VIEW INSTRUCTIONS\n"
          << "E] ENTER RADIUS OF CIRCLE\n"
          << "A] CALCULATE AREA\n"
          << "D] CALCULATE DIAMETER\n"
          << "C] CALCULATE CIRCUMFERENCE\n"
          << "F] CALCULATE AREA / DIAMETER / CIRCUMFERENCE\n"
          << "Q] QUIT\n\n"
          << "Choice: ";
}

/* **********************************************************
    displayInstructions (void)
    This function displays the program instructions.
   ********************************************************** */

void displayInstructions()
{
    cout << "WELCOME TO THE CIRCLE CALCULATOR.\n\n"
          << "To begin using with this program, select E] from the main menu to enter the radius\n"
          << "of your circle. Once done, you are able to select between the following menu items:\n\n"
          << "A] To calculate the area of your circle          (formula: PI * radius * radius)\n"
          << "D] To calculate the diameter of your circle      (formula: radius * 2.0)\n"
          << "C] To calculate the circumference of your circle (formula: 2.0 * PI * radius)\n\n"
         << "F] To calculate the area, diameter and circumference of your circle\n\n";
    cout << "While in main menu, you are able to change the radius of the circle at any time,\n"
          << "then perform one calculation [A, D, C] or [F] for all calculations.\n"
          << "To quit this program you should enter 'Q' or 'q'\n\n";
}
/* **********************************************************
    menu (accepts a reference to a Circle object)
    This function holds the main menu of the program.
   ********************************************************** */

void menu(Circle &uCircle)
{
    char choice = ' ';

    do
    {
        displayMenu();  
        cin >> choice;
        cout << "\n";
      
        switch (choice)
        {
            case 'V':
            case 'v':
            {
                displayInstructions();
                pauseSytem();
                clearScreen();
            } break;

            case 'E':
            case 'e':
            {
                getRadius(uCircle);
                pauseSytem();
                clearScreen();
            } break;

            case 'A':
            case 'a':
            {
                cout << setw(16) << "The radius of your circle is: "
                      << setw(14) << uCircle.getRadius() << setw(5) << "   feet.\n";
                cout << setw(20) << "The area of your circle is "
                      << setw(19) << uCircle.getArea() << " square feet.\n";
                pauseSytem();
                clearScreen();
            } break;

            case 'D':
            case 'd':
            {
                cout << setw(16) << "The radius of your circle is: "
                      << setw(14) << uCircle.getRadius() << setw(5) << "   feet.\n";
                cout << setw(20) << "The Diameter of your circle is: "
                      << setw(11) << uCircle.getDiameter() << setw(4) << " feet.\n";
                pauseSytem();
                clearScreen();
            } break;

            case 'C':
            case 'c':
            {
                cout << setw(16) << "The radius of your circle is: "
                      << setw(14) << uCircle.getRadius() << setw(5) << "   feet.\n";
                cout << setw(20) << "The Circumference of your circle is "
                      << setw(10) << uCircle.getCircumference() << " feet.\n";
                pauseSytem();
                clearScreen();
            } break;

            case 'F':
            case 'f':
            {
                cout << setw(16) << "The radius of your circle is: "
                      << setw(14) << uCircle.getRadius() << setw(5) << "   feet.\n";
                cout << setw(20) << "The area of your circle is "
                      << setw(19) << uCircle.getArea() << " square feet.\n"
                      << setw(20) << "The Diameter of your circle is "
                      << setw(12) << uCircle.getDiameter() << setw(10) << " feet.\n"
                      << setw(20) << "The Circumference of your circle is "
                      << setw(10) << uCircle.getCircumference() << " feet.\n";
                pauseSytem();
                clearScreen();
            } break;

            case 'Q':
            case 'q':
            {
                cout << "Thank you for trying this program! Have a nice day!";
            }
        }
    } while (toupper(choice) != 'Q');
}

/* **********************************************************
    getRadius (accepts a reference to a Circle object)
    The user is asked to enter the radius of a circle. If the
    radius is lower than or equal to 0, the user is asked to
    repeat the input. Else, the value is passed to the member
    variable of the circle object.
   ********************************************************** */

void getRadius(Circle &uCircle)
{
    double radius = 0.0;

    cout << "Enter the radius of a circle: ";
    cin >> radius;

    while (radius <= 0.0)
    {
        cout << "Enter radius greater than 0: ";
        cin >> radius;
    }

    uCircle.setRadius(radius);
}

Example Output:









No comments:

Post a Comment