Solution

//Programmer: Eric Barnes

//File: hw4.cpp

//Class: CS53

//Date 2/27/2014

//Purpose: To provide Ralph with a menu to allow him to select from multiple

// mathematical operations (Factorial, Fourth root, exponential)

// in the vain hope of improving his love life

#include <iostream>

using namespace std;

int main()

{

const float INIT_ROOT_GUESS = 20.0;

const int ROOT_REPS = 6;

float input_x;

int choice;

bool entered_x = false;

bool quit = false;

int fact;

float fourth_root;

float exp;

cout << "Hello Ralph, make selections from the menu to win Lisa's heart \n";

do

{

cout << "\t\t MENU \n"

<< "1. Enter a positve real number, x\n"

<< "2. Factorial of x\n"

<< "3. Fourth root of x\n"

<< "4. X-ponential of x (exp(x))\n"

<< "5. Quit" << endl;

cin >> choice;

switch (choice)

{

case 1:

do

{

cout << "\nEnter a positive real number: ";

cin >> input_x;

if(input_x < 0)

{

cout << "Ralph, no." << endl;

}

} while (input_x < 0);

entered_x = true;

break;

case 2:

if(entered_x)

{

fact = 1;

for(int i = 1; i <= static_cast<int>(input_x); i++)

fact *= i;

cout << "The factorial of " << static_cast<int>(input_x) << " is "

<< fact << endl;

}

else

cout << "Enter a positive real number first" << endl;

break;

case 3:

if(entered_x)

{

fourth_root = INIT_ROOT_GUESS;

for(int i = 0; i < ROOT_REPS; i++)

fourth_root = (3*fourth_root + input_x/(fourth_root * fourth_root

* fourth_root))/4;

cout << "The fourth root of " << input_x << " is " << fourth_root

<< endl;

}

else

cout << "Enter a postive real number first" << endl;

break;

case 4:

/*WARNING: THE OPTIMAL SOLUTION TO THIS CASE HAS BEEN OBFUSCATED TO MAKE

YOUR LIVES MORE DIFFICULT. LOOPS ARE YOUR FRIEND */

if(entered_x)

{

exp = 1 + input_x + input_x*input_x/2

+ input_x * input_x * input_x/(2*3)

+ input_x * input_x * input_x * input_x/(2*3*4)

+ input_x * input_x * input_x * input_x * input_x /(2*3*4*5);

cout << "e^" << input_x << " is " << exp << endl;

}

else

cout << "Enter a positive real number first" << endl;

break;

//END OF OBFUSCATED CODE, STOP WINCING

case 5:

cout << "Terminating program. Goodbye, Ralph" << endl;

quit = true;

break;

default:

cout << "Ralph stop trying to eat your calculator" << endl;

}

} while(!quit);

return 0;

}