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
|
//july fourth, 2014
#include <iostream>
#include <string>
#include <cmath>
#include <cstdlib>//simplify the answer
using namespace std;
int count=0;
//prototyping
double ans_1(double,double,double);
double ans_2(double,double,double);
double nanhe(double,double,double);
void simplify(double*,double*,double*);
int main()
{
double a,b, c;
cout<<"Quadratic Equation Solver \n";
cout<<"Enter a value for a: ";
cin>>a;
cout<<endl;
cout<<"Enter a value for b: ";
cin>>b;
cout<<endl;
cout<<"Enter a value for c: ";
cin>>c;
cout<<endl;
if ( isnan(sqrt((b*b)-(4*a*c))))
{
count++;
}
if (!count)
{
double answer01=ans_1(a,b,c);
double answer02=ans_2(a,b,c);
cout<<"X="<<answer01<<endl;
cout<<"X="<<answer02<<endl;
}
else if (count) //could route these imag ones to separate funcitons instead of count++
{
double answer01=ans_1(a,b,c);
double answer02=ans_1(a,b,c);
cout<<"X=("<<-b<<"+"<<answer01<<"i)/"<<2*a<<endl;
cout<<"X=("<<-b<<'-'<<answer02<<"i)/"<<2*a<<endl;
}
system("pause");
}
double ans_1(double a, double b, double c)
{
double ans1;
double temp_c=sqrt((b*b)-(4*a*c));
if (isnan(temp_c))
{
temp_c=nanhe(a,b,c);
}
if (!count)
{
ans1=((-b+temp_c)/(2*a));
}
else if (count)
{
ans1=((temp_c));
}
simplify(&a,&b,&ans1);
return ans1;
}
double ans_2(double a, double b, double c)
{
double ans2;
double temp_d=sqrt((b*b)-(4*a*c));
if (isnan(temp_d))
{
temp_d=nanhe(a,b,c);
}
if (!count)
{
ans2=((-b-temp_d)/(2*a));
}
else if (count)
{
ans2=(temp_d);
}
simplify(&a,&b,&ans2); //line under this should alter ans2 so its returning the simplified version instead, or just make a new variable instead of ans2
return ans2;
}
double nanhe(double a, double b, double c) //still need to apply simplify() to nanhe
{
double temp_help;
temp_help=sqrt(-1*((b*b)-(4*a*c)));
count++;
return temp_help;
}
void simplify(double* a, double* b, double* ans) //only run if complex
{
ans/=(2*a);
b/=(2*a);
}
|