vendredi 5 octobre 2018

How do I have my calculator output a full expression after it calculates the answer?

I am creating a calculator for a C++ class and I am trying to output a full equation instead of just having the program spit out the answer. I'm having a hard time figuring out how to have it change per case. I experimented with an if statement, but that ended up not working out. Any criticism is appreciated to my code, but I am looking for a relatively easy solution so I can output a full equation instead of just an answer to create a more user friendly experience.

Here is my code:

#include "pch.h"
#include <iostream>
#define _USE_MATH_DEFINES
#include <cmath>
#include <math.h>

using namespace std;

//---------- Function Prototypes -----------
void print_menu();
double get_value();
double divide(double, double);
double subtraction(double, double);
double multiplication(double, double);
double SARCC(double, double);
double VolRCC(double, double);
double SARCCone(double, double);
void ShowProgramHeader();

//--------------  Main -------------------
int main() {
    ShowProgramHeader();
    double operand1, operand2, answer;
    int choice, valid_choice;
    do {
        print_menu();
        cin >> choice;
        valid_choice = 1;           // assume choice is valid
        switch (choice) {
        case 0:                    // program will exit
            break;
        case 1:                    // addition
            operand1 = get_value();
            operand2 = get_value();
            answer = operand1 + operand2;
            break;
        case 2:                    // division
            operand1 = get_value();
            operand2 = get_value();
            answer = divide(operand1, operand2);
            break;
        case 3: // subtraction
            operand1 = get_value();
            operand2 = get_value();
            answer = subtraction(operand1, operand2);
            break;
        case 4: // multiplication
            operand1 = get_value();
            operand2 = get_value();
            answer = multiplication(operand1, operand2);
            break;
        case 5: // SA right circular cylinder
            operand1 = get_value();
            operand2 = get_value();
            answer = SARCC(operand1, operand2);
            break;
        case 6: // Vol. right circular cylinder
            operand1 = get_value();
            operand2 = get_value();
            answer = VolRCC(operand1, operand2);
            break;
        case 7: // SA right circular cone
            operand1 = get_value();
            operand2 = get_value();
            answer = SARCCone(operand1, operand2);
            break;
        default:
            valid_choice = 0;   // choice is invalid
            cout << "Invalid Choice." << endl;  
        }
        if (valid_choice) {   // if choice is valid, print the answer
                cout << endl << "Answer= " << answer  <<  endl;
        }
    } while (choice != 0);    // if not 0, loop back to start
    return 0;
}

//--------------  Functions -------------------

//----------------- get_value function ----------------
double get_value() {
    double temp_value;
    cout << "Please, enter a value: ";
    cin >> temp_value;
    cout << "Thanks." << endl;
    return temp_value;
}
//-------------------- print_menu function -------------
void print_menu() {
    cout << endl;
    cout << "Add (1)" << endl;
    cout << "Divide (2)" << endl;
    cout << "Subtract (3)" << endl;
    cout << "Multiply (4)" << endl;
    cout << "Surface Area of Right Circular Cylinder (5)" << endl;
    cout << "Volume of Right Circular Cylinder (6)" << endl;
    cout << "Surface Area of Right Circular Cone (7)" << endl;
    cout << "Exit (0)" << endl;
    cout << "Enter your choice: ";
}
//---------- Show Program Header function ----------
void ShowProgramHeader() {
    cout << "Grant Dearing" << "\n" << "CS120-02" << "\n" << "Lab 6" << "\n" << "5 October 2018" << endl;
}
//----------- Subtraction Function --------------
double subtraction(double subtractor, double subtractee) {
    return (subtractee - subtractor);
}
//----------- Multiplication Function ---------
double multiplication(double multiplier1, double multiplier2) {
    return (multiplier1 * multiplier2);
}
//----------- Divison Function ---------
double divide(double dividend, double divisor) {
    if (divisor == 0) {
        return 0;  // avoids divide by zero errors
    }
    else 
        return (dividend / divisor);
    }
//---------- SA Right Circular Cylinder --------
double SARCC(double radius, double height) {
        return ((2 * M_PI*radius*height) + (2 * M_PI*pow(radius, 2)));
}
//--------- Vol Right Circular Cylinder --------
double VolRCC(double radius, double height) {
    return (M_PI*pow(radius, 2)*height);
}
//---------- SA Right Circular Cone --------
double SARCCone(double radius, double height) {
    return ((M_PI*radius)*(radius + (sqrt(pow(height, 2) + pow(radius, 2)))));
}

Aucun commentaire:

Enregistrer un commentaire