debai24

/*

Ho Va Ten: Hoang Trung Kien

Lop: Thb 51

De Bai: Bai24.cpp

*/

//Cai dat lop phan so

#include<iostream>

#include<math.h>

using namespace std;

class phanso

{

private:

//phan so thi co tu so va mau so

double tu,mau;

public:

//ham tao khong doi so khoi tao gia tri ban dau cho tu so va mau so cua lop phan so

phanso()

{

tu=0;

mau=0;

}

//ham tao 2 doi so

phanso(double ts,double ms)

{

tu=ts;

mau=ms;

}

//vao ra voi doi tuong phan so (tu so va mau so)

friend istream& operator>>(istream& is,phanso &p);

friend ostream& operator<<(ostream& os,const phanso &p);

//ham ban khong phai la ham thanh vien nhung no lai co the truy nhap vao cac doi tuong cua lop nhu ham thanh vien

friend double uscln(double a,double b);

friend phanso toigian(const phanso &p);

//cac toan tu chong cong, tru, nhan, chia de tinh toan voi cac phan so

phanso operator+(const phanso &p);

phanso operator-(const phanso &p);

phanso operator*(const phanso &p);

phanso operator/(const phanso &p);

};

//----------Main program------------------------

int main()

{

phanso p1,p2,tong,hieu,tich,thuong;

cout<<"

Moi ban nhap phan so thu 1: ";cin>>p1;

cout<<"

Moi an nhap phan so thu 2: "; cin>>p2;

tong=p1+p2;hieu=p1-p2;

tich=p1*p2;thuong=p1/p2;

cout<<"

Tong cua 2 phan so chua toi gian la: "<<tong;

cout<<"

Tong cua 2 phan so o dang da toi gian la: "<<toigian(tong);

cout<<"

Hieu cua 2 phan so la: "<<hieu;

cout<<"

Hieu cua 2 pha so o dang da toi gian la: "<<toigian(hieu);

cout<<"

Tich cua 2 phan so la: "<<tich;

cout<<"

Tich cua 2 pha so o dang da toi gian la: "<<toigian(tich);

cout<<"

Thuong cua 2 phan so la: "<<thuong;

cout<<"

Thuong cua 2 pha so o dang da toi gian la: "<<toigian(thuong);

return 0;

}

//dinh nghia cac ham thanh vien cua lop phan so

//===============================================

istream& operator>>(istream& is,phanso &p)

{

char kt;

tt:

is>>p.tu>>kt>>p.mau;

if(p.mau==0)

{

cout<<"

mau so phai khac 0. Moi ban hhap lai: ";

}

return is;

}

//================================================

ostream& operator<<(ostream& os,const phanso &p)

{

os<<p.tu<<"/"<<p.mau;

return os;

}

//================================================

double uscln(double a,double b)

{

a=fabs(a);b=fabs(b);

while(a!=b)

if(a>b)a=a-b;

else b=b-a;

return a;

}

//================================================

phanso toigian(const phanso &p)

{

phanso ps;

ps.tu=p.tu/uscln(p.tu,p.mau);

ps.mau=p.mau/uscln(p.tu,p.mau);

return ps;

}

//=================================================

phanso phanso::operator+(const phanso &p)

{

phanso ps;

ps.tu=tu*p.mau+p.tu*mau;

ps.mau=mau*p.mau;

return ps;

//return toigian(ps);

}

//=================================================

phanso phanso::operator-(const phanso &p)

{

phanso ps;

ps.tu=tu*p.mau-p.tu*mau;

ps.mau=mau*p.mau;

return ps;

}

//=================================================

phanso phanso::operator*(const phanso &p)

{

phanso ps;

ps.tu=tu*p.tu;

ps.mau=mau*p.mau;

return ps;

}

//=================================================

phanso phanso::operator/(const phanso &p)

{

phanso ps;

ps.tu=tu*p.mau;

ps.mau=mau*p.tu;

return ps;

}

//het dinh nghia cho cac ham thanh vien cua lop phan so

Bạn đang đọc truyện trên: AzTruyen.Top

Tags: