Adapting a quadratic solver to solve imaginaries

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)


....


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
#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 ()
using namespace 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;
}
}
}
else if (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?
Last edited on
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.

Please
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.

Thanks again!
Haha, I couldn't wait! :P I solved it by doing this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
...

    dis = -dis;
    imagpart = sqrt(dis)/bottom;
    realpart = -b/bottom;                                                    //I have added support for negative roots
    cout << "x=" << realpart <<"+" <<imagpart << "i"<<endl;
    cout << "x=" << realpart <<"-" <<imagpart <<  "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;
}


Thanks a lot for this idea!

Would you like your forum user name in the "Dedicated to:" section?
Last edited on
Topic archived. No new replies allowed.