#include <bits/stdc++.h>
using namespace std;
class PHANSO {
int tuso;
int mauso;
public:
friend istream& operator>>(istream& x,PHANSO &y);
friend ostream& operator<<(ostream& x,PHANSO y);
PHANSO operator+(PHANSO y);
PHANSO operator!();
};
int ucln(int t, int m)
{
t=abs(t);m=abs(m);
if(t*m==0) return 1;
while(t!=m)
if(t>m) t-=m;
else m-=t;
return t;
}
istream& operator>>(istream& x,PHANSO &y)
{
cout<<"Nhap tu so :"; x>>y.tuso;
cout<<"Nhap mau so :"; x>>y.mauso;
return x;
}
ostream& operator<<(ostream& x,PHANSO y)
{
x<<y.tuso<<"/"<<y.mauso;
return x;
}
PHANSO PHANSO::operator+(PHANSO y)
{
PHANSO z;
z.tuso = tuso*y.mauso + mauso*y.tuso;
z.mauso = mauso*y.mauso;
return z;
}
PHANSO PHANSO::operator!()
{
PHANSO z;
int uc = ucln(z.tuso,z.mauso);
z.tuso = z.tuso/uc;
z.mauso = z.mauso/uc;
return z;
}
int main()
{
PHANSO a,b;
cin>>a;
cin>>b;
PHANSO c=a+b;
cout<<!c<<endl;
return 0;
}
Cho e hỏi là tại sao e không nạp chồng phương thức toán tử để rút gọn phân số được ạ. Em làm toàn ra sai ạ
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?