Câu 1:
void swap(int *a,int *b)
{ int c;
c=*a;
*a=*b;
*b=c;
}
void main()
{
clrscr();
int a,b,i,j,n;
int d[50];
cout<<"\nso phan tu cua day la n=";
cin>>n;
for(i=1;i<=n;i++)
{
cout<<"nhap so d["<<i<<"]=";
cin>>d[i];
}
cout<<"day duoc sap sep tang dan :\n";
for(i=1;i<=n;i++)
for(j=i+1;j<n+1;j++)
if(d[i]>d[j])
swap(&d[i],&d[j]);
for(i=1;i<=n;i++)
{
cout<<d[i]<<"\t";
}
cout<<"\nday duoc sap sep giam dan :\n";
for(i=1;i<=n;i++)
for(j=i+1;j<n+1;j++)
if(d[i]<d[j])
swap(&d[i],&d[j]);
for(i=1;i<=n;i++)
{
cout<<d[i]<<"\t";
}
getch();
}
22 trang |
Chia sẻ: luyenbuizn | Lượt xem: 1380 | Lượt tải: 0
Bạn đang xem trước 20 trang nội dung tài liệu Báo cáo Bài thực hành - Môn học: Lập trình hướng đối tượng C++, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
Giáo viên hướng dẫn : Nguyễn Đức Hiển
Sinh viên thực hiện : Nguyễn Tiến Hoài Nam
Lớp : 06I
Câu 1:
void swap(int *a,int *b)
{ int c;
c=*a;
*a=*b;
*b=c;
}
void main()
{
clrscr();
int a,b,i,j,n;
int d[50];
cout<<"\nso phan tu cua day la n=";
cin>>n;
for(i=1;i<=n;i++)
{
cout<<"nhap so d["<<i<<"]=";
cin>>d[i];
}
cout<<"day duoc sap sep tang dan :\n";
for(i=1;i<=n;i++)
for(j=i+1;j<n+1;j++)
if(d[i]>d[j])
swap(&d[i],&d[j]);
for(i=1;i<=n;i++)
{
cout<<d[i]<<"\t";
}
cout<<"\nday duoc sap sep giam dan :\n";
for(i=1;i<=n;i++)
for(j=i+1;j<n+1;j++)
if(d[i]<d[j])
swap(&d[i],&d[j]);
for(i=1;i<=n;i++)
{
cout<<d[i]<<"\t";
}
getch();
}
Câu 2:
struct PhanSo
{
int TuSo;
int MauSo; };
PhanSo operator + (PhanSo a ,PhanSo b);
PhanSo operator - (PhanSo a ,PhanSo b);
PhanSo operator * (PhanSo a ,PhanSo b);
PhanSo operator / (PhanSo a ,PhanSo b);
int operator > (PhanSo a ,PhanSo b);
int operator >=(PhanSo a ,PhanSo b);
int operator < (PhanSo a ,PhanSo b);
int operator <=(PhanSo a ,PhanSo b);
int operator ==(PhanSo a ,PhanSo b);
int operator !=(PhanSo a ,PhanSo b);
PhanSo RutGonPS (PhanSo a);
}
void main()
{
clrscr();
PhanSo a,b,c;
cout>a.TuSo;
cout>a.MauSo;
cout>b.TuSo;
cout>b.MauSo;
cout<<"\na="<<a.TuSo<<"/"<<a.MauSo;
cout<<"\nb="<<b.TuSo<<"/"<<b.MauSo;
c=a+b;
c=RutGonPS(c);
cout<<"\na+b="<<c.TuSo <<"/"<<c.MauSo;
c=a-b;
c=RutGonPS(c);
cout<<"\na-b="<<c.TuSo <<"/"<<c.MauSo;
c=a*b;
c=RutGonPS(c);
cout<<"\na*b="<<c.TuSo <<"/"<<c.MauSo;
c=a/b;
c=RutGonPS(c);
cout<<"\na/b="<<c.TuSo <<"/"<<c.MauSo;
if (a==b)
cout<<"\nPS a bang PS b";
else cout<<"\nPS b khac PS a";
if (a!=b)
cout<<"\nPS a khac PS b";
else cout<<"\nPS b bang PS a";
if (a>b)
cout<<"\nPS a lon hon PS b";
else if(b>a)
cout<<"\nPS b lon hon PS a";
else cout<<"\na=b";
if (a>=b)
cout<<"\nPS a lon hon hoac bang PS b";
else cout<<"\nPS b lon hon hoac bang PS a";
if (a<b)
cout<<"\nPS a be hon PS b";
else if (b<a)
cout<<"\nPS b be hon PS a";
else cout<<"\nb=a";
if (a<=b)
cout<<"\nPS a be hon hoac bang PS b";
else cout<<"\nPS b be hon hoac bang PS a";
getch();
}
// Dinh Nghia Cac Ham
PhanSo operator + (PhanSo a ,PhanSo b)
{
PhanSo c;
c.TuSo=a.TuSo*b.MauSo+a.MauSo*b.TuSo;
c.MauSo=a.MauSo*b.MauSo;
return c;
}
PhanSo operator - (PhanSo a ,PhanSo b)
{
PhanSo c;
c.TuSo=a.TuSo*b.MauSo-a.MauSo*b.TuSo;
c.MauSo=a.MauSo*b.MauSo;
return c;
}
PhanSo operator * (PhanSo a ,PhanSo b)
{
PhanSo c;
c.TuSo=a.TuSo*b.TuSo;
c.MauSo=a.MauSo*b.MauSo;
return c;
}
PhanSo operator / (PhanSo a ,PhanSo b)
{
PhanSo c;
c.TuSo=a.TuSo*b.MauSo;
c.MauSo=a.MauSo*b.TuSo;
return c;
}
int operator > (PhanSo a ,PhanSo b)
{
if(a.TuSo*b.MauSo>b.TuSo*a.MauSo)
return 1;
else
return 0;
}
int operator >= (PhanSo a ,PhanSo b)
{
if(a.TuSo*b.MauSo>=b.TuSo*a.MauSo)
return 1;
else
return 0;
}
int operator < (PhanSo a ,PhanSo b)
{
if(a.TuSo*b.MauSo<b.TuSo*a.MauSo)
return 1;
else
return 0;
}
int operator <= (PhanSo a ,PhanSo b)
{
if(a.TuSo*b.MauSo<=b.TuSo*a.MauSo)
return 1;
else
return 0;
}
int operator == (PhanSo a ,PhanSo b)
{
if(a.TuSo*b.MauSo==b.TuSo*a.MauSo)
return 1;
else
return 0;
}
int operator != (PhanSo a ,PhanSo b)
{
if(a.TuSo*b.MauSo!=b.TuSo*a.MauSo)
return 1;
else
return 0;
}
PhanSo RutGonPS (PhanSo a)
{
int UCLN;
int n=abs(a.TuSo),m=abs(a.MauSo);
while (n != 0 && m != 0)
if (n>m)
n -= m;
else
m -= n;
if (n != 0)
UCLN= n;
else
UCLN= m;
a.TuSo=a.TuSo/UCLN;
a.MauSo=a.MauSo/UCLN;
return a;
}
Câu 3:
struct sophuc
{
double thuc;
double ao;
};
sophuc lapsophuc()
{
int x,y;
sophuc tam;
cout<<"\nthuc:"<<endl;
cin>>x;
tam.thuc=x;
cout<<"\nao:"<<endl;
cin>>y;
tam.ao=y;
return tam;
}
void display(sophuc a)
{
cout<<"("<<a.thuc<<","<<a.ao<<")"<<endl;
}
sophuc operator + (sophuc a,sophuc b)
{
sophuc tam;
tam.thuc=a.thuc+b.thuc;
tam.ao=a.ao+b.ao;
return tam;
}
sophuc operator - (sophuc a,sophuc b)
{
sophuc tam;
tam.thuc=a.thuc-b.thuc;
tam.ao=a.ao-b.ao;
return tam;
}
sophuc operator * (sophuc a,sophuc b)
{
sophuc tam;
tam.thuc= (a.thuc*b.thuc)-(a.ao*b.ao);
tam.ao= (a.thuc*b.ao)+(b.thuc*a.ao);
return tam;
}
sophuc operator / (sophuc a,sophuc b)
{
sophuc tam;
if((b.thuc!=0.0)&&(b.ao!=0.0))
{ tam.thuc= ((a.thuc*b.thuc)+(a.ao*b.ao))/((b.thuc*b.thuc)+(b.ao*b.ao));
tam.ao= ((b.thuc*a.ao)-(a.thuc*b.ao))/((b.thuc*b.thuc)+(b.ao*b.ao));
}
return tam;
}
int operator == (sophuc a,sophuc b)
{
return ((a.thuc==b.thuc)&&(a.ao==b.ao));
}
int operator != (sophuc a,sophuc b)
{
return ((a.thuc!=b.thuc)||(a.ao!=b.ao));
}
void main()
{
clrscr();
sophuc x,y,z,t,s,r;
cout<<"\nso phuc x:";
x=lapsophuc();
cout<<"\nx";
display(x);
cout<<"\nso phuc y:";
y=lapsophuc();
cout<<"\ny";
display(y);
z=x+y;
cout<<"\ncong hai so phuc:";
display(z);
t=x-y;
cout<<"\nhieu hai so phuc:";
display(t);
s=x*y;
cout<<"\ntich hai so phuc:";
display(s);
r=x/y;
cout<<"\nthuong hai so phuc:";
display(r);
if(x==y)
{ cout<<"\nhai so phuc bang nhau";}
else
{ cout<< "\nhai so phuc khong bang nhau"; }
if(x!=y)
{ cout<<"\nhai so phuc khac nhau";}
else
{ cout<<"\nhai so phuc khong khac nhau";}
getch();
}
Câu 4:
class sinhvien
{
public:
char *ten;
float toan , ly ,hoa ,dtb;
void nhap();
void xuat();
friend void hoanvi(float *a,float *b);
};
void sinhvien::nhap()
{
cout<<"vui long nhap cac thong tin sau :\n";
cout<<"ten sinh vien :";
cin>>ten;
cout<<"\ndiem toan :";
cin>>toan;
cout<<"\ndiem ly :";
cin>>ly;
cout<<"\ndiem hoa :";
cin>>hoa;
dtb=(toan+ly+hoa)/3;
}
void sinhvien::xuat()
{
cout<<"\nten sinh vien :"<<ten;
cout<<"\ndiem trung binh :"<<dtb<<"\n";
}
void main()
{
clrscr();
int i,j;
sinhvien a[10];
for(i=0;i<=9;i++) //nhap thong tin 10 sinh vien
{
a[i].nhap();
}
for(i=0;i<=9;i++) //sap xep theo diem trung binh giam dan
for(j=i+1;j<=10;j++)
if((a[i].dtb)<(a[j].dtb))
{
float c;
char *tam;
c=a[i].dtb;
tam=a[i].ten;
a[i].dtb=a[j].dtb;
a[i].ten=a[j].ten;
a[j].dtb=c;
a[j].ten=tam;
}
cout<<"3 SV co diem trung binh cao nhat la : \n";
for(i=0;i<=2;i++) //in ra man hinh 3 sinh vien co dtb cao nhat
a[i].xuat();
getch();
}
Câu 5:
class hcnhat
{
private:
int chdai,chrong;
public:
void nhap();
int chuvi();
int dientich();
void xuat();
};
void hcnhat::nhap()
{
cout<<"nhap chieu dai :";
cin>>chdai;
cout<<"nhap chieu rong :";
cin>>chrong;
}
int hcnhat::chuvi()
{
return (chdai+chrong)*2;
}
int hcnhat::dientich()
{
return chdai*chrong;
}
void hcnhat::xuat()
{
cout<<"chieu dai la :"<<chdai;
cout<<"\nchieu rong la :"<<chrong;
cout<<"\nchu vi hcn la :"<<chuvi();
cout<<"\ndien tich hcn la :"<<dientich();
}
void main()
{
clrscr();
hcnhat a;
a.nhap();
a.xuat();
getch();
}
Câu 6:
class phanso
{
private:
int tuso, mauso;
public:
void nhap();
friend phanso rutgon(phanso p);
friend phanso cong(phanso p1,phanso p2);
friend phanso tru(phanso p1, phanso p2);
friend phanso nhan(phanso p1,phanso p2);
friend phanso chia(phanso p1,phanso p2);
void xuat();
};
void phanso::nhap()
{
phanso p1,p2;
cout<<"nhap tu so cua ps a :";
cin>>p1.tuso;
cout<<"\nnhap mau so cua ps a :";
cin>>p1.mauso;
cout<<"\nnhap tu so cua ps b :";
cin>>p2.tuso;
cout<<"\nnhap mau so cua ps b :";
cin>>p2.mauso;
}
int ucln(int x,int y)
{
x=abs(x);y=abs(y);
if(x*y==0)
return 1;
while(x!=y)
if(x>y)
x-=y;
else
y-=x;
return x;
}
phanso rutgon(phanso p)
{
phanso q;
int x;
x=ucln(p.tuso,p.mauso);
q.tuso=p.tuso/x;
q.mauso=p.mauso/x;
return q;
}
phanso cong(phanso p1,phanso p2)
{
p1.tuso=p1.tuso*(p2.mauso) + p2.tuso*(p1.mauso);
p1.mauso=p1.mauso*p2.mauso;
return rutgon(p1);
}
phanso tru(phanso p1,phanso p2)
{
p1.tuso=p1.tuso*p2.mauso - p2.tuso*p1.mauso;
p1.mauso=p1.mauso*p2.mauso;
return rutgon(p1);
}
phanso nhan(phanso p1,phanso p2)
{
p1.tuso=p1.tuso*p2.tuso;
p1.mauso=p1.mauso*p2.mauso;
return rutgon(p1);
}
phanso chia(phanso p1,phanso p2)
{
p1.tuso=p1.tuso*p2.mauso;
p1.mauso=p1.mauso*p2.tuso;
return rutgon(p1);
}
void phanso::xuat()
{
phanso p1,p2,c,t,n,ch;
c=cong(p1,p2);
t=tru(p1,p2);
n=nhan(p1,p2);
ch=chia(p1,p2);
cout<<"\na+b="<<c.tuso<<"/"<<c.mauso;
cout<<"\na-b="<<t.tuso<<"/"<<t.mauso;
cout<<"\na*b="<<n.tuso<<"/"<<n.mauso;
cout<<"\na : b="<<ch.tuso<<"/"<<ch.mauso;
}
void main()
{
clrscr();
phanso a;
a.nhap();
a.xuat();
getch();
}
Câu 7:
class sophuc
{
private:
double thuc;
double ao;
public:
void nhap()
{
cout<<"\nthuc=";
cin>>thuc;
cout<<"\nao=";
cin>>ao;
}
void in(sophuc a)
{
cout<<"("<<a.thuc<<","<<a.ao<<")"<<endl;
}
sophuc operator + ( sophuc a)
{
sophuc tam;
tam.thuc=this->thuc+a.thuc;
tam.ao =this->ao+a.ao;
return tam;
}
sophuc operator - ( sophuc a)
{
sophuc tam;
tam.thuc=this->thuc-a.thuc;
tam.ao =this->ao-a.ao;
return tam;
}
sophuc operator * ( sophuc a)
{
sophuc tam;
tam.thuc= (this->thuc*a.thuc)-(this->ao*a.ao);
tam.ao= (this->thuc*a.ao)+(a.thuc*this->ao);
return tam;
}
sophuc operator / ( sophuc a)
{
sophuc tam;
if((a.thuc!=0.0)&&(a.ao!=0.0))
tam.thuc= ((this->thuc*a.thuc)+(this->ao*a.ao))/((a.thuc*a.thuc)+(a.ao*a.ao));
tam.ao= ((a.thuc*this->ao)-(this->thuc*a.ao))/((a.thuc*a.thuc)+(a.ao*a.ao));
return tam;
}
int operator == (sophuc a)
{
return ((this->thuc==a.thuc)&&(this->ao==a.ao));
}
int operator != (sophuc a)
{
return ((this->thuc!=a.thuc)||(this->ao!=a.ao));
}
};
void main()
{
clrscr();
sophuc a,b,c;
cout<<"\nsophuc a";
a.nhap();
cout<<"\nsophuc b";
b.nhap();
c=a+b;
cout<<"\na+b=" ; c.in(c);
c=a-b;
cout<<"\na-b="; c.in(c);
c=a*b;
cout<<"\na*b="; c.in(c);
c=a/b;
cout<<"\na/b="; c.in(c);
if(a==b)
cout<<"\nhai so phuc bang nhau";
else
cout<<"\nhai so phuc khong bang nhau";
if(a!=b)
cout<<"\nhai so phuc khac nhau";
else
cout<<"\nhai so phuc khong khac nhau";
getch();
}
Câu 8:
struct matran
{
double mt[20][20];
int n;
};
ostream& operator <<(ostream& os, const matran& x)
{
os<<setprecision(2)<<setiosflags(ios::showpoint);
for(int i=1;i<=x.n;++i)
{
os<<"\n";
for(int j=1;j<=x.n;++j)
os<<setw(6)<<x.mt[i][j];
}
os<<"\n";
return os;
}
istream& operator>>(istream& is,matran& x)
{
cout<<"cap ma tran vuong:";
is>>x.n;
cout<<"nhap cac phan tu :\n";
for(int i=1;i<=x.n;++i)
for(int j=1;j<=x.n;++j)
{
cout<<"a["<<i<<"]["<<j<<"]=";
is>>x.mt[i][j];
}
return is;
}
matran operator +(matran a, matran b)
{ matran c;
for(int i=1;i<=a.n;++i)
for(int j= 1;j<=a.n;++j)
c.mt[i][j]= a.mt[i][j] + b.mt[i][j];
return c;
}
matran operator -(matran a,matran b)
{ matran c;
int i,j;
if(a.n!=b.n)
{
cout<<"\nkhong thuc hien duoc phep tru vi 2 MT khong cung cap";
getch();
return a;
}
else
for(i=1;i<=a.n;++i)
for(j= 1;j<=a.n;++j)
c.mt[i][j]= a.mt[i][j] - b.mt[i][j];
return c;
}
matran operator *(matran a, matran b)
{
if(a.n!=b.n)
{
cout<<"\nkhong thuc hien duoc phep nhan vi 2 MT khong cung cap";
getch();
return a;
}
else
{
matran c;
int i,j,k;
for(i=1;i<=a.n;++i)
for(j=1;j<=a.n;++j)
{
c.mt[i][j]=0.0;
for(k=1;k<=a.n;++k)
c.mt[i][j]+=a.mt[i][k]*b.mt[k][i];
}
return c;
}
}
void main()
{
clrscr();
matran a,b,c,d,e;
int n,i,j;
cout>a;
cout>b;
c = a+b;
cout<<"\nket qua A + B :";
for(i=1;i<=a.n;++i)
for(j=1;j<=a.n;++j)
cout<<"\tc["<<i<<"]["<<j<<"]="<<c.mt[i][j];
d=a-b;
cout<<"\nket qua A - B :";
for(i=1;i<=a.n;++i)
for(j=1;j<=a.n;++j)
cout<<"\td["<<i<<"]["<<j<<"]="<<d.mt[i][j];
e=a*b;
cout<<"\nket qua A * B :";
for(i=1;i<=a.n;++i)
for(j=1;j<=a.n;++j)
cout<<"\te["<<i<<"]["<<j<<"]="<<e.mt[i][j];
getch();
}
Câu 9:
class point
{
private:
int x,y; // hoanh do va tung do
public:
void nhap()
{
cout<< "\nhoanh do va tung do cua diem:" ;
cin>>x>>y;
}
void xuat()
{
cout<<x<<","<<y;
}
double do_dai(point n)
{
return sqrt(pow(x-n.x,2)+pow(y-n.y,2));
}
};
void main()
{
clrscr();
point a,b;
double c;
cout<<"\ntoa do diem a :";
a.nhap();
cout<<"\ndiem a:";
a.xuat();
cout<<"\ntoa do diem b:";
b.nhap();
cout<<"\ndiem b:";
a.xuat();
c= a.do_dai(b);
cout<<" \ndo dai giua hai diem:";
cout<<c;
getch();
}
Câu 10:
class DIEM
{
int x,y;
public :
DIEM()
{
x=y=0;
}
DIEM(int x1,int y1)
{
x=x1;y=y1;
}
DIEM(DIEM &d)
{
this->x=d.x;
this->y=d.y;
}
int operator[](int i)
{
if(i==1)return x;
else return y;
}
};
class DUONG_TRON:public DIEM
{
int r,md;
public:
DUONG_TRON():DIEM()
{
r=md=0;
}
DUONG_TRON(DIEM d,int r1, int md1):DIEM(d)
{
r=r1;md=md1;
}
void ve()
{
setcolor(md);
circle((*this)[1],(this)[2],r);
}
int getmd()
{
return md;
}
};
void ktdh()
{
int mh=0,mode=0;
initgraph(&mh,&mode,"");
}
void main()
{
ktdh();
DUONG_TRON dt(DIEM(100,100),80,MAGENTA);
dt.ve();
getch();
closegraph();
}
Câu 11:
class DIEM
{
private :
double x,y;
public :
DIEM()
{
x=y=0.0;
}
DIEM(double x1,double y1)
{
x=x1;y=y1;
}
void in()
{
cout<<"\nx="<<x<<" y="<<y;
}
};
class HINH_TRON : public DIEM
{
private :
double r;
public :
HINH_TRON()
{
r=0.0;
}
HINH_TRON(double x1,double y1,double r1):DIEM(x1,y1)
{
r=r1;
}
double getR()
{
return r;
}
};
void main()
{
HINH_TRON h(2.5,3.5,8);
clrscr();
cout<<"\nHinh tron co tam :";
h.in();
cout<<"\nCo ban kinh ="<<h.getR();
getch();
}
Câu 12:
class nguoi
{
private:
char *ten;
char *diachi;
int ns; // nam sinh
public:
nguoi()
{
ten=NULL;
diachi=NULL;
ns=0;
}
nguoi(char *ten1, char *diachi1, int ns1)
{
int n=strlen(ten1);
ten =new char[n+1];
strcpy(ten,ten1);
int m=strlen(diachi1);
diachi =new char[m+1];
strcpy(diachi,diachi1);
ns=ns1;
}
~nguoi()
{
if((ten!=NULL)&&(diachi!=NULL))
{
delete ten;
delete diachi;
ns=0;
}
}
void xuat()
{
cout<<"\nho va ten:"<<ten<<"\nnam sinh:"<<ns<<"\ndiachi:"<<diachi;
}
};
class mon_hoc
{
private:
char*mon;
int sotiet;
public:
mon_hoc()
{
mon=NULL;
sotiet=0;
}
mon_hoc(char*mon1,int sotiet1)
{
int n=strlen(mon1);
mon=new char[n+1];
strcpy(mon,mon1);
sotiet=sotiet1;
}
~mon_hoc()
{
if(mon!=NULL)
delete mon;
sotiet=0;
}
void xuat()
{
cout<<"\nTen mon hoc:"<<mon<<"\nSo tiet hoc:"<<sotiet;
}
};
class giao_vien:public nguoi
{
private:
mon_hoc mh;
public:
giao_vien():nguoi(),mh()
{}
giao_vien(char*ten1,char*diachi1,int ns1,char*mon1,int sotiet1):nguoi(ten1,diachi1,ns1),mh(mon1,sotiet1)
{}
~giao_vien()
{}
void xuat()
{
nguoi::xuat() ;
mh.xuat();
}
};
void main()
{
clrscr();
giao_vien a; // goi toi ham tao khong doi
giao_vien *b; //goi toi ham tao co doi
b= new giao_vien("nhu quynh","da nang",1988,"tin",60);
cout<<"\ngoi ham xuat tu lop giao vien";
b->xuat();
cout<<"\ngoi ham xuat tu lop nguoi";
b->nguoi::xuat();
getch();
delete b;
getch();
}
_____♠The End♠______
Các file đính kèm theo tài liệu này:
- bao_cao_c__9245.doc