Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Author: Chris Christoff
- // Current Build: 4.2.3
- // Last build time: 8:55 A.M/ 6/12/2012
- #include "assign5.h"
- #include <iostream>
- #include <fstream>
- #include <cmath>
- #include <limits>
- #include <stdlib.h>
- using namespace std;
- quadratic::quadratic()
- {
- }
- quadratic::fStruct quadratic::readCoeffs()
- {
- fStruct fVars;
- char coef[]= {'a','b','c'};
- for (int i=0; i<3; i++)
- {
- while (((cout << "Enter coefficient for "<<coef[i]<<": ") && !(cin >> fVars.coeffs[i])) || cin.peek() != '\n')
- {
- cout << "Invalid Input." << endl;
- cin.clear();
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- }
- }
- return fVars;
- }
- double quadratic::f(quadratic::fStruct fVars, double x)
- {
- double a = fVars.coeffs[0];
- double b = fVars.coeffs[1];
- double c = fVars.coeffs[2];
- double fOfX = ((a*(pow(x,2.0))) + (b*x) + c);
- return fOfX;
- }
- double quadratic::bisect(double xLeft, double xRight, double epsilon, quadratic::fStruct fVars, bool& error)
- {
- quadratic q;
- double xMid;
- double fMid;
- double fLeft = q.f(fVars, xLeft);
- double fRight = q.f(fVars, xRight);
- error = (fLeft * fRight) > 0;
- if(error)
- return -999.0;
- while(fabs(xLeft-xRight)>epsilon)
- {
- xMid = (xLeft + xRight) / 2.0;
- fMid = q.f(fVars, xMid);
- if(fMid==0.0)
- return xMid;
- else if ((fLeft * fMid) < 0.0)
- xRight = xMid;
- else
- xLeft = xMid;
- }
- return (xLeft+xRight) / 2.0;
- }
- void quadratic::toFile(quadratic::fStruct fVars, quadratic::rootStruct rootStruct)
- {
- double a = fVars.coeffs[0];
- double b = fVars.coeffs[1];
- double c = fVars.coeffs[2];
- ofstream dataStore;
- dataStore.open("results.dat", ios::out | ios::app);
- dataStore << endl << "Quadratic equation with the following coefficients: " << endl << "a: "
- << a << " b: " << b << " c: " << c << endl << "has the following roots" << endl << "Root1: "
- << rootStruct.roots[0] << " Root2: " << rootStruct.roots[1] << endl;
- return;
- }
- void quadratic::toScreen(quadratic::fStruct fVars)
- {
- double a = fVars.coeffs[0];
- double b = fVars.coeffs[1];
- double c = fVars.coeffs[2];
- cout << "The solution of a quadratic equation with coefficients:" << endl << "a=" << a << ", b="
- << b << ", c=" << c << endl << "has not been found." << endl;
- return;
- }
- void quadratic::linear(quadratic::fStruct fVars, double root)
- {
- double a = fVars.coeffs[0];
- double b = fVars.coeffs[1];
- double c = fVars.coeffs[2];
- cout << "Linear equation with coefficients:" <<endl << "b: " << b << " c: " << c << endl<<"Has root: "<<root <<endl;
- ofstream dataStore;
- dataStore.open("results.dat", ios::out | ios::app);
- dataStore << endl << "Linear equation with coefficients:" <<endl <<" b: " << b << " c: " << c << endl<<"Has root: " <<root <<endl;
- return;
- }
- void QuickTest(double a, double b, double c)
- {
- if (a==0.0 && b==0.0 && c!=0.0) // Horizontal line
- {
- cout << "By definition, the root of the horizontal line is" << c <<endl;
- ofstream dataStore;
- dataStore.open("results.dat", ios::out | ios::app);
- dataStore << endl << "By definition, the roof of the horizontal line is" << c <<endl;
- exit(1);
- }
- else if (a==0.0 && c==0.0 && b!=0.0) // Always 0
- {
- cout << "By definition, if a and c are zero, and b is defined, the root is always at 0." <<endl;
- ofstream dataStore;
- dataStore.open("results.dat", ios::out | ios::app);
- dataStore << endl << "By definition, if a and c are zero, and b is defined, the root is always at 0." <<endl;
- exit(2);
- }
- else if (a==0.0 && c==0.0 && c==0.0)
- {
- cout << "When all three coeff's are 0, there is no root." <<endl;
- ofstream dataStore;
- dataStore.open("results.dat", ios::out | ios::app);
- dataStore << endl << "When all three coeff's are 0, there is no root." <<endl;
- exit(3);
- }
- else
- {
- cout<< "An Error Has Occured. Program Terminated."<<endl;
- ofstream dataStore;
- dataStore.open("results.dat", ios::out | ios::app);
- dataStore << endl << "An error has occurred! Error code: 1." <<endl;
- exit(4);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment