Need constructive criticism for Quadratic equation calculator 2.0!
Better indented:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
#include <iostream>
#include <string>
#include <sstream>
#include <cmath>
using namespace std;
int main()
{
string use_again;
string a_var_s;
long double a_var = 0;
string b_var_s;
long double b_var = 0;
string c_var_s;
long double c_var = 0;
long double b_var_sqrd = 0;
long double discriminant = 0;
unsigned int discriminant_indication = 0;
long double divisor = 0;
long double dividend = 0;
long double root_var = 0;
long double res = 0;
long double res_2 = 0;
string correct_equation;
string correct_equation_validation;
do
{
cout << endl;
cout << "Quadratic equation calculator, 2.0." << endl;
cout << endl;
cout << "This program only calculates quadratic equations that are in ax^2 + bx + c = 0 form." << endl;
cout << endl;
cout << "This form is also known as standard form." << endl;
cout << endl;
while ( correct_equation != "y" )
{
cout << "Input the value of a:" << endl;
getline ( cin, a_var_s );
stringstream( a_var_s ) >> a_var;
cout << "Input the value of b:" << endl;
getline ( cin, b_var_s );
stringstream( b_var_s ) >> b_var;
cout << "Input the value of c:" << endl;
getline ( cin, c_var_s );
stringstream( c_var_s ) >> c_var;
cout << endl;
cout << "Did you mean to input the following equation: " << a_var << "x^2";
if ( b_var >= 0 )
cout << "+" << b_var << "x";
else
cout << b_var << "x";
if ( c_var >= 0 )
cout << "+" << c_var;
else
cout << c_var;
cout << " = 0?" << endl;
cout << endl;
do
{
cout << "Input 'y' if the equation was correct." << endl;
cout << "Input 'n' if the equation was incorrect." << endl;
cin >> correct_equation;
if ( correct_equation != "y" && correct_equation != "n" )
{
correct_equation_validation = "n";
cout << "Invalid user input. Please try again." << endl;
cout << endl;
}
else if ( correct_equation == "y" )
correct_equation_validation = "y";
else
{
correct_equation_validation = "y";
correct_equation = "n";
}
cin.ignore();
}
while ( correct_equation_validation == "n" );
}
b_var_sqrd = pow ( b_var, 2 );
discriminant = b_var_sqrd - 4*a_var*c_var;
cout << "The discriminant of the equation is " << discriminant << "." << endl;
cout << "Therefore: ";
if ( discriminant >-0.00000000001 && discriminant < 0.00000000001 ) // Never use == to compare floating point variables.
{
cout << "x can only have one real value." << endl;
discriminant_indication = 1;
}
else if ( discriminant < 0 )
{
cout << "x has no real values." << endl;
discriminant_indication = 0;
}
else
{
cout << "x has two real values." << endl;
discriminant_indication = 2;
}
root_var = sqrt( discriminant );
dividend = -1 * b_var + root_var;
divisor = 2 * a_var;
res = dividend/divisor;
if ( discriminant_indication == 1 )
cout << "x = " << res << "." << endl;
else if ( discriminant_indication == 2 )
{
cout << "x = " << res << "." << endl;
dividend = -1 * b_var - root_var;
divisor = 2 * a_var;
res_2 = dividend/divisor;
cout << "OR: x = " << res_2 << "." << endl;
}
cout << "Input 'y' to use this program again:" << endl;
cin >> use_again;
correct_equation = "n";
cin.ignore();
}
while ( use_again == "y" || use_again == "Y" );
cin.get();
return 0;
}
|
Topic archived. No new replies allowed.