Advertisement
AntonioVillanueva

Cuadraticas con raices complejas

Jun 4th, 2016
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. #include<iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. class cuadratica{
  7.     public:
  8.     cuadratica( double a,double b,double c);
  9.     void raiz ()const;
  10.  
  11.     private:
  12.     double discriminante()const ;
  13.     float a,b,c;
  14. };
  15. cuadratica::cuadratica (double a,double b,double c): a(a),b(b),c(c) {};
  16.  
  17. void cuadratica::raiz()const{
  18.    
  19.     if (discriminante()<0){//Raices complejas
  20.         cout <<-b/(2*a) <<"+"<<sqrt(abs(discriminante()))/(2*a)<<"i ,"
  21.              <<-b/(2*a) <<"-"<<sqrt(abs(discriminante()))/(2*a)<<"i "<<endl;
  22.     }else {//Raices reales
  23.         cout <<-(b+sqrt(discriminante()))/(2*a)<<","
  24.              <<-(b-sqrt(discriminante()))/(2*a)<<endl;
  25.     }  
  26. }
  27.  
  28. double cuadratica::discriminante ()const{return ( b*b-4*a*c);}
  29.  
  30. int main() {
  31.  
  32.     cuadratica eqReal(1,2,-8);//x^2+2x-8 raices 2 y -4 discriminante 36
  33.     cuadratica eqImaginaria(1,-2,2);//imaginaria 1+1i , 1-1i
  34.     cuadratica eqImaginaria2(1.5,-2.2,2.3);//imaginaria 0.733333+0.997775i ,0.733333-0.997775i
  35.  
  36.     eqReal.raiz();
  37.     eqImaginaria.raiz();
  38.     eqImaginaria2.raiz();
  39.    
  40.     return 0;
  41.  
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement