Telaryon

Struktúra alapok 4

Apr 8th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.91 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. typedef struct valosgyokok {
  5.     float x1, x2;
  6. } Valosgyokok;
  7.  
  8. Valosgyokok masodfoku_valosgyokei(int a, int b, int c, int d);
  9.  
  10. int main(void){
  11.     int a;
  12.     int b;
  13.     int c;
  14.  
  15.     Valosgyokok megoldas;
  16.     printf("Adja meg a masodfoku egyenlet konstansait: ");// a, b, c, NEM NULLA
  17.     scanf("%d, %d, %d", &a, &b, &c);
  18.     int d=b*b-4*a*c;
  19.     if (d>=0) {
  20.         megoldas=masodfoku_valosgyokei(a,b,c,d);
  21.         printf("Az egyenlet megoldasa: x1=%.2f, x2=%.2f\n", megoldas.x1, megoldas.x2);
  22.     }
  23.     else
  24.         printf("Az egyenletnek komplex megoldasa van.\n");
  25.     return 0;
  26. }
  27.  
  28. Valosgyokok masodfoku_valosgyokei(int a, int b, int c, int d){
  29.     Valosgyokok eredmeny;
  30.     if (d==0)
  31.         eredmeny.x1=eredmeny.x2 = -b/(float)(2*a);
  32.     else {
  33.         eredmeny.x1 = -b+sqrt(d)/(2*a);
  34.         eredmeny.x2 = -b-sqrt(d)/(2*a);
  35.     }
  36.     return eredmeny;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment