Skip to content

Instantly share code, notes, and snippets.

@beohoang98
Created October 9, 2017 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beohoang98/9ea592594c581bf777e1d6c8a5535566 to your computer and use it in GitHub Desktop.
Save beohoang98/9ea592594c581bf777e1d6c8a5535566 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
class PhanSo {
public:
int tu, mau;
PhanSo() {
tu = 0;
mau = 1;
}
PhanSo(int _tu, int _mau) {
tu = _tu;
mau = _mau;
if (mau == 0 && (tu = 0) && (mau = 1));
}
int getUCLN() {
int a = tu,
b = mau,
r = a % b;
while (r > 0) {
a = b;
b = r;
r = a % b;
}
return b;
}
void rutGon() {
int ucln = getUCLN();
tu /= ucln;
mau /= ucln;
}
PhanSo operator+ (const PhanSo& A) {
PhanSo tmp;
tmp.tu = tu*A.mau + mau*A.tu;
tmp.mau = mau*A.mau;
tmp.rutGon();
return tmp;
}
};
void main() {
freopen("INPUT.txt", "r", stdin);
freopen("OUTPUT.txt", "w", stdout);
//source
PhanSo Tong(0, 1);
PhanSo Chay(0, 1);
while (!feof(stdin)) {
cin >> Chay.tu >> Chay.mau;
Tong = Tong + Chay;
}
cout << Tong.tu << " " << Tong.mau;
fclose(stdin);
fclose(stdout);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment