csansoon

P9.03 P85696 Rational numbers (1)

Dec 5th, 2018
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.48 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Rational {
  5.          int num, den;
  6.      };
  7.      
  8. int gcd (int n, int d) {
  9.     int r = 0;
  10.     if(d == 0)
  11.         return n;
  12.     else {
  13.         r = gcd(d, n%d);
  14.     }
  15.     return r;
  16. }
  17.  
  18.  
  19. Rational rational(int n, int d) {
  20.     Rational res;
  21.   res.num=n/gcd(n,d);
  22.   res.den=d/gcd(n,d);
  23.   if (res.den<0) {
  24.     res.den=res.den*-1;
  25.     res.num=res.num*-1;
  26.   }
  27.   return res;
  28. }
  29.  
  30. // (c) Carlos Sansón (Best pro1 delegate ever for sure) @csansoon
Advertisement
Add Comment
Please, Sign In to add comment