Professional Documents
Culture Documents
BTHon So
BTHon So
#include <iostream>
using namespace std;
class HonSo;
class PhanSo;
int GCD(int a, int b);
class PhanSo {
private:
int tu,mau;
public:
friend istream& operator>>(istream& in, PhanSo &a);
friend ostream& operator<<(ostream& out, PhanSo a);
int getTu();
int getMau();
void setTu(int x);
void setMau(int x);
operator float();
PhanSo(float x);
PhanSo(HonSo x);
PhanSo();
PhanSo(int a, int b);
void RG();
bool operator<(PhanSo another);
bool operator>(PhanSo another);
};
class HonSo{
private:
int songuyen;
PhanSo PS;
public:
friend istream& operator>>(istream& in, HonSo& a);
friend ostream& operator<<(ostream& out, HonSo a);
HonSo();
HonSo(float x);
HonSo(PhanSo a);
operator float();
int getTu();
int getMau();
int getPhanNguyen();
void setTu(int x);
void setMau(int x);
1
void setPhanNguyen(int x);
bool operator<(HonSo another);
bool operator>(HonSo another);
};
PhanSo::PhanSo() {
this->tu=1;
this->mau=1;
}
PhanSo::PhanSo(int a, int b) {
this->tu=a;
this->mau=b;
}
PhanSo::PhanSo(float x) {
this->tu=1;
this->mau=1;
while (x!=int(x)) {
x*=10;
this->mau*=10;
}
this->tu=x;
int gcd= GCD(this->tu,this->mau);
this->tu/=gcd;
this->mau/=gcd;
}
PhanSo::PhanSo(HonSo x) {
this->tu=x.getPhanNguyen()*x.getMau()+x.getTu();
this->mau=x.getMau();
PhanSo a(this->tu,this->mau);
a.RG();
this->tu=a.getTu();
this->mau=a.getMau();
}
2
out << a.tu << "/" << a.mau;
return out;
}
PhanSo::operator float(){
return 1.0*this->tu/this->mau;
int PhanSo::getTu() {
return this->tu;
}
int PhanSo::getMau() {
return this->mau;
}
void PhanSo::setTu(int x) {
this->tu=x;
}
void PhanSo::setMau(int x) {
this->mau=x;
}
void PhanSo::RG() {
int gcd= GCD(this->tu,this->mau);
this->tu/=gcd;
this->mau/=gcd;
}
HonSo::HonSo() {
this->songuyen=1;
this->PS.setTu(1);
this->PS.setMau(1);
3
}
HonSo::HonSo(float x) {
this->songuyen=(int)x;
this->PS=PhanSo(x-(int)x);
this->PS.RG();
}
HonSo::HonSo(PhanSo a) {
this->songuyen=a.getTu()/a.getMau();
this->PS.setTu(a.getTu()%a.getMau());
this->PS.setMau(a.getMau());
this->PS.RG();
}
HonSo::operator float() {
return 1.0*this->songuyen+1.0*this->PS.getTu()/this->PS.getMau();
}
int HonSo::getTu() {
return this->PS.getTu();
}
int HonSo::getMau(){
return this->PS.getMau();
}
int HonSo::getPhanNguyen() {
return this->songuyen;
}
void HonSo::setTu(int x) {
this->PS.setTu(x);
}
void HonSo::setMau(int x) {
this->PS.setMau(x);
4
}
void HonSo::setPhanNguyen(int x) {
this->songuyen=x;
}
5
swap(a[left],a[r]);
return left;
}
int main() {
// chuyen phan so sang so thuc
PhanSo m;
cin >> m;
float c = float(m);
cout << c << endl;
6
cin >> e;
PhanSo d = PhanSo(e);
cout << d << endl;
7
sortMangHonSo(aa,0,nn-1);
for (int i=0; i<nn; i++) {
cout << aa[i] << " ";
}
cout << endl;
return 0;
}