Jump to content

Recommended Posts

Posted (edited)

Olá a todos! Sou novo no fórum e um "aspirante a programador", este é o meu primeiro post e pensei que podia partilhar o meu programa que resolve equações do 2º grau. É um programa como muitos outros mas com um toque pessoal. Espero que gostem e que vos seja útil.

#include<cmath>  //função raiz quadrada(sprt)
#include<iostream>	   //funções cout(output de texto) e cin(receber dados)
#include<iomanip>	    //instrucão endl(mudar de linha)
#include<conio.h>	    //função getch(parar o programa)
#include<stdlib.h>	   //função system(utilizar funcionalidades do sistema operativo)
#include<windows.h> //função Sleep(suspender o programa)
using namespace std;
int main(){
   system("title Solver.cpp"); //Pôr o nome do prog na janela
   system("color 02");			  //Mudar a cor das letras para verde
   float a, b, c, res1, res2; //Declaração de variáveis
   cout << "ax^2+bx+c" << endl << "Introduza os coeficientes: " << endl << "\ta: "; cin >> a;
   cout << "\tb: "; cin >> b;
   cout << "\tc: "; cin >> c;
   res1 = (-b + sqrt((b*b)-(4*a*c)))/(2*a);
   res2 = (-b - sqrt((b*b)-(4*a*c)))/(2*a);
   if (a == 0 || (b*b)-(4*a*c) < 0){ //Mensagens de erro
       cout << endl << endl;
       system("color 04");
       Sleep(1000);
       if (a == 0){
           cout << "<ERROR>: a = 0!" << endl;
          		  Sleep(1500);
       }
       else{
           cout << "<ERROR>: Negative Root!" << endl;
           Sleep(1500);
       }
       cout << "<ERROR>: No Real Roots!" << endl;			   //Não é necessário ao programa mas
       Sleep(1500);																	 //acho que fica bem XD
       cout << "<ERROR>: Unexpected Situation!" << endl;
       Sleep(1500);
       cout << "<WARNING>: The program crashed" << endl;
       Sleep(1500);
       cout << "<WARNING>: System is shutting down.";
       Sleep(1000); cout << ".";
       Sleep(1000); cout << ".";
       Sleep(1000); cout << ".";
       Sleep(1000); cout << ".";
       Sleep(1000); cout << ".";
       Sleep(1000);
       return 0;
   }else if(res2 == -0){								    //Quando as soluções são iguais aparece uma cena do
       cout << "\t[x1 = " << res1 << "]x2" << endl; //género x1=y x2=-y, não convem
   }
   else
       cout << "\t[x1 = " << res1 << "]\t[x2 = " << res2 << "]" << endl;
   getch();
   return 0;
}

Dado às configurações do browser so posso publicar assim.

Edited by Baderous
geshi
  • 3 weeks later...
Posted

Como eu gosto de ensinar 😉

1 - Primeiro tens de verificar as condições para o cálculo e só depois efectuar o respectivo cálculo ... ou seja

res1 = (-b + sqrt((b*b)-(4*a*c)))/(2*a);

res2 = (-b - sqrt((b*b)-(4*a*c)))/(2*a);

Tem de estar protegido por um if para ver se o a é zero e (b*b)-(4*a*c) > 0 senão o programa vai estourar 😁

2 - Se a = 0 , cais na situação bx+c=0 que também terás de resolver 😛

2. 1 - Se B=0, cais na situação c=0 ... depende do c se é ou não impossível a equação 😛

Como adoro matemática e costumo dar este exemplo quando explico nas primeiras aulas de programação ... não resisti 😁

  • 1 month later...
Posted (edited)

Representa a exponenciação.

ax2 + bx + c

Edited by KTachyon

“There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.”

-- Tony Hoare

Posted (edited)

Tenho uma dúvida. Não sei para que serve o símbolo "^" entre o ax e o 2.

cout << "ax^2+bx+c"

Cumps

a*x*x + bx +c

Edited by Guest
Posted

(ax*ax) + bx +c

Errado:

a*x*x + b*x + c

“There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.”

-- Tony Hoare

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.