11/5/2011, 18:08
hoacthiendieu wrote:CÁC BÀI TẬP NÀY LÀ MÌNH LÀM TRONG QUYỂN SÁCH CỦA TRƯỜNG MÌNH
DO THẠC SỸ NGUYỄN MẠNH CƯỜNG BIÊN SOẠN
bài 1:
- Code:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<iomanip.h>
#include<string.h>
class nhasx
{
char tennsx[30];
char dcnsx[30];
public:
void nhap();
void xuat();
friend class may;friend class maytinh;
friend void ttmt(may *a,int n);
};
class may
{
public:
char nh[30];
nhasx nsx;
int gt;
void nhap();
void xuat();
friend void sapxep(may *a,int n);
may(char *x1,char *x2,char *x3,int x4)
{
strcpy(nh,x1);strcpy(nsx.tennsx,x2);strcpy(nsx.dcnsx,x3);gt=x4;
}
may()
{}
};
class maytinh:public may
{
char tocdo[40];
char dlram[40];
char dlhdd[40];
public:
void nhap();
void xuat();
friend void ttmt(may *a,int n);
maytinh(char *x5,char *x6,char *x7,int x8):may(x5,x6,x7,x8)
{
}
maytinh()
{}
};
void nhasx::nhap()
{
cout<<"\n ten nha san xuat: ";
gets(tennsx);fflush(stdin);
cout<<"\n dia chi nha san xuat: ";
gets(dcnsx);fflush(stdin);
}
void nhasx::xuat()
{
cout<<"\n ten nha san xuat: " << tennsx;
cout<<"\n dia chi nha san xuat: " << dcnsx;
}
void may::nhap()
{
cout<<"\n nhan hieu: ";
gets(nh);fflush(stdin);
cout<<"\n gia thanh: ";
cin>>gt;
nsx.nhap();
}
void may::xuat()
{
nsx.xuat();
cout<<"\n nhan hieu: " << nh;
cout<<"\n gia thanh: " <<gt;
}
void maytinh::nhap()
{
may::nhap();
cout<<"\n toc do ram: ";
gets(tocdo);fflush(stdin);
cout<<"\n dung luong hdd: ";
gets(dlhdd);fflush(stdin);
cout<<"\n dung luong ram: ";
gets(dlram);fflush(stdin);
}
void maytinh::xuat()
{
may::xuat();
cout<<"\n toc do ram: " <<tocdo;
cout<<"\n dung luong hdd: " <<dlhdd;
cout<<"\n dung luong ram: " <<dlram;
}
void ttmt(may *a,int n)
{
for(int i=0;i<n;i++)
if(strcmp(a[i].nsx.tennsx,"ibm")==0)
a[i].xuat();
}
void sapxep(may *a,int n)
{
for(int i=0;i<n-1;i++)
for(int j=n-1;j>i;j--)
if(a[j-1].gt>a[j].gt)
{
may tg=a[j];
a[j]=a[j-1];
a[j-1]=tg;
}
}
void main()
{
clrscr();
may *a;int n;
cout<<"\n nhap so luong may tinh n= ";
cin>>n;
for(int i=0;i<n;i++)
a[i].nhap();
sapxep(a,n);
ttmt(a,n);
getch();
}
bai2:
- Code:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class xe
{
public:
int nsx;
int tlg;
public:
virtual void nhap()
{
cout<<"\n nam san xuat: ";
cin>>nsx;
cout<<"\n trong luong xe: ";
cin>>tlg;
}
virtual void xuat()
{
cout<<"\n nam san xuat: " << nsx << "\n trong luong xe: " << tlg;
}
virtual double tinhgt()
{
return (nsx*0.2+tlg);
}
};
class xetai:public xe
{
double trtai;
public:
void nhap()
{
cout<<"\n nam san xuat: ";
cin>>nsx;
cout<<"\n trong luong xe: ";
cin>>tlg;
cout<<"\n trong tai xe: ";
cin>>trtai;
}
void xuat()
{
cout<<"\n nam san xuat: " << nsx << "\n trong luong xe: " << tlg;
cout<<"\n trong tai xe: " << trtai;
}
double tinhgt()
{
return (trtai*200);
}
};
void main()
{
clrscr();
xe a, *p;
xetai b;
p=&a;
cout<<"\n thong tin ve xe noi chung: " ;
p->nhap();
p=&b;
cout<<"\n thong tin ve xe tai: " ;
p->nhap();
p=&a;
cout<<"\n gia thanh cua xe noi chung: " <<p->tinhgt();
p=&b;
cout<<"\n gia thanh cua xe tai la: " <<p->tinhgt();
p=&a;
cout<<"\n thong tin ve xe xe noi chung: ";
p->xuat();
p=&b;
cout<<"\n thong tin ve xe tai: " ;
p->xuat();
getch();
}
bài 3:
- Code:
#include<conio.h>
#include<iostream.h>
#include<stdio.h>
class tamthuc
{
float hesoq1;float hesop1;
float hesoq2;float hesop2;
float hesoq3;float hesop3;
public:
tamthuc()
{
}
tamthuc(float a1,float b1,float c1,float a2,float b2,float c2)
{
hesoq1=a1;hesop1=a2;
hesoq2=b1;hesop2=b2;
hesoq3=c1;hesop3=c2;
}
tamthuc operator +(tamthuc P)
{
tamthuc tg;
tg.hesoq1=this -> hesoq1 + P.hesop1;
tg.hesoq2=this -> hesoq2 + P.hesop2;
tg.hesoq3=this -> hesoq3 + P.hesop3;
return tg;
}
void nhap1()
{
cout<<"\n nhap he so 1 cua tam thuc Q: ";
cin>>hesoq1;
cout<<"\n nhap he so 2 cua tam thuc Q: ";
cin>>hesoq2;
cout<<"\n nhap he so 3 cua tam thuc Q: ";
cin>>hesoq3;
}
void nhap2()
{
cout<<"\n nhap he so 1 cua tam thuc P: ";
cin>>hesop1;
cout<<"\n nhap he so 2 cua tam thuc P: ";
cin>>hesop2;
cout<<"\n nhap he so 3 cua tam thuc P: ";
cin>>hesop3;
}
void xuat1()
{
cout<<"\n tam thuc Q la: "<<hesoq1<<"*x2 + "<<hesoq2<<"*x + "<<hesoq3<<endl;
}
void xuat2()
{
cout<<"\n tam thuc P la: "<<hesop1<<"*x2 + "<<hesop2<<"*x + "<<hesop3<<endl;
}
void xuat()
{
cout<<"\n cong hai tam thuc Q va P la: " <<(hesoq1+hesop1)<<"*x2 + "<<(hesoq2+hesop2)<<"*x + "<<(hesoq3+hesop3);
}
};
void main()
{
clrscr();
tamthuc Q;
Q.nhap1();
Q.xuat1();
tamthuc P;
P.nhap2();
P.xuat2();
tamthuc T=Q+P;
T.xuat();
getch();
}
bài 4:
- Code:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class sinhnhat
{
int thang,ngay,nam;
public:
void nhap();
void kiemtra();
int kiemtra2();
void xuat();
};
void sinhnhat::nhap()
{
cout<<"\n nhap thang= ";
cin>>thang;
cout<<"\n nhap ngay= ";
cin>>ngay;
cout<<"\n nhap nam = ";
cin>>nam;
}
void sinhnhat::kiemtra()
{
switch(thang)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if(ngay>31)
cout<<"\n ban nhap khong hop le.vi thang chi co 31 ngay";
else cout<<"\n ban nhap hop le.thang co 31 ngay";
break;
case 4:
case 6:
case 9:
case 11:
if(ngay>30)
cout<<"\n Ban nhap khong hop le.vi thang chi co 30 ngay";
else cout<<"\n ban nhap hop le.thang co 30 ngay";
break;
case 2:
if(ngay<29)
cout<<"\n ban nhap hop le";
else cout<<"\n ban nhap ko hop le";
if(nam%4==0)
cout<<"\n thang co 28 ngay";
else cout<<"\n thang co 29 ngay";
break;
}
}
int sinhnhat::kiemtra2()
{
if(thang==4 && ngay==12 && nam==1990)
{
return 1;
return 0;
}
}
void sinhnhat::xuat()
{
cout<<"\n thang " << thang << " ngay " << ngay << " nam " << nam;
}
void main()
{
clrscr();
sinhnhat a;
a.nhap();
a.kiemtra();
a.kiemtra2();
if(a.kiemtra2()==1)
cout<<"\n chuc mung sinh nhat anh trieu dep trai";
else
cout<<"\n rat tiec do khong phai sinh nhat ban";
a.xuat();
getch();
}
bài 5:
- Code:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class thoi_gian_sinh_nhat
{
int gio,phut,giay;
public:
void nhap()
{
cout<<"\n gio= ";
cin>>gio;
cout<<"\n phut= ";
cin>>phut;
cout<<"\n giay= ";
cin>>giay;
}
void set(int h=0,int p=0,int g=0)/* ham xac lap gio,phut,giay mac dinh la 0 */
{
gio=h;
phut=p;
giay=g;
}
void setgio(int h)
{
gio=h;
}
void setphut(int p)
{
phut= p;
}
void setgiay(int g)
{
giay=g;
}
void hien_thi_24() /* ham hien thi theo 24 tieng */
{
cout<< gio << "gio" << phut << "phut" << giay << "giay" <<endl;
}
void hien_thi_12() /* ham hien thi theo 12 tieng */
{
if(gio>12)
cout<< (gio-12) << " gio" << phut << "phut" << giay << "giay PM" <<endl;
else cout<< gio << "gio" << phut << "phut" << giay << "giay AM" <<endl;
}
void tang_gio() //ham tang gio len 1
{
gio++;
if(gio==24) gio=0;
}
void tang_phut()//ham tang phut len 1
{
phut++;
if(phut==60)
{
phut=0;
tang_gio();
}
}
void tang_giay()//ham tang giay len 1
{
giay++;
if(giay==60)
{
giay=0;
tang_phut();
}
}
};
void main()
{
clrscr();
thoi_gian_sinh_nhat time;
time.nhap1();
time.hien_thi_24();
cout<<" "<<endl;
time.hien_thi_12();
cout<<" "<<endl;
for(int i=1;i<=3600;i++)
{
time.tang_giay();
time.hien_thi_24();
cout<<" "<<endl;
time.hien_thi_12();
cout<<" "<<endl;
}
getch();
}
bài 6:
}
- Code:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<iomanip.h>
#include<string.h>
class nhasx
{
char tennsx[20];char dcnsx[30];
public:
friend class hang;friend class tv;
void input();void output();
friend void xuathang(tv *a,int n);
};
class date
{
int d,m,y;
public:
friend class tv;
void input();
void output();
};
class hang
{
public:
char th[30];nhasx nsx;int dg;
void input();void output();
hang(char *x1,char *x2,char *x3,int x4)
{
strcpy(th,x1);strcpy(nsx.tennsx,x2);strcpy(nsx.dcnsx,x3);dg=x4;
}
hang()
{}
};
class tv:public hang
{
int kt;date nn;
public:
void input();void output();
tv(int x1,int x2,int x3,int x4,char *x5,char *x6,char *x7,int x8):hang(x5,x6,x7,x8)
{
kt=x1;nn.d=x2;nn.m=x3;nn.y=x4;
}
tv()
{}
friend void xuathang(tv *a,int n);
};
void nhasx::input()
{
cout<<"\n ten nsx: ";gets(tennsx);fflush(stdin);
cout<<"\n dc nsx: ";gets(dcnsx);fflush(stdin);
}
void nhasx::output()
{
cout<<"\n ten nsx: "<<tennsx;
cout<<"\n dc nsx: "<<dcnsx;
}
void date::input()
{
cout<<"\n d= ";
cin>>d;
cout<<"\n m= ";
cin>>m;
cout<<"\n y= ";
cin>>y;
}
void date::output()
{
cout<<d<<"/"<<m<<"/"<<y;
}
void hang::input()
{
cout<<"\n ten hang ";gets(th);fflush(stdin);
nsx.input();cout<<"dg= ";
cin>>dg;
}
void hang::output()
{
cout<<"\ ten hang: "<<th;
nsx.output();
cout<<"\n dg= ";
}
void tv::input()
{
hang::input();
cout<<"\n kich thuoc: ";
cin>>kt;
nn.input();
}
void tv::output()
{
hang::output();
cout<<"\n kich thuoc: "<<kt;nn.output();
}
void xuathang(tv *a,int n)
{
for(int i=0;i<n;i++)
if(strcmp(a[i].nsx.tennsx,"lg")==0)
a[i].output();
}
void main()
{
clrscr();
tv *a;int n;
cout<<"\n n= ";
cin>>n;
for(int i=0;i<n;i++)
a[i].input();
xuathang(a,n);
getch();
- Code: