Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Antonio Villanueva Segura
- * Calculo Ecuacion 2° grado estilo POO c++
- *Para test puedes emplear ....
- *http://es.onlinemschool.com/math/assistance/equation/quadratic/
- */
- #include <iostream>
- #include <cmath>
- using namespace std;
- class Ecuacion{
- public:
- Ecuacion(double a=3,double b=60,double c=1);//Constructor con parametros por defecto
- void run();//Calculo ecuacion 2° grado
- private:
- double a,b,c;
- double det(double a,double b, double c);//Calculo determinante
- };
- /**********************************************************************/
- Ecuacion::Ecuacion(double a,double b,double c) :a(a),b(b),c(c){}
- /**********************************************************************/
- double Ecuacion::det(double a,double b, double c){return (b*b)-(4*a*c);}
- /**********************************************************************/
- void Ecuacion::run (){//Calculo ecuacion 2° grado
- double d=det(a,b,c);//Calcula determinante
- if (d<0){//Solucion Imaginaria i
- cout<<-b/(2*a)<<" + "<<sqrtf(abs(d))/(2*a)<<" i" <<endl;
- cout<<-b/(2*a)<<" - "<<sqrtf(abs(d))/(2*a)<<" i "<<endl;
- }
- else{//Solucion Real
- cout<< ((-b)+ sqrtf(d) ) / (2*a) <<endl;
- cout<< ((-b)-sqrtf(d)) /(2*a) <<endl;
- }
- }
- /**********************************************************************/
- /**********************************************************************/
- int main (){
- Ecuacion ecuacion;
- ecuacion.run();//Emplea parametro por defeto a=3 b=60 c=1
- //Calculo raices imaginarias a=1 , b=-2, c=5
- Ecuacion ecuacion2(1,-2,5);
- ecuacion2.run();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement