Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
48 views

Code

Uploaded by

Lâm Phạm
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Code

Uploaded by

Lâm Phạm
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 54

Bi 1://nhap vao nghe danh ca si, liet ke cac tennhacsi ma ca si do da hat nhung bai hat cua nhac si do nhap

vao ten nhac si, cho biet ca si nao da hat nhieu bai hat cua nhac si do nhat #include<iostream.h> #include<conio.h> #include<stdio.h> #include<string.h> #define MAX 100 class nguoi{ protected: char hoten[20],diachi[20],dienthoai[20]; public: void nhap(); void xuat(); }; void nguoi::nhap() { cout<<"\nnhap ho ten: "; fflush(stdin); gets(hoten); cout<<"\nnhap dia chi: "; fflush(stdin); gets(diachi); cout<<"\nnhap vao dien thoai: "; fflush(stdin); gets(dienthoai); } void nguoi::xuat() { printf("%s\t%s\t%s\n",hoten,diachi,dienthoai); } class BaiHat{ char tenbaihat[20],nhacsi[20]; int namsangtac; public: void NhapBaiHat(); void XuatBaiHat(); char* gettenbaihat(){return tenbaihat;}; }; void BaiHat::NhapBaiHat() { cout<<"\nnhap ten bai hat : "; fflush(stdin); gets(tenbaihat); cout<<"\nnhap nhac si: "; fflush(stdin); gets(nhacsi); cout<<"\nnhap vao nam sang tac: "; cin>>namsangtac; } void BaiHat::XuatBaiHat() { printf("%s\t%s\t%d\n",tenbaihat,nhacsi,namsangtac); } class casi:public nguoi{ public: char nghedanh[20]; int sobaihat; BaiHat Arr[MAX]; public: int i; void nhapcasi() { cout<<"Nhap nghe danh: "; fflush(stdin); gets(nghedanh); nhap();

}; class DSCS:public casi { int n,i; casi a[5]; public: void NhapDS() { printf("Nhap so ca si: "); scanf("%d",&n); for(i=0;i<n;i++) { printf("//==========================================\n"); a[i].nhapcasi(); a[i].NhapDSBaiHat(); } } void XuatDS() { for(i=0;i<n;i++) { printf("//===========================================\n"); a[i].xuatcasi(); a[i].XuatDSBaiHat(); } } void ThongTinCaSi(char tenbh[20]) { printf("Nhung ca si da hat bai %s la: ",tenbh); for(i=0;i<n;i++) { for(int j=0;j<a[i].getsobaihat();j++) if(strcmp(a[i].Arr[j].gettenbaihat(),tenbh)==0) { cout<<"\n//===============\n"; cout<<a[i].getnghedanh(); cout<<"\n"; } } cout<<"Khong co bai hat nao nhu vay ";

} void xuatcasi() { printf("%s\t",nghedanh); xuat(); } void NhapDSBaiHat() { cout<<"Nahp so bai hat cua ca si "<<nghedanh<<" la: "; cin>>sobaihat; for(i=0;i<sobaihat;i++) { cout<<"//===================="; Arr[i].NhapBaiHat(); } } void XuatDSBaiHat() { printf("Nhung bai hat cua ca si %s la: \n",nghedanh); for(i=0;i<sobaihat;i++) Arr[i].XuatBaiHat(); } char* getnghedanh(){return nghedanh;}; int getsobaihat() { return sobaihat; }

}; void main() { int chucnang; char b[20],d[20]; casi c; DSCS D; do { cout<<" ***Chon thao tac*** "; cout<<"\n 1. Nhap ca si"; cout<<"\n 2. Xuat ca si"; cout<<"\n 3. Nhap danh sach ca si"; cout<<"\n 4. Xuat danh sach ca si"; cout<<"\n 5. Nhap ten bai hat. Xuat ra thong tin cua ca si da hat bai hat do"; cout<<"\n 6. Nhap ten nhac si. Xuat ra thong tin cua ca si da hat bai hat do"; cout<<"\n 0. Thoat khoi chuong trinh"; cout<<"\n"; scanf("%d",&chucnang); switch(chucnang) { case 1: c.nhapcasi();c.NhapDSBaiHat();break; case 2: c.xuatcasi();c.XuatDSBaiHat();break; case 3: D.NhapDS(); break; case 4: D.XuatDS(); break; case 5: cout<<"Ten bai hat: "; fflush(stdin); gets(b); D.ThongTinCaSi(b); break; case 6: cout<<"Ten nhac si: "; fflush(stdin); gets(d); D.ThongTinNhacSi(d); break; } }while(chucnang!=0); }

} void ThongTinNhacSi(char tenns[20]) { printf("Nhung ca si da hat bai cua nhac si %s la: ",tenns); for(i=0;i<n;i++) { /*for(int j=0;j<a[i].sobaihat;j++) if(strcmp(a[i].Arr[j].gettenbaihat(),tenns)==0) { cout<<"\n//==============="; cout<<a[i].getnghedanh(); cout<<"\n"; } */ } }

Bi 2: Phn s #include<iostream.h> #include<conio.h> #include<stdio.h> class phanso{ int tu, mau; public:

}; class dayphanso{ phanso e[100]; int n; public: void nhapday(); void xuatday(); phanso tongday(); phanso timmax(); phanso timmin(); void sapxep(); }; int uscln(int a, int b) { while(a>0 && b>0) if(a>b)a-=b; else b-=a; return a+b; } void phanso::rutgon() { int us = uscln(tu,mau); tu/=us; mau/=us; } void phanso::nhap() { cout<<"Nhap tu: "; cin>>tu; cout<<"Nhap mau: "; cin>>mau; } void phanso::xuat() { cout<<tu<<"/"<<mau; } phanso phanso::operator+(phanso ps2) { phanso kq; kq.tu=tu*ps2.mau+mau*ps2.tu; kq.mau=mau*ps2.mau; return kq; } bool phanso::operator> (phanso ps2) { int t=tu*ps2.mau; int m=mau*ps2.tu; if(t>m) return true; return false; } bool phanso::operator< (phanso ps2) { int t=tu*ps2.mau; int m=mau*ps2.tu; if(t<m)

void nhap(); void xuat(); void gan(int t, int m) { tu=t; mau=m; } phanso operator+ (phanso ps2); bool operator> (phanso ps2); bool operator< (phanso ps2); void rutgon();

} void dayphanso::nhapday() { cout<<"nhap so pt: "; cin>>n; for(int i=0;i<n;i++) { cout<<"nhap ptu thu "<<i+1<<": \n"; e[i].nhap(); } } void dayphanso::xuatday() { for(int i=0;i<n;i++) { e[i].xuat(); cout<<"\t"; } } phanso dayphanso::tongday() { phanso sum; sum.gan(0,1); for(int i=0;i<n;i++) sum=sum+(e[i]); sum.rutgon(); return sum; } phanso dayphanso::timmax() { phanso max=e[0]; for(int i=1;i<n;i++) if(e[i]>max) max=e[i]; return max; } phanso dayphanso::timmin() { phanso min=e[0]; for(int i=1;i<n;i++) if(e[i]<min) min=e[i]; return min; } void dayphanso::sapxep() { phanso tam; for(int i=0;i<n;i++) for(int j=i+1;j<n;j++) if(e[j]>e[i]) { tam=e[i]; e[i]=e[j]; e[j]=tam; } } void main() { dayphanso D; D.nhapday(); cout<<"\nday vua nhap: "; D.xuatday(); cout<<"\nTong day: "; D.tongday().xuat(); cout<<"\nMax: ";

return true; return false;

D.timmax().xuat(); cout<<"\nMin: "; D.timmin().xuat(); D.sapxep(); cout<<"\nDay sap xep: "; D.xuatday(); }

Bi 3: n thc #include<iostream.h> #include<conio.h> #include<math.h> class donthuc{ float heso; float somu; friend class dathuc; public: donthuc(){heso=0;somu=0;} donthuc(float a,float b); friend istream&operator>>(istream&is,donthuc&a); friend ostream&operator<<(ostream&os,donthuc a); donthuc daoham1(); float tinhx(int x); donthuc operator - (); donthuc operator + (donthuc &b); }; istream&operator>>(istream&is,donthuc&a) { cout<<"\nnhap he so: "; is>>a.heso; cout<<"\nnhap so mu: "; is>>a.somu; return is; } ostream&operator<<(ostream&os,donthuc a) { cout<<a.heso<<"^"<<a.somu; return os; } donthuc donthuc::daoham1() { donthuc kq; kq.heso=heso*somu; kq.somu=somu-1; return kq; }; float donthuc::tinhx(int x) { float kq; kq=heso*(pow(x,somu)); return kq; } donthuc donthuc::operator -() { donthuc kq; kq.heso=-heso; kq.somu=somu; return kq; } donthuc donthuc::operator +(donthuc& b) { donthuc kq; kq.heso=heso+b.heso;

} class dathuc{ int n; donthuc *e; friend class donthuc; public: dathuc(){n=0;} void nhap(); void xuat(); void xoa(int vt); dathuc rutgon(); void them(donthuc x); dathuc operator + (dathuc B); dathuc operator - (dathuc B); dathuc operator * (dathuc B); }; void dathuc::nhap() { cout<<"\nnhap so luong don thuc: "; cin>>n; e=new donthuc [n+1]; for(int i=0;i<n;i++) { cout<<"\nnhap don thuc thu "<<i+1<<" : "; cin>>e[i]; } } void dathuc::xuat() { for(int i=0;i<n;i++) { cout<<e[i]; if(e[i+1].heso>0) cout<<" + "; } } void dathuc::xoa(int vt ) { for(int i=vt;i<n-1;i++) e[i]=e[i+1]; n--; } dathuc dathuc::rutgon() { for(int i=0;i<n-1;i++) for(int j=i+1;j<n;j++) if(e[i].somu==e[j].somu) { e[i]=e[i]+e[j]; xoa(j); j--; } } void dathuc::them(donthuc x) { e[n]=x; n++; } dathuc dathuc::operator + (dathuc B) { dathuc KQ; KQ.n=n+B.n; for(int i =0;i<n;i++) { KQ.them(e[i]);

kq.somu=somu; return kq;

} for(int j=0;j<B.n;j++) { KQ.them(B.e[j]); } KQ.rutgon(); return KQ;

} void main() { clrscr(); /* cout<<"DON THUC!\n"; donthuc a;*/ dathuc A; dathuc B; dathuc C; donthuc x; /* int x; cout<<"\nnhap don thuc!\n" ; cin>>a; cout<<"\ndon thuc da nhap!\n"; cout<<a; cout<<"\ndon thuc sau khi dao ham!\n"; cout<<a.daoham1(); cout<<"\ntinh gia tri tai x!\n"; cout<<"\nnhap gia tri: ";cin>>x; cout<<"\nket qua : "<<a.tinhx( x); cout<<"\ndon thuc sau khi doi dau!\n"; cout<<-a; cout<<"\n\n";*/ cout<<"DA THUC!"; cout<<"\nnhap da thuc !"; A.nhap(); cout<<"\nda thuc da nhap!"; A.xuat(); cout<<"\nnhap da thuc !"; B.nhap(); cout<<"\nda thuc da nhap!"; B.xuat(); cout<<"\nda thuc sau khi rut gon!"; A.rutgon(); A.xuat(); cout<<"\ntong hai da thuc:\n"; (A+B).xuat(); getch(); }

Bi 4: S phc #include<iostream.h> #include<conio.h> #include<math.h> class sophuc{ float a,b; friend class daysophuc; public: sophuc(){a=b=0;}; sophuc(float a, float b); friend istream&operator>>(istream&is,sophuc&z); friend ostream&operator<<(ostream&is,sophuc z); sophuc operator +(sophuc q); sophuc operator -(sophuc q); sophuc operator *(sophuc q);

}; class daysophuc{ int n; sophuc *e; friend class sophuc; public: daysophuc(){n=0;}; friend istream&operator>>(istream&is,daysophuc&z); friend ostream&operator<<(ostream&os,daysophuc z); sophuc tongday(); daysophuc operator *(); }; istream&operator>>(istream&is,sophuc&z) { cout<<"\nnhap phan thuc: "; is>>z.a; cout<<"\nnhap phan ao: "; is>>z.b; return is; } istream&operator>>(istream&is,daysophuc&z) { cout<<"\nnhap so luong : "; is>>z.n; for(int i=0;i<z.n;i++) { cout<<"\nnhap so phuc thu "<<i+1<<" : "; cin>>z.e[i]; } return is; } ostream&operator<<(ostream&os,daysophuc z) { for(int i=0;i<z.n;i++) { cout<<"\nso phuc thu "<<i+1<<" : "; cout<<z.e[i]; } return os; } ostream&operator<<(ostream&os,sophuc z) { cout<<z.a; if(z.b>=0) cout<<" + "<<z.b<<".i"; else cout<<z.b<<".i"; return os; } sophuc sophuc::operator +(sophuc q) { sophuc kq; kq.a=a+q.a; kq.b=b+q.b; return kq; } sophuc daysophuc::tongday() {

sophuc sophuc sophuc sophuc sophuc

operator operator operator operator operator

/(sophuc q); +(float x); -(float x); *(float x); /(float x);

sophuc sum=e[0]; for(int i=0;i<n;i++) { sum=sum+e[i]; } return sum; } sophuc sophuc::operator -(sophuc q) { sophuc kq; kq.a=a-q.a; kq.b=b-q.b; return kq; } sophuc sophuc::operator *(sophuc q) { sophuc kq; kq.a=(a*q.a)+(b*q.b); kq.b=(b*q.a)-(a*q.b); return kq; } sophuc sophuc::operator /(sophuc q) { sophuc kq; kq.a= ((a*q.a)-(b*q.b))/(pow(q.a,2)+pow(q.b,2)); kq.b= ((b*q.a)+(a*q.b))/(pow(q.a,2)+pow(q.b,2)); return kq; } void main() { sophuc z; sophuc q; cin>>z; cout<<z; cin>>q; cout<<q; cout<<"\ntong: "<<z+q; cout<<"\nhieu: "<<z-q; cout<<"\ntich: "<<z*q; cout<<"\nthuong: "<<z/q; daysophuc a; cin>>a; cout<<a; cout<<"\n"<<a.tongday(); }; Bi 5: Tp hp #include <iostream.h> #include <stdio.h> #include <conio.h> class Taphop{ int *e, n, a[100]; public: // Taphop(); //~Taphop(); void Nhap(); void Xuat(); int KTthuoc(int x); Taphop Giao(Taphop B); Taphop Hieu(Taphop B); Taphop Hop(Taphop B);

}; void Khoitao() {

Taphop HieuDX(Taphop B); void Tichdecat(Taphop B);

} void Taphop::Nhap() { int i; cin>>n; for(i=0; i<n; i++) { cout<<"Nhap phan tu thu "<<i+1<<":"; cin>>a[i]; } } void Taphop::Xuat() { int i; for(i=0; i<n; i++) cout<<" "<<a[i]; } int Taphop::KTthuoc(int x) { int i; for(i=0; i<n; i++) if(x==a[i]) return 1; return 0; } Taphop Taphop::Giao(Taphop B) { int i; Taphop C; C.n=0; for(i=0; i<n; i++) if(B.KTthuoc(a[i])==1) C.a[C.n++]=a[i]; return C; } Taphop Taphop::Hieu(Taphop B) { int i; Taphop C; C.n=0; for(i=0; i<n; i++) if(B.KTthuoc(a[i])==0) C.a[C.n++]=a[i]; return C; } Taphop Taphop::Hop(Taphop B) { int i, j; Taphop C; C.n=0; for(i=0; i<n; i++) C.a[C.n++]=a[i]; for(j=0; j<B.n; j++) if(C.KTthuoc(B.a[j])==0) C.a[C.n++]=B.a[j]; return C; } Taphop Taphop::HieuDX(Taphop B) { Taphop C; C=Hop(B).Hieu(Giao(B));

return C; } void main() { clrscr(); Taphop A, B, C, D, E, F; cout<<"Nhap so phan tu mang A: "; A.Nhap(); cout<<"Nhap so phan tu mang B: "; B.Nhap(); cout<<"A: "; A.Xuat(); cout<<"\nB: "; B.Xuat(); cout<<"\nA giao B la: "; C=A.Giao(B); C.Xuat(); cout<<"\nA hieu B la: "; D=A.Hieu(B); D.Xuat(); cout<<"\nA hop B la: "; E=A.Hop(B); E.Xuat(); cout<<"\nHieu doi xung A & B: "; F=A.HieuDX(B); F.Xuat(); getch(); } Bi 6; Bi ht /* bai 1:xay dung class nguoi: - cac thuoc tinh: hoten, diachi, namsinh - cac ham (hoac toan tu): nhap,xuat bai 2:xay dung class baihat: - cac thuoc tinh: tenbaihat, tennhacsi, namsangtac - cac ham (toan tu): nhap,xuat bai 3:xay dung class casi, ke thua class nguoi - them cac thuoc tinh: nghedanh, danh sach cac bai hat da trinh bay - cac ham (toan tu):nhap,xuat,liet ke danh sach bai hat da hat bai 4:xay dung class danhsachcasi, gom cac ham(toan tu): - nhapds - xuatds - liet ke cac bai hat da trinh bay cua tung casi - nhap vao tenbaihat, cho biet thong tin cac casi da hat bai hat do. - nhap vao tennhacsi, cho biet thong tin cac casi da hat nhung bai hat cua nhacsi do - nhap vao nghedanh ca si, liet ke cac tennhacsi ma casi do da hat nhung bai hat cua nhacsi do. - nhap vao tennhacsi, cho biet casi nao da hat nhieu bai hat cua nhac si do nhat. */ #include<iostream.h> #include<stdio.h> #include<conio.h> class nguoi { private: char hoten[10], diachi[20];int namsinh; public: void nhap() { cout<<"nhap hoten:"; fflush(stdin); gets(hoten); cout<<"nhap diachi:";

}; class baihat { private: char char char public: void {

} void xuat() { cout<<hoten<<"\t"<<namsinh<<"\t"<<diachi; }

fflush(stdin); gets(diachi); cout<<"Nhap nam sinh:"; cin>>namsinh;

tenbaihat[50]; tennhacsi [50]; namsangtac[4]; nhapbh() cout<<"\nNhap Ten Bai Hat : "; fflush(stdin); gets(tenbaihat); cout<<"\nNhap Ten Nhac Si : "; fflush(stdin); gets(tennhacsi); cout<<"\nNam Sang Tac : "; fflush(stdin); gets(namsangtac);

}; class casi:public nguoi{ public: char nghedanh[30]; baihat *dsbh; int slbh; void nhap(); void xuat(); void xuatlistbh(); }; void casi::nhap() { nguoi::nhap(); cout<<"\nNhap nghe danh: "; fflush(stdin); gets(nghedanh); cout<<"\nNhap so luong bai hat: "; cin>>slbh; dsbh = new baihat[slbh]; for(int i=0; i<slbh; i++) { cout<<"\nNhap bai hat thu "<<i+1; dsbh[i].nhapbh(); } } void casi::xuat() {

} void xuatbh() { cout<<"\nTen Bai Hat : "<<tenbaihat; cout<<"\nTen Nhac Si : "<<tennhacsi; cout<<"\nNam Sang Tac : "<<namsangtac; } char* getTenbh() { return tenbaihat; } char * gettennhacsi; { return tennhacsi; }

} void casi::xuatlistbh() { for(int i=0; i<slbh; i++) { dsbh[i].xuatbh(); } } class DSCS{ int n; casi a[10]; public: void NhapDScasi() { cout<<"Nhap so ca si"; cin>>n; for(int i=0;i<n;i++) a[i].nhap(); } void XuatDScasi() { for(int i=0;i<n;i++) { a[i].xuat(); cout<<"\n"; } } void LietKe_BaiHat_Casi() { for(int i=0;i<n;i++) { a[i].xuatlistbh(); cout<<"\n"; } } void LietKe_Casi_BaiHat(char tenbh[]) { for(int i=0; i<n; i++) for(int j=0; j<a[i].slbh; j++) if(strcmp(a[i].dsbh[j].getTenbh(),tenbh)==0) cout<<"\n"<<a[i].nghedanh; } void lietke_tennhacsi(char tennhacsi[]) { for(int i=0; i<n; i++) for(int j=0; j<a[i].slbh; j++) if(strcmp(a[i].dsbh[j].getTennhacsi(),tennhacsi)==0) cout<<"\n"<<a[i].nghedanh; } }; int main() { DSCS D; D.NhapDScasi(); D.XuatDScasi(); //D.LietKe_BaiHat_Casi(); char tenbh[30]; cout<<"\n Nhap vao ten bai hat:"; fflush(stdin); gets(tenbh); D.LietKe_Casi_BaiHat(tenbh); }

nguoi::xuat(); cout<<"\nNghe Danh: "<<nghedanh;

Bi 7: Ma trn #include<iostream.h> #include<conio.h> #include<stdio.h> class matran{ int e[20][20]; int d,c; public: void nhap(); void xuat(); matran cong(matran b); matran nhank(int k); matran nhan(matran b); matran luythua(int a); int tongbien(); int tongu(); int tongdong(); int tongcot(); }; void matran::nhap() { cout<<"\nnhap vao so dong: "; cin>>d; cout<<"\nnhap vao so cot: "; cin>>c; for(int i=0;i<d;i++) for(int j=0;j<c;j++) cin>>e[i][j]; } void matran::xuat() { int i,j; for(i=0; i<d; i++) { cout<<"\n"; for(j=0; j<c; j++) cout<<e[i][j]<<" "; } } matran matran::cong(matran b) { matran kq; kq.d=d; kq.c=c; for(int i=0;i<d;i++) for(int j=0;j<c;j++) kq.e[i][j]=e[i][j]+b.e[i][j]; return kq; } matran matran::nhank(int k) { matran kq; kq.d=d; kq.c=c; for(int i=0;i<d;i++) for(int j=0;j<c;j++) kq.e[i][j]=e[i][j]*k; return kq; } matran matran::nhan(matran b) { matran kq; kq.d=d; kq.c=b.c;

} matran matran::luythua(int a) { matran kq; kq=*this; for(int i=1;i<a;i++) kq=kq.nhan(*this); return kq; } int matran::tongbien() { int tong=0; int i,j; for(i=0;i<d;i++) for(j=0;j<c;j++) if(i==0||i==d-1||j==0||j==c-1) tong+=e[i][j]; return tong; } int matran::tongu() { int i,j; int tong=0; for(i=0;i<d;i++) for(j=0;j<c;j++) if(i==d-1||j==0||j==c-1) tong+=e[i][j]; return tong; } int matran::tongdong() { int i,j,a; int tong=0; for(i=0;i<d;i++) for(j=0;j<c;j++) if(a==i) tong+=e[i][j]; return tong; } void main() { int k,key,a,b; matran A,B,C,D,E,F; head: printf("\n1.nhap vao ma tran a: "); printf("\n2.xuat ra ma tran a: "); printf("\n3.nhap vao ma tran b: "); printf("\n4.xuat ra ma tran b: "); printf("\n5.tong matran a+b: "); printf("\n6.ma tran a nhan k: "); printf("\n7.matran a*b: "); printf("\n9.tong bien cua a "); printf("\n0.thoat. "); printf("\n"); scanf("%d",&key); switch(key) { case 1:

for(int i=0;i<d;i++) for(int j=0;j<kq.c;j++) { kq.e[i][j]=0; for(int k=0;k<c;k++) kq.e[i][j]+=e[i][k]*b.e[k][j]; } return kq;

A.nhap(); getch(); goto head; case 2: A.xuat(); getch(); goto head; case 3: B.nhap(); getch(); goto head; case 4: B.xuat(); getch(); goto head; case 5: C=A.cong(B); C.xuat(); getch(); goto head; case 6: cout<<"\nnhap vao k: "; cin>>k; D=A.nhank(k); D.xuat(); getch(); goto head; case 7: cout<<"\nmatran a*b= "; E=A.nhan(B); E.xuat(); getch(); goto head; case 8: cout<<"\nnhap vao so mu: "; cin>>a; F=A.luythua(a); F.xuat(); getch(); goto head; case 9: cout<<A.tongbien(); cout<<A.tongu(); cout<<"\nnhap a "; cin>>a; cout<<A.tongdong(); getch(); goto head; } } Class nhn vin #include #include #include #include "stdafx.h" "conio.h" "windows.h" "iostream"

using namespace std; class nguoi{ public: char name[30]; char diachi[50]; char phone[20];

};

void nhap(); void xuat();

class nhanvien:public nguoi{ int MaNV; int Tongluong; public: nhanvien() { MaNV=0; Tongluong=0; } void nhap(); void xuat(); int getMaNV() { return MaNV; } int getTongluong() { return Tongluong; } }; class DSNV{ nhanvien *info; int sl; public: void nhap(); void xuat(); void search(int key); void search_luong(); void sort_luong(); }; void nguoi::nhap() { cout<<"\nNhap Ten: "; fflush(stdin); gets(name); cout<<"\nNhap Dia Chi: "; fflush(stdin); gets(diachi); cout<<"\nNhap SDT: "; fflush(stdin); gets(phone); } void nguoi::xuat() { cout<<"\nName: "<<name; cout<<"\nDia Chi: "<<diachi; cout<<"\nSDT: "<<phone; } void nhanvien::nhap() { nguoi::nhap(); cout<<"\nNhap MaNV: "; cin>>MaNV; cout<<"\nNhap Tong Luong: "; cin>>Tongluong; } void nhanvien::xuat() { nguoi::xuat(); cout<<"\nMa Nhan Vien: "<<MaNV; cout<<"\nTong Luong: "<<Tongluong; } void DSNV::nhap() { cout<<"\nNhap SL nhan vien : "; cin>>sl;

info = new nhanvien[sl]; for(int i=0; i<sl; i++) { info[i].nhap(); }

void DSNV::xuat() { for(int i=0; i<sl; i++) { cout<<"\n----------------------------"; info[i].xuat(); } } void DSNV::search(int key) { for(int i=0; i<sl; i++) { if(info[i].getMaNV()==key) { info[i].xuat(); break; } } } void DSNV::search_luong() { int max=info[0].getTongluong(); for(int i=1; i<sl; i++) { if(info[i].getTongluong()>max) { max=info[i].getTongluong(); } } for(int i=0; i<sl; i++) { if(info[i].getTongluong()==max) { info[i].xuat(); } } } void DSNV::sort_luong() { nhanvien temp; for(int i=0; i<sl-1; i++) { for(int j=sl-1; j>i; j--) { if(info[j].getTongluong()>info[j-1].getTongluong()) { info[i]=temp; info[i]=info[j]; info[j]=temp; } } } } void main() { DSNV A; int key1; A.nhap();

A.xuat(); cout<<"\nNhap MSNV can tim: "; cin>>key1; A.search(key1); A.search_luong(); cout<<"\n"; A.sort_luong(); cout<<"\n"; A.xuat(); getch(); }

Class phn s bng ton t v friend #include<conio.h> #include <iostream.h> #include <math.h> #include <conio.h> #include <iostream.h> #include <math.h> class PS { private: int tu,mau; public: friend ostream& operator <<(ostream& os, PS p); friend istream& operator >>(istream& is,PS &p); int uscln(int x, int y); PS rutgon(PS p); PS operator +(PS p1); PS operator -(PS p1); PS operator *(PS p1); PS operator /(PS p1); }; ostream& operator<< (ostream& os, PS p) { os<< p.tu << '/' << p.mau ; return os; } istream& operator>> (istream& is,PS &p) { cout<<"\n Nhap tu va mau: " ; is>> p.tu >> p.mau ; return is; } int PS:: uscln(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; } PS PS::rutgon(PS p) { PS q; int x; x=uscln(p.tu,p.mau); q.tu = p.tu / x ; q.mau = p.mau/ x ; return q;

} PS PS ::operator +(PS p1) { tu = tu*p1.mau + p1.tu*mau; mau = p1 .mau * mau ; return rutgon(*this); } PS PS:: operator -(PS p1) { tu = tu*p1.mau - mau*p1.tu; mau = p1.mau * mau ; return rutgon(*this); } PS PS:: operator *(PS p1) { tu = p1.tu * tu ; mau = p1.mau * mau ; return rutgon(*this); } PS PS:: operator /(PS p1) { tu = tu * p1.mau ; mau = mau * p1.tu ; return rutgon(*this); } void main() { PS a,b; int chon; cout<<"\ncac chuc nang:"; cout<<"\n1.Cong 2 phan so"; cout<<"\n2.Tru 2 phan so"; cout<<"\n3.Nhan 2 phan so"; cout<<"\n4.Chia 2 phan so"; cout<<"\nchon chuc nang";cin>>chon; cout<<"\nNhap 2 phan So"; switch (chon) { case 1: { cin>>a; cin>>b; cout<<" Tong cua 2 Phan So la:"<<a+b; break; } case 2: { cin>>a; cin>>b; cout<<" Hieu cua 2 Phan So la:"<<a-b; break; } case 3: { cin>>a; cin>>b; cout<<" Tich cua 2 Phan So la:"<<a*b; break; } case 4: { cin>>a; cin>>b; cout<<" Thuong cua 2 Phan So la:"<<a/b; break; } } getch(); }

Class tp hp dng ton t #include "stdio.h" #include "conio.h" #include "iostream.h" class taphop { private: int n,*x; public: taphop(); taphop(int m); ~taphop(); friend istream& operator>>(istream &inp,taphop &th); friend ostream& operator<<(ostream &out,taphop th); void rutgon(); int ktra(int a); int ktrathuoc(taphop th2); int ktrabang(taphop th2); taphop giao(taphop th2); taphop hop(taphop th2); taphop hieu(taphop th2); }; //---------------------------------------------------taphop::taphop() { n=0; x=NULL; } //---------------------------------------------------taphop::taphop(int m) { n=m; x=new int[n]; } //---------------------------------------------------taphop::~taphop() { delete x; } //---------------------------------------------------istream& operator>>(istream &inp,taphop &th) { int i; cout<<"Nhap so luong phan tu,n=";inp>>th.n; th.x=new int[th.n]; for(i=0;i<th.n;i++) { cout<<"x["<<i+1<<"]=";inp>>th.x[i]; } return inp; } //---------------------------------------------------ostream& operator<<(ostream &out,taphop th) { int i ; out<<"\n" << th.n<< " ( " ; for(i=0;i<th.n;i++)

out<<th.x[i]<<" ,"; out<<"\b)"; return out; } //---------------------------------------------------void taphop::rutgon() { int i,j; i = 1 ; while(i<n) { for(j=0;j<i&&x[i]!=x[j];j++); if(j<i) {x[i]=x[n-1];n--;} else i++; } } //---------------------------------------------------int taphop::ktra(int a) { int i ; for(i=0;i<n;i++) if(a==x[i]) return 1; return 0; } //---------------------------------------------------int taphop::ktrathuoc(taphop th2) { int i; for(i=0;i<th2.n;i++) if(!((*this).ktra(th2.x[i])))return 0; return 1; } //---------------------------------------------------int taphop::ktrabang(taphop th2) { if(!((*this).ktrathuoc(th2))) return 0; if(!th2.ktrathuoc(*this)) return 0; return 1; } //---------------------------------------------------taphop taphop::giao(taphop th2) { taphop C; C.n=0; int i; for(i=0;i<n;i++) if(((*this).ktrathuoc(th2))) {C.x[n]=x[i];C.n++;} return C; } //---------------------------------------------------taphop taphop::hop(taphop th2) { taphop C=(*this); int i; for(i=0;i<th2.n;i++) if(!(th2.ktrathuoc(C))){C.x[C.n]=th2.x[i];C.n++;} return C; } //----------------------------------------------------

taphop taphop::hieu(taphop th2) { taphop C; C.n=0; int i; for(i=0;i<n;i++) if(!((*this).ktrathuoc(th2))){C.x[C.n]=x[1];C.n++;} return C; } //---------------------------------------------------void main() { taphop A,B,C; clrscr(); cout<<"**Nhap tap hop A**\n";cin>>A; cout<<"\n**Nhap tap hop B**\n";cin>>B; cout<<"\nCac tap hop da nhap la:"<<A<<B; A.rutgon(); B.rutgon(); cout<<"\n\nCac tap hop da dc rut gon la:"<<A<<B<<endl;; if(A.ktrathuoc(B))cout << "\nB la tap con cua A"; if(B.ktrathuoc(A))cout << "\nA la tap con cua B"; /*if(A.ktrabang(B)) cout<<"\nA=B"; C=A.hop(B); cout<<endl<<C; C=A.giao(B); cout<<endl<<C; C=A.hieu(B); cout<<endl<<C;*/ getch(); } CLASS THI GIAN #include<iostream> using namespace std; // xay dung lop thoi gian class Ctime { private : int h, m , s; public : Ctime() { h = m = s = 0; } Ctime ( int h1, int m1 , int s1 ) { h = h1; m = m1; s = s1; } Ctime ( const Ctime & u ) { this->h = u.h; this->m = u.m; this->s = u.s; } ~Ctime() {

this->h = 0; this->m = 0; this->s = 0; } friend istream & operator >> ( istream &is , Ctime & u ); friend ostream & operator << ( ostream &os , const Ctime & u); Ctime operator + ( Ctime b); Ctime operator - ( Ctime b ); Ctime operator + ( int a); Ctime operator -( int a ); Ctime operator --(); Ctime operator --( int); Ctime operator ++(); Ctime operator ++(int); Ctime operator +=(Ctime b ); bool operator != ( Ctime u); }; // ham ban nhap istream &operator >> ( istream &is , { cout << " nhap gio :"; is >> u.h; cout<< " hap phut :"; is >> u.m; cout <<" nhap giay :"; is >>u.s; return is; } // ham ban in Ctime & u)

ostream &operator << ( ostream &os , const Ctime &u) { cout<<" ( h / p / g):"; os << u.h << " : " << u.m << " : " << u . s << endl; return os; } // toan tu cong thoi gina Ctime Ctime::operator + ( Ctime b ) { Ctime kq; kq.h = this->h + b.h; kq.m = this->m + b.m; kq.s = this->s + b.s; return kq; } // toan tu tru thoi gian Ctime Ctime::operator - (Ctime b ) { Ctime kq; kq.h = this->h - b.h; kq.m = this->m - b.m; kq.s = this->s - b.s; return kq; } // toan tu tang them a giay Ctime Ctime ::operator + ( int a ) { this->s = this->s + a; return *this;

} // toan tu giam di a giay; Ctime Ctime ::operator - ( int a ) { this->s = this->s - a; return *this; } // toan tu tang them 1 giay; Ctime Ctime ::operator ++() { this->s += this->s; return *this; } // tang them 1 giay Ctime Ctime ::operator ++( int) { this->s+= this->s;; return * this; } // toan tu giam di 1 giay; Ctime Ctime ::operator --() { this->s -= this->s; return *this; } // toan tu giam di 1 giay; Ctime Ctime ::operator --( int ) { this->s -= this->s; return *this; } // toan tu khac nhau; bool Ctime ::operator != ( Ctime u ) { if ( (this->h != u.h) || (this->m !=u.m )|| (this->s = u.s)) { return 1; } return 0; } Ctime Ctime::operator +=( Ctime b ) { this->h += b.h; this->m += b.m; this->s += b.s; return *this; } // ham main int main() { Ctime a, a5, int x; cout<<" nhap cin>>a; cout<<" nhap cin>>b; cout<<" nhap cin>>x; b,c, d, e; thoi gian a :\n "; thoi gian b :\n "; thoi gian ma x tang len la : ";

Ctime a1 (a); Ctime a2(a); Ctime a3(a); Ctime a4(a); Ctime a6(a); c = a +b; cout<<" tong cua 2 thoi gian a va b la " <<c << endl; d = a - b; cout<<" hieu cua 2 thoi gian a va b la " << d << endl; e = a + x; cout<<" thoi gian a sau khi tang le x la "<< e << endl; a5 = a - x; cout<<" thoi gian a sau khi giam di x la " << a5 << endl; cout<<" thoi gian a sau khi tang len( tang sau ) " << (a1++) << endl; cout<<" thoi gian a sau khi tang len ( tang truoc ) " << (++a2) << endl; cout<<" thoi gian a sau khi giam di 1 lan la ( giam sau " << (a3--)<<endl; cout<<" thoi gian a sau khi giam di 1 lan la ( giam truoc) " << (-a4)<<endl; a6 +=b; cout<<" thoi gian sau khi thuc hien phep toan a+=b la " << a6 << endl; if ( a != b ) { cout<<" \n2 thoi gian khac nhau "<<endl; } else cout<<"\n 2 thoi gian giong nhau "<< endl; system ("pause"); return 0; }

CLASS SINH VIN #include<iostream.h> #include<iomanip.h> #include<process.h> #include<string.h> #include<conio.h> /////////////////////////////////////// class sinhvien { char hoten[30]; char masv[20]; char lop[20]; float dm1,dm2,dm3,dtb; char xeploai[20]; sinhvien* sv; //int sl; // sl=50; public: void nhapsv(int sl); void hienthisv(int sl); float diemtb() { return dtb=(dm1+dm2+dm3)/3; } void xeploaisv(); void hoanvisv(sinhvien&,sinhvien&); void sapxepsv(int sl); }; void sinhvien::nhapsv(int sl) { // int sl=2; for(int i=1;i<=sl;i++)

} } void sinhvien::hienthisv(int sl) { // int sl=2; cout<<setiosflags(ios::showpoint)<<setprecision(3); for(int i=1;i<=sl;i++) { cout<<"Ho ten sinh vien :"<<sv[i].hoten<<endl; cout<<"Ma sinh vien :"<<sv[i].masv<<endl; cout<<"Lop :"<<sv[i].lop<<endl; cout<<"Diem trung binh :"<<sv[i].dtb<<endl; cout<<"Xep loai :"<<sv[i].xeploai<<endl; } } void sinhvien::xeploaisv() { //int i; if(diemtb()<5) strcpy(xeploai,"yeu"); if(diemtb()>=5&&diemtb()<6.5) strcpy(xeploai,"trung binh"); if(diemtb()>=6.5&&diemtb()<8.5) strcpy(xeploai,"kha"); if(diemtb()>=8.5&&diemtb()<=10) strcpy(xeploai,"gioi"); } void sinhvien::hoanvisv(sinhvien& sv1,sinhvien& sv2) { sinhvien temp=sv1; sv1=sv2; sv2=temp; } void sinhvien::sapxepsv(int sl) { //int sl=2; for(int i=1;i<=sl-1;i++) for(int j=i+1;j<=sl;j++) { if(sv[i].diemtb()<sv[j].diemtb()) hoanvisv(sv[i],sv[j]); } // for(int k=1;k<=sl;k++) // sv[k].hienthisv(); } main() { int n; cout<<"Tong so sinh vien :";cin>>n; sinhvien a; a.nhapsv(n); cout<<endl<<"Danh sach sinh vien sap xep giam dan theo diem trung binh "<<endl; a.sapxepsv(n);

{ cout<<"Ho ten sinh vien thu "<<i<<" :"; cin.ignore(1); cin.getline(sv[i].hoten,30); cout<<"Ma sinh vien :"; cin.ignore(1); cin.getline(sv[i].masv,20); cout<<"Lop :"; cin.ignore(1); cin.getline(sv[i].lop,20); cout<<"Diem mon 1 :";cin>>sv[i].dm1; cout<<"Diem mon 2 :";cin>>sv[i].dm2; cout<<"Diem mon 3 :";cin>>sv[i].dm3;

a.hienthisv(n); return 0;

CLASS SINH VIN K THA #include #include #include #include "stdafx.h" "conio.h" "iostream" "windows.h"

using namespace std; class nguoi{ public: char name[30]; char diachi[50]; char phone[12]; public: void nhap(); void xuat(); }; class sinhvien:public nguoi{ public: int mssv; int dtoan; int dly; int dhoa; public: void nhapsv(); void xuatsv(); float dtb(); int getmssv(); char getname(); int getdtoan(); int getdly(); int getdhoa(); void load(); }; class dssv:public sinhvien{ public: sinhvien *info; int n; public: void nhapds(); void xuatds(); void xuatdtb(); void search_info(int mssv_s); void search_diem(int mssv_s); void sort_diem(); void sort_mssv(); }; void dssv::nhapds() { FILE *f; f=fopen("C:\\load.txt","r"); fscanf(f,"%d",&n); info=new sinhvien[n]; for(int i=0; i<n; i++) { fscanf(f,"%s",&info[i].name); fscanf(f,"%s",&info[i].diachi);

fscanf(f,"%s",&info[i].phone); fscanf(f,"%d",&info[i].mssv); fscanf(f,"%d",&info[i].dtoan); fscanf(f,"%d",&info[i].dly); fscanf(f,"%d",&info[i].dhoa); } fclose(f); cout<<"\nLoad complite !!";

void dssv::xuatds() { for(int i=0; i<n; i++) { info[i].xuatsv(); } } void dssv::xuatdtb() { for(int i=0; i<n; i++) { cout<<"\n------------------------"; cout<<"\n"<<info[i].name; cout<<"\nDTB: "<<info[i].dtb(); } } void dssv::search_info(int mssv_s) { sinhvien temp; for(int i=0; i<n; i++) { if(mssv_s==info[i].getmssv()) { info[i].xuat(); break; } } } void dssv::search_diem(int mssv_s) { for(int i=0; i<n; i++) { if(mssv_s==info[i].getmssv()) { cout<<"\n---------------------------"; cout<<"\nName: "<<info[i].name; cout<<"\nDiem Toan: "<<info[i].getdtoan(); cout<<"\nDiem Ly: "<<info[i].getdly(); cout<<"\nDiem Hoa: "<<info[i].getdhoa(); cout<<"\nDiem TB: "<<info[i].dtb(); break; } } } void dssv::sort_diem() { sinhvien temp; for(int i=0; i<n-1; i++) { for(int j=i; j<n; j++)

if(info[i].dtb()>info[j].dtb()) { temp=info[i]; info[i]=info[j]; info[j]=temp; }

void dssv::sort_mssv() { sinhvien temp; for(int i=0; i<n-1; i++) { for(int j=i; j<n; j++) { if(info[i].getmssv()>info[j].getmssv()) { temp=info[i]; info[i]=info[j]; info[j]=temp; } } } } void sinhvien::nhapsv() { nhap(); cout<<"\nNhap mssv: "; cin>>mssv; cout<<"\nNhap diem toan: "; cin>>dtoan; cout<<"\nNhap diem ly: "; cin>>dly; cout<<"\nNhap diem hoa: "; cin>>dhoa; } void sinhvien::xuatsv() { xuat(); cout<<"\nMSSV: "<<mssv; cout<<"\nDiem toan: "<<dtoan; cout<<"\nDiem ly: "<<dly; cout<<"\nDiem hoa: "<<dhoa; cout<<"\nDiem TB: "<<dtb(); } float sinhvien::dtb() { float temp; temp=(dtoan+dly+dhoa)/3; return temp; } int sinhvien::getmssv() { return mssv; } char sinhvien::getname() { return name[30]; } int sinhvien::getdtoan() { return dtoan;

} int sinhvien::getdly(){ return dly; } int sinhvien::getdhoa(){ return dhoa; } void nguoi::nhap() { cout<<"\nNhap ten: "; fflush(stdin); gets(name); cout<<"\nNhap dia chi: "; fflush(stdin); gets(diachi); cout<<"\nNhap phone number: "; fflush(stdin); gets(phone); } void nguoi::xuat() { cout<<"\n------------------------------"; cout<<"\nName: "<<name; cout<<"\nDia chi: "<<diachi; cout<<"\nPhone number: "<<phone; } void main() { dssv a1; int mssv_s, mssv_s2, chon; do{ system("cls"); cout<<"\nChon 1 De Load Danh Sach Sinh Vien"; cout<<"\nChon 2 De Xuat Danh Sach"; cout<<"\nChon 3 De Xem DTB Cua Tung SV"; cout<<"\nChon 4 De Xem Thong Tin SV Theo MSSV"; cout<<"\nChon 5 De Xem Diem Sinh Vien Theo MSSV"; cout<<"\nChon 6 De Sap Xep DS Theo DTB"; cout<<"\nChon 7 De Sap Xep DS Theo MSSV"; cout<<"\nChon 0 De Thoat"; cout<<"\nChon: "; cin>>chon; switch(chon) { case 1: { a1.nhapds(); break; } case 2: { a1.xuatds(); break; } case 3: { a1.xuatdtb(); break; } case 4: { cout<<"\nNhap MSSV Can Xem Thong Tin: "; cin>>mssv_s; a1.search_info(mssv_s); break; } case 5: { cout<<"\nNhap MSSV Can Xem Diem: "; cin>>mssv_s2; a1.search_diem(mssv_s2); break; } case 6: { a1.sort_diem();

} case 7: {

cout<<"\nSort complite..."; break;

} } getch(); }while(chon!=0);

a1.sort_mssv(); cout<<"\nSort complite..."; break;

Cu 1: Cho nh ngha lp a thc sau: class DaThuc { private: int n; //Bc a thc double *a; //Tr ti vng nh cha cc h s a thc x0, x1... public: DaThuc() { this->n = 0; this->a = NULL; } DaThuc(int n1) { this->n = n1; this->a = new double[n1 + 1]; } friend ostream &operator << (ostream &os, const DaThuc &d); friend istream &operator >> (istream &is, DaThuc &d); double f(double x); //Tnh gi tr a thc };

1. Vit phng thc thit lp c 2 tham s, phng thc hy b cho lp DaThuc. 2. Vit hm tnh gi tr ca a thc theo khai bo trn.

V d: cho a thc 3x4 x3 5x + 7 Vi x = 2: f(x) = 3*24 23 5*2 + 7 = 37 Cu 2: nh ngha lp CVector biu din khi nim Vector trong khng gian 3 chiu vi cc phng thc thit lp, hy b (nu c), hm tnh tch v hng ca 2 Vector, cc php ton >>, << nhp xut Veoctor (Ch khai bo cc hm / php ton, khng vit chi tit tng hm). Vit hm tnh tch v hng 2 Vector V d: Cho 2 Vector A(1, 2, 3), B(4, -5, 6) Tch v hng (A, B) = 1 * 4 + 2 * (-5) + 3 * 6 = 12 Cu 3: thong tin v 1 cn b trong c quan bao gm: m cn b, h v tn, nm sinh, s in thoi, nm vo lm. Vit nh ngha lp CanBo vi cc phng thc thit lp, hy b (nu c), cc php ton >>, << nhp xut, hm so snh nm lm vic ca 2 cn b. Vit hm main

thc thi lp cn b. Bi gii: Cu 1:

1. Vit phng thc thit lp c 2 tham s, phng thc hy b cho lp DaThuc.

Phng thc thit lp public DaThuc(int n1, double &a1) { this->n = n1; this->a = new double[this->n + 1]; for(int i = 0; i < this->n; i++) { this->a[i] = a1[i]; } } Phng thc hy b: ~DaThuc() { delete []a; }

1. Vit hm tnh gi tr ca a thc theo khai bo trn.

double f(double x) //Tnh gi tr a thc { double ketqua = 0; for(int i = n - 1; i >= 0; i--) { ketqua += a[i] * pow(x, i); } return ketqua; } Cu 2: nh ngha lp CVector biu din khi nim Vector trong khng gian 3 chiu vi cc phng thc thit lp, hy b (nu c), hm tnh tch v hng ca 2 Vector, cc php ton >>, << nhp xut Veoctor (Ch khai bo cc hm / php ton, khng vit chi tit tng hm). nh ngha lp Cvector: class CVector { private: int a[3]; public: CVector() { for(int i = 0; i < 3; i++) { this->a[i] = 0; } }

CVector(int a[3]) { for(int i = 0; i < 3; i++) { this->a[i] = a[i]; } } //Khong can phuong thuc huy --> don gian hoa <==> khong sai int TichVoHuong(CVector A); friend ostream &operator << (ostream &os, const CVector &C); friend istream &operator >> (istream &is, CVector &C); }; Vit hm tnh tch v hng 2 Vector int TichVoHuong(CVector A) { int tich = 0; for(int i = 0; i < 3; i++) { tich += this->a[i] * A.a[i]; } tich; } Cu 3: thong tin v 1 cn b trong c quan bao gm: m cn b, h v tn, nm sinh, s in thoi, nm vo lm. Vit nh ngha lp CanBo vi cc phng thc thit lp, hy b (nu c), cc php ton >>, << nhp xut, hm so snh nm lm vic ca 2 cn b. Vit hm main thc thi lp cn b. #include <iostream> #include <conio.h> #include <string.h> using namespace std; class CanBo { private: char ma[15]; char hoten[30]; unsigned namsinh; char sodt[15]; unsigned namvaolam; public: CanBo() { strcpy(this->ma, "0000"); strcpy(this->hoten, "????"); this->namsinh = 1900; strcpy(this->sodt, "0000000000"); this->namvaolam = 1900; } CanBo(char ma[15], char hoten[15], unsigned namsinh, char sodt[15], unsigned namvaolam) { strcpy(this->ma, ma); strcpy(this->hoten, hoten); this->namsinh = namsinh; strcpy(this->sodt, sodt);

this->namvaolam = namvaolam; } friend ostream &operator << (ostream &os, const CanBo &C) { os<<"Ma: "<<C.ma; os<<"\tHo & ten: "<<C.hoten; os<<"\tNam sinh: "<<C.namsinh; os<<"\tSo dt: "<<C.sodt<<endl; os<<"\tnamvaolam"<<C.namvaolam; return os; } friend istream &operator >> (istream &is, CanBo &C) { cout<<"\nNhap ma: "; is>>C.ma; cout<<"\nNhap ho ten: "; is>>C.hoten; cout<<"\nNhap nam sinh: "; is>>C.namsinh; cout<<"\nNhap so dien thoai: "; is>>C.sodt; cout<<"\nNhap nam vao lam: "; is>>C.namvaolam; return is; } bool operator > (CanBo A) { if(this->namvaolam < A.namvaolam) { return true; } return false; } bool operator < (CanBo A) { if(this->namvaolam > A.namvaolam) { return true; } return false; } bool operator == (CanBo A) { if(this->namvaolam == A.namvaolam) { return true; } return false; } }; int main() { CanBo A; cin>>A; cout<<A; CanBo B; cin>>B; cout<<B; if(A > B) { cout<<"\nA vao lam truoc B"; }

else if( A == B) { cout<<"\nA vao lam cung luc voi B"; } else { cout<<"\nA vao lam sau B"; } _getch(); return 0; }

Cu 1. Cho on chng trnh sau: #include<iostream.h> #include<conio.h> class A { public: A() { cout<<A:default<<endl; } A(int a) { cout<<A:parameter<<endl; } Virtual void xuat() { cout<<Xuat A<<endl; } }; class B : public A { public: B(int a):A(a) { cout<<B<<endl; } void xuat() { cout<<Xuat B<<endl; } }; void main() { A *a[2]; B *b[2]; a[0]=new A(); a[1]=new B(); b[0]=new A(10); b[1]=new B(30); a[0]->xuat(); a[1]->xuat(); b[0]->xuat(); b[1]->xuat(); } 1. Cho bit chng trnh trn (hm main) c li g khng ? nu c gii thch v sa li cho ng. 2. Cho bit kt qu sau khi chy hm main. Cu 2. nh ngha lp Cmatrix biu din khi nim ma trn vi cc phng thc thit lp,hy b(nu c), hm chuyn v 1 ma trn , cc php ton >>,<< nhp, xut ma trn (ch khai bo cc hm / php ton , khng vit chi tit tng hm). Vit hm chuyn v cho 1 ma trn. V d: Ma trn A c 4 dng , 3 ct 1 2 3 4 5 6 7 8 9

10 11 12 => Ma trn chuyn v ca A c 3 dng , 4 ct 1 4 7 10 2 5 8 11 3 6 9 12 Cu 3. Thng tin v 1 cn b trong c quan gm: m cn b , h v tn , nm sinh , s in thoi , nm vo lm. Vit nh ngha lp cn b vi cc phng thc thit lp, hy b(nu c), cc php ton >>,<< nhp xut, hm so snh tui ca 2 cn b . Vit hm main thc thi lp cn b ------------------HT-------------------

Sa li cu 1 cho d nhn:D #include<iostream> #include<conio.h> using namespace std; class A { public: A() { cout<<"A:default"<<endl; } A(int a) { cout<<"A:parameter"<<endl; } virtual void xuat() { cout<<"Xuat A"<<endl; } }; class B : public A { public: B(int a):A(a) { cout<<"B"<<endl; } void xuat() { cout<<"Xuat B"<<endl; } }; void main() { A *a[2]; B *b[2]; a[0]=new A(); a[1]=new B(); //Chu y b[0]=new A(10); //Chu y b[1]=new B(30); a[0]->xuat(); a[1]->xuat(); b[0]->xuat(); b[1]->xuat(); } a[1]=new B(); //Chu y Li: do B ko c hm khi to khng i s, phi c 1 hm khi to c i s mi n. a[1]=new B(10); //OK

b[0]=new A(10); //Chu y B l con ca A nn ko th to i tng *** t con c. Gii php, b dng ny hoc b[0]=new B(10); //OK Bi gii hon chnh: #include<iostream> #include<conio.h> using namespace std; class A { public: A() { cout<<"A:default"<<endl; } A(int a) { cout<<"A:parameter"<<endl; } virtual void xuat() { cout<<"Xuat A"<<endl; } }; class B : public A { public: B(int a):A(a) { cout<<"B"<<endl; } void xuat() { cout<<"Xuat B"<<endl; } }; void main() { A *a[2]; B *b[2]; a[0]=new A(); a[1]=new A(); //OK b[0]=new B(10); //OK b[1]=new B(30); a[0]->xuat(); a[1]->xuat(); b[0]->xuat(); b[1]->xuat(); _getch(); } Kt qu: A:default A:default A:parameter B A:parameter B Xuat A Xuat A Xuat B Xuat B

Cu 2. nh ngha lp Cmatrix biu din khi nim ma trn vi cc phng thc thit lp,hy b(nu c), hm chuyn v 1 ma trn , cc php ton >>,<< nhp, xut ma trn (ch khai bo cc hm / php ton , khng vit chi tit tng hm). Vit hm chuyn v cho 1 ma trn. #include<iostream> #include<conio.h> using namespace std; const int Col = 100; const int Row = 100; class CMatrix { private: int m, n; int data[Row][Col]; public: CMatrix() { m = n = 0; } CMatrix(int m, int n) { this->m = m; this->n = n; } friend ostream &operator << (ostream &os, const CMatrix &A) /*{ os<<"\nMa tran:\n"; for(int i = 0; i < A.m; i++) { for(int j = 0; j < A.n; j++) { os<<A.data[i][j]<<" "; } os<<"\n"; } return os; }*/ friend istream &operator >> (istream &is, CMatrix &A) /*{ cout<<"\nNhap so dong: "; is>>A.m; cout<<"\nNhap so cot: "; is>>A.n; for(int i = 0; i < A.m; i++) { for(int j = 0; j < A.n; j++) { cout<<"\nNhap phan tu A["<<i<<"]["<<j<<"]: "; is>>A.data[i][j]; } } return is; }*/ CMatrix ChuyenVi() { CMatrix A; A.m = this->n;

A.n = this->m; for(int i = 0; i < m; i++) { for(int j = 0; j < n; j++) { A.data[j][i] = this->data[i][j]; } } return A; } }; /* int main() { CMatrix A; CMatrix B; cin>>A; cout<<A; B = A.ChuyenVi(); cout<<"\n\n"; cout<<B; _getch(); return 0; } */ Cu ny, nhng dng ghi ch l ch test, khi vo lm bi, c th khng cn ghi cc dng ny. Cu 3. Thng tin v 1 cn b trong c quan gm: m cn b , h v tn , nm sinh , s in thoi , nm vo lm. Vit nh ngha lp cn b vi cc phng thc thit lp, hy b(nu c), cc php ton >>,<< nhp xut, hm so snh tui ca 2 cn b . Vit hm main thc thi lp cn b #include <iostream> #include <conio.h> #include <string.h> using namespace std; class CanBo { private: char ma[15]; char hoten[30]; unsigned namsinh; char sodt[15]; unsigned namvaolam; public: CanBo() { strcpy(this->ma, "0000"); strcpy(this->hoten, "????"); this->namsinh = 1900; strcpy(this->sodt, "0000000000"); this->namvaolam = 1900; } CanBo(char ma[15], char hoten[15], unsigned namsinh, char sodt[15], unsigned namvaolam) { strcpy(this->ma, ma); strcpy(this->hoten, hoten); this->namsinh = namsinh;

strcpy(this->sodt, sodt); this->namvaolam = namvaolam; } friend ostream &operator << (ostream &os, const CanBo &C) { os<<"Ma: "<<C.ma; os<<"\tHo & ten: "<<C.hoten; os<<"\tNam sinh: "<<C.namsinh; os<<"\tSo dt: "<<C.sodt<<endl; os<<"\tnamvaolam"<<C.namvaolam; return os; } friend istream &operator >> (istream &is, CanBo &C) { cout<<"\nNhap ma: "; is>>C.ma; cout<<"\nNhap ho ten: "; is>>C.hoten; cout<<"\nNhap nam sinh: "; is>>C.namsinh; cout<<"\nNhap so dien thoai: "; is>>C.sodt; cout<<"\nNhap nam vao lam: "; is>>C.namvaolam; return is; } bool operator > (CanBo A) { if(this->namsinh < A.namsinh) { return true; } return false; } bool operator < (CanBo A) { if(this->namsinh > A.namsinh) { return true; } return false; } bool operator == (CanBo A) { if(this->namsinh == A.namsinh) { return true; } return false; } }; int main() { CanBo A; cin>>A; cout<<A; CanBo B; cin>>B; cout<<B; if(A > B) { cout<<"\nA lon tuoi hon B";

} else if( A == B) { cout<<"\nA bang tuoi B"; } else { cout<<"\nA nho tuoi B"; } _getch(); return 0; }

class dy s nguyn s dng con tr #include #include #include #include #include "stdafx.h" "stdio.h" "conio.h" "windows.h" "iostream"

using namespace std; int n; class Dayso{ int *key; public: void create(); void destroy(); void nhap(); void xuat(); int sum(); int search_max(); int search_min(); void search_key(); void add_data(); void delete_data(); void delete_key(); void sort_A(); }; void Dayso::create() { key = new int[n]; for(int i=0; i<n; i++) key[i]=0; } void Dayso::destroy()

{ delete key; } void Dayso::nhap() { cout<<"\n"; for(int i =0; i< n; i++) { cout<<"Nhap Ptu "<<i+1<<" : "; cin>>key[i]; } } void Dayso::xuat() { for(int i=0; i< n; i++) { cout<<"\nPtu Thu "<<i+1<<" : "<<key[i]; } } int Dayso::sum() { int sum = 0; for(int i=0; i<n; i++) { sum = sum + key[i]; } return sum; } int Dayso::search_max() { int max= key[0]; for(int i=1; i<n; i++) { if(max<key[i]) max=key[i]; } return max; } int Dayso::search_min() { int min = key[0]; for(int i=1; i<n; i++) { if(min>key[i])

min=key[i]; } return min; } void Dayso::search_key() { int key_search, i; cout<<"\nNhap so can tim: ";cin>>key_search; for(i=0; i<n; i++) { if(key[i]==key_search) break; } if(i<n) cout<<"\nTim thay tai vi tri "<<i+1<<" trong day"; else cout<<"\Khong tim thay"; } void Dayso::add_data() { int vt, data; n=n+1; cout<<"\nVi tri can chen: "; cin>>vt; cout<<"\nNhap so can chen: "; cin>>data; for(int i=0; i<n; i++) { if((i+1)==vt) { key[n-1]=key[i]; key[i]=data; } } } void Dayso::delete_data() { int vt; cout<<"\nNhap vi tri ban muon xoa: "; cin>>vt; n=n-1; for(int i=0; i<n; i++) { if((i+1)==vt) key[i]=key[n]; } } void Dayso::delete_key() {

int data; cout<<"\nNhap phan tu ban muon xoa: "; cin>>data; n=n-1; for(int i=0; i<n; i++) { if(data==key[i]) key[i]=key[n]; } } void Dayso::sort_A() { int temp; for(int i=0; i<n; i++) { for(int j=i+1;j<n;j++) if(key[j]<key[i]) { temp=key[i]; key[i]=key[j]; key[j]=temp; } } } void main() { system("color A"); Dayso ds; int chon; cout<<"\nNhap So Luong Phan Tu ( n < 10 ): "; cin>>n; ds.create(); do{ system("cls"); cout<<"\nNhan 1 de nhap cac phan tu cho day"; cout<<"\nNhan 2 de xuat day"; cout<<"\nNhan 3 de sap xep cac phan tu trong day ( tang dan )"; cout<<"\nNhap 4 de xem tong day"; cout<<"\nNhan 5 de xem max va min trong day"; cout<<"\nnhan 6 de Tim 1 phan tu nao do trong mang"; cout<<"\nNhan 7 de them 1 phan tu vao day so"; cout<<"\nNhan 8 de xoa 1 phan tu bat ki khoi day"; cout<<"\nNhan 9 de xoa 1 phan tu co gia tri x khoi day"; cout<<"\nNhan 10 de huy toan bo day"; cout<<"\nNhan 0 de thoat"; cout<<"\n\nChon: "; cin>>chon; switch(chon) {

case 1: { ds.nhap(); break; } case 2: { ds.xuat(); break; } case 3: { ds.sort_A(); cout<<"\nSort complete..."; break; } case 4: { ds.xuat(); cout<<"\nTong Day La: "<<ds.sum(); break; } case 5: { ds.xuat(); cout<<"\nMax La: "<<ds.search_max(); cout<<"\nMin La: "<<ds.search_min(); break; } case 6: { ds.search_key(); break; } case 7: { ds.xuat(); ds.add_data(); cout<<"\nChen Thanh Cong..."; break; } case 8: { ds.xuat(); ds.delete_data(); ds.sort_A(); cout<<"\nDelete complete..."; break; }

case 9: { ds.xuat(); ds.delete_key(); ds.sort_A(); cout<<"\nDelete complete..."; break; } case 10: { cout<<"\nThao tac nay cua ban se lam tat ca ptu tro ve gia tri mac dinh ban dau"; ds.destroy(); Sleep(1000); cout<<"\nDelete complete..."; ds.create(); break; } } getch(); }while(chon!=0); } LP VECTOR a. To lp Vector m t cc i tng vector, lp gm cc thnh phn: - S phn t: n - Mt con tr *v tr n cc phn t d liu. - Hm thit lp mt tham s dng cp pht b nh cho cc phn t d liu. - Hm thit lp sao chp. - Hm hu b. - Hm nhp cc phn t ca vc t. - Hm hin th cc phn t d liu ca vector. - Ton t = gn mt vector cho mt vector. - Khai bo ton t + l hm bn ca lp Vector, dng cng hai vector. b. Vit ton t + cng hai vector, kt qu tr v l mt vector. c. Vit chng trnh nhp vo hai vector a, b. In ra mn hnh cc phn t d liu ca vector a, vector b v vector a+b #include<iostream.h> #include<conio.h> //-------------class vector {

private: int n; float *v; public: //Ham thiet lap mot tham so dung de cap phat bo nho cho cac phan tu du lieu vector(int size=0) { n=size; v=new float[size]; } // Ham thiet lap sao chep vector(vector &a); //Hem huy bo. ~vector() { for(int i=0;i<n;i++) delete v; v++; } //Ham nhap cac phan tu cua vector void nhap(); //Ham hien thi cac phan tu du lieu cua vector void display(); //Toan tu = de gan mot vector cho mot vector void operator=(vector &a); // ham kiem tra hai vector co bang nhau hay khong friend int kiemtra(vector &a,vector &b); //Khai bao toan tu + la ham ban cua lop Vector, dung de cong hai vector. friend vector operator+(vector &a,vector &b);

}; //--------------------------------void vector::nhap() { cout<<"Nhap vao:n="; cin>>n; v=new float[n]; for(int i=0;i<n;i++) { cout<<"Nhap phan tu thu:"<<i+1<<":"; cin>>v[i]; } } //-----------------------------------------void vector::display() { for(int i=0;i<n;i++) cout<<v[i]<<" "; } //-----------------------------------------vector::vector(vector &a) { n=a.n; v=new float[n]; for(int i=0;i<n;i++) v[i]=a.v[i]; } //----------------------------------------void vector::operator=(vector &a)

{ n=a.n; v=new float[n]; for(int i=0;i<n;i++) v[i]=a.v[i]; } //-----------------------------------------int kiemtra(vector &a,vector &b) { if(a.n==b.n) return 1; return 0; } //------------------------------------------vector operator+(vector&a,vector&b) { vector c; c.n=a.n; c.v=new float[c.n]; for(int i=0;i<c.n;i++) c.v[i]=a.v[i]+b.v[i]; return c; } //-------------------------------------------void main() { vector a,b; a.nhap(); b.nhap();

a.display();cout<<"\n"; b.display(); if(kiemtra(a,b)==1) { cout<<"Tong cua hai vector la:\n"; (a+b).display(); } else cout<<"Hai vector khong cung co"; }

Cu 1: Lp trnh hng i tng l g ? Trnh by u im ca lp trnh hng i tng ? LT HT l phng php lp trnh: M t chnh xc cc i tng trong th gii thc. Ly i tng lm nn tng xy dng thut ton. Thit k xoay quanh d liu ca h thng. Chng trnh c chia thnh cc lp i tng. D liu c ng gi, che du v bo v. i tng lm vic vi nhau thng qua thng bo. Chng trnh c thit k t di ln (Bottom-Up).

u im ca LT HT: Loi b cc on m lp li. To ra chng trnh an ton, bo mt. D dng m rng, nng cp. Rt ngn thi gian xy dng h thng. Tng nng xut v hiu qu hn. Chng trnh c thit k theo ng qui trnh

Cu 2: Thit lp lp S phc biu din khi nim s phc vi hai thnh phn d liu thc v o. Xy dng cc phng thc sau: - Ton t xut/nhp s phc (ton t <<, >>) - Ton t cng, tr hai s phc (ton t +, -) Vit hm main() thc hin cc phng thc trn.
class sp { float thuc, ao; public: sp() {thuc=ao=0;};

friend istream & operator >> (istream &in, sp &a); friend ostream & operator << (ostream &os, sp a); sp operator + (sp b); sp operator - (sp b); }; istream & operator >> (istream &in, sp &a) { cout<<Nhap phan thuc: ; cin>>a.thuc; cout<<Nhap phan ao: ; cin>>a.ao; return in; } ostream & operator << (ostream &os, sp a) { cout<<Phan thuc: <<a.thuc<< ; cout<<Phan ao: <<a.ao<<i; return os; } sp sp::operator + (sp b) { sp kq; kq.thuc=thuc+b.thuc; kq.ao=ao+b.ao; return kq; } sp sp::operator - (sp b) { sp kq; kq.thuc=thuc-b.thuc; kq.ao=ao-b.ao; return kq; } void main() { sp a, b; cout<<Nhap 2 sp:; cin>>a>>b; cout<<Tong:<<(a+b); cout<<Hieu:<<(a-b); getch(); }

Cu 3: S dng tnh k tha xy dng cc loi i tng: cng nhn, ngh s, ca s. Cho v d v in thng tin cc i tng .
class Nguoi { char Hoten[30]; int NS; public: Nguoi() {NS=1990;}; void nhap (){ cout<<Hoten:; gets(Hoten); cout<<NamSinh:; cin>>NS; }; void xuat () { cout<<Hoten:<<Hoten; cout<<NamSinh:<<NS; }; }; class CongNhan:public Nguoi { char CM[100]; public: CongNhan() {Nguoi();}; void nhap (){ Nguoi::nhap(); cout<<ChuyenMon:;gets(CM);}; void xuat () { cout<<CongNhan:; Nguoi::xuat(); cout<<ChuyenMon<<CM;}; }; class CaSi:public NgheSi { int Caxe; public: CaSi() {NgheSi();}; void nhap (){ NgheSi::nhap(); cout<<Caxe:; cin>>Caxe;}; void xuat () { cout<<CaSi:; NgheSi::xuat(); cout<<Caxe<<Caxe;}; }; void main() { CongNhan a; NgheSi b; CaSi c; a.nhap(); b.nhap(); c.nhap(); a.xuat(); b.xuat(); c.xuat(); getch(); }

M hnh trc quan cc i tng nh sau: Ngi

Cng Nhn

Ngh S

Ca S

class NgheSi:public Nguoi { int TN[100]; public: NgheSi() {Nguoi();TN=0;}; void nhap (){ Nguoi::nhap(); cout<<ThamNien:; cin>>TN);}; void xuat () { cout<<NgheSi:; Nguoi::xuat(); cout<<ThamNien<<TN;}; };

Kt qu qu trnh nhp/xut cc thng tin c th nh sau: CongNhan: Hoten: Nguyen Chi Trung NamSinh: 1991 ChuyenMon: Ky su Tin Hoc NgheSi: Hoten: Ngoc Dan Thanh NamSinh: 1970 ThamNien: 21 nam CaSi: Hoten: Le Nhat Minh NamSinh: 1989 ThamNien: 4 nam Caxe: 2500 USD Trang 54

You might also like