Advertisement
Guest User

Untitled

a guest
Jul 28th, 2015
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. //#include "PraticeLib.h"
  4. using namespace std;
  5.  
  6.  
  7. void WhatTheProgramWillDo()
  8. {
  9. cout << "Program to solve quadratic equation: ax^2 + bx + c = 0";
  10. }
  11. double GetToleranceValue()
  12. {
  13. double toler;
  14. cout << "\n\nEnter a positive tolerance value: " << flush;
  15. cin >> toler;
  16. while (toler <= 0.0)
  17. {
  18. cout << "\n*****ERROR"
  19. << "\nPlease enter in a positive tolerance value: " << flush;
  20. cin >> toler;
  21. }
  22. return toler;
  23. }
  24. int GetNumberOfDecimalPlaces()
  25. {
  26. int ndec;
  27. cout << "Enter the desired number of decimal places: " << flush;
  28. cin >> ndec;
  29. while (ndec <= 0)
  30. {
  31. cout << "\n*****ERROR"
  32. << "\nPlease enter in a positive value for number of decimals: " << flush;
  33. cin >> ndec;
  34. }
  35. cout << fixed << setprecision(ndec);
  36. return ndec;
  37. }
  38. double GetValuesFor_a_b_c(double &a, double &b, double &c)
  39. {
  40. cout << "\nEnter coefficients for a, b, and c "
  41. << "\n(where \"a\" is a non-zero number): " << flush;
  42. cin >> a >> b >> c;
  43. while (a == 0.0)
  44. {
  45. cout << "\n*****ERROR"
  46. << "\nPlease enter in a non-zero number for \"a\""
  47. << "\n(make sure you're solving for a quadratic problem): " << flush;
  48. cin >> a >> b >> c;
  49. }
  50. //discrm = b*b - 4.0f * a*c;
  51. return (b*b - 4.0*a*c);
  52. }
  53. int SolveQuadraticEquation(double &a, double &b, double &c/*, double &value1, double &value2*/)
  54. {
  55. //int solutionType;
  56. int ndec;
  57. double discrm, toler, realDblRt, xReal, imagr, value1, value2;
  58. toler = GetToleranceValue();
  59. ndec = GetNumberOfDecimalPlaces();
  60. discrm = GetValuesFor_a_b_c(a, b, c);
  61.  
  62. if (discrm >= toler)
  63. {
  64. value1 = (-b + sqrt(discrm)) / (2 * a);
  65. value2 = (-b - sqrt(discrm)) / (2 * a);
  66. return 1;
  67. }
  68. else if (discrm > -toler)
  69. {
  70. realDblRt = -b / (2 * a);
  71. value1 = realDblRt;
  72. return 0;
  73. }
  74. else
  75. {
  76. xReal = -b / (2 * a);
  77. imagr = (sqrt(abs(discrm))) / (abs(2 * a));
  78. value1 = xReal;
  79. value2 = imagr;
  80. return -1;
  81. }
  82. //return solutionType;
  83. }
  84. void WriteQuadraticEquationSolution(int &solutionType, double &value1, double &value2)
  85. {
  86. double a, b, c, discrm;
  87. solutionType = SolveQuadraticEquation(a, b, c);
  88. discrm = b*b - 4.0*a*c;
  89. if (solutionType == 1)
  90. {
  91. value1 = (-b + sqrt(discrm)) / (2 * a);
  92. value2 = (-b - sqrt(discrm)) / (2 * a);
  93. cout << "\n\nThe equation has two real roots: \n"
  94. << "\n x1 = " << value1
  95. << "\n x2 = " << value2;
  96. }
  97. else if (solutionType == 0)
  98. {
  99. value1 = -b / (2 * a);
  100. cout << "\n\nThe equation has one double real root: \n"
  101. << "\n " << value1;
  102. }
  103. else /*if (solutionType == -1)*/
  104. {
  105. value1 = -b / (2 * a);
  106. value2 = (sqrt(abs(discrm))) / (abs(2 * a));
  107. cout << "\n\nThe equation has two complex roots: \n"
  108. << "\n c1 = " << value1 << " + " << value2 << "i"
  109. << "\n c2 = " << value1 << " - " << value2 << "i";
  110. }
  111. }
  112. bool DoYouWantToSolveAnotherQuadraticEquation()
  113. {
  114. bool solveAnother;
  115. cout << "\n\nDo you want to find the roots of another quadratic?"
  116. << "\nEnter 0 for no, or non-zero for yes: " << flush;
  117. cin >> solveAnother;
  118. if (solveAnother == 0)
  119. solveAnother = false;
  120. else
  121. solveAnother = true;
  122. return solveAnother;
  123. }
  124.  
  125. int main()
  126. {
  127. double /*a, b, c, */value1, value2;
  128. int solutionType, solveAnother;
  129.  
  130. do{
  131. WhatTheProgramWillDo();
  132. //GetToleranceValue();
  133. //GetNumberOfDecimalPlaces();
  134. //GetValuesFor_a_b_c(a, b, c);
  135. //SolveQuadraticEquation(a, b, c);
  136. WriteQuadraticEquationSolution(solutionType, value1, value2);
  137. DoYouWantToSolveAnotherQuadraticEquation();
  138. } while (solveAnother = true);
  139.  
  140. cin.get();
  141. cin.get();
  142. return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement