1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
//Function prototypes: menu, summation, factorial, and exponential.
void printmenu();
void showSummation(int, int);
void showFactorial(long, long);
void showExponential(double, double);
int main()
{
int choice;
int sum, maxNumber;
long initialFactorial, factorialTotal;
double exponent, exponentialTotal;
const int SUMMATION = 1,
FACTORIAL = 2,
EXPONENTIAL = 3,
END_PROGRAM = 4;
do
{
printmenu();
cin >> choice;
//seriously, what goes here?
while (choice < SUMMATION || choice > END_PROGRAM)
{
cout << "Please enter a valid menu choice: ";
cin >> choice;
}
if (choice != END_PROGRAM)
{
switch (choice)
{
case SUMMATION: showSummation(sum, maxNumber);
break;
case FACTORIAL: showFactorial(initialFactorial, factorialTotal);
break;
case EXPONENTIAL: showExponential(exponent, exponentialTotal);
break;
default: cout << "Invalid input. Please re-enter." << endl;
}
}
} while (choice != END_PROGRAM);
}
void printmenu()
{
cout << "Enter 1 to calculate a sum." << endl;
cout << "Enter 2 to calculate a factorial." << endl;
cout << "Enter 3 to calculate an exponent." << endl;
cout << "Enter 4 to end the program." << endl;
}
void showSummation(int sum, int maxNumber)
{
sum = 0;
cout << "Enter a positive whole number.";
cin >> maxNumber;
while (maxNumber < 0)
{
cout << "Sorry, please re-enter a positive whole number.";
cin >> maxNumber;
}
for (int counter = 1; counter <= maxNumber; counter++)
{
sum += counter;
}
cout << "The summation of numbers from 1 to " << maxNumber << " is " << sum << endl;
cout << endl;
}
void showFactorial(long initialFactorial, long factorialTotal)
{
// Create the product of all numbers from 1 to the number given by the user
factorialTotal = 1;
cout << "Enter a positive whole number.";
cin >> initialFactorial;
while (initialFactorial < 0)
{
cout << "Sorry, please re-enter a positive whole number.";
cin >> initialFactorial;
}
for (int counter = 1; counter <= initialFactorial; counter++)
{
factorialTotal *= counter;
}
cout << "Factorial of " << initialFactorial << " is " << factorialTotal << endl;
cout << endl;
}
void showExponential(double exponent, double exponentialTotal)
{
// Create the answer of 2^exponent, where the exponent is input by the user
exponentialTotal = 1;
cout << "Enter a positive number.)";
cin >> exponent;
while (exponent < 0)
{
cout << "Sorry, please re-enter a positive number.";
cin >> exponent;
}
for (int counter = 1; counter <= exponent; counter++)
exponentialTotal = 2 * exponentialTotal;
cout << "Exponential total of 2**" << exponent << " is " << exponentialTotal << endl;
}
|