Hello, guys.
I wrote the code to a Quadratic Equations Solver and I'd like for you to run the program and read the source code and give me tips on what I can improve, what is good and what is bad, suggestions, etc. .
#include <iostream>
#include <locale.h>
#include <math.h>
#include <cmath>
#include <complex>
#include <windows.h>
#include <conio.h>
#include <limits>
using namespace std;
float a = 0; float b = 0; float c = 0;float x1 = 0; float x2 = 0;
double Delta = 0;
bool repetir = false;
int i = 0; int Sim_ou_Nao = 0;
void Instrucoes(bool);
void ClearScreen();
float Descobrir_Coeficientes (float,float,float, bool);
float Calcular_Raizes(float, float, float);
float Mostrar_Raizes (float,float);
float Mostrar_Raizes_Complexas(float,float, float);
bool Repetir(bool);
void Instrucoes(bool repetir)
{
if ( (i == 0 ) || (repetir == true))
{
cin.sync();
cout << "\nWhen typing the coeficients, utilize only numbers \nand the '.' to represent fractional numbers." << endl;
cout << "\nDo not utilize fractions or exponents. " << endl << endl;
cout << "Press any key to continue." << endl;
getch();
cout << "------------------------------------------------------------------------------" << endl;
Descobrir_Coeficientes(a,b,c, repetir);
}
}
void ClearScreen()
{
HANDLE hStdOut;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD count;
DWORD cellCount;
COORD homeCoords = { 0, 0 };
hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
if (hStdOut == INVALID_HANDLE_VALUE) return;
/* Get the number of cells in the current buffer */
if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return;
cellCount = csbi.dwSize.X *csbi.dwSize.Y;
/* Fill the entire buffer with spaces */
if (!FillConsoleOutputCharacter(
hStdOut,
(TCHAR) ' ',
cellCount,
homeCoords,
&count
)) return;
/* Fill the entire buffer with the current colors and attributes */
if (!FillConsoleOutputAttribute(
hStdOut,
csbi.wAttributes,
cellCount,
homeCoords,
&count
)) return;
/* Move the cursor home */
SetConsoleCursorPosition( hStdOut, homeCoords );
}
float Descobrir_Coeficientes(float a, float b, float c, bool repetir)
{
cin.sync();
cout << "\nType in the coeficient of the term that follows x²."<< endl;
cin >> a;
cin.sync();
if(!cin) // or if(cin.fail())
{
// user didn't input a number
cin.clear(); // reset failbit
cin.ignore(numeric_limits<streamsize>::max(), '\n'); //skip bad input
// next, request user reinput
cout << "The coeficient needs to be a number." << endl << endl;
getch();
ClearScreen();
Descobrir_Coeficientes(a,b,c,repetir);
}
if (a == 0)
{
cout << "\nThis coeficient can not be zero. Type again." << endl;
Descobrir_Coeficientes(a,b,c,repetir);
}
else
{
cin.sync();
cout << "\nType in the coeficient of the term that follows x." << endl;
cin >> b;
if(!cin) // or if(cin.fail())
{
// user didn't input a number
cin.clear(); // reset failbit
cin.ignore(numeric_limits<streamsize>::max(), '\n'); //skip bad input
// next, request user reinput
cout << "\n\nThe coeficient needs to be a number." << endl << endl;
getch();
ClearScreen();
Descobrir_Coeficientes(a,b,c,repetir);
}
}
cin.sync();
cout << "\nType in the coeficient of the independent term." << endl;
cin >> c;
if(!cin) // or if(cin.fail())
{
// user didn't input a number
cin.clear(); // reset failbit
cin.ignore(numeric_limits<streamsize>::max(), '\n'); //skip bad input
// next, request user reinput
cout << "\n\nThe coeficient needs to be a number." << endl << endl;
getch();
ClearScreen();
Descobrir_Coeficientes(a,b,c,repetir);
}
cin.sync();
i++;
Calcular_Raizes(a,b,c);
return 0;
}
float Calcular_Raizes(float a, float b,float c)
{
double x_1 = 0; double x_2 = 0;
Delta = pow(b,2) - 4*a*c;
if (Delta < 0)
{
Mostrar_Raizes_Complexas(a,b,c);
}
else
{
x_1 = (-b + sqrt(Delta))/(2*a);
x_2 = (-b - sqrt(Delta))/(2*a);
x1 = x_1; x2 = x_2;
Mostrar_Raizes(x1,x2);
}
return 0;
}
float Mostrar_Raizes(float x1, float x2)
{
cout << "\nThe roots of the given equation are " << x1 << " e " << x2 << endl;
cout << "\nPress any key to continue." << endl;
getch();
cout << "------------------------------------------------------------------------------" << endl;
cin.sync();
Repetir(repetir);
return 0;
}
float Mostrar_Raizes_Complexas(float a,float b, float c)
{
float Parte_Real = -b/(2*a);
float Delta_Imaginario = abs(pow(b,2) - 4*a*c);
float Imaginario = (sqrt(Delta_Imaginario))/(2*a);
complex<float> x1C(Parte_Real,Imaginario) ;
complex<float> x2C(Parte_Real,-Imaginario);
cout << "\nThe roots of the given equation are" << real(x1C) << "+" << imag(x1C) << "i" << " e " << real(x2C) << imag(x2C) << "i"<< endl;
cout << "\nPress any key to continue." << endl;
getch();
cout << "------------------------------------------------------------------------------" << endl;
cin.sync();
Repetir(repetir);
return 0;
}
bool Repetir (bool repetir)
{
cout << "\n\nDo you wish to use the program again?(1/2)" << endl;
cin >> Sim_ou_Nao;
cin.sync();
if (Sim_ou_Nao == 1)
{
repetir = true;
cin.sync();
ClearScreen();
Instrucoes(repetir);
}
else if ( Sim_ou_Nao == 2)
{
cout << "\nGood-bye!" << endl;
}
else if(!cin) // or if(cin.fail())
{
// user didn't input a number
cin.clear(); // reset failbit
cin.ignore(numeric_limits<streamsize>::max(), '\n'); //skip bad input
// next, request user reinput
cout << "\nInvalid option." << endl << endl;
Repetir(repetir);
}
return 0;
}
int main()
{
SetConsoleTitle("Quadratic Equation Solver");
setlocale(LC_ALL, "Portuguese");
Instrucoes(repetir);
return 0;
}
Thanks for your time.
Petcheco