Advertisement
DevilDaga

Rational

Jul 23rd, 2014
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.60 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. typedef struct
  4. {
  5.     int n;
  6.     int d;
  7. }rational;
  8.  
  9. void doPositiveDenom ( rational *rat )
  10. {
  11.     if ( rat->d < 0 )
  12.     {
  13.         rat->n *= -1;
  14.         rat->d *= -1;
  15.     }
  16. }
  17.  
  18. void read ( rational *x )
  19. {
  20.     while ( 1 )
  21.     {
  22.         scanf ( "%d %d", &x->n, &x->d );
  23.         doPositiveDenom ( x );
  24.         if ( x->d )
  25.             break;
  26.         printf ( "Denominator can not be 0.\nTry Again.\n" );
  27.     }
  28. }
  29.  
  30. int hcf ( int a, int b )
  31. {
  32.     if ( a % b == 0 )
  33.         return b;
  34.     return hcf ( b, a % b );
  35. }
  36.  
  37. void Simplify ( rational *x )
  38. {
  39.     int gcd = hcf ( x->n, x->d );
  40.     x->n /= gcd;
  41.     x->d /= gcd;
  42. }
  43.  
  44. void Add ( rational *x, rational y )
  45. {
  46.     x->n = ( x->n * y.d ) + ( x->d * y.n );
  47.     x->d = x->d * y.d;
  48. }
  49.  
  50. void Sub ( rational *x, rational y )
  51. {
  52.     x->n = ( x->n * y.d ) - ( x->d * y.n );
  53.     x->d = x->d * y.d;
  54. }
  55.  
  56. void Mul ( rational *x, rational y )
  57. {
  58.     x->n *= y.n;
  59.     x->d *= y.d;
  60. }
  61.  
  62. void Div ( rational *x, rational y )
  63. {
  64.     x->n *= y.d;
  65.     x->d *= y.n;
  66. }
  67.  
  68. void Reciprocal ( rational *x )
  69. {
  70.     int t = x->n;
  71.     x->n = x->d;
  72.     x->d = t;
  73. }
  74.  
  75. int Compare ( rational x, rational y )
  76. {
  77.     int num_x = x.n * y.d, num_y = y.n * x.d;
  78.     if ( num_x < num_y )
  79.         return -1;
  80.     else if ( num_x == num_y )
  81.         return 0;
  82.     else
  83.         return 1;
  84. }
  85.  
  86. void main ( )
  87. {
  88.     int choice, compare;
  89.     rational result, next;
  90.     printf ( "Enter the first rational number (Numerator first)\n" );
  91.     read ( &result );
  92.     while ( 1 )
  93.     {
  94.         printf ( "1:\tAddition\n2:\tSubtraction\n3:\tMultiplication\n4:\tDivision\n5:\tCompare\n6:\tReciprocal\n7:\tDisplay\n0:\tEXIT\nCHOICE:\t" );
  95.         scanf ( "%d", &choice );
  96.         if ( !choice )
  97.             break;
  98.         if ( choice < 6 )
  99.         {
  100.             printf ( "\nEnter the next rational number (Numerator first)\n" );
  101.             read ( &next );
  102.         }
  103.         switch ( choice )
  104.         {
  105.             case 1:
  106.                 Add ( &result, next );
  107.                 break;
  108.             case 2:
  109.                 Sub ( &result, next );
  110.                 break;
  111.             case 3:
  112.                 Mul ( &result, next );
  113.                 break;
  114.             case 4:
  115.                 Div ( &result, next );
  116.                 break;
  117.             case 5:
  118.                 compare = Compare ( result, next );
  119.                 break;
  120.             case 6:
  121.                 Reciprocal ( &result );
  122.                 break;
  123.             case 7:
  124.                 break;
  125.             default:
  126.                 printf ( "INVALID CHOICE.\nTRY AGAIN.\n\n" );
  127.                 continue;
  128.         }
  129.         Simplify ( &result );
  130.         printf ( "\n" );
  131.         if ( choice == 5 )
  132.         {
  133.             if ( compare == -1 )
  134.                 printf ( "%d/%d > %d/%d\n\n", next.n, next.d, result.n, result.d );
  135.             else if ( !compare )
  136.                 printf ( "%d/%d = %d/%d\n\n", result.n, result.d, next.n, next.d );
  137.             else
  138.                 printf ( "%d/%d > %d/%d\n\n", result.n, result.d, next.n, next.d );
  139.         }
  140.         else
  141.             printf ( "RESULT:\t\t%d/%d\n\n", result.n, result.d );
  142.     }
  143.     printf ( "RESULT:\t\t%d/%d\n", result.n, result.d );
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement