Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- //#include "PraticeLib.h"
- using namespace std;
- void WhatTheProgramWillDo()
- {
- cout << "Program to solve quadratic equation: ax^2 + bx + c = 0";
- }
- double GetToleranceValue()
- {
- double toler;
- cout << "\n\nEnter a positive tolerance value: " << flush;
- cin >> toler;
- while (toler <= 0.0)
- {
- cout << "\n*****ERROR"
- << "\nPlease enter in a positive tolerance value: " << flush;
- cin >> toler;
- }
- return toler;
- }
- int GetNumberOfDecimalPlaces()
- {
- int ndec;
- cout << "Enter the desired number of decimal places: " << flush;
- cin >> ndec;
- while (ndec <= 0)
- {
- cout << "\n*****ERROR"
- << "\nPlease enter in a positive value for number of decimals: " << flush;
- cin >> ndec;
- }
- cout << fixed << setprecision(ndec);
- return ndec;
- }
- double GetValuesFor_a_b_c(double &a, double &b, double &c)
- {
- cout << "\nEnter coefficients for a, b, and c "
- << "\n(where \"a\" is a non-zero number): " << flush;
- cin >> a >> b >> c;
- while (a == 0.0)
- {
- cout << "\n*****ERROR"
- << "\nPlease enter in a non-zero number for \"a\""
- << "\n(make sure you're solving for a quadratic problem): " << flush;
- cin >> a >> b >> c;
- }
- //discrm = b*b - 4.0f * a*c;
- return (b*b - 4.0*a*c);
- }
- int SolveQuadraticEquation(double &a, double &b, double &c/*, double &value1, double &value2*/)
- {
- //int solutionType;
- int ndec;
- double discrm, toler, realDblRt, xReal, imagr, value1, value2;
- toler = GetToleranceValue();
- ndec = GetNumberOfDecimalPlaces();
- discrm = GetValuesFor_a_b_c(a, b, c);
- if (discrm >= toler)
- {
- value1 = (-b + sqrt(discrm)) / (2 * a);
- value2 = (-b - sqrt(discrm)) / (2 * a);
- return 1;
- }
- else if (discrm > -toler)
- {
- realDblRt = -b / (2 * a);
- value1 = realDblRt;
- return 0;
- }
- else
- {
- xReal = -b / (2 * a);
- imagr = (sqrt(abs(discrm))) / (abs(2 * a));
- value1 = xReal;
- value2 = imagr;
- return -1;
- }
- //return solutionType;
- }
- void WriteQuadraticEquationSolution(int &solutionType, double &value1, double &value2)
- {
- double a, b, c, discrm;
- solutionType = SolveQuadraticEquation(a, b, c);
- discrm = b*b - 4.0*a*c;
- if (solutionType == 1)
- {
- value1 = (-b + sqrt(discrm)) / (2 * a);
- value2 = (-b - sqrt(discrm)) / (2 * a);
- cout << "\n\nThe equation has two real roots: \n"
- << "\n x1 = " << value1
- << "\n x2 = " << value2;
- }
- else if (solutionType == 0)
- {
- value1 = -b / (2 * a);
- cout << "\n\nThe equation has one double real root: \n"
- << "\n " << value1;
- }
- else /*if (solutionType == -1)*/
- {
- value1 = -b / (2 * a);
- value2 = (sqrt(abs(discrm))) / (abs(2 * a));
- cout << "\n\nThe equation has two complex roots: \n"
- << "\n c1 = " << value1 << " + " << value2 << "i"
- << "\n c2 = " << value1 << " - " << value2 << "i";
- }
- }
- bool DoYouWantToSolveAnotherQuadraticEquation()
- {
- bool solveAnother;
- cout << "\n\nDo you want to find the roots of another quadratic?"
- << "\nEnter 0 for no, or non-zero for yes: " << flush;
- cin >> solveAnother;
- if (solveAnother == 0)
- solveAnother = false;
- else
- solveAnother = true;
- return solveAnother;
- }
- int main()
- {
- double /*a, b, c, */value1, value2;
- int solutionType, solveAnother;
- do{
- WhatTheProgramWillDo();
- //GetToleranceValue();
- //GetNumberOfDecimalPlaces();
- //GetValuesFor_a_b_c(a, b, c);
- //SolveQuadraticEquation(a, b, c);
- WriteQuadraticEquationSolution(solutionType, value1, value2);
- DoYouWantToSolveAnotherQuadraticEquation();
- } while (solveAnother = true);
- cin.get();
- cin.get();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement