chriscct1

file where I want it to be

Jun 13th, 2012
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.35 KB | None | 0 0
  1. // Author: Chris Christoff
  2. // Current Build: 4.2.3
  3. // Last build time: 8:55 A.M/ 6/12/2012
  4.  
  5. #include "assign5.h"
  6. #include <iostream>
  7. #include <fstream>
  8. #include <cmath>
  9. #include <limits>
  10. #include <stdlib.h>
  11. using namespace std;
  12.  
  13. quadratic::quadratic()
  14. {
  15.  
  16. }
  17.  
  18. quadratic::fStruct quadratic::readCoeffs()
  19. {
  20.     fStruct fVars;
  21.     char coef[]= {'a','b','c'};
  22.     for (int i=0; i<3; i++)
  23.     {
  24.         while (((cout << "Enter coefficient for "<<coef[i]<<": ") && !(cin >> fVars.coeffs[i])) || cin.peek() != '\n')
  25.         {
  26.             cout << "Invalid Input."  << endl;
  27.             cin.clear();
  28.             cin.ignore(numeric_limits<streamsize>::max(), '\n');
  29.         }
  30.     }
  31.  
  32.     return fVars;
  33. }
  34.  
  35. double quadratic::f(quadratic::fStruct fVars, double x)
  36. {
  37.     double a = fVars.coeffs[0];
  38.     double b = fVars.coeffs[1];
  39.     double c = fVars.coeffs[2];
  40.     double fOfX = ((a*(pow(x,2.0))) + (b*x) + c);
  41.  
  42.     return fOfX;
  43. }
  44.  
  45. double quadratic::bisect(double xLeft, double xRight, double epsilon, quadratic::fStruct fVars, bool& error)
  46. {
  47.     quadratic q;
  48.  
  49.     double xMid;
  50.     double fMid;
  51.  
  52.     double fLeft = q.f(fVars, xLeft);
  53.     double fRight = q.f(fVars, xRight);
  54.  
  55.     error = (fLeft * fRight) > 0;
  56.  
  57.     if(error)
  58.         return -999.0;
  59.  
  60.     while(fabs(xLeft-xRight)>epsilon)
  61.     {
  62.         xMid = (xLeft + xRight) / 2.0;
  63.         fMid = q.f(fVars, xMid);
  64.  
  65.         if(fMid==0.0)
  66.             return xMid;
  67.         else if ((fLeft * fMid) < 0.0)
  68.             xRight = xMid;
  69.         else
  70.             xLeft = xMid;
  71.     }
  72.  
  73.     return (xLeft+xRight) / 2.0;
  74. }
  75.  
  76. void quadratic::toFile(quadratic::fStruct fVars, quadratic::rootStruct rootStruct)
  77. {
  78.     double a = fVars.coeffs[0];
  79.     double b = fVars.coeffs[1];
  80.     double c = fVars.coeffs[2];
  81.  
  82.     ofstream dataStore;
  83.     dataStore.open("results.dat", ios::out | ios::app);
  84.     dataStore << endl << "Quadratic equation with the following coefficients: " << endl << "a: "
  85.     << a << " b: " << b << " c: " << c << endl << "has the following roots" << endl << "Root1: "
  86.     << rootStruct.roots[0] << " Root2: " << rootStruct.roots[1] << endl;
  87.     return;
  88. }
  89.  
  90. void quadratic::toScreen(quadratic::fStruct fVars)
  91. {
  92.     double a = fVars.coeffs[0];
  93.     double b = fVars.coeffs[1];
  94.     double c = fVars.coeffs[2];
  95.  
  96.     cout << "The solution of a quadratic equation with coefficients:" << endl << "a=" << a << ", b="
  97. << b << ", c=" << c << endl << "has not been found." << endl;
  98.     return;
  99. }
  100.  
  101. void quadratic::linear(quadratic::fStruct fVars, double root)
  102. {
  103.     double a = fVars.coeffs[0];
  104.     double b = fVars.coeffs[1];
  105.     double c = fVars.coeffs[2];
  106.  
  107.     cout << "Linear equation with coefficients:" <<endl << "b: " << b << "  c: " << c << endl<<"Has root: "<<root <<endl;
  108.  
  109.     ofstream dataStore;
  110.     dataStore.open("results.dat", ios::out | ios::app);
  111.     dataStore << endl << "Linear equation with coefficients:" <<endl <<" b: " << b << "  c: " << c << endl<<"Has root: " <<root <<endl;
  112.     return;
  113. }
  114. void QuickTest(double a, double b, double c)
  115. {
  116.     if (a==0.0 && b==0.0 && c!=0.0)  // Horizontal line
  117.     {
  118.         cout << "By definition, the root of  the horizontal line is" << c <<endl;
  119.         ofstream dataStore;
  120.         dataStore.open("results.dat", ios::out | ios::app);
  121.         dataStore << endl << "By definition, the roof of  the horizontal line is" << c <<endl;
  122.         exit(1);
  123.     }
  124.     else if (a==0.0 && c==0.0 && b!=0.0)  // Always 0
  125.     {
  126.         cout << "By definition, if a and c are zero, and b is defined, the root is always at 0." <<endl;
  127.         ofstream dataStore;
  128.         dataStore.open("results.dat", ios::out | ios::app);
  129.         dataStore << endl << "By definition, if a and c are zero, and b is defined, the root is always at 0." <<endl;
  130.         exit(2);
  131.     }
  132.     else if (a==0.0 && c==0.0 && c==0.0)
  133.     {
  134.         cout << "When all three coeff's are 0, there is no root." <<endl;
  135.         ofstream dataStore;
  136.         dataStore.open("results.dat", ios::out | ios::app);
  137.         dataStore << endl << "When all three coeff's are 0, there is no root." <<endl;
  138.         exit(3);
  139.     }
  140.  
  141.     else
  142.     {
  143.         cout<< "An Error Has Occured. Program Terminated."<<endl;
  144.         ofstream dataStore;
  145.         dataStore.open("results.dat", ios::out | ios::app);
  146.         dataStore << endl << "An error has occurred! Error code: 1." <<endl;
  147.         exit(4);
  148.     }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment