Bài tập lập trình nâng cao
Bai1
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class hocsinh
{
char hoten[25];
int lop;
float dtoan,dly,dhoa,tb;
public:
void nhap()
{
cout<<"Ho ten:";
fflush(stdin);
gets(hoten);
cout<<"lop:";cin>>lop;
cout<<"diemtoan:";cin>>dtoan;
cout<<"diem ly:";cin>>dly;
cout<<"diem hoa:";cin>>dhoa;
tb=(dtoan+dly+dhoa)/3;
}
void xuat()
{
cout<<"
hoten:"<<hoten;
cout<<"
lop:"<<lop;
cout<<"
diem toan:"<<dtoan;
cout<<"
diem ly:"<<dly;
cout<<"
diem hoa:"<<dhoa;
cout<<"
diemtrungbinh:"<<tb;
}
void sapxep()
{
if(tb>=5)
{
cout<<"
"<<hoten;
cout<<" "<<lop;
cout<<" "<<dtoan;
cout<<" "<<dly;
cout<<" "<<dhoa;
cout<<" "<<tb;
}
}
};
hocsinh sinhvien[100];
main()
{
int i,n;
cout<<"nhap so sinh vien: ";cin>>n;
for(i=0;i<n;i++)
{
sinhvien[i].nhap();
}
for(i=0;i<n;i++)
{
sinhvien[i].xuat();
cout<<"
";
}
cout<<"
nhung sinh vien co diem trung binh >=5";
cout<<"
Hoten Lop Diem toan Diem ly Diem hoa Diem tb";
for(i=0;i<n;i++)
{
sinhvien[i].sapxep();
}
getch();
}
_______________________________________
Bài 2:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class thucpham
{
char tenthucpham[25];
float giaca;
public:
void nhap()
{
cout<<"
ten thuc pham:";
fflush(stdin);
gets(tenthucpham);
cout<<"gia ca: ";cin>>giaca;
}
void xuat()
{
cout<<"
Ten thuc pham: "<<tenthucpham;
cout<<"
Gia ca: "<<giaca;
}
};
class tphop:public thucpham
{
int sohop;
public:
void nhap()
{
thucpham::nhap();
cout<<"so hop: ";cin>>sohop;
}
void xuat()
{
thucpham::xuat();
cout<<"
So hop:"<<sohop;
cout<<"
";
}
};
class tpkhop:public thucpham
{
float khoiluong;
public:
void nhap()
{
thucpham::nhap();
cout<<"khoi luong: ";cin>>khoiluong;
}
void xuat()
{
thucpham::xuat();
cout<<"
Khoi luong: "<<khoiluong;
}
};
tphop tp[100]; // Bien toan cuc
tpkhop t[100]; // Bien toan cuc
int main()
{
int i,n;
cout<<"Nhap so luong san pham dong hop:";cin>>n;
for(i=0;i<n;i++)
{
tp[i].nhap();
}
for(i=0;i<n;i++)
{
tp[i].xuat();
}
cout<<"
Nhap so luong san pham khong hop:";cin>>n;
for(i=0;i<n;i++)
{
t[i].nhap();
}
for(i=0;i<n;i++)
{
t[i].xuat();
}
getch();
}
_____________________________________________
Bài 3:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class card
{
char tuadesach[100],tacgia[100];
public:
int soluongsach;
void nhap()
{
cout<<"Tua de sach:";
fflush(stdin);
gets(tuadesach);
cout<<"Tac gia:";
gets(tacgia);
cout<<"So luong sach: ";cin>>soluongsach;
}
void xuat()
{
cout<<"Tua de sach:"<<tuadesach;
cout<<"
Tac gia:"<<tacgia;
cout<<"
So luong sach:"<<soluongsach;
}
};
card s[100];
main()
{
int i,vt,n,max;
cout<<"Nhap so sach:";cin>>n;
for(i=0;i<n;i++)
{
s[i].nhap();
}
max=s[1].soluongsach;
for(i=0;i<n;i++)
{
if(s[i].soluongsach>max)
{
max=s[i].soluongsach;
vt=i;
}
}
cout<<"Cuon sach co so luong lon nhat la:"<<max;
s[vt].xuat();
getch();
}
_________________________________________
Bài 4:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class date
{
int ngay,thang,nam;
public:
void nhap()
{
cout<<"
Ngay:";cin>>ngay;
cout<<"Thang:";cin>>thang;
cout<<"Nam: ";cin>>nam;
}
void xuat()
{
cout<<" "<<ngay<<"/"<<thang<<"/"<<nam;
}
};
class nhansu:public date
{
int cmnd;
public:
char ten[25];
void nhap()
{
cout<<"Ten:";
fflush(stdin);
cout<<"ngay sinh";
date::nhap();
cout<<"So chung minh nhan dan:";cin>>cmnd;
}
void xuat()
{
cout<<"
"<<" "<<ten<<" ";
date::xuat();
cout<<" "<<cmnd<<"";
}
};
nhansu nv[100];;
int main()
{
int n,i,j;
nhansu tg;
cout<<"Nhap so nhan su:";cin>>n;
for(i=0;i<n;i++)
{
nv[i].nhap();
}
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(nv[i].ten<nv[j].ten)
{
tg=nv[i];
nv[i]=nv[j];
nv[j]=tg;
}
cout<<"
Day sap xep tang dan:
";
cout<<"----------ten----------|"<<"------ngaysinh-------"<<"|-------so cmnd-----------";
for(i=0;i<n;i++)
{
nv[i].xuat();
}
getch();
}
____________________________________________
Bài 5
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class ps
{
int tu,mau;
public:
friend ostream & operator<<(ostream&,ps);
friend istream & operator>>(istream&,ps&);
ps operator + (ps);
ps operator - (ps);
};
ostream & operator<<(ostream & os,ps u)
{
os<<u.tu<<"/"<<u.mau<<endl;
return os;
}
istream & operator>>(istream & is,ps & u)
{
cout<<"phan tu:";is>>u.tu;
cout<<"phan mau:";is>>u.mau;
return is;
}
ps ps::operator +(ps v)
{
ps w;
w.tu=tu*v.mau+v.tu*mau;
w.mau=mau*v.mau;
return w;
}
ps ps::operator-(ps h)
{
g.tu=tu*h.mau-h.tu*mau;
g.mau=mau*h.mau;
return g;
}
int main()
{
ps a,b,c,d;
cin>>a;cout<<a;
cin>>b;cout<<b;
c=a+b;d=a-b;
cout<<"
Ket qua phep cong la:"<<c;
cout<<"
Ket qua phep tru la:"<<d;
getch();
}
_______________________________________
Bài 6:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class xe
{
char nhanhieu[100];
float gia;
int namsanxuat;
public:
void nhap()
{
cout<<"nhan hieu:";
fflush(stdin);
gets(nhanhieu);
cout<<"gia:";cin>>gia;
cout<<"nam san xuat: ";cin>>namsanxuat;
}
void xuat()
{
cout<<"
Nhan hieu:"<<nhanhieu;
cout<<"
Gia:"<<gia;
cout<<"
Nam san xuat:"<<namsanxuat;
}
};
class oto:public xe
{
int socho;
float trongtai;
public:
void nhap()
{
cout<<"so cho:";cin>>socho;
cout<<"trong tai:";cin>>trongtai;
xe::nhap();
}
void xuat()
{
cout<<"
So cho:"<<socho;
cout<<"
Trong tai:"<<trongtai;
xe::xuat();
}
};
xe x[100];
main()
{
int i,n;
cout<<"Nhap so o to:";cin>>n;
for(i=0;i<n;i++)
{
x[i].nhap();
}
cout<<"
Danh sach o to vua nhap:";
for(i=0;i<n;i++)
{
x[i].xuat();
}
getch();
}
_________________________________________
Bài 7:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class cdcanhac
{
char tendia[25];
float giatien;
public:
int soluongbai;
void nhap()
{
cout<<"Ten dia:";
fflush(stdin);
gets(tendia);
cout<<"So luong bai";cin>>soluongbai;
cout<<"Gia tien";cin>>giatien;
}
void xuat()
{
cout<<"
Ten dia:"<<tendia;
cout<<"
So luong bai:"<<soluongbai;
cout<<"
Gia tien:"<<giatien;
}
};
cdcanhac cd[100];
main()
{
int i,vt,n,max;
cout<<"Nhap so CD ca nhac:";cin>>n;
for(i=0;i<n;i++)
{
cd[i].nhap();
}
max=cd[1].soluongbai;
for(i=0;i<n;i++)
{
if(cd[i].soluongbai>max)
{
max=cd[i].soluongbai; vt=i;
}
}
cout<<"
CD co so luong bai lon nhat la:";
cd[vt].xuat();
getch();
}
_______________________________
Bài 8:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class vecto
{
int x,y;
public:
friend ostream & operator<<(ostream&,vecto);
friend istream & operator>>(istream&,vecto &);
vecto operator +(vecto);
vecto operator -(vecto);
};
ostream & operator<<(ostream& os,vecto u)
{
os<<"("<<u.x<<","<<u.y<<")"<<endl;
return os;
}
istream & operator>>(istream& is,vecto &u)
{
cout<<"nhap x:";is>>u.x;
cout<<"nhap y:";is>>u.y;
return is;
}
vecto vecto::operator +(vecto v)
{
vecto w;
w.x=x+v.x;
w.y=y+v.y;
return w;
}
vecto vecto::operator -(vecto h)
{
vecto t;
t.x=this->x-h.x;
t.y=this->y-h.y;
return t;
}
int main()
{
vecto a,b,c,d;
cin>>a;cout<<a;
cin>>b;cout<<b;
c=a+b;d=a-b;
cout<<"Ket qua phep cong la:"<<c;
cout<<"
Ket qua phep tru la:"<<d;
getch();
}
________________________________
Bài 9:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class date
{
int ngay,thang,nam;
public:
void nhap()
{
cout<<"Ngay:";cin>>ngay;
cout<<"Thang:";cin>>thang;
cout<<"Nam: ";cin>>nam;
}
void xuat()
{
cout<<" "<<ngay<<"/"<<thang<<"/"<<nam;
}
};
class phieunhap:public date
{
char tenhang[25];
public:
int ngaynhap,soluong;
void nhap()
{
cout<<"Ten hang:";
fflush(stdin);
gets(tenhang);
cout<<"So luong:";cin>>soluong;
date::nhap();
}
void xuat()
{
cout<<"
"<<tenhang;
cout<<" "<<soluong;
date::xuat();
}
};
phieunhap ph[100];
main()
{
int i,j,n;//nv[100];
cout<<"Nhap so phieu nhap:";cin>>n;
for(i=0;i<n;i++)
{
ph[i].nhap();
}
cout<<"
Thong tin phieu nhap hang co so luong >100 la";
cout<<"
+-----------+--------------+-------------+";
cout<<"
Ten hang | So luong | Ngay nhap";
cout<<"
+-----------+--------------+-------------+";
for(i=0;i<n;i++)
{
if(ph[i].soluong>100)
{
ph[i].xuat();
}
cout<<"
+-----------+--------------+-------------+";
}
getch();
}
_____________________________________________
Bài 10:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class person
{
char hoten[25],gioitinh[10];
int ngay,thang,nam;
public:
void nhap()
{
cout<<"Ho ten:";
fflush(stdin);
gets(hoten);
cout<<"Gioi tinh:";
gets(gioitinh);
cout<<"Ngay:";cin>>ngay;
cout<<"Thang:";cin>>thang;
cout<<"Nam: ";cin>>nam;
}
void xuat()
{
cout<<"
"<<hoten;
cout<<" "<<gioitinh;
cout<<" "<<ngay<<"/"<<thang<<"/"<<nam;
}
};
class student:public person
{
public:
int lop;
float diemthi;
void nhap()
{
person::nhap();
cout<<"lop";cin>>lop;
cout<<"diemthi";cin>>diemthi;
}
void xuat()
{
person::xuat();
cout<<" "<<lop;
cout<<" "<<diemthi;
}
};
student sv[100];
main()
{
int i,n;
cout<<"Nhap so sinh vien:";cin>>n;
for(i=0;i<n;i++)
{
sv[i].nhap();
}
cout<<"
Danh sach sinh vien vua nhap la:";
cout<<"
+-------+--------------+----------------+---------+-------------+";
cout<<"
Ho ten | Gioi tinh | Ngay sinh | Lop | Diem thi";
cout<<"
+-------+--------------+----------------+---------+-------------+";
for(i=0;i<n;i++)
{
sv[i].xuat();
cout<<"
+-------+--------------+----------------+---------+-------------+";
}
getch();
}
_________________________________
Bạn đang đọc truyện trên: TruyenTop.Vip