void quadratic_solver()
{
std::vector<char> equation;
double a = 0, b = 0, c = 0;
double answer1 = 0, answer2 = 0;
double inRoot = 0;
char character;
std::cout << "Enter the equation (e.g. 2x^2 - 5x + 3|): ";
while( ( std::cin >> character ) && ( character != '|' ) )
{
equation.push_back(character);
}
for( int i = 0; i < equation.size(); i++ )
{
if( equation[i] == 'x' )
{
if( equation[i + 1] == '^' )
{
if( equation[i + 2] == '2' )
{
a = equation[i - 1];
}
}
else if( equation[i + 1] != '^' )
{
b = equation[i - 1];
}
}
}
c = equation[equation.size()];
inRoot = pow( b, 2 ) - ( 4 * a * c );
if( inRoot < 0 )
{
std::cout << "Error: Number inside of square root is negative.";
}
else if( ( a == 0 ) || ( b == 0 ) || ( c == 0 ) )
{
std::cout << "Error: Not a quadratic equation.";
}
else
{
answer1 = ( -b + sqrt( inRoot ) ) / ( 2 * a );
answer2 = ( -b - sqrt( inRoot ) ) / ( 2 * a );
std::cout << "x = " << answer1 << ", x = " << answer2;
}
wait_continue();
}
If anyone can suggest a better way of doing this and/or where I'm going wrong that'd be great, thanks :)


Sign In
Create Account

Back to top









