Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.84 KB | None | 0 0
  1. // Implement the structure here
  2. struct fraction_st {
  3.     unsigned int numerator;
  4.     unsigned int denominator;
  5. };
  6.  
  7. #include "fraction.h"
  8. #include <stdio.h>
  9. #include <assert.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12.  
  13.  
  14. /* Algorithm for determining greatest common divisor, needed in (d) */
  15. /* The function returns gcd between the two parameters, u and v */
  16. /* Taken from http://en.wikipedia.org/wiki/Binary_GCD_algorithm */
  17. unsigned int gcd(unsigned int u, unsigned int v)
  18. {
  19.     // simple cases (termination)
  20.     if (u == v)
  21.         return u;
  22.  
  23.     if (u == 0)
  24.         return v;
  25.  
  26.     if (v == 0)
  27.         return u;
  28.  
  29.     // look for factors of 2
  30.     if (~u & 1) // u is even
  31.     {
  32.         if (v & 1) // v is odd
  33.             return gcd(u >> 1, v);
  34.         else // both u and v are even
  35.             return gcd(u >> 1, v >> 1) << 1;
  36.     }
  37.  
  38.     if (~v & 1) // u is odd, v is even
  39.         return gcd(u, v >> 1);
  40.  
  41.     // reduce larger argument
  42.     if (u > v)
  43.         return gcd((u - v) >> 1, v);
  44.  
  45.     return gcd((v - u) >> 1, u);
  46. }
  47.  
  48. /* Exercise a: Set fraction
  49.  * Parameters: numerator and denominator
  50.  * Returns: pointer to allocated fraction
  51.  */
  52. Fraction* setFraction(unsigned int numerator, unsigned int denominator)
  53. {
  54.     Fraction *p_fraction = malloc(sizeof(Fraction));
  55.     p_fraction->numerator = numerator;
  56.     p_fraction->denominator = denominator;
  57.     return p_fraction;
  58. }
  59.  
  60. unsigned int getNum(const Fraction *f)
  61. {
  62.     return f->numerator;
  63. }
  64.  
  65. unsigned int getDenom(const Fraction *f)
  66. {
  67.     return f->denominator;
  68. }
  69.  
  70. void freeFraction(Fraction *f)
  71. {
  72.     free(f);
  73. }
  74.  
  75.  
  76.  
  77. /* Exercise b: Compare values
  78.  * Parameters: two fractions to be compared
  79.  * Returns:
  80.  * -1 if a is smaller than b
  81.  * 0 if the fractions are equal
  82.  * 1 if a is larger than b
  83.  */
  84. int compFraction(const Fraction *a, const Fraction *b)
  85. {
  86.     double aa = a->numerator * 1.0 / a->denominator;
  87.     double bb = b->numerator * 1.0 / b->denominator;
  88.     if (aa < bb) {
  89.         return -1;
  90.     }
  91.     else if (aa > bb) {
  92.         return 1;
  93.     }
  94.     return 0;
  95. }
  96.  
  97. /* Exercise c: Add values
  98.  * Parameters: two fractions to be added
  99.  * Returns: sum of the fractions
  100.  */
  101. Fraction* addFraction(const Fraction *a, const Fraction *b)
  102. {
  103.     unsigned int numerator = (a->numerator * b->denominator) + (b->numerator * a->denominator);
  104.     unsigned int denomerator = a->denominator * b->denominator;
  105.     return setFraction(numerator, denomerator);
  106. }
  107.  
  108. /* Reduce fraction
  109.  * Parameters: Fraction to be reduced. Reduction happens on the object itself */
  110. void reduceFraction(Fraction* val)
  111. {
  112.     unsigned int a = gcd(val->numerator, val->denominator);
  113.     val->numerator = val->numerator / a;
  114.     val->denominator = val->denominator / a;
  115. }
  116.  
  117. /* Not needed, but it will be useful to implement this */
  118. /*
  119. void printFraction(const Fraction *val)
  120. {
  121.    
  122. }
  123. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement