Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C++ by Giang ( 5 years ago )
#include <bits/stdc++.h>
using namespace std;
int UCLN(int a, int b);
class PS{
private:
    int TS, MS;
public:
    PS(){
        TS = 0;
        MS = 1;
    }
    PS(int a, int b){
        TS = a;
        MS = b;
    }
    PS toiGian(){
        PS c;
        c.TS = TS / UCLN(TS, MS);
        c.MS = MS / UCLN(TS, MS);
        return c;
    }
    void xuat(){
        toiGian();
        cout << TS << "/" << MS;
    }
    PS operator+(PS Q);
    PS operator-(PS Q);
    PS operator*(PS Q);
    PS operator/(PS Q);
};

int main(){
    PS a(1,2);
    PS b(2,5);
    PS cong = a + b;
    cout << "Cong: ";
    cong.xuat();
    cout << "\nTru: ";
    PS tru = a - b;
    tru.xuat();
    cout << "\nNhan: ";
    PS nhan = a * b;
    nhan.xuat();
    cout << "\nChia: ";
    PS chia = a/ b;
    chia.xuat();
}

int UCLN(int a, int b)
{
    a = abs(a);
    b = abs(b);
    while (a * b != 0)
    {
        if (a > b)
            a %= b;
        else
            b %= a;
    }
    return a + b;
}

PS PS::operator+(PS Q){
    PS K;
    K.TS = TS * Q.MS + MS * Q.TS;
    K.MS = MS * Q.MS;
    return K;
}
PS PS::operator-(PS Q){
    PS K;
    K.TS = TS * Q.MS - MS * Q.TS;
    K.MS = MS * Q.MS;
    return K;
}
PS PS::operator*(PS Q){
    PS K;
    K.TS = TS * Q.TS;
    K.MS = MS * Q.MS;
    return K;
}
PS PS::operator/(PS Q){
    PS K;
    K.TS = TS * Q.MS;
    K.MS = MS * Q.TS;
    if(K.MS != 0)
    return K;
}

 

Revise this Paste

Your Name: Code Language: