Hello, I have quite heavily altered Ryan Parker's quadratic solver but there is one thing which is still alluding me: adding support for complex solutions.
Here is my code: (it is part of a much larger program)
#include <iostream>
#include <cmath>
#include <complex> //Seemingly unused libraries are used for other parts of the program.
#include <stdio.h>
#include <conio.h>
#include <cstdlib>
#include <ctime>
float x1plusb;
float x2plusd;
int one;
int two;
int three;
int quadchoice;
int x;
int a;
int b;
int c; //At the moment the source code is messy as my bedroom. I will tidy it up and use conventional notation.
double bsquared;
double fourac;
double dis;
double rootdis;
float realpart;
float imaganswer1; //Imagianary/complex soloutions do not calculate correctly yet.
float imaganswer2;
int bottom;
double answer1;
double answer2;
float x1plusa;
float x2plusc;
int main ()
usingnamespace std;
{
//The original code can be found at http://www.cplusplus.com/forum/beginner/59933/
//I have corrected some of the spelling mistakes, tidied up some of the code, tried to add support for complex solutions, added support for (ax+b)(cx+d) expansion and added a return to menu feature.
cout << "Quadratic equation Solver \n" << endl;
cout << "Type 1 and press enter if your problem is in standard form" << endl;
cout << "Type 2 to expand brackets in (ax+b)(cx+d) form." << endl;
cin >> quadchoice;
if (quadchoice == 1)
{
{
system("cls");
cout << "ax^2+bx+c" << endl;
cout << "Input the 'a' variable: " << endl;
cin >> a;
cout << "Input the 'b' variable: " << endl;
cin >> b;
cout << "Input the 'c' variable: " << endl;
cin >> c;
bottom = 2*a;
bsquared = pow (b,2);
fourac = 4*a*c;
dis = bsquared - fourac;
cout << "The discriminent is: " << dis << endl;
if (dis < 0)
{
complex<double> imagpart;
imagpart = sqrt(dis)-fourac/bottom;
realpart = -b/bottom; //I have added support for negative roots
cout << "x=" << realpart <<"+" <<imagpart.imag() << "i"<<endl;
cout << "x=" << realpart <<"-" <<imagpart.imag() << "i" <<endl;
cout << "\n" << endl;
cout << "\n" << endl; //Negative roots part of code here
printf("Press enter to return to the main menu for another computation");
fflush(stdout);
getchar();
getchar();
system("CLS");
goto Gausmainmenu; return 0;
}
else
{
rootdis = sqrt(dis);
answer1 = (-b+rootdis);
answer2 = (-b-rootdis);
cout << "x=" << answer1 / bottom << endl;
cout << "x=" << answer2 / bottom << endl;
cout << "\n" << endl;
cout << "\n" << endl;
printf("Press enter to return to the main menu for another computation");
fflush(stdout);
getchar();
getchar();
system("CLS");
goto Gausmainmenu;
return 0;
}
}
}
elseif (quadchoice == 2)
{
{
cout << "Input the a variable:" << endl;
cin >> x1plusa;
cout << "Input the b variable:" << endl;
cin >> x1plusb;
cout << "input the c variable:" << endl; //I added (ax+b)(cx+d) support
cin >> x2plusc;
cout << "Input the d variable:" << endl;
cin >> x2plusd;
cout << "The brackets expanded:" << x1plusa*x2plusc << "x^2+" << (x1plusb*x2plusc)+(x1plusa*x2plusd) << "x+" << x1plusb*x2plusd << endl;
printf("Press enter to return to the main menu for another computation");
fflush(stdout);
getchar();
getchar();
system("CLS");
goto Gausmainmenu;
return 0;
}
}
else
{
cout << "Only 1 or 2 can be chosen!";
printf("Press enter to return to the main menu for another computation");
fflush(stdout);
getchar();
getchar();
system("CLS");
goto Gausmainmenu;
return 0;
}
.....
How do I make my program correctly calculate complex solutions?
Bump- please help me out, I have been stuck on this for days! The person who can point me towards a solution will have their name in the "Dedicated to" part of the program if they desire.
If the value of the discriminant is positive (b2-4ac) > 0
then everything works as planned. If the discriminant is negative, then the solutions are imaginary. However, the only difference (as far as the program is concerned) is the additional char "i".
So, if you calculate the value of the discriminant and it is less than 0,
simply set the discriminant variable to the negative of the discriminant,
1 2
if( dis<0)
dis = -dis;
solve the equation with this new (now positive) discriminant, and append char i to the answer.
Yes, I know that this solution is a bad hack, but, to be honest, I've never really used <complex>
Thanks very much for this suggestion! I'll try it as soon as I can; I have school tomorrow and it's 23:25 where I am so I am going to have to hit the hay now.