CB100

Notas, resueltos y tps de la materia Algoritmos y Estructuras de Datos
Index Commits Files Refs README
guias/1/ej26.cpp (746B)
   1 #include <iostream>
   2 #include <iomanip>
   3 #include <cmath>
   4 
   5 typedef struct {
   6     float x1, x2;
   7 } Vec2f;
   8 
   9 bool cuadratic_has_real_roots(float a, float b, float c) {
  10     return ((b*b)-(4*a*c)) >= 0;
  11 }
  12 
  13 Vec2f cuadratic_roots(float a, float b, float c) {
  14     if(cuadratic_has_real_roots(a, b, c) == false)
  15         return (Vec2f){0,0};
  16 
  17     Vec2f res;
  18     res.x1 = ((-b)+std::sqrt((b*b)-(4*a*c)))/(2*a);
  19     res.x2 = ((-b)-std::sqrt((b*b)-(4*a*c)))/(2*a);
  20     return res;
  21 }
  22 
  23 int main (void) {
  24     float a, b, c;
  25     Vec2f p;
  26 
  27     std::cin >> a;
  28     std::cin >> b;
  29     std::cin >> c;
  30 
  31     p = cuadratic_roots(a,b,c);
  32     std::cout << "x1=" << std::left << std::setw(4) << p.x1 
  33               << "x2=" << std::setw(4) << p.x2 << std::endl;
  34     return 0;
  35 }