Bài tập C++ xây dựng lớp PHANSO

Xây dựng lớp PHANSO bao gồm các thuộc tính( chỉ truy cập được bên trong lớp):
Tử số và mẫu số.
Xây dựng các toán tử chèn dòng<< và trích dòng>>, +,-,*,/ cho lớp PHANSO.
Xây dung chương trình có các chức năng sau:
1. Thêm một phân số mới.
2. Xóa 1 phân số khỏi dãy (nhập vào số thứ tự của phân số cần xóa)
3. Đưa ra màn hình phân số phân số có giá trị lớn nhất của dãy
4. Đưa ra màn hình phân số có giá trị nhỏ nhất của dãy
5. Thoát khỏi chương trình.
 

VSupport

Ngây thơ trong tối
Bài này dài quá, bạn tham khảo bài này rồi tự làm nhé. trong void main viết thêm nhập mảng phân số và xử lý yêu cầu với các toán tử đã được định nghĩa sẵn chỉ việc sử dụng thôi


Mã:
#include"conio.h"
#include"math.h"
#include <iostream>



using namespace std;
class phanso
{
private:
float tu,mau;
public:
phanso(float =0,float =1);
~phanso(void);
phanso operator+(const phanso &);
phanso operator-(const phanso &);
phanso operator*(const phanso &);
phanso operator/(const phanso &);
bool operator<(phanso &);
bool operator<=(phanso &);
bool operator>(phanso &);
bool operator>=(phanso&);
bool operator==(phanso&);
bool operator!=(phanso&);
friend ostream &operator<<(ostream &out,const phanso &a);
friend istream &operator>>(istream &in,phanso &a);
};




phanso::phanso(float b,float c)
{
tu=b;mau=c;
}

phanso::~phanso(void)
{
}

phanso phanso::operator+(const phanso &x)
{
phanso c;
c.tu=tu*x.mau+mau*x.tu;
c.mau=mau*x.mau;
return c;
}

ostream &operator<<(ostream &out,const phanso &a)
{
if(a.tu/a.mau==0) out<<0;
else if(float(a.tu/a.mau)==int(a.tu/a.mau)) out<<a.tu/a.mau;
else
{
int x=abs(a.tu),y=abs(a.mau);
while(x!=y)
if(x>y)x-=y;
else y-=x;
out<<(a.tu<0 || a.mau<0 ?"-":"")<<abs(a.tu/x)<<"/"<<abs(a.mau/x);
}
return out;
}

istream &operator>>(istream &in,phanso &a)
{
cout<<"tu=";in>>a.tu;
cout<<"mau=";in>>a.mau;
return in;
}

phanso phanso::operator-(const phanso&x)
{
phanso c;
c.tu=tu*x.mau-mau*x.tu;
c.mau=mau*x.mau;
return c;
}

phanso phanso::operator*(const phanso&x)
{
phanso c;
c.tu=tu*x.tu;
c.mau=mau*x.mau;
return c;
}

phanso phanso::operator/(const phanso&x)
{
phanso c;
c.tu=tu*x.mau;
c.mau=mau*x.tu;
return c;
}

bool phanso::operator<(phanso&x)
{
if(tu*x.mau<mau*x.tu) return true;return false;
}

bool phanso::operator<=(phanso&x)
{
if(tu*x.mau<=mau*x.tu) return true;return false;
}

bool phanso::operator>(phanso&x)
{
if(tu*x.mau>mau*x.tu) return true;return false;
}

bool phanso::operator>=(phanso&x)
{
if(tu*x.mau>=mau*x.tu) return true;return false;
}

bool phanso::operator==(phanso&x)
{
if(tu*x.mau==mau*x.tu) return true;return false;
}

bool phanso::operator!=(phanso&x)
{
if(tu*x.mau!=mau*x.tu) return true;return false;
}



void main()
{
phanso a,b,c;
cin>>a>>b;
cout<<a+b<<endl;
cout<<a-b<<endl;
cout<<a*b<<endl;
cout<<a/b<<endl;
if(a>b) cout<<1;else cout<<0;
if(a>=b) cout<<1;else cout<<0;
if(a<b) cout<<1;else cout<<0;
if(a<=b) cout<<1;else cout<<0;
if(a==b) cout<<1;else cout<<0;
if(a!=b) cout<<1;else cout<<0;
system("pause");
}
 

taplamhacker

♥ Thanh Trâm ♥
:troll:
mới test cài cái cơ bản + giá trị nhập vào đúng
bạn kiểm tra lại xem dc chưa nhé
bài này còn ngắn lắm chưa có gì gọi là dài đâu :conan:

[AH]
Mã:
#include"conio.h"
#include"math.h"
#include <iostream>
using namespace std;
class phanso
{
	private:
	float tu,mau;
	public:
	phanso(float =0,float =1);
	~phanso();
	int gettu();
	int getmau();
	phanso operator+(phanso b);
	phanso operator-(phanso b);
	phanso operator*(phanso b);
	phanso operator/(phanso b);
	friend ostream &operator<<(ostream &out,const phanso &a);
	friend istream &operator>>(istream &in,phanso &a);
};
class dsphanso
{
	private:
	int n;
	int soluong;
	phanso a[10000];
	public:
	dsphanso(int n=0);
	~dsphanso();
	void append(phanso x);
	void remove(int vt);
	void nhap();
	void xuat();
	phanso max();
	phanso min();
};
dsphanso::dsphanso(int x)
{
	n=x;
	soluong=0;
}
dsphanso::~dsphanso()
{
	soluong=0;
	n=10000;
}
void dsphanso::append(phanso x)
{
	a[soluong++]=x;
}
void dsphanso::remove(int vt)
{
	--soluong;
	for(int i = vt ; i < soluong ; i++)
	{
		a[i]=a[i+1];
	}
}
phanso dsphanso::max()
{
	float tmp = (float)a[0].gettu()/(float)a[0].getmau();
	int vt=0;
	for(int i = 1 ; i < soluong ; i ++)
	{
		if( ((float)a[i].gettu()/(float)a[i].getmau()) > tmp)
		{
			tmp = ((float)a[i].gettu()/(float)a[i].getmau());
			vt=i;
		}
	}
	return a[vt];
}
phanso dsphanso::min()
{
	float tmp = (float)a[0].gettu()/(float)a[0].getmau();
	int vt=0;
	for(int i = 1 ; i < soluong ; i ++)
	{
		if( ((float)a[i].gettu()/(float)a[i].getmau()) < tmp)
		{
			tmp = ((float)a[i].gettu()/(float)a[i].getmau());
			vt=i;
		}
	}
	return a[vt];
}
void dsphanso::nhap()
{
	cout<<"Nhap so phan so: ";
	int tmp;
	do
	{
		cin>>tmp;
		if(tmp+soluong>n || tmp+soluong <= 0)
			cout<<"Sai so luong roi"<<endl;
	}while(tmp+soluong>n || tmp+soluong <= 0);
	for(int i = 0 ; i < tmp ; i ++)
	{
		phanso tmp;
		cin>>tmp;
		dsphanso::append(tmp);
	}
}
void dsphanso::xuat()
{
	for(int i = 0 ; i < soluong ; i ++)
		cout<<a[i]<<endl;
}
phanso::phanso(float b,float c)
{
	tu=b;
	mau=c;
}
phanso::~phanso()
{
}
int phanso::gettu()
{
	return tu;
}
int phanso::getmau()
{
	return mau;
}
ostream &operator<<(ostream &out,const phanso &a)
{
	if(a.tu/a.mau==0) out<<0;
	else 
		if(float(a.tu/a.mau)==int(a.tu/a.mau)) out<<a.tu/a.mau;
	else
	{
		int x=abs((int)a.tu),y=abs((int)a.mau);
		while(x!=y)
		if(x>y)x-=y;
		else y-=x;
		out<<(a.tu<0 || a.mau<0 ?"-":"")<<abs((int)a.tu/x)<<"/"<<abs((int)a.mau/x);
	}
	return out;
}
istream &operator>>(istream &in,phanso &a)
{
	cout<<"tu=";
	in>>a.tu;
	cout<<"mau=";
	do
	{
		in>>a.mau;
		if(a.mau ==0)
			cout<<"Mau Phai Khac Khong"<<endl;
	}
	while(a.mau==0);
	return in;
}
phanso phanso::operator+(phanso x)
{
	phanso c;
	c.tu=tu*x.mau+mau*x.tu;
	c.mau=mau*x.mau;
	return c;
}
phanso phanso::operator-(phanso x)
{
	phanso c;
	c.tu=tu*x.mau-mau*x.tu;
	c.mau=mau*x.mau;
	return c;
}
phanso phanso::operator*(phanso x)
{
	phanso c;
	c.tu=tu*x.tu;
	c.mau=mau*x.mau;
	return c;
}
phanso phanso::operator/(phanso x)
{
	phanso c;
	c.tu=tu*x.mau;
	c.mau=mau*x.tu;
	return c;
}
int menu()
{
	cout<<"1.Them phan so moi"<<endl;
	cout<<"2.Xoa 1 phan so khoi day"<<endl;
	cout<<"3.Tim phan so lon nhat"<<endl;
	cout<<"4.Tim phan so nho nhat"<<endl;
	cout<<"5.Xuat "<<endl;
	cout<<"0.Thoat"<<endl;	
	int chon ;
	cin>>chon;
	return chon;
}
int main()
{
	int sl,chon=0,vt;
	cout<<"So Luong phan tu cua mang, toi da la 10000: ";
	do
	{
		cin>>sl;
		if(sl<=0 || sl>10000)
			cout<<"So luong phai lon hon 0 va nho hon 10000"<<endl;
	}while(sl<=0 || sl>10000);
	dsphanso A= dsphanso(sl);
	do
	{
		chon=menu();
		switch(chon)
		{
			case 1:
				A.nhap();
				break;
			case 2:
				cout<<"Nhap vi tri can xoa(bat dau tu 0): ";
				cin>>vt;
				A.remove(vt);
				break;
			case 3:
				cout<<A.max();
				break;
			case 4:
				cout<<A.min();
				break;
			case 5:
				A.xuat();
				break;
			case 0:
				cout<<"BYE"<<endl;
				break;
			default:
				cout<<"Chon Sai"<<endl;
		}	
	}while(chon != 0);
//	cout<<endl;
	system("pause");
}
[/AH]
 

VSupport

Ngây thơ trong tối
Taplamhacker code lên tay quá ta :nhay::gach:
 
Sửa lần cuối:

latruongthinh

Gà nhất Forum
:troll:
mới test cài cái cơ bản + giá trị nhập vào đúng
bạn kiểm tra lại xem dc chưa nhé
bài này còn ngắn lắm chưa có gì gọi là dài đâu :conan:

[AH]
Mã:
#include"conio.h"
#include"math.h"
#include <iostream>
using namespace std;
class phanso
{
    private:
    float tu,mau;
    public:
    phanso(float =0,float =1);
    ~phanso();
    int gettu();
    int getmau();
    phanso operator+(phanso b);
    phanso operator-(phanso b);
    phanso operator*(phanso b);
    phanso operator/(phanso b);
    friend ostream &operator<<(ostream &out,const phanso &a);
    friend istream &operator>>(istream &in,phanso &a);
};
class dsphanso
{
    private:
    int n;
    int soluong;
    phanso a[10000];
    public:
    dsphanso(int n=0);
    ~dsphanso();
    void append(phanso x);
    void remove(int vt);
    void nhap();
    void xuat();
    phanso max();
    phanso min();
};
dsphanso::dsphanso(int x)
{
    n=x;
    soluong=0;
}
dsphanso::~dsphanso()
{
    soluong=0;
    n=10000;
}
void dsphanso::append(phanso x)
{
    a[soluong++]=x;
}
void dsphanso::remove(int vt)
{
    --soluong;
    for(int i = vt ; i < soluong ; i++)
    {
        a[i]=a[i+1];
    }
}
phanso dsphanso::max()
{
    float tmp = (float)a[0].gettu()/(float)a[0].getmau();
    int vt=0;
    for(int i = 1 ; i < soluong ; i ++)
    {
        if( ((float)a[i].gettu()/(float)a[i].getmau()) > tmp)
        {
            tmp = ((float)a[i].gettu()/(float)a[i].getmau());
            vt=i;
        }
    }
    return a[vt];
}
phanso dsphanso::min()
{
    float tmp = (float)a[0].gettu()/(float)a[0].getmau();
    int vt=0;
    for(int i = 1 ; i < soluong ; i ++)
    {
        if( ((float)a[i].gettu()/(float)a[i].getmau()) < tmp)
        {
            tmp = ((float)a[i].gettu()/(float)a[i].getmau());
            vt=i;
        }
    }
    return a[vt];
}
void dsphanso::nhap()
{
    cout<<"Nhap so phan so: ";
    int tmp;
    do
    {
        cin>>tmp;
        if(tmp+soluong>n || tmp+soluong <= 0)
            cout<<"Sai so luong roi"<<endl;
    }while(tmp+soluong>n || tmp+soluong <= 0);
    for(int i = 0 ; i < tmp ; i ++)
    {
        phanso tmp;
        cin>>tmp;
        dsphanso::append(tmp);
    }
}
void dsphanso::xuat()
{
    for(int i = 0 ; i < soluong ; i ++)
        cout<<a[i]<<endl;
}
phanso::phanso(float b,float c)
{
    tu=b;
    mau=c;
}
phanso::~phanso()
{
}
int phanso::gettu()
{
    return tu;
}
int phanso::getmau()
{
    return mau;
}
ostream &operator<<(ostream &out,const phanso &a)
{
    if(a.tu/a.mau==0) out<<0;
    else 
        if(float(a.tu/a.mau)==int(a.tu/a.mau)) out<<a.tu/a.mau;
    else
    {
        int x=abs((int)a.tu),y=abs((int)a.mau);
        while(x!=y)
        if(x>y)x-=y;
        else y-=x;
        out<<(a.tu<0 || a.mau<0 ?"-":"")<<abs((int)a.tu/x)<<"/"<<abs((int)a.mau/x);
    }
    return out;
}
istream &operator>>(istream &in,phanso &a)
{
    cout<<"tu=";
    in>>a.tu;
    cout<<"mau=";
    do
    {
        in>>a.mau;
        if(a.mau ==0)
            cout<<"Mau Phai Khac Khong"<<endl;
    }
    while(a.mau==0);
    return in;
}
phanso phanso::operator+(phanso x)
{
    phanso c;
    c.tu=tu*x.mau+mau*x.tu;
    c.mau=mau*x.mau;
    return c;
}
phanso phanso::operator-(phanso x)
{
    phanso c;
    c.tu=tu*x.mau-mau*x.tu;
    c.mau=mau*x.mau;
    return c;
}
phanso phanso::operator*(phanso x)
{
    phanso c;
    c.tu=tu*x.tu;
    c.mau=mau*x.mau;
    return c;
}
phanso phanso::operator/(phanso x)
{
    phanso c;
    c.tu=tu*x.mau;
    c.mau=mau*x.tu;
    return c;
}
int menu()
{
    cout<<"1.Them phan so moi"<<endl;
    cout<<"2.Xoa 1 phan so khoi day"<<endl;
    cout<<"3.Tim phan so lon nhat"<<endl;
    cout<<"4.Tim phan so nho nhat"<<endl;
    cout<<"5.Xuat "<<endl;
    cout<<"0.Thoat"<<endl;    
    int chon ;
    cin>>chon;
    return chon;
}
int main()
{
    int sl,chon=0,vt;
    cout<<"So Luong phan tu cua mang, toi da la 10000: ";
    do
    {
        cin>>sl;
        if(sl<=0 || sl>10000)
            cout<<"So luong phai lon hon 0 va nho hon 10000"<<endl;
    }while(sl<=0 || sl>10000);
    dsphanso A= dsphanso(sl);
    do
    {
        chon=menu();
        switch(chon)
        {
            case 1:
                A.nhap();
                break;
            case 2:
                cout<<"Nhap vi tri can xoa(bat dau tu 0): ";
                cin>>vt;
                A.remove(vt);
                break;
            case 3:
                cout<<A.max();
                break;
            case 4:
                cout<<A.min();
                break;
            case 5:
                A.xuat();
                break;
            case 0:
                cout<<"BYE"<<endl;
                break;
            default:
                cout<<"Chon Sai"<<endl;
        }    
    }while(chon != 0);
//    cout<<endl;
    system("pause");
}
[/AH]
Hôm nào Taplamhacker hướng dẫn anh viết Code nào :v
 
Sửa lần cuối bởi điều hành viên:

VSupport

Ngây thơ trong tối
Thanh niên thích dạy đời chia sẻ :(
Bình thường thôi bạn, cái này gặp mình mình cũng tư vấn vậy. Đáng lẽ các bạn nên suy nghĩ và tự làm, sau đó có lỗi gì thì post lên đây mọi người sẽ tư vấn sửa lỗi. Như thế sẽ nhớ lâu hơn và sau này gặp các bài tương tự có thể dễ dàng làm nhé :gach:
Chứ làm sẵn kiểu này rất dễ quên và có thể không hiểu hết code:troll2:
 

Thống kê

Chủ đề
102,787
Bài viết
470,610
Thành viên
340,593
Thành viên mới nhất
winspire
Top