Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #include <stdlib.h>
- typedef struct
- {
- long int numerator;
- long int denominator;
- int corrapted;
- int infinity;
- } rational_t;
- static long int gcd(long int a, long int b)
- {
- if (b == 0)
- return labs(a);
- return gcd(b, a % b);
- }
- void add(long int a_num, long int a_den, long int b_num, long int b_den)
- {
- rational_t flag_inf;
- long int nod = gcd(labs(a_den), labs(b_den));
- if (nod == 1)
- printf("%ld\n", (a_num * b_den + b_num * a_den)/(a_den * b_den));
- else
- if (nod == 0)
- {
- flag_inf.infinity = 1;
- }
- else
- printf("%ld\n", (a_num * (b_den/nod) + b_num * (a_den/nod))/((b_den/nod)*a_den));
- }
- int main()
- {
- rational_t first, second;
- first.numerator = 4;
- first.denominator = 7;
- second.numerator = 2;
- second.denominator = 21;
- add(first.numerator, first.denominator, second.numerator, second.denominator);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement