You are on page 1of 63

Cu 1: Vit chng trnh thc hin: 1/ Ci t lp PHN S c cc thuc tnh: t s, mu s, c cc phng thc: to, hy, nhp, xem, ti gin,

cc ton t ti bi +,-,*,/,<<,>>. 2/ p dng: - To lp 3 i tng phn s ps,ps1,ps2. - Nhp v cho xem phn s ps1; Nhp v cho xem phn s ps2 s dng <<,>>. - Thc hin cc php ton +,-,*, / phn s v in kt qu ra mn hnh. Bi gii: //Khai bao thu vien #include<conio.h> #include<stdio.h> #include<iomanip.h> #include<math.h> //Khai bao cau truc lop class phanso { private: int tu,mau; public: phanso(int _tu=0,int _mau=0) { tu=_tu;mau=_mau;} ~phanso()

{tu=mau=0;} void nhap() { cout<<"\nNhap tu so: ";cin>>tu; do { cout<<"\nNhap mau so: ";cin>>mau; }while(!mau); if (mau<0) { tu=-tu;mau=-mau;} } void xem() {if(tu==0) cout<<"0"; else if(mau==1) cout<<tu; else if(mau<0) cout<<"-"<<tu<<"/"<<mau; else cout<<tu<<"/"<<mau; } friend ostream & operator<<(ostream&,phanso&); friend istream & operator>>(istream&,phanso&); 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.tu,p.mau); q.tu=p.tu/x; q.mau=p.mau/x; return q; } phanso operator +(phanso); phanso operator -(phanso); phanso operator *(phanso); phanso operator /(phanso); }; //Khai bao cac phuong thuc ostream &operator<<(ostream &xuat,phanso &ps) { xuat<<ps.tu<<"/"<<ps.mau; return xuat; } istream& operator>>(istream &nhap,phanso &ps) { cout<<"Nhap tu va mau: ";nhap>>ps.tu>>ps.mau;

return nhap; } phanso phanso::operator +(phanso p1) {phanso q; q.tu=tu*p1.mau+p1.tu*mau; q.mau=mau*p1.mau; return rutgon(q); } phanso phanso::operator -(phanso p1) {phanso q; q.tu=tu*p1.mau -mau*p1.tu; q.mau=p1.mau*mau; return rutgon(q); } phanso phanso::operator *(phanso p1) {phanso q; q.tu=tu*p1.tu; q.mau=mau*p1.mau; return rutgon(q); } phanso phanso::operator /(phanso p1) {phanso q; q.tu=tu*p1.mau;

q.mau=mau*p1.tu; return rutgon(q); } int main() {phanso ps,ps1,ps2; cout<<"\nPhan so 1 ";ps1.nhap(); cout<<"\nPhan so 1 la: ";ps1.xem(); cout<<"\nPhan so 2: ";ps2.nhap(); cout<<"\nPhan so 2 la: ";ps2.xem(); ps=ps1+ps2; cout<<"\nTong la: "<<ps; ps=ps1-ps2; cout<<"\nHieu la: "<<ps; ps=ps1*ps2; cout<<"\nTich la: "<<ps; ps=ps1/ps2; cout<<"\nThuong la: "<<ps; getch(); } Cu 2: vit chng trnh thc hin: 1/ Ci t lp S PHC c cc thuc tnh: thc, o, c cc phng thc : to, hy, nhp, xem, cc ton t ti bi +, -,*, /, << , >> 2/ p dng: - To lp 3 i tng phn s sp, sp1,sp2. - Nhp v cho xem phn s sp1; Nhp v cho xem phn s sp2 s dng <<, >>. - Thc hin cc php ton +, -, *, / s phc v in kt qu ra mn hnh. Bi gii: //Khai bao thu vien

#include<conio.h> #include<stdio.h> #include<iomanip.h> #include <math.h> //khai bao cau truc lop class sophuc { private: float thuc,ao; public: sophuc(float t=0,float a=0) {thuc=t;ao=a; ~sophuc(){thuc=ao=0;} void nhap() {cout<<"\nPhan thuc: ";cin>>thuc; cout<<"Phan ao: "; cin>>ao;} void xem() { cout<<thuc; if (ao !=0) if (ao<0) cout<<"-"; else cout<<"+"; cout<<ao<<"i"; } sophuc operator +(sophuc); sophuc operator -(sophuc); sophuc operator *(sophuc);

sophuc operator /(sophuc); friend ostream & operator<<(ostream&,sophuc&); friend istream & operator>>(istream&,sophuc&); }; //khai bao cac phuong thuc ngoai lop //dung ham thanh phan sophuc sophuc::operator +(sophuc sp) {sophuc tg; tg.thuc=thuc+sp.thuc; tg.ao=ao+sp.ao; return tg; } sophuc sophuc::operator -(sophuc sp) { sophuc tg; tg.thuc=thuc-sp.thuc; tg.ao=ao=sp.ao; return tg; } sophuc sophuc::operator *(sophuc sp) {sophuc tg; tg.thuc=thuc*sp.thuc-ao*sp.ao; return tg; } sophuc sophuc::operator /(sophuc sp) { sophuc tg; tg.ao=thuc*sp.ao+ao*sp.thuc;

tg.thuc=(thuc*sp.thuc+ao*sp.ao)/(pow(sp.thuc,2)+pow(sp.ao,2)); tg.ao=(thuc*sp.ao-ao*sp.thuc)/(pow(sp.thuc,2)+pow(sp.ao,2)); return tg; } //dung ham ban ostream &operator<<(ostream &xuat,sophuc &sp) { cout<<"\n"; if((sp.thuc==0)&&(sp.ao==0)) cout<<"0"; else if(sp.thuc==0 && sp.ao !=0) cout<<sp.ao<<"i"; else if(sp.thuc !=0 && sp.ao==0) cout<<sp.thuc; else if(sp.ao>0) cout<<sp.thuc<<"+"<<sp.ao<<"i"; else cout<<sp.thuc<<"-"<<fabs(sp.ao)<<"i"; return xuat; } istream &operator>>(istream &nhap,sophuc &sp) {cout<<"Thuc: "; nhap>>sp.thuc; cout<<"ao: ";nhap>>sp.ao; return nhap; } int main() {sophuc sp,sp1,sp2; cout<<"so phuc 1: "; sp1.nhap(); cout<<"So phuc 1 la: "; sp1.xem(); cout<<"so phuc 2: "; sp2.nhap();

cout<<"So phuc 2 la: "; sp2.xem(); sp=sp1+sp2; cout<<"\nTong la: "<<sp; sp=sp1-sp2; cout<<"\nHieu la: "<<sp; sp=sp1*sp2; cout<<"\nTich la: "<<sp; sp=sp1/sp2; cout<<"\nThuong la: "<<sp; getch(); } Cu 3: Vit chng trnh thc hin: 1/ Ci t lp TAM GIC c cc thuc tnh kch thc a, b, c v cc phng thc: to, hy, nhp: nhp kch thc tam gic c kim tra tnh ng n ca tam gic, xem, S: tr v din tch, C: tr v chu vi. 2/ p dng: - Cp nht 3 i tng tg1,tg2,tg3. - In thng tin ra ca 3 i tng tam gic ra mn hnh - In ra i tng tam gic c din tch ln nht. Bi gii: #include<conio.h> #include<stdio.h> #include<iostream.h> #include<iomanip.h> #include<math.h> class tamgiac {private:float a,b,c; public: void khoitao(float _a,float _b,float _c)

{ a=_a;b=_b;c=_c;} ~tamgiac() {a=b=c=0;} void xem() {printf("a=%8.2f b=%8.2f c=%8.2f S=%8.2f C=%8.2f",a,b,c,S(),C());} void nhap();

float S() {return sqrt((C()/2)*((C()/2)-a)*((C()/2)-b)*((C()/2)-c));} float C() { return (a+b+c);} } tg1,tg2,tg3; void tamgiac::nhap() {do {cout<<"Nhap kich thuoc tam giac\n"; cout<<"Nhap a="; cin>>a; cout<<"Nhap b="; cin>>b; cout<<"Nhap c="; cin>>c; if(!((a+b>c)&&(a+c>b)&&(b+c>a))) cout<<"Nhap chua thoa"; } while (!((a+b>c)&&(a+c>b)&&(b+c>a))); } int main()

{cout<<"Nhap doi tuong tam giac 1\n"; tg1.nhap();tg1.C();tg1.S();cout<<"Nhap doi tuong tam giac 2\n"; tg2.nhap();tg2.C();tg1.S();cout<<"Nhap doi tuong tam giac 3\n"; tg3.nhap();tg3.C();tg1.S();cout<<"\n";getch(); cout<<"Thong tin tam giac1\n";tg1.xem();cout<<"\n"; cout<<"thong tin tam giac 2\n";tg2.xem();cout<<"\n"; cout<<"thong tin tam giac 3\n";tg3.xem();cout<<"\n";getch(); cout<<"Tam giac co s lon nhat\n"; if((tg1.S()>tg2.S())&&(tg1.S()>tg3.S())) tg1.xem();cout<<"\n"; if((tg1.S()<tg2.S())&&(tg2.S()>tg3.S())) tg2.xem();cout<<"\n"; if((tg3.S()>tg1.S())&&(tg3.S()>tg2.S())) tg3.xem();cout<<"\n"; getch(); } Cu 4: Vit chng trnh thc hin: 1/ Ci t lp TAM GIC c cc thuc tnh kch thc a, b, c v cc phng thc: nhp:Nhp kch thc tam gic c kim tra tnh ng n ca tam gic, xem, loi tam gic: tr v loi tam gic thuc ( vung, cn, u, thng). 2/ p dng: - Cp nht N i tng tam gic, N nguyn dng nhp t bn phm - In danh sch cc i tng tam gic ra mn hnh. Bi gii: //Khai bao thu vien

#include<conio.h> #include<stdio.h> #include<iostream.h> #include<iomanip.h> #include<math.h> class tamgiac {private:int a,b,c; public: tamgiac(int _a,int _b,int _c) {a=_a;b=_b;c=_c;} void xem() {printf("a=%5d b=%5d c=%5d\n",a,b,c);} void nhap(); void loaitg(); }; void tamgiac::nhap() {do {cout<<"Nhap kich thuoc tam giac\n"; cout<<"Nhap a=";cin>>a; cout<<"Nhap b=";cin>>b; cout<<"Nhap c=";cin>>c; if(!(((a+b)>c)&&((a+c)>b)&&((b+c)>a))) cout<<"Chua nhap thoa man! Nhap lai\n";

}while (!(((a+b)>c)&&((a+c)>b)&&((b+c)>a))); } void tamgiac::loaitg() {if((a==b)&&(b==c)&&(c==a)) cout<<"a= "<<a<<"b= "<<b<<"c= "<<c<<"-->Tam giac da cho la tam giac deu\n"; else if((a==b)||(b==c)||(c==a)) cout<<"a= "<<a<<"b= "<<b<<"c= "<<c<<"-->Tam giac da cho la tam giac can\n"; else if ((pow(a,2)==pow(b,2)+pow(c,2))||(pow(b,2)==pow(a,2)+pow(c,2))|| (pow(c,2)==pow(a,2)+pow(b,2))) cout<<"a= "<<a<<""<<"b= "<<b<<""<<"c= "<<c<<"-->Tam giac da cho la tam giac vuong\n"; else cout<<"a= "<<a<<"b= "<<b<<"c= "<<c<<"-->Tam giac da cho la tam giac thuong\n"; } int main() {int i,n; tamgiac *ds; cout<<"Nhap n tam giac n=";cin>>n; for(i=0;i<n;i++) ds[i].nhap(); cout<<"\nThong tin tam giac\n"; for(i=0;i<n;i++) ds[i].xem();

cout<<"\nThong tin loai tam giac:\n"; for(i=0;i<n;i++) ds[i].loaitg();getch(); } Cu 5: Vit chng trnh thc hin: 1/ Ci t lp IM m t im trong h ta - Cc c cc thuc tnh ta x, y nguyn v cc phng thc to, hy, nhp, xem, hm bn khong cch: tr v khong cch gia 2 dim cho trc. 2/ p dng: - Cp nht i tng im A, B, C. - Tnh khong cch on thng AB, AC, BC v in kt qu ra mn hnh, kim tra xem AB, AC,BC c to thnh tam gic khng? Thng bo kt qu ra mn hnh. Bi gii: //khai bao thu vien #include <conio.h> #include <stdio.h> #include <iostream.h> #include <iomanip.h> #include <math.h> //khai bao lop class diem {private:int x,y; public:

diem(int _x=0,int _y=0) {x=_x;y=_y;} ~diem() { x=y=0;} void xem() {cout<<"("<<x<<","<<y<<")";} void nhap() { cout<<"x,y= ";cin>>x;cin>>y; } //khai bao Ham ban friend double kc2diem(diem d1,diem d2); }a,b,c; //khai bao cac phuong thuc double kc2diem(diem d1,diem d2) { return (sqrt(pow((d2.x-d1.x),2)+pow((d2.y-d1.y),2)));} //Khai bao chuong trinh chinh int main() {double d,d1,d2; cout<<"Nhap thong tin diem gia tri cach nhau bang dau cach trang vd: 2 3\n"; cout<<"Nhap toa do diem A: ";a.nhap(); cout<<"Nhap toa do diem B: ";b.nhap(); cout<<"Nhap toa do diem C: ";c.nhap();getch(); cout<<"Hien thi toa do cac diem\n"; cout<<"Diem A: ";a.xem();cout<<"\n"; cout<<"Diem B: ";b.xem();cout<<"\n";

cout<<"Diem C: ";c.xem();cout<<"\n"; getch(); d=kc2diem(a,b); d1=kc2diem(a,c); d2=kc2diem(b,c); cout<<"Tinh khoang cach cac doan thang\n"; cout<<"Doan thang AB:"<<d;cout<<"\n"; cout<<"Doan thang AC:"<<d1;cout<<"\n"; cout<<"Doan thang BC:"<<d2; getch(); cout<<"\nKiem tra tinh tam giac\n"; if(((d+d1)>d2)&&((d+d2)>d1)&&((d1+d2)>d)) cout<<"\n Ba dt tao thanh tam giac"; else cout<<"\n Ba dt khong tao thanh tam giac"; getch(); } Cu 9: Vit chng trnh thc hin: 1/ Ci t lp A THC c thuc nh n: cp a thc, *a: cha h s ca a thc v cc phng thc: to, hy, nhp, xem: in a thc ra mn hnh, gi tr: tr v gi tr a thc khi bit gi tr x. 2/ p dng: - Nhp a thc cp N, N nguyn dng nhp t bn phm. - In a thc ra mn hnh

- In ra gi tr a thc vi gi tr x nhp t bn phm. Bi gii: //Khai bao thu vien #include<conio.h> #include<stdio.h> #include<iostream.h> #include<math.h> #include<iomanip.h> //Khai bao cau truc lop class dathuc { private:int n; double *a; public: dathuc(){ n=0;a=NULL;} ~dathuc() {n=0;delete a;} void nhap() { cout<<"Bac cua da thuc n: ";cin>>n; a=new double[n+1]; cout<<"He so cua da thuc: \n"; for(int i=0;i<=n;i++) {cout<<"He so bac: "<<i<<"= ";cin>>a[i];} } void xem() {cout<<"Cac he so: "; for(int i=n;i>=0;i--) if (i==0) cout<<"+"<<a[i]; else if(i==1) cout<<a[i]<<"x";

else if (a[i]>0) cout<<a[i]<<"x^"<<i<<"+"; else if ((a[i-1]<0) && (a[i]<0)) cout<<a[i]<<"x^"<<i; else if(a[i]==0) cout<<"";} float giatri(int x) { double s=0; for(int i=0;i<=n;i++) s=s+(a[i]*pow(x,i)); return s ;} }; int main() { dathuc d;int x; cout<<"NHAP DA THUC: \n";d.nhap();getch(); cout<<"Hien thi da thuc: \n";d.xem(); cout<<"\nTinh gia tri cua da thuc:\n"; cout<<"\n Nhap x: "; cin>>x; cout<<"Gia tri da thuc: "<<d.giatri(x);getch(); } Cu 10: Vit chng trnh thc hin: 1/ Ci t lp IM MN c cc thuc tnh: m sinh vin, h v tn, im b phn, im thi v cc phng thc: to, hy, nhp: m sinh vin a t

ngoi va m bo thuc tnh kha, xem, DHP: tr v im hc phn ca mn hc tnh theo cng thc im_hc_phn=(im_b_phn*3+im_thi*7)/10, XL: tr v xp loi hc tp ca mn hc tnh theo quy ch hin hnh. 2/ p dng: - Nhp danh sch im mn ca N sinh vin, N nguyn dng nhp t bn phm. - In danh sch im tng hp ra mn hnh gm cc thng tin: m sinh vin, h tn, im b phn, im thi, im hc phn, xp loi. - In ra top 10 sinh vin c kt qu mn hc tt nht. - In ra thng tin cn b khi bit m cn b nhp t bn phm - In ra thng tin cn b khi bit h v tn cn b nhp t bn phm. #include<conio.h> #include<stdio.h> #include<iostream.h> #include<iomanip.h> #include<math.h> //Khai bao cau truc lop class diemmon { private: char masv[6] , hoten[25]; float diembp,diemthi; public: diemmon(char _masv[6]="",char _hoten[25]="",float _diembp=0,float _diemthi=0) { strcpy(masv,_masv);

strcpy(hoten,_hoten); } ~diemmon()

diembp=_diembp;diemthi=_diemthi;

{ char masv[6]="";char hoten[25]="";diembp=0;diemthi=0;} float dhp() { return (((diembp*3)+(diemthi*7))/10);} char *getsbd() { return masv;} char *getht() { return hoten;} void nhap(char ma[6]) { strcpy(masv,ma); cin.ignore(1); cout << "Ho ten sinh vien: ";cin.get(hoten,25); cout << "Diem bo phan: ";cin>>diembp; cout << "Nhap diem thi: ";cin >> diemthi; } void xem() { printf("%1s %15s %8.2f %8.2f %8.2f %15s\n",masv,hoten,diembp,diemthi,dhp(),xl());} char *xl() {if(dhp() >= 9 ) return ("Xuat sac"); if(dhp() >= 9 )

return ("Xuat sac"); else if( dhp() >=8 ) return("Gioi"); else if( dhp() >=7 ) return("Kha"); else if( dhp() >=6 ) return("Trung binh kha"); else if( dhp() >=5 ) return("Trung binh"); else return("Kem"); } }; //Khai bao cac thu tuc void nhap(diemmon *ds, int n) { int j=0;int i=0;char ma[6]; while(i<n) { cout<<"Nhap thong tin sinh vien thu:"<<i+1; cout<<"\nMa sinh vien:";cin>>ma; j=0; while((j<i)&&(strcmp(ds[j].getsbd(),ma))!= 0) j++; if(j<i)

cout<<"Ma sinh vien nay da co, nhap lai!\n"; else {ds[i].nhap(ma); i++;} } } //In danh sach diem tong hop void inds(diemmon *ds,int n) { cout<<"\n DANH SACH DIEM TONG HOP \n";

cout<<setw(4)<<"Masv "<<setw(15)<<" Ho ten"<<setw(8)<<" Diem BP"<<setw(8)<<" Diem Thi"<<setw(8)<<" Diem HP"<<setw(15)<<" Xep loai\n"; for(int i=0;i<n;i++) ds[i].xem(); } //In top 10 sinh vien co ket qua tot nhat void intop10(diemmon *ds,int n) { diemmon d; int i,j; cout<<"\n DANH SACH DIEM 10 NGUOI CAO NHAT \n "; cout<<setw(4)<<"Masv "<<setw(15)<<" Ho ten"<<setw(8)<<" Diem BP"<<setw(8)<<" Diem Thi"<<setw(8)<<" Diem HP"<<setw(15)<<" Xep loai\n"; for(i=0;i<n-1;i++) for(j=i+1;j<n;j++) { if(ds[i].dhp()<ds[j].dhp()) {d=ds[i];ds[i]=ds[j]; ds[j]=d;} }

if (n>10) n=10; for(i=0;i<n;i++) ds[i].xem(); } //Thu tuc tim kiem theo ma void timkiemma(diemmon *ds, int n) { char ma[6]; cout<<"Nhap ma sinh vien can tim kiem: ";cin.ignore(1); cin.get(ma,6); int i=0; while ((i<n) && (strcmp(ds[i].getsbd(),ma)!=0)) i++; if(i<n) { cout<<"\nDa tim thay sinh vien co ma: "<<ds[i].getsbd()<<"\n"; cout<<setw(4)<<"Masv "<<setw(15)<<" Ho ten"<<setw(8)<<" Diem BP"<<setw(8)<<" Diem Thi"<<setw(8)<<" Diem HP"<<setw(15)<<" Xep loai\n"; ds[i].xem(); } else cout<<"\nKhong tim thay sinh vien co ma: "<<ds[i].getsbd()<<"\n"; } //Thu tuc tim kiem theo ho ten void timkiemten(diemmon *ds, int n)

{ char ht[25]; cout<<"Nhap ho ten sinh vien can tim kiem:";cin.ignore(1); cin.get(ht,25); int i=0; while ( (i<n) && (strcmp(ds[i].getht(),ht)!=0)) i++; if(i<n) {cout<<"Da tim thay sinh vien co ho ten: "<<ds[i].getht()<<"\n"; cout<<setw(4)<<"Masv "<<setw(15)<<" Ho ten"<<setw(8)<<" Diem BP"<<setw(8)<<" Diem Thi"<<setw(8)<<" Diem HP"<<setw(15)<<" Xep loai\n"; ds[i].xem();} else cout<<"Khong tim thay sinh vien co ho ten: "<<ds[i].getht(); }

//Chuong trinh chinh int main() { int i,n; cout<<"Nhap so sinh vien:";cin>>n; diemmon *ds = new diemmon[n]; cout<<"NHAP DANH SACH SINH VIEN:\n"; nhap(ds,n);getch(); cout<<"HIEN THI THONG TIN DIEM SINH VIEN:\n";

inds(ds,n);getch(); cout<<"HIEN THI THONG TIN 10 SV CO DIEM CAO NHAT\n"; intop10(ds,n);getch(); cout<<"TIM KIEM SINH VIEN THEO MA:\n"; timkiemma(ds,n);getch(); cout<<"TIM KIEM SINH VIEN THEO TEN:\n"; timkiemten(ds,n);getch(); } Cu 11: Vit chng trnh thc hin: 1/ Ci t lp TH SINH c cc thuc tnh: s bo danh, h v tn, ton, l, ha v cc phng thc: to , hy, nhp: s bo danh khng trng, xem, tng: tr v tng im, trng tuyn: tr v 1 nu trng tuyn, ngc li tr v 0 vi iu kin trng tuyn l tng im >=16 khng c mn no im 0. 2/ p dng: - Nhp danh sch im N th sinh, N nguyn dng nhp t bn phm - In danh sch im th sinh ra mn hnh gm cc thng tin: SBD, h v tn, ton, l, ha, tng im. - In danh sch trng tuyn. Bi gii: #include<conio.h> #include<stdio.h> #include<iostream.h> #include<math.h> #include<iomanip.h>

class thisinh { private: char sbd[6],hoten[25]; float toan,ly,hoa; public: thisinh(char ma[6]="",char ht[25]="",float _toan=0,float _ly=0,float _hoa=0) { strcpy(sbd,ma); strcpy(hoten,ht); toan=_toan;ly=_ly;hoa=_hoa; } ~thisinh() {char sbd[6]="";char hoten[25]="";toan=ly=hoa=0;} void nhap(char sobd[10]) { strcpy(sbd,sobd); cin.ignore(1); cout<<"Ho va ten : ";cin.get(hoten,25); cout<<"Nhap Diem toan: "; cin>>toan; cout<<"Nhap diem Ly : "; cin>>ly; cout<<"Nhap diem Hoa: ";cin>>hoa; } void xem() { printf("\n%6s %25s %8.2f %8.2f %8.2f %8.2f",sbd,hoten,toan,ly,hoa,tongdiem());} char *getsbd() { return sbd;}

float tongdiem() {return (toan+ly+hoa);} int trungtuyen() { if((tongdiem()>=16)&&(toan>0)&&(ly>0)&&(hoa>0))

return (1); else return (0);} }; void nhap(thisinh *ds,int n) {int j=0;int i=0;char sobd[10]; while(i<n) { cout<<"Nhap thong tin thi sinh thu: "<<i+1; cout<<"\nSo bao danh: ";cin>>sobd; j=0; while((j<i) && (strcmp(ds[j].getsbd(),sobd)!=0)) j++; if (j<i) cout<<"\nSo bao danh nay da co!, nhap lai\n"; else {ds[i].nhap(sobd); i++;} } } void inds(thisinh *ds,int n) {cout<<"\n THONG TIN SINH VIEN \n";

cout<<setw(6)<<"SoBD"<<setw(20)<<"Ho ten"<<setw(5)<<"Diem toan"<<setw(5)<<"Diem Ly"<<setw(5)<<"Diem hoa"<<setw(5)<<"Tong diem"<<endl; for(int i=0;i<n;i++) ds[i].xem(); } void intt(thisinh *ds,int n) { int k; cout<<"\nDANH SACH TRUNG TUYEN \n"; cout<<setw(6)<<"SoBD"<<setw(20)<<"Ho ten"<<setw(5)<<"Diem toan"<<setw(5)<<"Diem Ly"<<setw(5)<<"Diem hoa"<<setw(5)<<"Tong diem"<<endl; for(k=0;k<n;k++) if(ds[k].trungtuyen()==1) ds[k].xem();} int main() { int i,n; cout<<"Nhap so thi sinh:";cin>>n; thisinh *ds= new thisinh[n]; cout<<"\n NHAP THONG TIN THI SINH\n"; nhap(ds,n);getch(); cout<<"\n HIEN THI THONG TIN THI SINH\n"; inds(ds,n);getch(); cout<<"\n HIEN THI DANH SACH TRUNG TUYEN\n"; intt(ds,n);

getch(); } Cu 14: Vit chng trnh thc hin: 1/ Ci t lp HC SINH c cc thuc tnh: m hc sinh, h v tn, nm sinh, v cc phng thc: to, nhp: m hc sinh khng trng, xem. Ci t lp dn xut IM k tha HC SINH c cc thuc tnh: ton, vn, ngoi ng v cc phng thc: to, nhp, xem, DTB: tr v im trung bnh trong ton vn h s 2, ngoi ng h s 1. 2/ p dng: - Nhp danh sch n hc sinh, in danh sch ra mn hnh - Nhp im tng ng cho hc sinh v in ra mn hnh bng im tng hp Bi gii: #include<conio.h> #include<stdio.h> #include<iostream.h> #include<math.h> #include<iomanip.h> class hocsinh { private: char mahs[6],hoten[25],ngay[10]; public: hocsinh(char ma[6]="",char ht[25]="",char n[10]="") { strcpy(mahs,ma); strcpy(hoten,ht); strcpy(ngay,n); }

char *getsbd(){ return mahs;} void xem() { printf("%4s %10s %10s",mahs,hoten,ngay);} void nhap(char sbd[6]) { strcpy(mahs,sbd); cin.ignore(1); cout<<"Ho va ten : "; cin.get(hoten,25); cin.ignore(1); cout<<"Ngay sinh : ";cin.get(ngay,10);fflush(stdin);} }; class diem: public hocsinh { private: float toan,van,ngoaingu; public: diem(char ma[6]="",char ht[25]="",char n[10]="",float t=0,float v=0,float nn=0):hocsinh(ma,ht,n) {toan=t;van=v;ngoaingu=nn;} void nhap(); void xem() {hocsinh::xem(); printf(" %8.2f %8.2f %8.2f %8.2f\n",toan,van,ngoaingu,dtb()); } float dtb()

{ return ((toan*2+van*2+ngoaingu)/5);} }; void diem::nhap() { cout<<"Diem toan: "; cin>>toan; cout<<"Diem van: "; cin>>van; cout<<"Diem ngoai ngu: ";cin>>ngoaingu; } //Khai bao thu tuc nhap danh sach gom n thi sinh void nhap(diem *ds,int n) { int i=0;char sbd[6]; while(i<n) { cout<<"Nhap thong tin HS thu:"<<i+1; cout<<"\n Ma hoc sinh: "; cin>>sbd; int j=0; while((j<i) && (strcmp(ds[j].getsbd(),sbd)!=0)) j++; if (j<i) cout<<"\nma hoc sinh nay da co\n"; else {ds[i].hocsinh::nhap(sbd);ds[i].nhap(); i++;} } } //Khai bao thu tuc in danh sach hoc sinh void inds(diem *ds,int n) {cout<<"\n THONG TIN HOC SINH \n";

cout<<setw(4)<<"Ma HS"<<setw(10)<<"Ho ten"<<setw(10)<<"nam sinh"<<setw(8)<<"Toan"<<setw(8)<<"Van"<<setw(8)<<"NN"<<setw(8)<<"Tong diem\n"; for(int i=0;i<n;i++) ds[i].xem();} // Chuong trinh chinh int main() { diem *ds; int n; cout<<"Nhap so hoc sinh: "; cin>>n; ds=new diem[n]; cout<<"NHAP THONG TIN HOC SINH\n"; nhap(ds,n); getch(); cout<<"HIEN THI DANH SACH \n"; inds(ds,n);getch(); } Cu 15: Vit chng trnh thc hin: 1/ Ci t lp VT T c cc thuc tnh: m vt t, tn vt t, n v tnh, v cc phng thc: to, nhp, xem. Ci t lp dn xut VT NHP k tha lp VT T c cc thuc tnh: s lng, n gi v cc phng thc: to, nhp, xem, thnh tin: tr v s_lng*n_gi. Ci t lp VT XUT k tha lp VT NHP c cc thuc tnh: s lng, n gi v cc phng thc: to, hy, nhp, xm, thnh tin: s_lng* n_gi. 2/ p dng: - Nhp mt vt t. - Nhp vt t nhp tng ng t bn phm, in vt t nhp ra mn hnh

- Nhp vt t xut t bn phm, s lng xut <=s lng nhp, In vt t xut ra mn hnh. - In thng tin vt t cn cha xut ra mn hnh Bi gii: #include <conio.h> #include <stdio.h> #include <iostream.h> #include <iomanip.h> #include <string.h> #include <stdlib.h> #include <math.h> class Vattu { char MVT[10], TenVattu[30], DVT[20]; public: Vattu(char mvt[6]="", char t[20] ="", char dvt[10]="") { strcpy(MVT,mvt); strcpy(TenVattu,t); strcpy(DVT,dvt); } ~Vattu(){ strcpy(MVT,""); strcpy(TenVattu,""); strcpy(DVT,"");} void Nhap()

{ cout<<"Ma Vat tu: "; cin>>MVT; cin.ignore(1); cout<<"Ten Vat tu: "; cin.get(TenVattu,20); cin.ignore(1); cout<<"Don vi tinh: "; cin.get(DVT,10);} void Xem() { printf("\n%10s %20s %10s",MVT,TenVattu,DVT);} void InVT() { cout<<"\nMa Vat tu: "<<MVT; cout<<"\nTen Vat tu: "<<TenVattu; cout<<"\nDon vi tinh: "<<DVT; } }; class Vattunhap: public Vattu { protected: int SLN; float DGN; public: Vattunhap(char MVT[]="", char t[] ="", char dvt[]="", int sl=0, float dg=0): Vattu(MVT,t,dvt){ SLN=sl; DGN=dg;} void Nhap() { InVT(); cout<<"\nSo luong nhap: "; cin>> SLN; cout<<"Don gia: "; cin>> DGN; cout<<"Thanh tien: "<<TT();

} void Xem() {Vattu::Xem();printf(" %d %10.2f %10.2f", SLN, DGN,TT());} void InVTN() { InVT(); cout<<"\n So luong nhap: "<< SLN; cout<<"\nDon gia nhap: "<< DGN; } float TT(){return SLN*DGN;} }; class Vattuxuat: public Vattunhap { int SLX; float DGX; public: Vattuxuat(char MVT[]="", char t[] ="", char dvt[]="", int sl=0, float dg=0,int slx=0, float dgx=0): Vattunhap(MVT,t,dvt,sl,dg){ SLX=slx; DGX=dgx;} void Nhap(){InVT(); do{ cout<<"\nSo luong xuat: "; cin>> SLX; if(SLX>SLN) cout<<"Khong du so luong xuat, nhap lai!\n"; }while(SLX>SLN); SLN=SLN-SLX; cout<<"Don gia: "; cin>> DGX; cout<<"Thanh tien: "<<TT();}

void Xem() {Vattu::Xem();printf(" %d %10.2f %10.2f", SLX, DGX,TT());} void InVTX() { InVT(); cout<<"\n So luong xuat: "<< SLX; SLN= SLN- SLX; cout<<"\nDon gia xuat: "<< DGX; } float TT(){return SLX * DGX;} }; int main() { Vattuxuat h; cout<<"\nNhap thong tin vat tu: \n"; h.Vattu::Nhap(); cout<<"\nVat tu vua nhap la: \n"; h.Vattu::Xem(); cout<<"\nNhap vat tu:\n"; h.Vattunhap::Nhap(); cout<<"\n Vat tu vua nhap la: \n"; h.Vattunhap::Xem(); cout<<"\nXuat vat tu: \n"; h.Nhap(); cout<<"\nVat tu xuat la: \n";

h.Xem(); cout<<"\nVat tu con lai trong kho la: \n"; h.Vattunhap::Xem(); getch(); return 0; } Cu 16: Vit chng trnh thc hin: 1/ Ci t lp HNG c cc thuc tnh: m hng, tn hng, n v tnh v cc phng thc: to, nhp, xem. Ci t lp dn xut HNG NHP k tha lp HNG c cc thuc tnh: s lng, n gi v cc phng thc: to, nhp, xem, thnh tin: tr v s_lng*n_gi. Ci t lp HNG XUT k tha lp HNG NHP c cc thuc tnh: s lng, n gi, v cc phng thc: to, nhp, xem, thnh tin: s_lng*n_gi. 2/ p dng: - Nhp mt mt hng no - Nhp hng nhp tng ng t bn phm, in hng nhp ra mn hnh - Nhp hng xut t bn phm, s lng xut <= s lng nhp, In hng xut ra mn hnh. - In thng tin hng cn cha xut ra mn hnh Bi gii: #include<conio.h> #include<stdio.h> #include<iostream.h> #include<math.h> #include<iomanip.h>

//Khai bao cau truc lop class hang { private: char mahang[6];char tenhang[20]; char dvtinh[10]; public: hang(char MH[6]="", char TH[6]="",char dvt[10]="") { strcpy(mahang,MH); strcpy(tenhang,TH); strcpy(dvtinh,dvt); } ~hang() {strcpy(mahang,""); strcpy(tenhang,""); strcpy(dvtinh,"");} void nhap() {cout<<"Ma mat hang: ";cin>>mahang;cin.ignore(1); cout<<"Ten mat hang: ";cin.get(tenhang,20); cin.ignore(1); cout<<"Don vi tinh: ";cin.get(dvtinh,10); } void xem() {printf("\n%6s %20s %10s",mahang,tenhang,dvtinh);}

void INHANG() {cout<<"Ma mat hang: "<<mahang<<"\n"; cout<<"Ten mat hang: "<<tenhang<<"\n"; cout<<"Don vi tinh: "<<dvtinh<<"\n"; } }; //Khai bao lop hangnhap class hangnhap:public hang { protected: int soluongn;float dongian; public: hangnhap(char MH[6]="", char TH[6]="",char dvt[10]="",int sln=0,float dgn=0):hang(MH,TH,dvt) {soluongn=sln;dongian=dgn;} void nhaphang() { INHANG(); cout<<"So luong nhap: ";cin>>soluongn; cout<<"Don gia nhap: ";cin>>dongian; cout<<"Thanh tien: "<<tt(); } void xem() { hang::xem();printf("%6d %8.2f %8.2f",soluongn,dongian,tt());}

void INHANGN() { INHANG(); cout<<"So luong nhap: "<<soluongn; cout<<"\nDon gia nhap: "<<dongian; cout<<"\nThanh tien: "<<tt()<<"\n"; } float tt() { return soluongn*dongian;} };

//Khai bao lop hangxuat class hangxuat: public hangnhap {int soluongx;float dongiax; public: hangxuat(char MH[6]="", char TH[6]="",char dvt[10]="",int sln=0,float dgn=0,int slx=0,float dgx=0):hangnhap(MH,TH,dvt,sln,dgn) { soluongx=slx;dongiax=dgx;} void nhapx() {INHANGN(); do {cout<<"So luong xuat: ";cin>>soluongx; if (soluongx>soluongn) cout<<"Ban nhap chua du luong xuat,Nhap lai!";

}while(soluongx>soluongn); soluongn=soluongn-soluongx; cout<<"Don gia xuat: ";cin>>dongiax; cout<<"Thanh tien: "<<tt(); } void xemx() { hang::xem();printf("%6d %8.2f %8.2f",soluongx,dongiax,tt()); } float tt() { return soluongx*dongiax;} void INHANGX() {INHANG(); cout<<"So luong xuat: "<<soluongx; soluongn=soluongn-soluongx; cout<<"Don gia xuat: "<<dongiax; } }; int main() { hangxuat h; cout<<"\nNhap thong tin hang: \n"; h.hang::nhap();getch(); cout<<"\nHang vua nhap la: \n";

h.hang::xem();getch(); cout<<"\nNhap hang:\n"; h.hangnhap::nhaphang();getch(); cout<<"\n Hang vua nhap la: \n"; h.hangnhap::xem();getch(); cout<<"\nhang xuat: \n"; h.nhapx();getch(); cout<<"\n Hang xuat la: \n"; h.xemx();getch(); cout<<"\n Hang con lai trong kho la: \n"; h.hangnhap::xem(); getch(); } Cu 17: Vit chng trnh thc hin 1/ Ci t lp KHCH HNG c cc thuc tnh: h v tn, a ch, in thoi v cc phng thc to, nhp, xem. Ci t lp IN c cc thuc tnh s cng t, ch s mi, ch s c, v cc phng thc: to, nhp: s cng t khng trng, xem. Ci t lp THANH TON k tha t 2 lp KHCH HNG v IN c cc phng thc: lng in: tr v ch_s_mi-ch_s_c, thnh tin: tr v thnh tin bng lng in nhn vi n gi bc thang nh sau t s 1 n 100 gi 750, t s 101 n 150 gi 1200, t s 151 tr ln tnh gi 2500, to, nhp, xem. 2/ p dng: - Nhp danh sch n khch hng t bn phm. N nguyn dng

- In ra thng tin tng hp thanh ton khch hng s dng in trong thng Bi gii: //Khai bao thu vien #include<conio.h> #include<stdio.h> #include<iostream.h> #include<iomanip.h> #include<math.h>

//Khai bao cau truc lop khachhang class khachhang { private: char hoten[25];char diachi[25]; char dienthoai[12]; public: khachhang(char ht[25]="",char dc[25]="",char dt[12]="") {strcpy(hoten,ht); strcpy(diachi,dc); strcpy(dienthoai,dt); } void nhapkh(); void xemkh();

}; // Khai bao cac phuong thuc cua lop khach hang void khachhang::nhapkh() {cout<<"Nhap ho ten: "; cin.ignore(1);cin.get(hoten,25); cout<<"Nhap dia chi: "; cin.ignore(1);cin.get(diachi,25); cout<<"Nhap dien thoai: "; cin.ignore(1);cin.get(dienthoai,12); } void khachhang::xemkh() {cout<<hoten<<diachi<<dienthoai;}

//Khai bao lop dien; class dien { private: char soct[6]; int cscu,csmoi; public: dien(char so[6]="",int _cscu=0,int _csmoi=0) {strcpy(soct,so); cscu=_cscu; csmoi=_csmoi;

} void nhapd(char sct[6]) { strcpy(soct,sct); cout<<"Nhap chi so cu: ";cin>>cscu; cout<<"Nhap chi so moi: ";cin>>csmoi; } void xemd() { printf("\n%6s %5d %5d",soct,cscu,csmoi);} int getcscu() {return cscu;} int getcsmoi() {return csmoi;} char *getsct() {return soct;} }; class thanhtoan: public khachhang, public dien { public: int luongdien() { return (getcsmoi()-getcscu());} int thanhtien() { int tt; if (luongdien()<=100) tt= luongdien()*750;

else if(luongdien()<=150) tt= (luongdien()-100)*1200+100*750; else tt= (luongdien()-150)*2500+(50*1200)+(100*750); return tt; } thanhtoan(char ht[25]="",char dc[25]="",char dt[12]="",char so[6]="",int _cscu=0,int _csmoi=0):khachhang(ht,dc,dt),dien(so,_cscu,_csmoi) {} void nhap() { khachhang::nhapkh(); } void xem() { khachhang::xemkh(); dien::xemd(); printf("%5d %5d\n",luongdien(),thanhtien()); } };

//Khai bao thu tuc nhap n khach hang void nhap(thanhtoan *ds, int n) { int i=0;char sct[6]; while(i<n)

{ cout<<"Nhap thong tin khach hang thu: "<<i+1<<"\n"; cout<<"So cong to: ";cin>>sct; int j=0; while((j<i) && (strcmp(ds[j].getsct(),sct)!=0)) j++; if(j<i) cout<<"So cong to nay da co\n"; else { ds[i].dien::nhapd(sct);ds[i].nhap(); i++; } } getch(); } void hienthi(thanhtoan *ds,int n) { for(int i=0;i<n;i++) ds[i].xem(); getch(); } int main() { int n;

thanhtoan *d; cout<<"Nhap so ho dung dien: ";cin>>n; d= new thanhtoan[n]; nhap(d,n); hienthi(d,n); }

Cu 18: Vit chng trnh thc hin 1/ Ci t lp SINH VIN c cc thuc tnh: m sinh vin, h tn, nm sinh v cc phng thc: to, nhp: m sinh vin khng trng, xem. Ci t lp IM MN c cc thuc tnh: im l thuyt, im thc hnh v cc phng thc: to, nhp, xem. Ci t lp TNG KT k tha t hai lp SINH VIN v IM MN vi cc phng thc: TB: tr v im trung bnh= l thuyt+thc hnh*2)/3, XL: tr v xp loi hc tp da vo im trung bnh, to, nhp, xem. 2/ p dng - Nhp danh sch im sinh vin ca mt lp - In ra mn hnh danh sch tng kt im tng kt ca sinh vin Bi gii: //Khai bao thu vien #include<conio.h> #include<stdio.h> #include<iostream.h> #include<iomanip.h> #include<math.h>

//Khai bao cau truc lop sinhvien class sinhvien { private: char masv[6],hoten[25],ns[10]; public: sinhvien(char ma[6]="",char ht[25]="",char n[10]="") { strcpy(masv,ma); strcpy(hoten,ht); strcpy(ns,n); } void nhap(char msv[6]) { strcpy(masv,msv); cin.ignore(1); cout<<"Ten sinh vien: ";cin.get(hoten,25); cin.ignore(1); cout<<"Nhap ngay sinh: ";cin.get(ns,10);fflush(stdin); } void xem() { printf("%6s %20s %10s",masv,hoten,ns);} char *getmasv() {return masv;}

}; //Khai bao cau truc lop diem mon class diemmon { private: float dlt,dth; public: diemmon(float _dlt,float _dth) { dlt=_dlt;dth=_dth; }

void nhap() { cout<<"Diem ly thuyet: ";cin>>dlt; cout<<"Diem thuc hanh: ";cin>>dth; } void xem() { printf("%8.2f %8.2f",dlt,dth);} float getdlt() {return dlt;} float getdth() {return dth;} }; //Khai bao cau truc lop tongket class tongket: public sinhvien, public diemmon { public:

tongket(char ma[6]="",char ht[25]="",char n[10]="",float _dlt=0,float _dth=0):sinhvien(ma,ht,n),diemmon(_dlt,_dth) {} float dtb() { return ((getdlt()+getdth()*2)/3); }

char *xl() { if (dtb()>=9) return("Xuat sac"); else if(dtb()>=8) return("Gioi"); else if(dtb()>=7) return("Kha"); else if(dtb()>=6) return("Trung Binh Kha"); else if(dtb()>=5) return("Trung Binh"); else return("Yeu"); } void nhap() {diemmon::nhap();} void xem() {

sinhvien::xem();diemmon::xem(); printf("%8.2f %15s\n",dtb(),xl()); } }; //Khai bao cac thu tuc thong thuong void nhap(tongket *ds, int n) { int i=0;char msv[6]; while(i<n) { cout<<"Nhap thong tin sinh vien thu: "<<i+1; cout<<"Ma sinh vien: ";cin>>msv; int j=0; while((j<i) && (strcmp(ds[j].getmasv(),msv)!=0)) j++; if(j<i) cout<<"ma hoc sinh nay da co\n"; else { ds[i].sinhvien::nhap(msv);ds[i].nhap(); i++; } } }

void hienthi(tongket *ds, int n) { cout<<"Danh sach diem cua hoc sinh:\n"; for(int i=0;i<n;i++) ds[i].xem(); } int main() { tongket *d;int n; cout<<"Nhap so sinh vien: ";cin>>n; d=new tongket[n]; cout<<"THONG TIN SINH VIEN:\n"; nhap(d,n); getch(); cout<<"HIEN THI THONG TIN SINH VIEN:\n"; hienthi(d,n); getch(); }

Cu 19: Vit chng trnh thc hin: 1/ Ci t lp CN B c cc thuc tnh: m cn b, h v tn, nm sinh, gii tnh, qu qun v cc phng thc: to, hy, nhp: m cn b khng trng, xem. Ci t lp LNG c cc thuc tnh: HSL v cc phng thc: to, nhp, xem, lng: tr v LCB*HSL, BHYT:1.5%lng, BHXH: 6%lng,

BHTN: 1%lng. (LCB=730000). Ci t lp GIO VIN k tha t hai lp CN B v LNG c cc phng thc: pclop: tr v 40%luong, thc linh: tr v s tin gio vin nhn c sau khi tr i chi ph, to, nhp, xem. 2/ p dng: - Nhp danh sch cn b lng gio vin ca mt khoa no - In ra danh sch lng cn b ra mn hnh Bi gii: //Khai bao thu vien #include<conio.h> #include<stdio.h> #include<iostream.h> #include<iomanip.h> #include<math.h> // Khai bao cau truc lop class canbo { private: char macb[6],hoten[25],ns[10],gt[4],qq[30]; public: canbo(char ma[6]="",char _ht[25]="",char _ns[10]="",char _gt[3]="",char _qq[30]="") { strcpy(macb,ma);strcpy(ns,_ns); strcpy(hoten,_ht);strcpy(gt,_gt); strcpy(qq,_qq);

} ~canbo() { strcpy(macb,"");strcpy(ns,"");strcpy(hoten,"");strcpy(gt,"");strcpy(qq,"");} void nhap(char cb[6]) { strcpy(macb,cb); //cout<<"\nMa can bo: ";cin.ignore(1);cin.get(macb,6); cout<<"Ho va ten: ";cin.ignore(1);cin.get(hoten,25); cout<<"Ngay sinh: ";cin.ignore(1);cin.get(ns,10);fflush(stdin); cout<<"Gioi tinh: ";cin.ignore(1);cin.get(gt,4);fflush(stdin); cout<<"Que quan: ";cin.ignore(1);cin.get(qq,30);fflush(stdin); } void xem() {printf("\n%6s %10s %10s %4s %10s",macb,hoten,ns,gt,qq);} char *getma() {return macb;} }; class luong { private: float hsl; public: luong(float _hsl=0)

{ hsl=_hsl; } void nhap() { cout<<"He so luong: ";cin>>hsl; } void xem() { printf("%8.2f %8.2f %8.2f %8.2f %8.2",hsl,LUONG(),bhyt(),bhxh(),bhtn());} float LUONG() {return (730000*hsl);} float bhyt() {return ((LUONG()*1.5)/100);} float bhxh() {return ((LUONG()*6)/100);} float bhtn() {return ((LUONG()*1)/100);}

}; class giaovien: public canbo, public luong { public: giaovien(char ma[6]="",char _ht[25]="",char _ns[10]="",char _gt[4]="",char _qq[30]="",float _hsl=0):canbo(ma,_ht,_ns,_gt,_qq),luong(_hsl)

{} void nhap() {luong::nhap();} void xem() {canbo::xem();luong::xem(); printf("%8.2f %8.2f",pclop(),thuclinh());} float pclop() {return ((LUONG()*40)/100);} float thuclinh() {return (LUONG()+pclop()-bhyt()-bhxh()-bhtn());} }; // Khai bao thu tuc nhap void nhap(giaovien *ds, int n) { int i=0;int j=0;char cb[10]; while(i<n) { cout<<"Nhap thong tin sinh vien thu: "<<i+1; cout<<"Nhap macb: ";cin>>cb; while((j<i) && (strcmp(ds[j].getma(),cb)!=0)) j++; if(j<i) cout<<"Ma can bo nay da co\n"; else

{ds[i].canbo::nhap(cb);ds[i].nhap(); i++;} } } // Thu tuc In luong void inluong(giaovien *ds, int n) { cout<<"\n VIEN \n"; for(int i=0;i<n;i++) ds[i].xem(); } //Chuong trinh chinhh int main() { giaovien *d; int n; cout<<"Nhap so can bo: ";cin>>n; d= new giaovien[n]; cout<<"NHAP THONG TIN CAN BO:\n"; nhap(d,n); getch(); cout<<"HIEN THI DANH SACH LUONG\n"; inluong(d,n); DANH SACH LUONG GIAO

getch(); } Cu 20: Vit chng trnh thc hin 1/ ci t lp IM m t im trong h ta cc gm cc thuc tnh: ta im x,y nguyn v cc phng thc: to, nhp, xem. Ci t lp dn xut ON THNG k tha lp im c thuc tnh ta im u mt th 2 x1,y1 nguyn v cc phng thc: to, nhp, xem, di: tr v d di ca on thng, hm bn tnh song song: tr v 1 nu 2 on thng song song vi nhau ngc li tr v 0, hm bn tnh vung gc: tr v 1 nu 2 ng thng vung gc, ngc li tr v 0. 2/ p dng: - Nhp ta 2 on thng AB, CD. In ta cc on thng ra mn hnh - In ra mn hnh di 2 on thng AB, CD. Xt tnh vung gc v song song ca 2 ng thng Cu 21:Vit chng trnh thc hin: 1/ Ci t lp ON THNG c ta im 2 u mt l x,y v x1,y1 kiu nguyn v cc thuc tnh to, nhp, xem, di. Ci t lp dn xut TAM GIC k tha lp ON THNG c thuc tnh l ta im th 3 l x2,y2 kiu nguyn v cc phng thc: to, nhp: nhp ta cc im to thnh tam gic trong h XOY, xem, ly S: tr v din tch, ly C: Tr v chu vi. 2/ p dng: - Cp nht ta ca tam gic t bn phm - In ra thng tin ca tam gic . Bi gii: //Khai bao thu vien

#include<conio.h> #include<stdio.h> #include<iostream.h> #include<math.h> //khai bao cau truc lop class doanthang { private: int x,y,x1,y1; public: doanthang(int _x=0,int _y=0,int _x1=0,int _y1=0) { x=_x;y=_y;x1=_x1;y1=_y1;} void nhap() { cout<<"Nhap x: ";cin>>x; cout<<"Nhap y: ";cin>>y; cout<<"Nhap x1: ";cin>>x1; cout<<"Nhap y1: ";cin>>y1; } void xem() { cout<<"\nToa do diem 1: ("<<x<<","<<y<<")"; cout<<"\nToa do diem 2: ("<<x1<<","<<y1<<")"; } int getx(){return x;}

int gety(){return y;} int getx1(){return x1;} int gety1(){return y1;}

float dodai(int a,int b,int c, int d) { return sqrt(pow(c-a,2)+pow(d-b,2));} }; //khai bao lop ke thua class tamgiac: public doanthang { private: int x2,y2; public: tamgiac(int _x=0,int _y=0,int _x1=0,int _y1=0, int _x2=0,int _y2=0):doanthang(_x,_y,_x1,_y1) { x2=_x2;y2=_y2;} void nhap(); void xem() { doanthang::xem(); cout<<"\nToa do diem 3: ("<<x2<<","<<y2<<")"; cout<<"\nChu vi tam giac: "<<C(); cout<<"\nDien tich tam giac: "<<S(); } float C()

{ float a,b,c; a=dodai(getx(),gety(),getx1(),gety1()); b=dodai(getx(),gety(),x2,y2); c=dodai(getx1(),gety1(),x2,y2); return (a+b+c); } float S() { float a,b,c; a=dodai(getx(),gety(),getx1(),gety1()); b=dodai(getx(),gety(),x2,y2); c=dodai(getx1(),gety1(),x2,y2); return sqrt((C()/2)*((C()/2)-a)*((C()/2)-b)*((C()/2)-c)); } }; void tamgiac::nhap() { float a,b,c; do { doanthang::nhap(); cout<<"Nhap x2: ";cin>>x2; cout<<"Nhap y2: ";cin>>y2; a=dodai(getx(),gety(),getx1(),gety1()); b=dodai(getx(),gety(),x2,y2);

c=dodai(getx1(),gety1(),x2,y2); if (!((a+b>c) && (a+c>b) && (c+b>a))) cout<<"Nhap lai"; else cout<<"Thoa man tam giac\n"; }while (!((a+b>c) && (a+c>b) && (c+b>a))); } int main() { tamgiac d; cout<<"Nhap thong tin tam giac: \n";d.nhap(); getch(); cout<<"Hien thi thong tin tam giac:"; d.xem(); getch(); }

You might also like