Đăng ký Đăng nhập

Tài liệu Tổn hợp Bài tập C++

.DOC
9
203
121

Mô tả:

PHAN II Cau II.1: Lý thuyết Cau II.2: Lý thuyết Cau II.3: #include #include #include class Phanso { int TS,MS; public: Phanso(int tu=0,int mau=1) {TS=tu; MS=mau;} ~Phanso() {TS=0;MS=1;} Phanso operator + (Phanso); Phanso operator - (Phanso); friend Phanso operator *(Phanso,Phanso); friend Phanso operator /(Phanso,Phanso); int operator ==(Phanso); int operator > (Phanso); int operator < (Phanso); int operator >= (Phanso); int operator <= (Phanso); friend istream & operator >>(istream &Nhap, Phanso &PS); friend ostream & operator <<(ostream &Xuat, Phanso &PS); void Toigian(); }; void Phanso::Toigian() { int a=abs(TS),b=abs(MS); if(a%b==0) {TS/=MS; MS=1;} else { int r; do { r=a%b; a=b;b=r; }while (r!=0); TS/=a; MS/=a; } } Phanso Phanso::operator + (Phanso Ps) { Phanso tam; tam.TS=TS*Ps.MS +MS*Ps.TS; tam.MS=MS*Ps.MS; tam.Toigian(); return tam; } Phanso Phanso::operator - (Phanso Ps) { Phanso tam; tam.TS=TS*Ps.MS - MS*Ps.TS; tam.MS=MS*Ps.MS; tam.Toigian(); return tam; } Phanso operator * (Phanso P1, Phanso P2) { Phanso tam; tam.TS= P1.TS * P2.TS; tam.MS= P1.MS * P2.MS; tam.Toigian(); return tam; } Phanso operator / (Phanso P1, Phanso P2) { Phanso tam; tam.TS= P1.TS * P2.MS; tam.MS= P1.MS * P2.TS; tam.Toigian(); return tam; } int Phanso::operator == (Phanso PS) { if (TS *PS.MS == MS*PS.TS) return 1; else return 0; } int Phanso::operator > (Phanso PS) { if (TS *PS.MS > MS*PS.TS) return 1; else return

Tài liệu liên quan