Advertisement
Guest User

Untitled

a guest
Jul 29th, 2015
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cmath>
  4. #include<cstdlib>
  5. using namespace std;
  6. int mdc(int a,int b)
  7. {
  8. if(b==0)
  9. return a;
  10. else
  11. return mdc(b,a%b);
  12. }
  13. int main()
  14. {
  15. int a,b,c,d,t,i,hor,lob,div;
  16. char ope;
  17. scanf("%d\n",&t);
  18. for(i=1; i<=t; i++)
  19. {
  20. scanf("%d / %d %c %d / %d",&a,&b,&ope,&c,&d);
  21. if(ope=='+')
  22. {
  23. hor=b*d;
  24. lob=a*d+b*c;
  25. div=mdc(hor,lob);
  26. if(div<0)
  27. div*=-1;
  28. }
  29. else if(ope=='-')
  30. {
  31. hor=b*d;
  32. lob=a*d-b*c;
  33. div=mdc(hor,lob);
  34. if(div<0)
  35. div*=-1;
  36. }
  37. else if(ope=='*')
  38. {
  39. hor=b*d;
  40. lob=a*c;
  41. div=mdc(hor,lob);
  42. if(div<0)
  43. div*=-1;
  44. }
  45. else if(ope=='/')
  46. {
  47. hor=b*c;
  48. lob=a*d;
  49. div=mdc(hor,lob);
  50. }
  51.  
  52. printf("%d/%d = %d/%d\n",lob,hor,lob/div,hor/div);
  53. }
  54. return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement