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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
/*****************************
* Data types declarations *
******************************/
struct Quadratic_equation
{
int a;
int b;
int c;
};
/*****************************
* Function declarations *
******************************/
void get_quadratic_equation(Quadratic_equation &e);
int load_equation(Quadratic_equation V[], int n);
void display_equation(Quadratic_equation &e);
void _swap(Quadratic_equation V[], int lowerIndex, int higherIndex);
void sort_equation_array(Quadratic_equation V[], int howMany);
void print(Quadratic_equation V[], int howMany);
void print1(Quadratic_equation V[], int howMany);
void print2(Quadratic_equation V[], int howMany);
/******************************
* INT MAIN *
******************************/
int main()
{
const int SIZE = 100;
Quadratic_equation A[SIZE] = {0};
int howMany = 0;
cout << "** Enter Quadratic Equations **" << endl;
howMany = load_equation(A, SIZE);
sort_equation_array(A, howMany);
cout << "** Quadratic equations with one root **" << endl;
// Display
print(A, howMany);
cout << "** Quadratic equations with two roots **" << endl;
print1(A, howMany);
cout << "** Quadratic equations with complex roots **" << endl;
print2(A, howMany);
}
/*************************************
* Function definitions *
**************************************/
void get_quadratic_equation(Quadratic_equation &e)
{
cin >> e.a; //Here we input the three integers to represent the quadratic equation
cin >> e.b;
cin >> e.c;
}
int load_equation(Quadratic_equation V[], int n)
{
Quadratic_equation e;
int counter = 0;
get_quadratic_equation(e);
while (e.a != 0) // when a is 0, the array won't continue
{
V[counter] = e;
++counter;
if (counter == n) break;
get_quadratic_equation(e);
}
return counter;
}
void display_equation(Quadratic_equation &e)
{
float x1; //the actual roots, x1 and x2
int a = e.a;
int b = e.b;
int c = e.c;
int d = (( b * b ) - ( 4 * a * c )); // the discriminant
if (d == 0)
{
cout << a << "x^2 ";
if (b < 0)
{
cout << b << "x";
}
else if (b > 0)
{
cout << "+" << b << "x";
}
if (c < 0)
{
cout << " " << c << " ";
}
else if (c > 0)
{
cout << " +" << c << " ";
}
x1 = ( ( - b ) / ( 2 * a ) );
cout << " " << "root 1 = " << fixed << setprecision(2) << x1 << endl;
}
}
void display_equation1(Quadratic_equation &e)
{
float x1; //the actual roots, x1 and x2
float x2;
int a = e.a;
int b = e.b;
int c = e.c;
int d = (( b * b ) - ( 4 * a * c )); // the discriminant
if (d > 0)
{
cout << a << "x^2 ";
if (b < 0)
{
cout << b << "x";
}
else if (b > 0)
{
cout << "+" << b << "x";
}
if (c < 0)
{
cout << " " << c << " ";
}
else if (c > 0)
{
cout << " +" << c << " ";
}
x1 = ( ( - b + sqrt ( d * 1.0) ) / ( 2 * a ) ); //* 1.0 because else the answer would be an int
cout << " root 1 = " << fixed << setprecision(2) << x1 << " ";
x2 = ( ( - b - sqrt ( d * 1.0 ) ) / ( 2 * a ) );//* 1.0 because else the answer would be an int
cout << "root 2 = " << fixed << setprecision(2) << x2 << endl;
}
}
void display_equation2(Quadratic_equation &e)
{
float x1; //the actual roots, x1 and x2
float x2;
int a = e.a;
int b = e.b;
int c = e.c;
int d = (( b * b ) - ( 4 * a * c )); // the discriminant
if (d < 0)
{
cout << a << "x^2 ";
if (b < 0)
{
cout << b << "x";
}
else if (b > 0)
{
cout << "+" << b << "x";
}
if (c < 0)
{
cout << " " << c << " ";
}
else if (c > 0)
{
cout << " +" << c << " ";
}
x1 = ( ( - b ) / ( 2 * a * 1.0) ); //* 1.0 because else the answer would be an int
x2 = ( sqrt(-d * 1.0) / ( 2 * a ));
cout << " root 1 = " << fixed << setprecision(2) << x1
<< "+i" << x2 << " ";
cout << " root 2 = " << fixed << setprecision(2) << x1
<< "-i" << x2 << endl;
}
}
void _swap(Quadratic_equation V[], int lowerIndex, int higherIndex)
{
Quadratic_equation foo = V[lowerIndex];
V[lowerIndex] = V[higherIndex];
V[higherIndex] = foo;
}
void sort_equation_array(Quadratic_equation V[], int howMany)
{
for ( int sort = 0; sort < howMany; sort++ )
{
for (int i = (sort + 1); i <= howMany -1; i++)
{
if (V[sort].a > V[i].a)
{
_swap(V, sort, i);
}
else if (V[sort].a == V[i].a &&
V[sort].b > V[i].b)
{
_swap(V, sort, i);
}
else if (V[sort].a == V[i].a &&
V[sort].b == V[i].b &&
V[sort].c > V[i].c)
{
_swap(V, sort, i);
}
}
}
}
void print(Quadratic_equation V[], int howMany)
{
for (int i = 0; i < howMany; i++)
{
display_equation(V[i]);
}
}
void print1(Quadratic_equation V[], int howMany)
{
for (int i = 0; i < howMany; i++)
{
display_equation1(V[i]);
}
}
void print2(Quadratic_equation V[], int howMany)
{
for (int i = 0; i < howMany; i++)
{
display_equation2(V[i]);
}
}
|