Advertisement
kkricardokaka95

Rational/Exp1

Jul 24th, 2014
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.33 KB | None | 0 0
  1. //stupid code. Don't bother reading.
  2.  
  3. #include<stdio.h>
  4.  
  5. typedef struct
  6. {
  7.     int n,d;
  8. }rational;
  9.  
  10. void simple(int n,int d)
  11. {printf("\nThe simplified form of %d/%d is %d/%d\n",n,d,n/simp(n,d),d/simp(n,d));
  12. }
  13.  
  14. int simp(int n,int m)
  15.     { if(m<=n && n%m == 0)
  16.        return m;
  17.       if(n < m)
  18.        return simp(m,n);
  19.       else
  20.        return simp(m,n%m);
  21.      }
  22. void add(int m,int n,int c,int d)
  23. {  
  24.     int a,b,g;
  25.    
  26.     a=m*d+c*n;
  27.     b=d*n;
  28.     g=simp(a,b);
  29.     a/=g;
  30.     b/=g;
  31.     printf("\nAddition of %d/%d and %d/%d is %d/%d\n",m,n,c,d,a,b);
  32. }
  33.  
  34. void sub(int m,int n,int c,int d)
  35. {  
  36.     int a,b,g;
  37.    
  38.     a=m*d-c*n;
  39.     b=d*n;
  40.     g=simp(a,b);
  41.     a/=g;
  42.     b/=g;
  43.        
  44.     printf("\nSubtraction of %d/%d and %d/%d is %d/%d\n",m,n,c,d,a,b);
  45. }
  46.  
  47. void mul(int m,int n,int c,int d)
  48. {  
  49.     int a,b,g;
  50.    
  51.     a=m*c;
  52.     b=n*d;
  53.     g=simp(a,b);
  54.     a/=g;
  55.     b/=g;
  56.        
  57.     printf("\nProduct of %d/%d and %d/%d is %d/%d\n",m,n,c,d,a,b);
  58. }
  59.  
  60. void div(int m,int n,int c,int d)
  61. {  
  62.     int a,b,g;
  63.     a=m*d;
  64.     b=n*c;
  65.    
  66.     g=simp(a,b);
  67.     a/=g;
  68.     b/=g;
  69.        
  70.     printf("\nProduct of %d/%d and %d/%d is %d/%d\n",m,n,c,d,a,b);
  71. }
  72.  
  73. void comp(int m,int n,int c,int d)
  74. {  
  75.     float a,b;
  76.    
  77.     a=(float)m/n;
  78.     b=(float)c/d;
  79.     if(a>b)
  80.     printf("\nGreater among %d/%d and %d/%d is %d/%d\n",m,n,c,d,m,n);
  81.     else
  82.     printf("\nGreater among %d/%d and %d/%d is %d/%d\n",m,n,c,d,c,d);
  83. }
  84.  
  85. void invertendo(int m,int n)
  86. {   printf("\nAfter Invertendo the rational number becomes %d/%d\n",n,m);
  87. }
  88.    
  89. void main()
  90. {
  91.     int c; 
  92.     rational x,y;
  93.     printf("\nChoose the operation you would like to perform\n");  
  94.     printf("\n1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n5.Comparison\n6.Invertendo\n7.Simplify the rational number\n0.Exit\n");
  95.     scanf("%d",&c);
  96. while(c>7)
  97. {   printf("\nInvalid Choice. Try again\n");
  98.     scanf("%d",&c);
  99. }  
  100. switch(c)
  101. {
  102.     case 1:
  103. {  
  104.     printf("\nEnter the numerator and denominator of the first rational number\n");
  105.     scanf("%d%d",&x.n,&x.d);
  106.     printf("\nEnter the numerator and denominator of the second rational number\n");
  107.     scanf("%d%d",&y.n,&y.d);
  108.     add(x.n,x.d,y.n,y.d);
  109. break;
  110. }
  111.     case 2:
  112. {   printf("\nEnter the numerator and denominator of the first rational number\n");
  113.     scanf("%d%d",&x.n,&x.d);
  114.     printf("\nEnter the numerator and denominator of the second rational number\n");
  115.     scanf("%d%d",&y.n,&y.d);
  116.     sub(x.n,x.d,y.n,y.d);
  117. break;
  118. }
  119. case 3:
  120. {   printf("\nEnter the numerator and denominator of the first rational number\n");
  121.     scanf("%d%d",&x.n,&x.d);
  122.     printf("\nEnter the numerator and denominator of the second rational number\n");
  123.     scanf("%d%d",&y.n,&y.d);
  124.     mul(x.n,x.d,y.n,y.d);
  125. break;
  126. }
  127. case 4:
  128. {   printf("\nEnter the numerator and denominator of the first rational number\n");
  129.     scanf("%d%d",&x.n,&x.d);
  130.     printf("\nEnter the numerator and denominator of the second rational number\n");
  131.     scanf("%d%d",&y.n,&y.d);
  132.     div(x.n,x.d,y.n,y.d);
  133. break;
  134. }
  135. case 5:
  136. {   printf("\nEnter the numerator and denominator of the first rational number\n");
  137.     scanf("%d%d",&x.n,&x.d);
  138.     printf("\nEnter the numerator and denominator of the second rational number\n");
  139.     scanf("%d%d",&y.n,&y.d);
  140.     comp(x.n,x.d,y.n,y.d);
  141. break;
  142. case 6:
  143. {   printf("\nEnter the numerator and denominator of the rational number\n");
  144.     scanf("%d%d",&x.n,&x.d);
  145.     invertendo(x.n,x.d);
  146. break;
  147. }  
  148. case 7:
  149. {   printf("\nEnter the numerator and denominator of the rational number\n");
  150.     scanf("%d%d",&x.n,&x.d);
  151.     simple(x.n,x.d);
  152. break;
  153. }
  154. }
  155. }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement