Đề bài : ThucHanh10_Class_NapChongToanTu.pdf
Cài đặt lớp SoHuuTi để lưu 1 số hữu tỉ. Số hữu tỉ có dạng a/b trong đó a(tử số) & b(mẫu số) là các số nguyên dương. Ngoài ra cần biến để xác định dấu (+/-) của số hữu tỉ. Xây dựng các hàm sau:
1) Hàm khởi tạo & hàm hủy. Hàm khởi tạo cần hỗ trợ chuyển đổi kiểu tự động.
2) Hàm tối giản. Ví dụ 2/4 sẽ được lưu dưới dạng 1/2. Các số hữu tỉ phải luôn được lưu dưới dạng số tối giản. Gợi ý : tìm ước chung lớn nhất của a & b. Định nghĩa các toán tử sau :
a. Toán tử << & >> để in và nhập cho lớp SoHuuTi (sử dụng trả về tham chiếu)
b. Toán tử cộng, trừ và đổi dấu
c. Toán tử nhân, chia, lấy phần nguyên
d. Toán tử tăng (++), giảm (--). Ví dụ a/b++ kết quả : (a)++/(b)++ . Xử lý tăng/giảm trước/sau; tăng/giảm khiến b=0;
e. Toán tử so sánh >, <, ==, >=, <=, !=. Kết quả trả về kiểu bool
Chú ý : khi cài đặt các toán tử nên sử dụng lại các toán tử khác để cài đặt
3) Viết thuật toán sắp xếp để sắp xếp 1 dãy các số hữu tỉ.
________________________________________________________________________
Code : http://pastebin.com/ewnG56Fv hoặc thuchanh10-bai1.cpp
________________________________________________________________________
#include <cstdlib>
#include <iostream>
using namespace std;
class SoHuuTi
{
public:
SoHuuTi();
SoHuuTi(int,int);
~SoHuuTi();
int gettu()
{
return this->tu;
};
int getmau()
{
return this->mau;
};
void settu(int tu)
{
this-> tu= tu;
}
void setmau(int mau)
{
this->mau= mau;
}
void setSoHuuTi(int, int);
friend istream& operator>> (istream&, SoHuuTi&);
friend ostream& operator<< (ostream&, SoHuuTi&);
// truoc ++t
const SoHuuTi operator++();
// sau t++
const SoHuuTi operator++(int);
friend const SoHuuTi operator+ (const SoHuuTi&, const SoHuuTi&);
friend const SoHuuTi operator- (const SoHuuTi&, const SoHuuTi&);
friend const SoHuuTi operator- (const SoHuuTi&);
friend bool operator< (const SoHuuTi&, const SoHuuTi&);
friend bool operator> (const SoHuuTi&, const SoHuuTi&);
friend bool operator!= (const SoHuuTi&, const SoHuuTi&);
private:
int tu;
int mau;
void quyChuanSoHuuTi();
};
const SoHuuTi operator* (const SoHuuTi&, const SoHuuTi&);
const SoHuuTi operator/ (const SoHuuTi&, const SoHuuTi&);
const SoHuuTi operator% (const SoHuuTi&, const SoHuuTi&);
bool operator<= (const SoHuuTi&, const SoHuuTi&);
bool operator>= (const SoHuuTi&, const SoHuuTi&);
bool operator== (const SoHuuTi&, const SoHuuTi&);
SoHuuTi::SoHuuTi()
{
tu = 0;
mau = 1;
};
SoHuuTi::~SoHuuTi()
{
};
SoHuuTi::SoHuuTi(int x,int y)
{
this->tu=x;
this->mau=y;
};
void SoHuuTi::quyChuanSoHuuTi()
{
int tu_, mau_;
tu_ = tu; mau_ = mau;
if ( tu_ <0 || mau_ <0 )
{
}
while ( tu_!=mau_ )
{
if ( tu_ > mau_ )
{
tu_ -= mau_;
}
else mau_ -= tu_;
}
if ( tu<0 || mau<0 ) tu*= (-1);
tu/= tu_;
mau/= tu_;
};
void SoHuuTi::setSoHuuTi(int a, int b)
{
tu= a;
mau= b;
quyChuanSoHuuTi();
};
istream& operator>> (istream& nhap,SoHuuTi& x)
{
nhap>>x.tu>>x.mau;
x.quyChuanSoHuuTi();
return nhap;
};
ostream& operator<< (ostream& xuat, SoHuuTi& x)
{
//x.quyChuanSoHuuTi();
xuat<<x.tu<<"/"<<x.mau;
return xuat;
};
const SoHuuTi SoHuuTi::operator++()
{
SoHuuTi x;
x.tu= tu++;
x.mau = mau++;
x.quyChuanSoHuuTi();
return x;
};
const SoHuuTi SoHuuTi::operator++(int)
{
int x_ = tu;
int y_ = mau;
tu++; mau++;
return SoHuuTi(x_, y_);
};
const SoHuuTi operator+ (const SoHuuTi& a, const SoHuuTi& b)
{
SoHuuTi c;
c.tu = a.tu * b.mau + b.tu*a.mau;
c.mau = a.mau * b.mau;
c.quyChuanSoHuuTi();
return c;
};
const SoHuuTi operator- (const SoHuuTi& a, const SoHuuTi& b)
{
SoHuuTi c,d,e;
c=a; d=b;
if ( c==d ) return e;
if ( d>c )
{
d.tu = d.tu * c.mau - c.tu*d.mau;
d.mau = c.mau * d.mau;
d.quyChuanSoHuuTi();
d.tu *= (-1);
return d;
}
else
{
c.tu = c.tu * d.mau - d.tu*c.mau;
c.mau = c.mau * d.mau;
c.quyChuanSoHuuTi();
return c;
}
};
const SoHuuTi operator- (const SoHuuTi& a)
{
SoHuuTi b;
b= a;
b.tu *= (-1);
return b;
};
bool operator< (const SoHuuTi& a, const SoHuuTi& b)
{
if ( a.tu*b.mau < b.tu*a.mau) return true;
else return false;
};
bool operator> (const SoHuuTi& a, const SoHuuTi& b)
{
if ( a.tu*b.mau > b.tu*a.mau) return true;
else return false;
};
bool operator!= (const SoHuuTi& a, const SoHuuTi& b)
{
if ( a.tu*b.mau != b.tu*a.mau) return true;
else return false;
};
const SoHuuTi operator* (const SoHuuTi& a, const SoHuuTi& b)
{
SoHuuTi c,d;
c= a; d= b;
int tu_ = c.gettu() * d.gettu();
int mau_= c.getmau() * d.getmau();
return SoHuuTi(tu_,mau_);
};
const SoHuuTi operator/ (const SoHuuTi& a, const SoHuuTi& b)
{
SoHuuTi c,d;
c= a; d= b;
int tu_ = c.gettu() * d.getmau();
int mau_= c.getmau() * d.gettu();
return SoHuuTi(tu_,mau_);
};
const SoHuuTi operator% (const SoHuuTi& a, const SoHuuTi& b)
{
SoHuuTi c,d,e;
c= a; d= b;
if ( c<d ) return c;
else if ( c == d ) return e;
else
{
while ( c > d )
{
c = c-d;
if ( c==d ) return e;
}
return c;
}
};
bool operator<= (const SoHuuTi& a, const SoHuuTi& b)
{
SoHuuTi c,d;
c= a; d= b;
if ( c<d || c==d ) return true;
else return false;
};
bool operator>= (const SoHuuTi& a, const SoHuuTi& b)
{
SoHuuTi c,d;
c= a; d= b;
if ( c>d || c==d ) return true;
else return false;
};
bool operator== (const SoHuuTi& a, const SoHuuTi& b)
{
SoHuuTi c,d;
c=a; d=b;
int tu_ = c.gettu()* d.getmau() - d.gettu()*c.getmau();
int mau_ = c.getmau() - d.getmau();
if ( tu_ == 0 && mau_ == 0 ) return true;
else return false;
};
bool Tang(SoHuuTi a, SoHuuTi b)
{
if ( a>b ) return true;
else return false;
}
bool Giam(SoHuuTi a, SoHuuTi b)
{
if ( a<b ) return true;
else return false;
}
void SapXepSoHuuTi(SoHuuTi* a, int n, bool (SoSanh)(SoHuuTi, SoHuuTi))
{
SoHuuTi tam;
for ( int i=0; i<n-1; i++ )
{
for ( int j=i+1; j<n; j++ )
{
if ( SoSanh(a[i], a[j]) )
{
tam= a[i];
a[i]= a[j];
a[j]= tam;
}
}
}
cout << "Day sau khi duoc sap xep :" << endl;
for ( int i=0; i<n; i++)
{
cout << a[i] << " ";
}
cout << endl;
}
int main()
{
SoHuuTi* a;
a= new SoHuuTi[10];
int n;
cout << "Nhap so luong so huu ti can sap xep: n= ";
cin >> n;
cout << "Bat dau nhap so huu ti: " << endl;
for ( int i= 0; i<n; i++ )
{
cout << "So thu " << i+1 << ":" << endl;;
cin >> a[i] ;
cout << a[i] << endl;
}
int kieusapxep=0;
while (kieusapxep!=1 && kieusapxep!=2)
{
cout << "Chon kieu sap xep: Tang dan or Giam dan(1 or 2), nhap so: " ;
cin >> kieusapxep;
switch (kieusapxep)
{
case 1: SapXepSoHuuTi(a, n, Tang); break;
case 2: SapXepSoHuuTi(a, n, Giam); break;
}
}
system("PAUSE");
return EXIT_SUCCESS;
}