Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4.  
  5. typedef struct
  6. {
  7. long int numerator;
  8. long int denominator;
  9. int corrapted;
  10. int infinity;
  11. } rational_t;
  12.  
  13. static long int gcd(long int a, long int b)
  14. {
  15. if (b == 0)
  16. return labs(a);
  17. return gcd(b, a % b);
  18. }
  19.  
  20. void add(long int a_num, long int a_den, long int b_num, long int b_den)
  21. {
  22. rational_t flag_inf;
  23. long int nod = gcd(labs(a_den), labs(b_den));
  24. if (nod == 1)
  25. printf("%ld\n", (a_num * b_den + b_num * a_den)/(a_den * b_den));
  26. else
  27. if (nod == 0)
  28. {
  29. flag_inf.infinity = 1;
  30. }
  31. else
  32. printf("%ld\n", (a_num * (b_den/nod) + b_num * (a_den/nod))/((b_den/nod)*a_den));
  33. }
  34.  
  35. int main()
  36. {
  37. rational_t first, second;
  38. first.numerator = 4;
  39. first.denominator = 7;
  40. second.numerator = 2;
  41. second.denominator = 21;
  42. add(first.numerator, first.denominator, second.numerator, second.denominator);
  43. return 0;
  44.  
  45.  
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement