Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //stupid code. Don't bother reading.
- #include<stdio.h>
- typedef struct
- {
- int n,d;
- }rational;
- void simple(int n,int d)
- {printf("\nThe simplified form of %d/%d is %d/%d\n",n,d,n/simp(n,d),d/simp(n,d));
- }
- int simp(int n,int m)
- { if(m<=n && n%m == 0)
- return m;
- if(n < m)
- return simp(m,n);
- else
- return simp(m,n%m);
- }
- void add(int m,int n,int c,int d)
- {
- int a,b,g;
- a=m*d+c*n;
- b=d*n;
- g=simp(a,b);
- a/=g;
- b/=g;
- printf("\nAddition of %d/%d and %d/%d is %d/%d\n",m,n,c,d,a,b);
- }
- void sub(int m,int n,int c,int d)
- {
- int a,b,g;
- a=m*d-c*n;
- b=d*n;
- g=simp(a,b);
- a/=g;
- b/=g;
- printf("\nSubtraction of %d/%d and %d/%d is %d/%d\n",m,n,c,d,a,b);
- }
- void mul(int m,int n,int c,int d)
- {
- int a,b,g;
- a=m*c;
- b=n*d;
- g=simp(a,b);
- a/=g;
- b/=g;
- printf("\nProduct of %d/%d and %d/%d is %d/%d\n",m,n,c,d,a,b);
- }
- void div(int m,int n,int c,int d)
- {
- int a,b,g;
- a=m*d;
- b=n*c;
- g=simp(a,b);
- a/=g;
- b/=g;
- printf("\nProduct of %d/%d and %d/%d is %d/%d\n",m,n,c,d,a,b);
- }
- void comp(int m,int n,int c,int d)
- {
- float a,b;
- a=(float)m/n;
- b=(float)c/d;
- if(a>b)
- printf("\nGreater among %d/%d and %d/%d is %d/%d\n",m,n,c,d,m,n);
- else
- printf("\nGreater among %d/%d and %d/%d is %d/%d\n",m,n,c,d,c,d);
- }
- void invertendo(int m,int n)
- { printf("\nAfter Invertendo the rational number becomes %d/%d\n",n,m);
- }
- void main()
- {
- int c;
- rational x,y;
- printf("\nChoose the operation you would like to perform\n");
- printf("\n1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n5.Comparison\n6.Invertendo\n7.Simplify the rational number\n0.Exit\n");
- scanf("%d",&c);
- while(c>7)
- { printf("\nInvalid Choice. Try again\n");
- scanf("%d",&c);
- }
- switch(c)
- {
- case 1:
- {
- printf("\nEnter the numerator and denominator of the first rational number\n");
- scanf("%d%d",&x.n,&x.d);
- printf("\nEnter the numerator and denominator of the second rational number\n");
- scanf("%d%d",&y.n,&y.d);
- add(x.n,x.d,y.n,y.d);
- break;
- }
- case 2:
- { printf("\nEnter the numerator and denominator of the first rational number\n");
- scanf("%d%d",&x.n,&x.d);
- printf("\nEnter the numerator and denominator of the second rational number\n");
- scanf("%d%d",&y.n,&y.d);
- sub(x.n,x.d,y.n,y.d);
- break;
- }
- case 3:
- { printf("\nEnter the numerator and denominator of the first rational number\n");
- scanf("%d%d",&x.n,&x.d);
- printf("\nEnter the numerator and denominator of the second rational number\n");
- scanf("%d%d",&y.n,&y.d);
- mul(x.n,x.d,y.n,y.d);
- break;
- }
- case 4:
- { printf("\nEnter the numerator and denominator of the first rational number\n");
- scanf("%d%d",&x.n,&x.d);
- printf("\nEnter the numerator and denominator of the second rational number\n");
- scanf("%d%d",&y.n,&y.d);
- div(x.n,x.d,y.n,y.d);
- break;
- }
- case 5:
- { printf("\nEnter the numerator and denominator of the first rational number\n");
- scanf("%d%d",&x.n,&x.d);
- printf("\nEnter the numerator and denominator of the second rational number\n");
- scanf("%d%d",&y.n,&y.d);
- comp(x.n,x.d,y.n,y.d);
- break;
- case 6:
- { printf("\nEnter the numerator and denominator of the rational number\n");
- scanf("%d%d",&x.n,&x.d);
- invertendo(x.n,x.d);
- break;
- }
- case 7:
- { printf("\nEnter the numerator and denominator of the rational number\n");
- scanf("%d%d",&x.n,&x.d);
- simple(x.n,x.d);
- break;
- }
- }
- }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement