#include <iostream>
#include <sstream>
#include <cmath>
void quadratic( double a, double b , double c , std::string &x1 , std::string &x2 )
{
// ( -b +/- sqrt( b*b - ( 4 * a * c ) ) ) / ( 2 * a )
//if discriminant is negative it is imaginary
double discriminant = b * b - ( 4 * a * c );
double denominator = 2 * a;
bool imaginary = false;
if( discriminant < 0 )
{
imaginary = true;
discriminant = -discriminant;
}
std::stringstream ss;
if( imaginary )
{
ss << -b / denominator;
x1 = ss.str() + '+';
x2 = ss.str() + '-';
ss.str("");
ss.clear();
ss << sqrt( discriminant ) / denominator;
x1 += ss.str() + 'i';
x2 += ss.str() + 'i';
}
else
{
ss << ( -b + sqrt( discriminant ) ) / denominator;
x1 = ss.str();
ss.str("");
ss.clear();
ss << ( -b - sqrt( discriminant ) ) / denominator ;
x2 = ss.str();
}
}
int main()
{
double a = 1.0;
double b = -11.0;
double c = 36.5;
std::string x1 = "";
std::string x2 = "";
quadratic( a , b , c , x1 , x2 );
std::cout << x1 << std::endl;
std::cout << x2 << std::endl;
quadratic( -1 , 10 , 5 , x1 , x2 );
std::cout << x1 << std::endl;
std::cout << x2 << std::endl;
return(0);
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3N0cmVhbT4KI2luY2x1ZGUgPGNtYXRoPgoKdm9pZCBxdWFkcmF0aWMoIGRvdWJsZSBhLCBkb3VibGUgYiAsIGRvdWJsZSBjICwgc3RkOjpzdHJpbmcgJngxICwgc3RkOjpzdHJpbmcgJngyICkKewogICAgLy8gKCAtYiArLy0gc3FydCggYipiIC0gKCA0ICogYSAqIGMgKSApICkgLyAoIDIgKiBhICkKICAgIC8vaWYgZGlzY3JpbWluYW50IGlzIG5lZ2F0aXZlIGl0IGlzIGltYWdpbmFyeQogICAgZG91YmxlIGRpc2NyaW1pbmFudCA9IGIgKiBiIC0gKCA0ICogYSAqIGMgKTsKICAgIGRvdWJsZSBkZW5vbWluYXRvciA9IDIgKiBhOwogICAgYm9vbCBpbWFnaW5hcnkgPSBm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