Skip to content

Instantly share code, notes, and snippets.

@brandonjank
Created February 9, 2017 02:38
Show Gist options
  • Save brandonjank/98da826920e0dc17b7289a7553b598b8 to your computer and use it in GitHub Desktop.
Save brandonjank/98da826920e0dc17b7289a7553b598b8 to your computer and use it in GitHub Desktop.
/* Calculator Assignment 5
*
* A simple calculator program controlled program, controlled by a menu and
* divided into separate functions.
*
* Name: Brandon Jank
* Section: CS 120-05
* Due Date: 10/06/2014
* Assignment #5
*
* v1.0.0 - 2014/09/30 - Assignment 5 - Copied program from book.
* - Fixed the way the program exits so that it doesn't reprint the user's last answer.
* - Added subtraction and multiplication options.
* - Added a minimum option to the program. It should accept two numbers and return the smaller one.
* - Added an option that returns a random number between 0 and an upper bound given by the user.
* - Added and option to calculate the volume of a cylinder.
*/
#include <iostream>
#include <time.h> // time()
#include <math.h> // M_PI
#include <cmath> // pow()
#include <cstdlib> // rand(), srand()
#include "CurseWin.h"
using namespace std;
//---------- Function Prototypes -----------
void print_menu(CurseWin &);
double get_value(CurseWin &);
double divide(double, double);
double subtraction(double, double); // Assignment 5
double multiplication(double, double); // Assignment 5
double minimum(double, double); // Assignment 5
double random(int); // Assignment 5
double volume_cylinder(double, double); // Assignment 5
//-------------- Main -------------------
int main()
{
CursWin outputWin(0,0,2,79), inputWin(2,0,6,50), menuWin(2,50,6,79);
double operand1, operand2, answer;
int choice, valid_choice;
char Operator;
print_menu(menuWin);
do{
srand(time(NULL)); // Assignment 5: Make random more random
inputWin << "Enter a menu choice: " << cflush;
inputWin >> choice;
valid_choice = 1; // assume choice is valid
switch(choice){
case 0: // program will exit
break;
case 1: // addition
operand1 = get_value(inputWin, "first value");
operand2 = get_value(inputWin, "second value");
answer = operand1 + operand2;
Operator = '+';
break;
case 2: // division
operand1 = get_value(inputWin, "first value");
operand2 = get_value(inputWin, "second value");
answer = divide(operand1,operand2);
Operator = '/';
break;
case 3: // Assignment 5: subtraction
operand1 = get_value(inputWin, "first value");
operand2 = get_value(inputWin, "second value");
answer = subtraction(operand1,operand2);
Operator = '-';
break;
case 4: // Assignment 5: multiplication
operand1 = get_value(inputWin, "first value");
operand2 = get_value(inputWin, "second value");
answer = multiplication(operand1,operand2);
Operator = '*';
break;
case 5: // Assignment 5: minimum of two values
operand1 = get_value(inputWin, "first value");
operand2 = get_value(inputWin, "second value");
answer = minimum(operand1,operand2);
Operator = 'm';
break;
case 6: // Assignment 5: random with bound
operand1 = get_value("integer for upper bound");
answer = random(operand1);
Operator = 'r';
break;
case 7: // Assignment 5: volume of a cylinder
operand1 = get_value("radius");
operand2 = get_value("height");
answer = volume_cylinder(operand1,operand2);
Operator = 'V';
break;
default:
valid_choice = 0; // choice is invalid
inputWin << "Invalid Choice." << cendl;
}
if(valid_choice && choice != 0){ // if choice is valid, print the answer // Assignment 5: Do not reprint answer on exit.
outputWin << cnl << operand1 << " " << Operator << " " << operand2 << " = " << answer << cendl;
}
}while(choice != 0); // if not 0, loop back to start
return 0;
}
//-------------- Functions -------------------
double divide(double dividend, double divisor){
if(divisor == 0){
return 0; // avoids divide by zero errors
}
else
return (dividend/divisor);
}
// Subtraction function for Assignment 5
double subtraction(double x, double y){
return x - y;
}
// Multiplication function for Assignment 5
double multiplication(double x, double y){
return x * y;
}
// Minimum of two values function for Assignment 5
double minimum(double x, double y){
if (x < y) return x; else return y;
}
// Random number with upper bound function for Assignment 5
double random(int x){
return rand() % x;
}
// Volume of a Cylinder function for Assignment 5
double volume_cylinder(double r, double h){
return M_PI * pow(r,2) * h;
}
//----------------- get_value function ----------------
double get_value(CurseWin &inw, string value_name){
double value;
inw << "Please, enter the " << value_name << ": " << cflush;
inw >> value;
inw << cendl;
return value;
}
//-------------------- print_menu function -------------
void print_menu(CurseWin &outw){
outw << cnl;
outw << "Add (1)" << cnl;
outw << "Divide (2)" << cnl;
outw << "Subtract (3)" << cnl;
outw << "Multiply (4)" << cnl;
outw << "Minimum (5)" << cnl;
outw << "Random (6)" << cnl;
outw << "Volume of a Cylinder (7)" << cnl;
outw << "Exit (0)" << cnl;
outw << "Enter your choice (0-7): " << cflush;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment