#include #include #include #include #include /* * Created by Drake Vidkjer on 10/25/15 * Last modified on 10/25/15 * Version .1 * Assignment 4 * */ using namespace std; //declare funciton prototypes bool inGood(string); char promptDo(); void checkDo(char); void prompt_for_variables(); bool checkVariables(int, float, float, float); void integrate(); void sum(); bool allGood(string, string, string); void convertVals(string, string, string); float solveFunct(float); bool posGood(string); void integrate_rectangle(); void integrate_trap(); //define global variables float upBoundVal, lowBoundVal; int functVal; int main() { char whatDo; while(whatDo != 'E') { //Call prompt function to see what to do whatDo = promptDo(); //check what to do and do it checkDo(whatDo); } return 0; } //check if user input is a good positive integer //precondition: bool inGood(string input) { //check if first is a negative sign if(input[0] == 45 || (input[0] > 48 && input[0] < 57)) { for(int j = 1; j < input.size(); j++) { if((input[j] < 48 || input[j] > 57) && input[j] != 46) { cout << "Bad input, enter a new guess" << endl; return false; } } } return true; } bool posGood(string input) { for(int x = 0; x < input.size(); x++) { if(input[x] < 48 || input[x] > 57) { cout << "Bad input, enter a new guess" << endl; return false; } } return true; } //Ask user what to do char promptDo() { //define local variables char usIn; //prompt user and take in input cout << "What would you like to do? (S)um, (I)ntegrate, or (E)xit?: "; cin >> usIn; return usIn; } //Check what user wants to do and call correct function void checkDo(char whatDo) { //check input to see what to do if(whatDo == 'S') { sum(); } else if(whatDo == 'I') { integrate(); } return; } //Prompt user for bounds, value for x and function to be used void prompt_for_variables() { //Define local variables bool good = false; string funct, upBound, lowBound; while(good == false) { //ask which function to use cout << "1) f(x)= 2x^5+x^3-10x+2 \n2) f(x)= 6x^2-x+10 \n3) f(x)= 5x+3 \n4) f(x)= 2x^3+120 \n5) f(x)= 2x^2 \nWhich function would you like to use?: "; cin >> funct; cout << "Enter lower bound: "; cin >> lowBound; //ask for upper/lower bounds cout << "Enter upper bound: "; cin >> upBound; //make sure all the valuse are good good = allGood(funct, upBound, lowBound); //Convert values to floats convertVals(funct, upBound, lowBound); if(functVal < 1 || functVal > 5) { good = false; cout << "Sorry, bad function input" << endl; } else if (lowBoundVal > upBoundVal) { good = false; cout << "Sorry, lower bound greater than upper bound" << endl; } } //Convert values to floats convertVals(funct, upBound, lowBound); } //check to make sure all the values entered are good bool allGood(string funct, string upBound, string lowBound) { if(inGood(funct) == false) { return false; } else if(inGood(upBound) == false) { return false; } else if(inGood(lowBound) == false) { return false; } else { return true; } } //convert all the inputed values into floats and ints void convertVals(string funct, string upBound, string lowBound) { functVal = atoi(funct.c_str()); upBoundVal = strtof(upBound.c_str(), NULL); lowBoundVal = strtof(lowBound.c_str(), NULL); } //solve the appropriate function with the x value inputed float solveFunct(float x) { if(functVal == 1) { return (2*pow(x, 5) + pow(x, 3) - 10*x + 2); } else if(functVal == 2) { return (6*pow(x, 2) - x + 10); } else if(functVal == 3) { return (5*x+3); } else if(functVal == 5) { return (2*pow(x, 2)); } else return 0; } //sum a function over certain bounds void sum() { //define local variables double answer; //call prompt_for_variables prompt_for_variables(); for (int i = lowBoundVal; i <= upBoundVal; i++) { answer = answer + solveFunct(i); } cout << "Sum of f" << functVal << " from " << lowBoundVal << " to " << upBoundVal << " is " << answer << endl; //cout << "functVal, upBoundVal, lowBoundVal\n" << functVal << " " << upBoundVal << " " << lowBoundVal << endl; } void integrate_rectangle() { string rects; int rectangles; double rectSize, answer; bool rectsGood = false; while(rectsGood == false) { cout << "How many rectangles do you want?: "; cin >> rects; rectsGood = posGood(rects); } rectangles = atoi(rects.c_str()); rectSize = (upBoundVal - lowBoundVal)/rectangles; for(double z = lowBoundVal; z <= upBoundVal; z = z+rectSize) { answer = answer + (solveFunct(z)*rectSize); //cout << answer << endl; } //print answer cout << "The answer is: " << answer << endl; } void integrate_trap() { string traps; int trapezoids; double trapSize, answer; bool trapsGood = false; while(trapsGood == false) { cout << "How many trapezoids do you want?: "; cin >> traps; trapsGood = posGood(traps); } trapezoids = atoi(traps.c_str()); trapSize = (upBoundVal - lowBoundVal)/trapezoids; for(double z = lowBoundVal; z <= upBoundVal; z = z+trapSize) { answer = answer + (((solveFunct(z)+solveFunct(z+trapSize))/2)*trapSize); } //print answer cout << "The answer is: " << answer << endl; } //integrate a funciton over certain bounds void integrate() { //define local variables char trapRect; //call prompt_for_variables prompt_for_variables(); //calculate things //Ask if trapezoid or rectangle or both and do that accordingly while(1) { cout << "Would you like to use the (T)rapezoidal or (R)ectangle method or (B)oth?: "; cin >> trapRect; if(trapRect == 'R') { integrate_rectangle(); break; } else if(trapRect == 'T') { integrate_trap(); break; } else if(trapRect == 'B') { integrate_rectangle(); integrate_trap(); break; } } }