fork download
  1. #include <iostream>
  2. using namespace std;
  3. using ll = long long;
  4.  
  5. ll gcd(ll a, ll b) {
  6. while (b != 0) {
  7. ll tmp = b;
  8. b = a % b;
  9. a = tmp;
  10. }
  11. return a;
  12. }
  13. struct PhanSo {
  14. ll tu, mau;
  15.  
  16. void rutgon() {
  17. ll div = gcd(abs(tu), abs(mau));
  18. tu /= div;
  19. mau /= div;
  20. if (mau < 0) {
  21. tu = -tu; mau = -mau;
  22. }
  23. }
  24. };
  25.  
  26. PhanSo Cong (PhanSo a, PhanSo b) {
  27. return {a.tu * b.mau + b.tu * a.mau, a.mau * b.mau};
  28. }
  29.  
  30. PhanSo Tru (PhanSo a, PhanSo b) {
  31. return {a.tu * b.mau - b.tu * a.mau, a.mau * b.mau};
  32. }
  33.  
  34. PhanSo Nhan(PhanSo a, PhanSo b) {
  35. return {a.tu * b.tu, a.mau * b.mau};
  36. }
  37.  
  38. PhanSo Chia (PhanSo a, PhanSo b) {
  39. return {a.tu * b.mau, a.mau * b.tu};
  40. }
  41.  
  42. void HienThi (PhanSo p) {
  43. p.rutgon();
  44. cout << p.tu << '/' << p.mau << ' ';
  45. }
  46.  
  47. void Input(PhanSo &p) {
  48. cin >> p.tu >> p.mau;
  49. }
  50.  
  51. int main() {
  52. PhanSo A, B;
  53. Input(A); Input(B);
  54.  
  55. HienThi(Cong(A, B));
  56. HienThi(Tru(A, B));
  57. HienThi(Nhan(A, B));
  58. HienThi(Chia(A, B));
  59. }
  60.  
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
-1/0 -1/0 -1/0 -1/0