Ir para conteúdo
Fórum Script Brasil

Resolvedor de equações quadráticas


PedroCunha

Posts Recomendados

Olá, amigos.

Poderiam me dizer o que acham do meu programa? Críticas, sugestões, etc. .

#include <iostream>
#include <locale>
#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 << "\não digitar os coeficientes, utilize apenas números e o '.' para representar\nnúmeros decimais." << endl;
    cout << "\nNão utilize frações nem expoentes. " << endl << endl;
    cout << "Pressione qualquer tecla para continuar" << 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 << "\nDigite o valor do coeficiente que acompanha 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 << "O coeficiente precisa ser um número." << endl << endl;
    getch();
    ClearScreen();
    Descobrir_Coeficientes(a,b,c,repetir);
    }
    if (a == 0)
    {
        cout << "\nEsse coeficiente não pode ser nulo. Digite novamente." << endl;
        Descobrir_Coeficientes(a,b,c,repetir);
    }
    else
    {
    cin.sync();
    cout << "\nDigite o valor do coeficiente que acompanha 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\nO coeficiente precisa ser um número." << endl << endl;
    getch();
    ClearScreen();
    Descobrir_Coeficientes(a,b,c,repetir);
    }
    }
    cin.sync();
    cout << "\nDigite o valor do coeficiente do termo independente" << 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\nO coeficiente precisa ser um número." << 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 << "\nAs raízes da equação dada são " << x1 << " e " << x2 << endl;
    cout << "\nPressione qualquer tecla para continuar." << 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 << "\nAs raízes da equação dada são " << real(x1C) << "+" <<  imag(x1C) << "i" << " e " << real(x2C) <<  imag(x2C) << "i"<< endl;
    cout << "\nPressione qualquer tecla para continuar." << endl;
    getch();
    cout << "------------------------------------------------------------------------------" << endl;
    cin.sync();
    Repetir(repetir);
    return 0;
}

bool Repetir (bool repetir)
{
    cout << "\n\nDeseja usar o programa novamente?(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 << "\nAdeus!" << 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 << "\nOpção inválida." << endl << endl;
        Repetir(repetir);
    }
    return 0;
}

int main()
{
    SetConsoleTitle("Resolvedor de Equacoes do Segundo Grau");
    setlocale(LC_ALL, "Portuguese");
    Instrucoes(repetir);
    return 0;
}

Grato pela atenção.

Att.,

Pedro

Link para o comentário
Compartilhar em outros sites

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152k
    • Posts
      651,8k
×
×
  • Criar Novo...