So sánh phân số trong c

1. Viết chương trình nhập vào một phân số, rút gọn phân số và xuất kết quả.

2. Viết chương trình nhập vào hai phân số, tìm phân số lớn nhất và xuất kết quả.

3. Viết chương trình nhập vào hai phân số. Tính tổng, hiệu, tích, thương giữa chúng và xuất kết quả.

PhanSo.h

ifndef PHAN_SO

define PHAN_SO

class PhanSo { private:

int iTuSo;  
int iMauSo;  
private:
int UCLN(int a, int b);
public:
PhanSo();  
PhanSo(int iTu, int iMau);  
void Nhap();  
void Xuat();  
void RutGon();  
PhanSo Cong (const PhanSo &);  
PhanSo Tru (const PhanSo &);  
PhanSo Nhan (const PhanSo &);  
PhanSo Chia (const PhanSo &);  
int SoSanh (const PhanSo &);  
~PhanSo();      
};

endif

PhanSo.cpp

include "PhanSo.h"

include

using namespace std; PhanSo::PhanSo() { iTuSo = 0; iMauSo = 1; } PhanSo::PhanSo(int iTu, int iMau) { iTuSo = iTu; if (iMau != 0) {

iMauSo = iMau;  
} else {
iMauSo = 1;  
} } void PhanSo::Nhap() { cout << "\tNhap tu So: "; cin >> iTuSo; cout << "\tNhap mau so: "; cin >> iMauSo; } void PhanSo::Xuat() { cout << iTuSo << "/" << iMauSo << "\n"; } int PhanSo::UCLN(int a, int b) { if (a==0 ||b==0) return a+b; while (a !=b) { if(a>b)
                a=a-b;  
            else  
                b=b-a;  
        }  
        return a;  
    }
void PhanSo::RutGon() { int iUCLN = UCLN(iTuSo, iMauSo); iTuSo = iTuSo / iUCLN; iMauSo = iMauSo / iUCLN; } PhanSo PhanSo::Cong(const PhanSo &b) {
return PhanSo((iTuSo*b.iMauSo) + (iMauSo*b.iTuSo),iMauSo*b.iMauSo);  
} PhanSo PhanSo::Tru(const PhanSo &b) {
  return PhanSo((iTuSo*b.iMauSo) - (iMauSo*b.iTuSo),iMauSo*b.iMauSo);  
} PhanSo PhanSo::Nhan(const PhanSo &b) {
 return PhanSo(this->iTuSo*b.iTuSo,this->iMauSo*b.iMauSo);  
} PhanSo PhanSo::Chia(const PhanSo &b) { return PhanSo(this->iTuSo*b.iMauSo,this->iMauSo*b.iTuSo); } int PhanSo::SoSanh(const PhanSo &b) { int x = this->iTuSo / this->iMauSo; int y = b.iTuSo / b.iMauSo; if (x < y) return -1; if (x > y)
return 1;  
else return 0; } PhanSo::~PhanSo() { cout << "\nVao ham huy"; }

main.cpp

include "PhanSo.h"

include

include

using namespace std; void main() { PhanSo ps1,ps2; cout << "Phan so 1:\n"; ps1.Nhap(); cout << "\tPhan so vua nhap:"; ps1.Xuat(); ps1.RutGon(); cout << "\tPhan so rut gon:"; ps1.Xuat(); cout << "Phan so 2:\n"; ps2.Nhap(); cout << "\tPhan so vua nhap:"; ps2.Xuat(); ps2.RutGon(); cout << "\tPhan so rut gon:"; ps2.Xuat(); PhanSo psTong = ps1.Cong(ps2); cout << "\nCong hai phan so: "; psTong.RutGon(); psTong.Xuat(); PhanSo psTich = ps1.Nhan(ps2); cout << "\nNhan hai phan so: "; psTich.RutGon(); psTich.Xuat(); PhanSo psThuong = ps1.Chia(ps2); cout << "\nChia hai phan so: "; psThuong.RutGon(); psThuong.Xuat(); int ss = ps1.SoSanh(ps2); switch (ss) { case -1:

cout << "\nPhan so 1 nho hon phan so 2";  
break;  
case 1:
cout << "\nPhan so 1 lon hon phan so 2";  
break;  
case 0:
cout << "\nPhan so 1 bang phan so 2";  
break;  
default:
break;  
} PhanSo psHieu = ps1.Tru(ps2); cout << "\nTru hai phan so: "; psHieu.RutGon(); psHieu.Xuat(); _getch(); } Đề bài:

Xây dựng lớp Phân số với tử số và mẫu số nguyên và các hàm thành viên sau:

+ Cấu tử mặc định

+ Cấu tử có tham số

+ Toán tử nhập xuất

+ Toán tử cộng, trừ và nhân hai phân số

+ Rút gọn phân số

+ So sánh hai phân số: ==, !=, >, <, >=, <=.

Nhập vào một mảng các phân số: tính và in ra:

+ Phân số lớn nhất, phân số nhỏ nhất

+ Tổng và tích của các phân số đã nhập vào.

+ Sắp xếp các phân số theo thứ tự tăng dần.

Bài làm:

include

using namespace std; int ucln(int a, int b) { int tam = a; while(!(a%tam==0&&b%tam==0&&tam>0)) tam –; return tam;

} class Phanso { int ts, ms; public: Phanso(); Phanso(int ts1, int ms1); friend istream & operator >> (istream &, Phanso &); friend ostream & operator << (ostream &, Phanso &); Phanso operator + (Phanso &); Phanso operator – (Phanso &); Phanso operator * (Phanso &); void rutgon(); bool operator == (Phanso &); bool operator != (Phanso &); bool operator > (Phanso &); bool operator < (Phanso &); bool operator >= (Phanso &); bool operator <= (Phanso &); }; Phanso::Phanso() { ts = 0; ms =1; } Phanso::Phanso(int ts1, int ms1) { ts = ts1; ms = ms1; } ostream & operator << (ostream &os, Phanso &r) { os << r.ts << “/” << r.ms; return os; } istream & operator >> (istream &is, Phanso &r) { cout << “Nhap tu so:”; is >> r.ts; cout << “Nhap mau so:”; is >> r.ms; return is; } Phanso Phanso::operator+(Phanso &r) { int ts1, ms1; ts1 = ts*r.ms + ms*r.ts; ms1 = ms * r.ms; int uc = ucln(ts1, ms1); ts1 = ts1/uc; ms1 = ms1/uc; return Phanso(ts1, ms1); } Phanso Phanso::operator-(Phanso &r) { int ts1, ms1; ts1 = ts*r.ms – ms*r.ts; ms1 = ms * r.ms; int uc = ucln(ts1, ms1); ts1 = ts1/uc; ms1 = ms1/uc; return Phanso(ts1, ms1); } Phanso Phanso::operator*(Phanso &r) { int ts1, ms1;

ts1 = ts*r.ts; ms1 = ms * r.ms; int uc = ucln(ts1, ms1); ts1 = ts1/uc; ms1 = ms1/uc; return Phanso(ts1, ms1); } bool Phanso::operator > (Phanso &r) { int ts1, ms1; ts1 = ts*r.ms – ms*r.ts; ms1 = ms * r.ms; return (ts1*ms1>0); } bool Phanso::operator == (Phanso & r) { if(!(*this>r) && !(r>*this)) return true; return false; } bool Phanso::operator != (Phanso & r) { if(!(*this==r)) return true; return false; } bool Phanso::operator < (Phanso & r) { if(r>*this) return true; return false; } void Phanso::rutgon() { int uc = ucln(ts, ms); ts = ts/uc; ms = ms/uc; } void sapxep(Phanso a[], int n) { Phanso tam; int i, j; for (i=0;i> n; a = new Phanso[n]; for(i=0;i> a[i]; } cout << “Mang cac phan so vua nhap vao:\n”; for(i=0;ia[m1]) m1 = i; if(a[i]