Advertisement
Miquel_Fuster

Ecuaciones de segundo grado

Oct 19th, 2023
1,051
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #include <stdnoreturn.h>
  6.  
  7. unsigned calculoRaices(double *a, double *b, double c) {
  8.     double x1, x2, discr;
  9.  
  10.     if(*a == 0) {
  11.                                 // caso !a, !b, c
  12.         if(*b == 0) {
  13.             return 0;
  14.         }
  15.                                 // caso !a, b, !c
  16.         if(c == 0) {
  17.             *a = 0;
  18.             return 1;
  19.         }
  20.                                 // caso !a, b, c
  21.         *a = -c / *b;
  22.         return 1;
  23.     }
  24.  
  25.     if(*b == 0) {
  26.                                 // caso a, !b, !c
  27.         if(c == 0) {
  28.             *a = 0;
  29.             return 1;
  30.         }
  31.                                 // caso a, !b, c
  32.         if(-c / *a < 0) {
  33.                                     // subcaso -c/a < 0
  34.             return 0;
  35.         }
  36.  
  37.         x1 = sqrt(-c / *a);
  38.         *a = x1;
  39.         *b = -x1;
  40.         return 2;
  41.     }
  42.  
  43.                                 // caso a, b, !c
  44.     if(c == 0) {
  45.         x1 = -*b / *a;
  46.         *a = 0;
  47.         *b = x1;
  48.         return 2;
  49.     }
  50.  
  51.                                 // caso a, b, c
  52.     discr = *b * *b - 4 * *a * c;
  53.                                     // subcaso discriminante < 0
  54.     if(discr < 0) {
  55.         return 0;
  56.     }
  57.  
  58.                                     // subcaso discriminante = 0
  59.     if(discr == 0) {
  60.         *a = -*b / (2 * *a);
  61.         return 1;
  62.     }
  63.  
  64.                                     // subcaso discriminante > 0
  65.     x1 = (-*b + sqrt(discr)) / (2 * *a);
  66.     x2 = (-*b - sqrt(discr)) / (2 * *a);
  67.     *a = x1;
  68.     *b = x2;
  69.     return 2;
  70. }
  71.  
  72. noreturn void imprimirErrorEntrada(char *nombrePrograma) {
  73.     printf("Uso: %s A B C\n", nombrePrograma);
  74.     puts("   donde A, B y C deben ser numeros reales [notacion de punto decimal].");
  75.     exit(EXIT_FAILURE);
  76. }
  77.  
  78. int main(int argc, char *argv[]) {
  79.     double a, b, c;
  80.     unsigned resultados;
  81.  
  82.     if(argc != 4) {
  83.         imprimirErrorEntrada(argv[0]);
  84.     }
  85.  
  86.     if(
  87.         !sscanf(argv[1], "%lf", &a) ||
  88.         !sscanf(argv[2], "%lf", &b) ||
  89.         !sscanf(argv[3], "%lf", &c)) {
  90.         imprimirErrorEntrada(argv[0]);
  91.     }
  92.  
  93.     resultados = calculoRaices(&a, &b, c);
  94.  
  95.     switch(resultados) {
  96.         case 0:
  97.             puts("No hay raices computables en R");
  98.             break;
  99.         case 1:
  100.             printf("Hay una raiz: %lf\n", a);
  101.             break;
  102.         case 2:
  103.             printf("Hay dos raices: %lf y %lf\n", a, b);
  104.             break;
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement