Advertisement
tegusta

Equazioni fino al 2° grado

Dec 16th, 2011
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. void tasto(void) {
  6.      fflush(stdin);
  7.      cout << "\n\nPremere Invio per continuare.";
  8.      getchar();
  9. }//tasto
  10.  
  11. /* INPUT */
  12. void leggi(float &a, float &b, float &c, bool &k){
  13.      
  14.      cout<<"Inserisci i valori dell'equazione nell'ordine: ax^2 bx c ---> ";
  15.      cin>>a>>b>>c;
  16.      if(a!=0){
  17.          cout<<"\nL'equazione ha quindi valore: "<<a<<"x^2 + "<<b<<"x + "<<c<<" = 0";
  18.          k=true;
  19.      }//if
  20.      else
  21.          cout<<"\nL'equazione ha quindi valore: "<<b<<"x + "<<c<<" = 0";
  22. }//leggi
  23.  
  24. /* ELABORAZIONE */
  25. float delta(float &a, float &b, float &c){
  26.     float d;
  27.     d=pow(b,2)-(4*a*c);
  28.     return d;
  29. }//delta
  30.  
  31. void eqPrimo(float b, float c, float &x, bool &p){
  32.     p=true;
  33.     x= -c/b;
  34. }//eqPrimo
  35.  
  36. void eqSecondo(float a, float b, float c, float &x1, float &x2, bool &s){
  37.     s=true;
  38.     float del;
  39.     x1=(-b-sqrt(del))/2*a;
  40.     x2=(-b+sqrt(del))/2*a;
  41. }//eqSecondo
  42.  
  43. /* OUTPUT */
  44. void scrivi(float x, float x1, float x2, bool &p, bool &s){
  45.     if(p)
  46.         cout<<"\nIl risultato e`: "<<x;
  47.     if(s)
  48.         cout<<"\nI risultati sono x1 = "<<x1<<" , x2 = "<<x2;
  49. }//scrivi
  50.  
  51. int main(){
  52.     float a,b,c,d,del,x,x1,x2;
  53.     bool k,p,s;
  54.     leggi(a,b,c,k);
  55.     if(a!=0){
  56.         del=delta(a,b,c);
  57.         if(del<0){
  58.             cout<<"\nImpossibile!";
  59.         }//if
  60.         else
  61.             eqSecondo(a,b,c,x1,x2,s);
  62.             scrivi(x,x1,x2,p,s);
  63.     }//if
  64.     else
  65.         if(b!=0){
  66.             eqPrimo(b,c,x,p);
  67.             scrivi(x,x1,x2,p,s);
  68.         }//if
  69.         else
  70.             if(c==0)
  71.                 cout<<"\nEquazione indeterminata!";
  72.             else
  73.                 cout<<"\nEquazione mpossibile!";
  74.     tasto();
  75.     return 0;
  76. }//main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement