Advertisement
Guest User

Untitled

a guest
May 30th, 2013
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <algorithm>
  5. #include <math.h>
  6. #include <cmath>
  7.  
  8. using namespace std;
  9.  
  10. class Polynomial {
  11.     float a, b, c, value;
  12.    
  13. public:
  14.     Polynomial ();
  15.     void functionDefine(Polynomial poly);
  16.     void functionValue(Polynomial poly);
  17.     void functionZero(Polynomial poly);
  18.     //void functionMonoty(Polynomial poly);
  19. };
  20.  
  21. namespace Utils {
  22.     void mainMenu(Polynomial poly);
  23.     void skipToMenu();
  24.     string save(string str);
  25.     int exitProgram();
  26.  
  27.     void mainMenu(Polynomial poly) {
  28.         int choice = 0;
  29.    
  30.         cout << "   ---------- Main Menu ----------" << endl;
  31.    
  32.         cout << "1: ---- Define New Polynomial ----" << endl;
  33.         cout << "2: ------ Solve for x-value ------" << endl;
  34.         cout << "3: --------- Find roots ----------" << endl;
  35.         cout << "4: ------------ Exit -------------" << endl << endl;
  36.    
  37.         cout << "Enter the value of the submenu you want to enter: ";
  38.    
  39.         cin >> choice;
  40.         cout << endl;
  41.    
  42.         switch(choice) {
  43.            
  44.             case 1:
  45.                 poly.functionDefine(poly);
  46.                 break;
  47.            
  48.             //case 2:
  49.             //  poly.functionMonoty(poly);
  50.             //  break;
  51.            
  52.             case 2:
  53.                 poly.functionValue(poly);
  54.                 break;
  55.            
  56.             case 3:
  57.                 poly.functionZero(poly);
  58.                 break;
  59.  
  60.             case 4:
  61.                 exitProgram();
  62.                 break;
  63.         }
  64.     }
  65.  
  66.     void skipToMenu() {
  67.         cout << "Press enter to return to the main menu."<< endl << endl;
  68.         system("pause > nul");
  69.  
  70.     }
  71.  
  72.     string save(string str)  {
  73.         string saved = "Yes or no"; // To do.
  74.         return saved;
  75.     }
  76.  
  77.     int exitProgram() {
  78.         return 0;
  79.     }
  80. }
  81.  
  82. Polynomial::Polynomial() {
  83.     a = 0;
  84.     b = 0;
  85.     c = 0;
  86.  
  87. }
  88.  
  89. void Polynomial::functionDefine (Polynomial poly) {
  90.     char currentChar = 'a';
  91.    
  92.     cout << "---- Define New Polynomial ----" << endl;
  93.        
  94.     for (int i = 0; i < 3; i++) {
  95.         cout << "Enter a value for " << currentChar << ": ";
  96.        
  97.         switch(i)
  98.         {
  99.             case 0:
  100.                 cin >> poly.a; break;
  101.             case 1:
  102.                 cin >> poly.b; break;
  103.             case 2:
  104.                 cin >> poly.c; break;
  105.         }
  106.        
  107.         if (!cin) {
  108.             cout << "Please enter a number instead." << endl;
  109.             system("pause");
  110.         }
  111.  
  112.         currentChar = static_cast<char>(currentChar + 1);
  113.        
  114.     }
  115.    
  116.     cout << endl;
  117.     cout << "The function is: f(x) = " << poly.a << "x^2 + " << poly.b << "x + " << poly.c << "." << endl << endl;
  118.  
  119.     Utils::skipToMenu();
  120.     Utils::mainMenu(poly);
  121.    
  122. }
  123.  
  124. void Polynomial::functionValue(Polynomial poly) {
  125.     int input;
  126.  
  127.     cout <<"------ Solve for x-value ------" << endl << endl;
  128.  
  129.     cout << "Enter the x-value that you would like to evaluate the equation for: ";
  130.     cin >> input;
  131.  
  132.     value = poly.a * pow(input, 2) + poly.b * input + poly.c;
  133.     cout << "The value of the function for x = " << input << " is " << value << endl << endl;
  134.  
  135.     Utils::mainMenu(poly);
  136. }
  137.  
  138. void Polynomial::functionZero(Polynomial poly) {
  139.     float d = pow(poly.b, 2) - 4 * poly.a * poly.c;
  140.    
  141.     if (d > 0) {
  142.        
  143.         float result = ((-poly.b) + sqrt(abs(d))) / (2 * poly.a);
  144.         float secondResult = (-poly.b - sqrt(abs(d))) / (2 * poly.a);
  145.         cout << "The polynomials roots are at x = " << result << " and x = " << secondResult << "." << endl << endl;
  146.     }
  147.    
  148.     else if (d == 0) {
  149.         float result = ((-b) + sqrt(abs(d))) / (2 * a);
  150.         cout << "The polynomials root is at x = " << result << "." << endl << endl;
  151.     }
  152.    
  153.     else {
  154.         cout << "The polynomial has no roots." << endl << endl;
  155.     }
  156.  
  157.     Utils::mainMenu(poly);
  158. }
  159.  /*
  160. void Polynomial::functionMonoty(Polynomial poly) {
  161.    
  162.    
  163.    
  164. }
  165. */
  166. int main () {
  167.     Polynomial poly;
  168.     Utils::mainMenu(poly);
  169.    
  170.     return 0;
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement