fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ms(s,n) memset(s,n,sizeof(s))
  5. #define all(a) a.begin(),a.end()
  6. #define present(t, x) (t.find(x) != t.end())
  7. #define sz(a) int((a).size())
  8. #define FOR(i, a, b) for (int i = (a); i < (b); ++i)
  9. #define FORd(i, a, b) for (int i = (a) - 1; i >= (b); --i)
  10. #define pb push_back
  11. #define pf push_front
  12. #define fi first
  13. #define se second
  14. #define mp make_pair
  15. #define endl "\n"
  16.  
  17.  
  18. typedef long long ll;
  19. typedef unsigned long long ull;
  20. typedef long double ld;
  21. typedef pair<int,int> pi;
  22. typedef vector<int> vi;
  23. typedef vector<pi> vii;
  24.  
  25. const int MOD = (int) 1e9+7;
  26. const int INF = (int) 1e9+2804;
  27. inline ll gcd(ll a,ll b){ll r;while(b){r=a%b;a=b;b=r;}return a;}
  28. inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
  29.  
  30. class PhanSo{
  31. private:
  32. ll tu, mau;
  33. public:
  34. PhanSo(){
  35. }
  36. PhanSo(ll a, ll b){
  37. this->tu = a;
  38. this->mau = b;
  39. }
  40.  
  41. friend PhanSo operator + (const PhanSo a, const PhanSo b){
  42. ll mc = lcm(a.mau, b.mau);
  43. PhanSo tong;
  44. tong.tu = mc / a.mau * a.tu + mc / b.mau * b.tu;
  45. tong.mau = mc;
  46. ll g = gcd(tong.tu, tong.mau);
  47. tong.tu /= g;
  48. tong.mau /= g;
  49. return tong;
  50. }
  51.  
  52. friend ostream& operator << (ostream& out, const PhanSo a){
  53. out << a.tu << "/" << a.mau << endl;
  54. return out;
  55. }
  56.  
  57. friend istream& operator >> (istream& in, PhanSo& a){
  58. in >> a.tu >> a.mau;
  59. return in;
  60. }
  61. };
  62.  
  63. int main() {
  64. PhanSo p(1,1), q(1,1);
  65. cin >> p >> q;
  66. cout << p + q;
  67. return 0;
  68. }
  69.  
  70.  
  71.  
  72.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
2/1