47 lines
1.0 KiB
C++
47 lines
1.0 KiB
C++
|
#include "Trinome.h"
|
||
|
|
||
|
#include <cmath>
|
||
|
|
||
|
using namespace std;
|
||
|
|
||
|
/* [0] CONSTRUCTEUR
|
||
|
=========================================================*/
|
||
|
Trinome::Trinome(){
|
||
|
this->init(3);
|
||
|
}
|
||
|
|
||
|
/* [1] Renvoie le nom du type de calcul
|
||
|
=========================================================*/
|
||
|
string Trinome::getLibelle(){
|
||
|
return "ax^2 + bx + c = 0";
|
||
|
}
|
||
|
|
||
|
/* [2] Effectue le calcul et renvoie le resultat
|
||
|
=========================================================*/
|
||
|
float* Trinome::calc(){
|
||
|
const int a( this->operands[0] ),
|
||
|
b( this->operands[1] ),
|
||
|
c( this->operands[2] );
|
||
|
|
||
|
float delta = pow(b,2) - 4*a*c;
|
||
|
|
||
|
// DELTA > 0 -> 2 racines
|
||
|
if( delta > 0 ){
|
||
|
this->result = (float*) malloc(2*sizeof(float));
|
||
|
|
||
|
this->result[0] = (-b-sqrt(delta)) / (2*a);
|
||
|
this->result[1] = (-b+sqrt(delta)) / (2*a);
|
||
|
|
||
|
// DELTA = 0 -> 1 racine
|
||
|
}else if( delta == 0 ){
|
||
|
this->result = (float*) malloc(2*sizeof(float));
|
||
|
|
||
|
this->result[0] = (-b) / (2*a);
|
||
|
|
||
|
// DELTA < 0 -> 0 racine
|
||
|
}else{
|
||
|
this->result = (float*) malloc(0*sizeof(float));
|
||
|
}
|
||
|
|
||
|
return this->result;
|
||
|
}
|