fork download
  1.  
  2.  
  3.  
  4. #include<bits/stdc++.h>
  5. using namespace std;
  6.  
  7. int gcd(int a,int b)
  8. {
  9. a=abs(a);
  10. b=abs(b);
  11. if(a<b) swap(a,b);
  12. if(b==0) return a;
  13. return gcd(b,a-b);
  14. }
  15.  
  16. struct PhanSo{
  17. int x,y;
  18. };
  19.  
  20. void nhap(PhanSo &a,PhanSo &b){
  21. cin>>a.x>>a.y;
  22. cin>>b.x>>b.y;
  23. }
  24. void tong(PhanSo a,PhanSo b)
  25. {
  26. PhanSo p;
  27. p.x=a.x*b.y+a.y*b.x;
  28. p.y=a.y*b.y;
  29. int c=gcd(p.x,p.y);
  30. p.x/=c;
  31. p.y/=c;
  32. cout<<p.x<<"/"<<p.y<<endl;
  33.  
  34. }
  35.  
  36. void hieu(PhanSo a,PhanSo b)
  37. {
  38.  
  39. PhanSo p;
  40. p.x=a.x*b.y-b.x*a.y;
  41. p.y=a.y*b.y;
  42. int c=gcd(p.x,p.y);
  43. cout<<p.x/c<<"/"<<p.y/c<<endl;
  44. }
  45.  
  46. void tich(PhanSo a,PhanSo b)
  47. {
  48. PhanSo p;
  49. p.x=a.x*b.x;
  50. p.y=a.y*b.y;
  51. int c=gcd(p.x,p.y);
  52. p.x/=c;
  53. p.y/=c;
  54. cout<<p.x<<"/"<<p.y<<endl;
  55. }
  56.  
  57. void thuong(PhanSo a,PhanSo b)
  58. {
  59. PhanSo p;
  60. p.x=a.x*b.y;
  61. p.y=a.y*b.x;
  62. int c=gcd(p.x,p.y);
  63. p.x/=c;
  64. p.y/=c;
  65. cout<<p.x<<"/"<<p.y<<endl;
  66. }
  67.  
  68. int main()
  69. {
  70. PhanSo a,b;
  71. nhap(a,b);
  72. tong(a,b);
  73. hieu(a,b);
  74. tich(a,b);
  75. thuong(a,b);
  76. }
  77.  
Success #stdin #stdout 0.01s 5496KB
stdin
1 2 3 4
stdout
5/4
-1/4
3/8
2/3