bai tap c++

Màu nền
Font chữ
Font size
Chiều cao dòng

06-03

#include<iostream.h>

#include<conio.h>

class bac1{

private:

            float a,b;

public:

            void nhap(){

            cout<<"Nhap a: ";cin>>a;

            cout<<"Nhap b: ";cin>>b;

}

void giai(){

            if(a==0)

                  if(b==0) cout<<"

Phuong trinh vo so nghiem

";

                  else cout<<"

Phuong trinh vo nghiem

";

            else cout<<"

Nghiem cua phuong trinh la X= "<<(-b/a)<<"

";

}

};//het lop

void main(){

            bac1 pt[10];

            for(int i=0;i<10;i++){

                cout<<"

Doi tuong thu "<<i+1<<" :

";

                pt[i].nhap();

                pt[i].giai();

                                         }

getch();

}

07-03

#include<iostream.h>

#include<conio.h>

#include<math.h>

class bac2{

private:

            float a,b,c;

public:

            void nhap(){

            cout<<"Nhap a: ";cin>>a;

            cout<<"Nhap b: ";cin>>b;

            cout<<"Nhap c: ";cin>>c;

}

void giai(){

            if(a==0)

                if(b==0)

                         if(c==0) cout<<"

Phuong trinh vo so nghiem";

                         else cout<<"

Phuong trinh vo nghiem";

                else cout<<"

Nghiem cua phuong trinh la X= "<<(-c/b);

            else {

                float dt=b*b-4*a*c;

                if(dt<0) cout<<"

Phuong trinh vo nghiem";

                else if(dt==0) cout<<"

Nghiem kep cua phuong trinh la: "<<(-b)/(2*a);

                         else{

                             cout<<"

Nghiem cua phuong trinh la:

";

                             cout<<"X1= "<<((-b+sqrt(dt))/(2*a))<<"

";

                             cout<<"X2= "<<((-b-sqrt(dt))/(2*a));

                             }

                  }

            }

};

void main(){

            bac2 pt[5];

            for(int i=0;i<5;i++){

                cout<<"

Doi tuong thu "<<i+1<<" :

";

                pt[i].nhap();

                pt[i].giai();

                                        }

getch();

}

08-03

#include<iostream.h>

#include<conio.h>

class circle{

private:

            float r;

public:

            circle(){r=0;}

            circle(float d){r=d;}

            circle(circle &r1){r=r1.r;}

            float dientich(){

                        return(3.14*r*r);

                                    }

            void display(){ cout<<"

Dien tich duong tron : "<<dientich();}

            friend float operator+(circle &a,circle &b){

                        return(a.dientich()+b.dientich());

                                                                           }

};// het lop

void main(){

float r1,r2;

cout<<"

Nhap ban kinh duong tron thu nhat :";cin>>r1;

cout<<"

Nhap ban kinh duong tron thu hai  :";cin>>r2;

circle c1(r1),c2(r2);       

c1.display();c2.display();

cout<<"

Tong dien tich hai duong tron la: "<<c1+c2;

getch();

}

09-03

#include<iostream.h>

#include<conio.h>

class rectangle{

private:

            float a,b;

public:

            rectangle(){a=b=0;}

            rectangle(float x,float y){a=x;b=y;}

            rectangle(rectangle &cn){a=cn.a;b=cn.b;}

            float dientich(){return(a*b);}

            void display(){cout<<"

Dien tich hinh chu nhat la : "<<dientich();}

            friend float operator+(rectangle &cn1,rectangle &cn2){

                        return(cn1.dientich()+cn2.dientich());

                        }

};

void main(){

float a1,a2,b1,b2;

cout<<"Nhap chieu dai ";cin>>a1;

cout<<"Nhap chieu rong ";cin>>b1;

cout<<"Nhap chieu dai ";cin>>a2;

cout<<"Nhap chieu rong ";cin>>b2;

rectangle h1(a1,b1),h2(a2,b2);

h1.display();h2.display();

cout<<"

Tong dien tich hai hinh la: "<<h1+h2;

getch();

}

10-03

#include<iostream.h>

#include<conio.h>

class mytime{

private:

            int h,m,s;

public:

            mytime(int hh=0,int mm=0,int ss=0){h=hh;m=mm;s=ss;}

            void set(int hh,int mm,int ss){h=hh;m=mm;s=ss;}

            void display(){

                        cout<<h<<":"<<m<<":"<<s;

            }

            friend int operator>(mytime &t1,mytime &t2);

};

            int operator>(mytime &t1,mytime &t2){

                        if(t1.h>t2.h) return 1;

                        if((t1.h==t2.h)&&(t1.m>t2.m)) return 1;

                        if((t1.h==t2.h)&&(t1.m==t2.m)&&(t1.s>t2.s)) return 1;

                        return 0;

            }

void main(){

mytime t1,t2;

int hh,mm,ss;

cout<<"nhap thoi gian t1(gio,phut,giay):";cin>>hh>>mm>>ss;

t1.set(hh,mm,ss);

cout<<"nhap thoi gian t2(gio,phut,giay):";cin>>hh>>mm>>ss;

t2.set(hh,mm,ss);

cout<<"

Thoi gian lon hon la: ";

if(t1>t2) t1.display();else t2.display();

getch();

}

11-03

#include<iostream.h>

#include<conio.h>

class mydate{

private:

            int d,m,y;

public:

            mydate(int dd=1,int mm=1,int yy=1980){d=dd;m=mm;y=yy;}

            void set(int dd,int mm,int yy){d=dd;m=mm;y=yy;}

            void display(){cout<<d<<"-"<<m<<"-"<<y;}

            friend int operator>(mydate &d1,mydate &d2);

};

            int operator>(mydate &d1,mydate &d2){

                        if(d1.y>d2.y) return 1;

                        if((d1.y==d2.y)&&(d1.m>d2.m)) return 1;

                        if((d1.y==d2.y)&&(d1.m==d2.m)&&(d1.d>d2.d))return 1;

                        return 0;

            }

void main(){

int dd,mm,yy;

mydate d1,d2;

cout<<"

Nhap thoi gian d1(ngay,thang,nam): ";cin>>dd>>mm>>yy;

d1.set(dd,mm,yy);

cout<<"

Nhap thoi gian d2(ngay,thang,nam): ";cin>>dd>>mm>>yy;

d2.set(dd,mm,yy);

cout<<"

Thoi gian lon hon la: ";

if(d1>d2) d1.display(); else d2.display();

getch();

}

12-03

#include<iostream.h>

#include<conio.h>

class mydate{

private:

            int d,m,y;

public:

            mydate(int dd=1,int mm=1,int yy=1980){d=dd;m=mm;y=yy;}

            void set(int dd,int mm,int yy){d=dd;m=mm;y=yy;}

            void display(){cout<<d<<"-"<<m<<"-"<<y;}

            friend mydate operator++(mydate &a);

};

            mydate operator++(mydate &a){

              mydate b=a;

              if(((a.m==4)||(a.m==6)||(a.m==9)||(a.m==11))&&(a.d==30)){

                  b.d=1;b.m=a.m+1; return b;

              }

              if(((a.m==1)||(a.m==3)||(a.m==5)||(a.m==7)||(a.m==8)||(a.m==10))&&(a.d==31)){

                  b.d=1;b.m=a.m+1; return b;

             }

              if((a.d==31)&&(a.m==12)) {b.d=1;b.m=1;b.y=a.y+1; return b;}

              if(a.m==2){

                if((a.y%4==0)&&(a.d==29)){b.d=1;b.m=a.m+1; return b;}

                if((a.y%4!=0)&&(a.d==28)){b.d=1;b.m=a.m+1; return b;}

              }

              b.d=a.d+1;

              return b;

}

void main(){

int dd,mm,yy;

mydate d1,d2;

cout<<"

Nhap ngay , thang , nam :";cin>>dd>>mm>>yy;

d1.set(dd,mm,yy);

cout<<"

Sau khi tang ngay len 1 don vi:";(++d1).display();

getch();

}

13.-03

#include<iostream.h>

#include<conio.h>

class datetime{

private:

            int n,m,y,hh,mm,ss;

public:

            datetime(int a=1,int b=1,int c=1980,int d=0,int e=0,int f=0){

                        n=a;m=b;y=c;hh=d;mm=e;ss=f;

            }

            void set(int a,int b,int c,int d,int e,int f){

                        n=a;m=b;y=c;hh=d;mm=e;ss=f;

            }

            void display(){cout<<hh<<" gio "<<mm<<" phut "<<ss<<" giay";

                        cout<<", ngay "<<n<<"-"<<m<<"-"<<y;

            }

            friend int operator>(datetime &dt1,datetime &dt2);

};

            int operator>(datetime &dt1,datetime &dt2){

              if(dt1.y>dt2.y)return 1;

              if((dt1.y==dt2.y)&&(dt1.m>dt2.m))return 1;

              if((dt1.y==dt2.y)&&(dt1.m==dt2.m)&&(dt1.n>dt2.n))return 1;

              if((dt1.y==dt2.y)&&(dt1.m==dt2.m)&&(dt1.n==dt2.n)&&(dt1.hh>dt2.hh)) return 1;

              if((dt1.y==dt2.y)&&(dt1.m==dt2.m)&&(dt1.n==dt2.n)&&(dt1.hh==dt2.hh)&&(dt1.mm>dt2.mm)) return 1;

              if((dt1.y==dt2.y)&&(dt1.m==dt2.m)&&(dt1.n==dt2.n)&&(dt1.hh==dt2.hh)&&(dt1.mm==dt2.mm)&&(dt1.ss>dt2.ss)) return 1;

              return 0;

}

void main(){

int a,b,c,d,e,f;

datetime dt1;

cout<<"

Nhap ngay , thang , nam :";cin>>a>>b>>c;

cout<<"

Nhap gio , phut , giay :";cin>>d>>e>>f;

dt1.set(a,b,c,d,e,f);dt1.display();

getch();

}

14-03

#include<iostream.h>

#include<conio.h>

class mydate{

private:

            int d,m,y;

public:

            mydate(int dd=1,int mm=1,int yy=1){

                        d=dd;m=mm;y=yy;

                                                              }

            void set(int dd,int mm,int yy){

                        d=dd;m=mm;y=yy;

                                                      }

            void display(){

                        cout<<d<<"-"<<m<<"-"<<y;

                              }

            friend int operator>(mydate &d1,mydate &d2);

            friend int operator>=(mydate &d1,mydate &d2);

            friend int operator<(mydate &d1,mydate &d2);

            friend int operator<=(mydate &d1,mydate &d2);

};//het lop

            int operator>(mydate &d1,mydate &d2){

               if(d1.y>d2.y) return 1;

               if((d1.y==d2.y)&&(d1.m>d2.m)) return 1;

               if((d1.y==d2.y)&&(d1.m==d2.m)&&(d1.d>d2.d))return 1;

               return 0;

            }

            int operator>=(mydate &d1,mydate &d2){

               if(d1.y>=d2.y) return 1;

               if((d1.y==d2.y)&&(d1.m>=d2.m)) return 1;

               if((d1.y==d2.y)&&(d1.m==d2.m)&&(d1.d>=d2.d))return 1;

               return 0;

            }

            int operator<(mydate &d1,mydate &d2){

               if(d1.y<d2.y) return 1;

               if((d1.y==d2.y)&&(d1.m<d2.m)) return 1;

               if((d1.y==d2.y)&&(d1.m==d2.m)&&(d1.d<d2.d))return 1;

               return 0;

            }

            int operator<=(mydate &d1,mydate &d2){

               if(d1.y<=d2.y) return 1;

               if((d1.y==d2.y)&&(d1.m<=d2.m)) return 1;

               if((d1.y==d2.y)&&(d1.m==d2.m)&&(d1.d<=d2.d))return 1;

               return 0;

            }

void main(){

mydate date[2];

int d,m,y;

for(int i=0;i<2;i++){

cout<<"

Nhap doi tuong thu "<<i<<"

";

cout<<"Nhap ngay,thang,nam: ";cin>>d>>m>>y;

date[i].set(d,m,y);

}

if(date[0]>date[1]) cout<<"

Doi tuong thu nhat lon hon";

else cout<<"doi tuong thu hai lon hon";

getch();

}

15-03

#include<iostream.h>

#include<conio.h>

#include<string.h>

#include<stdio.h>

class person{

private:

            char *name;

            char *address;

            long int phone;

public:

            person(char *ten,char *dc,long int dt){

                        name=new char[strlen(ten)+1];

                        strcpy(name,ten);

                        address=new char[strlen(dc)+1];

                        strcpy(address,dc);

                        phone=dt;

            }

            ~person(){

                        delete name;

                        delete address;

            }

            char *getname(){ return name;}

            char *getaddress(){ return address;}

            long int getphone(){ return phone;}

            void display(){

                        cout<<"

name: "<<name;

                        cout<<"

address :"<<address;

                        cout<<"

so dien thoai :"<<phone;

            }

};//het lop

class list{

private:

            int maxsize;

            person **people;

public:

            list(int max);

            ~list();

            void nhap();

            void display();

            void find(char *);

};//het lop

            list::list(int max){

                        maxsize=max;

                        people=new person *[maxsize];

            }

            list::~list(){

            for(int i=0;i<maxsize;i++) delete people[i];

                        delete people;

                        }

            void list::nhap(){

                        long int dt;

                        char ten[30];

                        char dc[30];

                        for(int i=0;i<maxsize;i++){

                        cout<<"

ten : ";gets(ten);

                        cout<<"

dia chi: ";gets(dc);

                        cout<<"

so dien thoai: ";cin>>dt;

                        people[i]=new person(ten,dc,dt);

                                                            }

}

            void list::display(){

                        for(int i=0;i<maxsize;i++) people[i]->display();

                                                }

            void list::find(char *ten){

                        int index=-1;

                        for(int i=0;i<maxsize;i++)

                        if(strcmp(people[i]->getname(),ten)==0) index=i;

                        if(index==-1) cout<<"\ Khong co ten nay";

                        else people[index]->display();

                                                            }

void main(){

clrscr();

list canbo(3);

canbo.nhap();

canbo.display();

char ten[30];

cout<<"

Nhap ho ten can tim: ";gets(ten);

canbo.find(ten);

getch();

}

16A-03

#include<iostream.h>

#include<conio.h>

#include<string.h>

#include<stdio.h>

class mathang{

private:

            char ten[20];

            int soluong;

            float giamua,giaban;

            float chenhlech(){ return(giaban-giamua);}

public:

            void nhaphang(int q){ soluong+=q;}

            void xuathang(int q){ soluong-=q;}

            mathang(char *t,int s,float m,float b){

                        strcpy(ten,t);

                        soluong=s;

                        giamua=m;

                        giaban=b;

                                                                  }

            friend ostream& operator<<(ostream &out,mathang &mh);

};//het lop

            ostream& operator<<(ostream &out,mathang &mh){

                        out<<"

============================

";

                        out<<"

Ten mat hang: "<<mh.ten;

                        out<<"

So luong: "<<mh.soluong;

                        out<<"

Gia mua: "<<mh.giamua;

                        out<<"

Gia ban: "<<mh.giaban;

                        out<<"

Chenh lech gia mua - ban: "<<mh.chenhlech();

                        return out;

                                                                                       }

void main(){

mathang *pmh[5];

char *ten;

int s;

float m,b;

for(int i=0;i<5;i++){

            cout<<"

Nhap mat hang thu :"<<i+1;

            ten=new char[20];

            cout<<"

Ten hang: ";gets(ten);

            cout<<"

So luong: ";cin>>s;

            cout<<"

Gia mua:";cin>>m;

            cout<<"

Gia ban: ";cin>>b;

            pmh[i]=new mathang(ten,s,m,b);

                            }

pmh[1]->nhaphang(2);

pmh[2]->xuathang(2);

for(i=0;i<5;i++)

cout<< *pmh[i];

getch();

}

16B -03

#include<iostream.h>

#include<conio.h>

#include<math.h>

class point{

private:

            int x,y;

public:

            point(int a=0,int b=0){x=a;y=b;}

            point(point &a){x=a.x;y=a.y;}

            void display(){

                        cout<<"

Toa do: "<<x<<"  "<<y;

                              }

            friend float kcach(point &a,point &b){

                        return(sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2)));

                                                                 }

};//het lop

class line{

private:

            point a,b;

public:

            line(point &x,point &y){a=x;b=y;}

            float distance(){ return(kcach(a,b));}

            void display(){

                        a.display();

                        b.display();

                        cout<<"

Chieu dai doan thang: "<<distance();

                              }

};//het lop

void main(){

int x1,y1,x2,y2;

cout<<"

Nhap toa do diem thu nhat (x1,y1): ";cin>>x1>>y1;

cout<<"

Nhap toa do diem thu hai (x2,y2): ";cin>>x2>>y2;

point a(x1,y1),b(x2,y2);

line l(a,b);

l.display();

getch();

}

17-03

#include<iostream.h>

#include<conio.h>

#include<math.h>

class point{

private:

            float x,y;

public:

            point(float a=0,float b=0){x=a;y=b;}

            void display(){

                        cout<<x<<"  "<<y<<"

";

                              }

            friend float kcach(point &a,point &b){

                        return (sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2)));

                                                                 }

};//het lop

class triangle{

private:

            point a,b,c;

public:

            triangle(point x,point y,point z){a=x;b=y;c=z;}

            float chuvi(){

                        return (kcach(a,b)+kcach(a,c)+kcach(b,c));

                             }

            void display(){

                        cout<<"Toa do 3 diem :

";

                        cout<<"Toa do diem A :";a.display();

                        cout<<"Toa do diem B :";b.display();

                        cout<<"Toa do diem C :";c.display();

                        cout<<"Chu vi tam giac : "<<chuvi()<<"

";

                               }

};//het lop

void main(){

float x1,x2,x3,y1,y2,y3;

cout<<"

Nhap toa do 3 diem :

";

cout<<"Diem thu nhat :";cin>>x1>>y1;

cout<<"Diem thu hai  :";cin>>x2>>y2;

cout<<"Diem thu ba   :";cin>>x3>>y3;

point a(x1,y1),b(x2,y2),c(x3,y3);

triangle t(a,b,c);

t.display();

getch();

}

18-03

#include<iostream.h>

#include<conio.h>

class point{

private:

            float x,y;

public:

            point(float a=0,float b=0){x=a;y=b;}

            void move(float dx,float dy){x=x+dx;y=y+dy;}

            void display(){cout<<x<<"  "<<y<<"

";}

};

class triangle{

private:

            point a,b,c;

public:

            triangle(point x,point y,point z){a=x;b=y;c=z;}

            void move(float dx,float dy){

                a.move(dx,dy);b.move(dx,dy);c.move(dx,dy);

            }

            void display(){

                cout<<"Toa do diem A: ";a.display();

                cout<<"Toa do diem B: ";b.display();

                cout<<"Toa do diem C: ";c.display();

            }

};

void main(){

float x1,x2,x3,y1,y2,y3;

cout<<"

Nhap toa do diem thu nhat:";cin>>x1>>y1;

cout<<"

Nhap toa do diem thu hai :";cin>>x2>>y2;

cout<<"

Nhap toa do diem thu ba  :";cin>>x3>>y3;

point a(x1,y1),b(x2,y2),c(x3,y3);

triangle h(a,b,c);

cout<<"Toa do cac dinh cua tam giac truoc khi tinh tien

";

h.display();

h.move(2,3);

cout<<"Toa do cac dinh cua tam giac sau khi tinh tien

";

h.display();

getch();

}

22-03

#include<iostream.h>

#include<conio.h>

#include<math.h>

class ps{

private:

            int ts,ms;

public:

            ps(int t=0,int m=1){ts=t;ms=m;}

            void set(int t,int m){ts=t;ms=m;}

            void nhap();

            void display(){

                        cout<<ts<<"/"<<ms;

            }

            friend ps rutgon(ps &a);

            friend ps operator+(ps &,ps &);

};//het lop

            void ps::nhap(){

                        cout<<"

Nhap tu so: ";cin>>ts;

                        do{

                          cout<<"

Nhap mau so: ";cin>>ms;

                        }while(ms==0);

            }

            ps rutgon(ps &a){

            int x,y;

                x=abs(a.ts);y=abs(a.ms);

                if(x==0)return a;

                while(x!=y) if(x>y)x=x-y; else y=y-x;

                a.ts=a.ts/x;a.ms=a.ms/x;

                return a;

                                    }

            ps operator+(ps &a,ps &b){

            return rutgon(ps(a.ts*b.ms+a.ms*b.ts,a.ms*b.ms));

}

void main(){

ps a,b;

a.nhap();

b.nhap();

cout<<"tong cua chung la:";(a+b).display();

getch();

}

23-03

//cac toan tu duoc dinh nghia la ham ban cua lop

#include<iostream.h>

#include<conio.h>

#include<math.h>

class ps{

private:

int ts,ms;

public:

            ps(int tu=0,int mau=1){ts=tu;ms=mau;}

            friend ps toigian(ps &a);

            friend ps operator+(const ps &a,const ps &b){

                return toigian(ps(a.ts*b.ms+b.ts*a.ms,a.ms*b.ms));

            }

            friend ps operator-(const ps &a,const ps &b){

                return toigian(ps(a.ts*b.ms-b.ts*a.ms,a.ms*b.ms));

            }

            friend ps operator/(const ps &a,const ps &b){

                return toigian(ps(a.ts*b.ms,b.ts*a.ms));

            }

            friend ps operator*(const ps &a,const ps &b){

                return toigian(ps(a.ts*b.ts,b.ms*a.ms));

            }

            friend int operator>(const ps &a,const ps &b){

                return((a.ts*b.ms-b.ts*a.ms)*a.ms*b.ms>0);

            }

            friend int operator>=(const ps &a,const ps &b){

                return((a.ts*b.ms-b.ts*a.ms)*a.ms*b.ms>=0);

            }

            friend int operator<(const ps &a,const ps &b){

                return((a.ts*b.ms-b.ts*a.ms)*a.ms*b.ms<0);

            }

            friend int operator<=(const ps &a,const ps &b){

                return((a.ts*b.ms-b.ts*a.ms)*a.ms*b.ms<=0);

            }

            friend int operator==(const ps &a,const ps &b){

                return(a.ts*b.ms==b.ts*a.ms);

            }

            friend int operator!=(const ps &a,const ps &b){

                return(a.ts*b.ms!=b.ts*a.ms);

            }

            friend ps operator-(const ps &a){

            return ps(-a.ts,a.ms);

            }

            friend ps operator++(ps &a){

            return a+1;

            }

            friend ps operator--(ps &a){

            return a-1;

            }

            friend ps operator +=(ps &a,const ps &b){

            a=a+b;

            return a;

            }

            friend ps operator -=(ps &a,const ps &b){

            a=a-b;

            return a;

            }

            friend ostream &operator <<(ostream &out,const ps &a);

            friend istream &operator >>(istream &in,ps &a);

};

            ostream &operator <<(ostream &out,const ps &a){

            ps c=a;

            toigian(c);

            out<<c.ts<<"/"<<c.ms;

            return out;

            }

            istream &operator >>(istream &in,ps &a){

            cout<<"

nhap tu so: ";

            in>>a.ts;

            do{

              cout<<"

nhap mau so :";in>>a.ms;

              }while(a.ms==0);

            return in;

            }

            ps toigian(ps &a){

                int x,y;

                x=abs(a.ts);y=abs(a.ms);

                if(x==0) return a;

                while(x!=y) if(x>y) x=x-y;else y=y-x;

                return (ps(a.ts/x,a.ms/x));

            }

void main(){

clrscr();

ps a[5],tong,tich(1,1);

            for(int i=0;i<5;i++){

            cout<<"

nhap phan so thu "<<i+1<<"

";

            cin>>a[i];

            tong+=a[i];

            tich=tich*a[i];

            }

cout<<"tong la: "<<tong<<"

";

cout<<"tich la: "<<tich;

ps min,max;

            min=a[0];max=a[0];

            for(i=1;i<5;i++){

            if (a[i]<min) min=a[i];

            if (a[i]>max) max=a[i];

            }

cout<<"

phan so lon nhat la: "<<max;

cout<<"

phan so be nhat la: "<<min;

ps tmp;

            for(i=0;i<4;i++)

            for(int j=i+1;j<5;j++)

                        if(a[i]>a[j]){

                              tmp=a[i];

                              a[i]=a[j];

                              a[j]=tmp;

                                         }

cout<<"

mang sau khi sap xep

";

for(i=0;i<5;i++) cout<<a[i]<<"   ";

getch();

}

23A-03

//cac toan tu duoc dinh nghia la ham ban cua lop

#include<iostream.h>

#include<conio.h>

#include<math.h>

class ps{

private:

int ts,ms;

public:

            ps(int tu=0,int mau=1){ts=tu;ms=mau;}

            friend ps toigian(ps &a);

            friend ps operator+(const ps &a,const ps &b){

                return toigian(ps(a.ts*b.ms+b.ts*a.ms,a.ms*b.ms));

            }

            friend ps operator-(const ps &a,const ps &b){

                return toigian(ps(a.ts*b.ms-b.ts*a.ms,a.ms*b.ms));

            }

            friend ps operator/(const ps &a,const ps &b){

                return toigian(ps(a.ts*b.ms,b.ts*a.ms));

            }

            friend ps operator*(const ps &a,const ps &b){

                return toigian(ps(a.ts*b.ts,b.ms*a.ms));

            }

            friend int operator>(const ps &a,const ps &b){

                return((a.ts*b.ms-b.ts*a.ms)*a.ms*b.ms>0);

            }

            friend int operator>=(const ps &a,const ps &b){

                return((a.ts*b.ms-b.ts*a.ms)*a.ms*b.ms>=0);

            }

            friend int operator<(const ps &a,const ps &b){

                return((a.ts*b.ms-b.ts*a.ms)*a.ms*b.ms<0);

            }

            friend int operator<=(const ps &a,const ps &b){

                return((a.ts*b.ms-b.ts*a.ms)*a.ms*b.ms<=0);

            }

            friend int operator==(const ps &a,const ps &b){

                return(a.ts*b.ms==b.ts*a.ms);

            }

            friend int operator!=(const ps &a,const ps &b){

                return(a.ts*b.ms!=b.ts*a.ms);

            }

            friend ps operator-(const ps &a){

            return ps(-a.ts,a.ms);

            }

            friend ps operator++(ps &a){

            return a+1;

            }

            friend ps operator--(ps &a){

            return a-1;

            }

            friend ps operator +=(ps &a,const ps &b){

            a=a+b;

            return a;

            }

            friend ps operator -=(ps &a,const ps &b){

            a=a-b;

            return a;

            }

            friend ostream &operator <<(ostream &out,const ps &a);

            friend istream &operator >>(istream &in,ps &a);

};

            ostream &operator <<(ostream &out,const ps &a){

            ps c=a;

            toigian(c);

            out<<c.ts<<"/"<<c.ms;

            return out;

            }

            istream &operator >>(istream &in,ps &a){

            cout<<"

nhap tu so: ";  in>>a.ts;

            do{

              cout<<"

nhap mau so :";in>>a.ms;

              }while(a.ms==0);

            if(a.ms<0){a.ts=-a.ts;a.ms=-a.ms;}

            return in;

            }

            ps toigian(ps &a){

                int x,y;

                x=abs(a.ts);y=abs(a.ms);

                if(x==0) return a;

                while(x!=y) if(x>y) x=x-y;else y=y-x;

                return (ps(a.ts/x,a.ms/x));

            }

void main(){

clrscr();

ps a[5],tong,tich(1,1);

            for(int i=0;i<5;i++){

            cout<<"

nhap phan so thu "<<i+1<<"

";

            cin>>a[i];

            tong+=a[i];

            tich=tich*a[i];

            }

cout<<"tong la: "<<tong<<"

";

cout<<"tich la: "<<tich;

ps min,max;

            min=a[0];max=a[0];

            for(i=1;i<5;i++){

            if (a[i]<min) min=a[i];

            if (a[i]>max) max=a[i];

            }

cout<<"

phan so lon nhat la: "<<max;

cout<<"

phan so be nhat la: "<<min;

ps tmp;

            for(i=0;i<4;i++)

            for(int j=i+1;j<5;j++)

                        if(a[i]>a[j]){

                              tmp=a[i];

                              a[i]=a[j];

                              a[j]=tmp;

                                         }

cout<<"

mang sau khi sap xep

";

for(i=0;i<5;i++) cout<<a[i]<<"   ";

getch();

}

23B - 03

//cac toan tu duoc dinh nghia la ham thanh phan cua lop

#include<iostream.h>

#include<conio.h>

#include<math.h>

class ps{

private:

int ts,ms;

public:

            ps(int tu=0,int mau=1){ts=tu;ms=mau;}

            friend ps toigian(ps &a);

            ps operator+(const ps &a){

                return toigian(ps(ts*a.ms+a.ts*ms,ms*a.ms));

            }

            ps operator-(const ps &a){

                return toigian(ps(ts*a.ms-a.ts*ms,a.ms*ms));

            }

            ps operator/(const ps &a){

                return toigian(ps(ts*a.ms,a.ts*ms));

            }

            ps operator*(const ps &a){

                return toigian(ps(ts*a.ts,ms*a.ms));

            }

            int operator>(const ps &a){

                return((ts*a.ms-a.ts*ms)*a.ms*ms>0);

            }

            int operator>=(const ps &a){

                return((ts*a.ms-a.ts*ms)*ms*a.ms>=0);

            }

            int operator<(const ps &a){

                return((ts*a.ms-a.ts*ms)*ms*a.ms<0);

            }

            int operator<=(const ps &a){

                return((ts*a.ms-a.ts*ms)*ms*a.ms<=0);

            }

            int operator==(const ps &a){

                return(ts*a.ms==a.ts*ms);

            }

            int operator!=(const ps &a){

                return(ts*a.ms!=a.ts*ms);

            }

            ps operator-(){

            return ps(-ts,ms);

            }

            ps operator++(){

            return *this=*this+1;

            }

            ps operator--(){

            return *this=*this-1;

            }

            ps operator +=(const ps &a){

            return *this=*this+a;

            }

            ps operator -=(const ps &a){

            return *this=*this-a;

            }

            friend ostream &operator <<(ostream &out,const ps &a);

            friend istream &operator >>(istream &in,ps &a);

};

            ostream &operator <<(ostream &out,const ps &a){

            ps c=a;

            toigian(c);

            out<<c.ts<<"/"<<c.ms;

            return out;

            }

            istream &operator >>(istream &in,ps &a){

            cout<<"

nhap tu so: ";

            in>>a.ts;

            do{

              cout<<"

nhap mau so :";in>>a.ms;

              }while(a.ms==0);

            if(a.ms<0){a.ts=-a.ts;a.ms=-a.ms;}

            return in;

            }

            ps toigian(ps &a){

                int x,y;

                x=abs(a.ts);y=abs(a.ms);

                if(x==0) return a;

                while(x!=y) if(x>y) x=x-y;else y=y-x;

                return (ps(a.ts/x,a.ms/x));

            }

void main(){

clrscr();

ps a[5],tong,tich(1,1);

            for(int i=0;i<5;i++){

            cout<<"

nhap phan so thu "<<i+1<<"

";

            cin>>a[i];

            tong+=a[i];

            tich=tich*a[i];

            }

cout<<"tong la: "<<tong<<"

";

cout<<"tich la: "<<tich;

ps min,max;

            min=a[0];max=a[0];

            for(i=1;i<5;i++){

            if (a[i]<min) min=a[i];

            if (a[i]>max) max=a[i];

            }

cout<<"

phan so lon nhat la: "<<max;

cout<<"

phan so be nhat la: "<<min;

ps tmp;

            for(i=0;i<4;i++)

            for(int j=i+1;j<5;j++)

                        if(a[i]>a[j]){

                              tmp=a[i];

                              a[i]=a[j];

                              a[j]=tmp;

                                         }

cout<<"

mang sau khi sap xep

";

for(i=0;i<5;i++) cout<<a[i]<<"   ";

getch();

}

24-03

#include<iostream.h>

#include<conio.h>

class complex{

private:

            int pt,pa;

public:

            complex(int x=0,int y=0){ pt=x;pa=y;}

            void set(int r,int a){ pt=r;pa=a;}

            void nhap(){

                  cout<<"Phan thuc: ";cin>>pt;

                  cout<<"Phan ao: ";cin>>pa;

            }

            void display(){

                  if(pa<0) cout<<pt<<pa<<"*j";

                  else cout<<pt<<"+"<<pa<<"*j";

                  }

            friend complex operator+(complex &a,complex &b){

                        return complex(a.pt+b.pt,a.pa+b.pa);

                                                }

};

void main(){

clrscr();

complex x,y;

x.nhap();y.nhap();

cout<<"

tong cua chung la: ";(x+y).display();

getch();

}

25-03

#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

class vector{

private:

            int n;

            float *v;

public:

            vector(){}

            vector(vector &a);

            ~vector(){ delete v;}

            void nhap();

            void display();

            friend vector operator+(vector &a,vector &b);

            void operator=(vector &a);

};

            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::nhap(){

                        cout<<"

nhap n: ";cin>>n;

                        v=new float[n];

                        for(int i=0;i<n;i++){

                              cout<<"

nhap pt thu "<<i<<": ";

                              cin>>v[i];

                                                    }

                                       }

            void vector::display(){

                        for(int i=0;i<n;i++) cout<<v[i]<<"    ";

                        cout<<"

";

                                          }

            vector operator+(vector &a,vector &b){

                        if(a.n==b.n){

                        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;  }

                        else {

                            cout<<"

So pt cua hai mang phai bang nhau.";

                            getch();

                            exit (0);

                              }

                                                                 }

            void vector::operator=(vector &a){

                        delete v;

                        n=a.n;v=new float[n];

                        for(int i=0;i<n;i++) v[i]=a.v[i];

                                                             }

void main(){

vector a,b;

a.nhap();b.nhap();

a.display();b.display();

vector c;

c=a+b;

c.display();

getch();

}

25A -03

#include<iostream.h>

#include<conio.h>

class vector{

private:

            int n;

            int *v;

public:

            vector(){}

            vector(int size){

                        n=size;

                        v=new int[size];

                                    }

            vector(vector &a);

            ~vector(){ delete v;}

            void nhap();

            void display();

            friend vector operator+(vector &a,vector &b);

            void operator=(vector &a);

};

            vector::vector(vector &a){

                        n=a.n;

                        v=new int[n];

                        for(int i=0;i<n;i++) v[i]=a.v[i];

                                                 }

            void vector::nhap(){

                        cout<<"

hap n: ";cin>>n;

                        n=new float[n];

                        for(int i=0;i<n;i++){

                              cout<<"

nhap pt thu "<<i<<": ";

                              cin>>v[i];

                                                    }

                                       }

            void vector::display(){

                        for(int i=0;i<n;i++) cout<<v[i]<<"    ";

                                          }

            vector operator+(vector &a,vector &b){

                        vector c(a.n);

                        for(int i=0;i<c.n;i++) c.v[i]=a.v[i]+b.v[i];

                        return c;

                                                                 }

            void vector::operator=(vector &a){

                        delete v;

                        n=a.n;v=new int[n];

                        for(int i=0;i<n;i++) v[i]=a.v[i];

                                                             }

void main(){

//int n;

//cout<<"nhap n:";cin>>n;

vector a,b;

a.nhap();b.nhap();

a.display();b.display();

vector c;

c=a+b;

c.display();

getch();

}

26-03

#include<iostream.h>

#include<conio.h>

class vector{

private:

            int n;

            float *v;

public:

            vector(){}

            vector(vector &a);

            ~vector(){ delete v;}

            void nhap();

            void display();

            friend vector operator*(float k,vector &a);

};//het lop

            void vector::nhap(){

                        cout<<"

Nhap n: ";cin>>n;

                        v=new float[n];

                        for(int i=0;i<n;i++){

                                    cout<<"

Nhap pt thu "<<i<<" : ";

                                    cin>>v[i];

                                                    }

                           }

            void vector::display(){

                        for(int i=0;i<n;i++) cout<<v[i]<<"     ";

                        cout<<"

";

                                          }

            vector::vector(vector &a){

                        n=a.n;

                        v=new float[n];

                        for(int i=0;i<n;i++) v[i]=a.v[i];

                                                 }

            vector operator*(float k,vector &a){

                        vector c;

                        c.n=a.n;

                        c.v=new float[c.n];

                        for(int i=0;i<c.n;i++) c.v[i]=k*a.v[i];

                        return c;

                                                               }

void main(){

vector a;

a.nhap();

cout<<"Vector a la:  ";a.display();

float k;

cout<<"

Nhap k: ";cin>>k;

cout<<"Tich cua "<<k<<"*a la:  ";(k*a).display();

getch();

}

27 -03

#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

class matrix{

private:

            int n,m;

            float **data;

public:

            matrix(){}

            matrix(matrix &a);

            ~matrix();

            void nhap();

            void display();

            matrix & operator=(matrix &a);

            friend matrix operator+(matrix &a,matrix &b);

            friend matrix operator-(matrix &a,matrix &b);

            friend matrix operator*(matrix &a,matrix &b);

};//het lop

            matrix::matrix(matrix &a){

                        n=a.n;m=a.m;

                        data=new float*[n];

                        for(int i=0;i<n;i++)

                        data[i]=new float[m];

                        for(i=0;i<n;i++)

                        for(int j=0;j<m;j++)

                        data[i][j]=a.data[i][j];

                                                 }

            matrix::~matrix(){

                        for(int i=0;i<n;i++) delete data[i];

                        delete data;

                                     }

            void matrix::nhap(){

                        cout<<"

Nhap so hang: ";cin>>n;

                        cout<<"

Nhap so cot: ";cin>>m;

                        data=new float*[n];

                        for(int i=0;i<n;i++)

                        data[i]=new float[m];

                        for(i=0;i<n;i++)

                        for(int j=0;j<m;j++){

                            cout<<"

Nhap phan tu thu "<<i<<","<<j<<": ";

                            cin>>data[i][j];}

                                      }

            void matrix::display(){

                        for(int i=0;i<n;i++){

                           for(int j=0;j<m;j++)

                           cout<<data[i][j]<<"   ";

                           cout<<"

";

                                                    }

                           cout<<"

";

                                          }

            matrix & matrix::operator=(matrix &a){

                        n=a.n;m=a.m;

                        data=new float*[n];

                        for(int i=0;i<n;i++)

                        data[i]=new float[m];

                        for(i=0;i<n;i++)

                        for(int j=0;j<m;j++)

                        data[i][j]=a.data[i][j];

                        return *this;

                                                   }

            matrix operator+(matrix &a,matrix &b){

                        if((a.n==b.n)&&(a.m==b.m)){

                                    matrix c;

                                    c.n=a.n;c.m=a.m;

                                    c.data=new float*[c.n];

                                    for(int i=0;i<c.n;i++)

                                    c.data[i]=new float[c.m];

                                    for(i=0;i<c.n;i++)

                                    for(int j=0;j<c.m;j++)

                                    c.data[i][j]=a.data[i][j]+b.data[i][j];

                                    return c;

                                                                 }

                        else {

                                    cout<<"\Hai ma tran khong cung cap.";

                                    getch();

                                    exit(0);

                              }

                                                               }

            matrix operator-(matrix &a,matrix &b){

                        if((a.n==b.n)&&(a.m==b.m)){

                                    matrix c;

                                    c.n=a.n;c.m=a.m;

                                    c.data=new float*[c.n];

                                    for(int i=0;i<c.n;i++)

                                    c.data[i]=new float[c.m];

                                    for(i=0;i<c.n;i++)

                                    for(int j=0;j<c.m;j++)

                                    c.data[i][j]=a.data[i][j]-b.data[i][j];

                                    return c;

                                                                 }

                        else {

                                    cout<<"\Hai ma tran khong cung cap.";

                                    getch();

                                    exit(0);

                              }

                                                               }

            matrix operator*(matrix &a,matrix &b){

                        if(a.m==b.n){

                                    matrix c;

                                    c.n=a.n;c.m=b.m;

                                    c.data=new float*[c.n];

                                    for(int i=0;i<c.n;i++)

                                    c.data[i]=new float[c.m];

                                    for(i=0;i<c.n;i++)

                                    for(int j=0;j<c.m;j++){

                                         c.data[i][j]=0;

                                         for(int k=0;k<a.m;k++)

                                         c.data[i][j]=c.data[i][j]+a.data[i][k]*b.data[k][j];

                                                                  }

                                    return c;

                                        }

                        else{

                            cout<<"

Khong the thuc hien phep nhan nay !";

                            getch();

                            exit(0);

                           }

                                                                 }

void main(){

matrix a,b;

a.nhap();b.nhap();

cout<<"

Ma tran a la:

";a.display();

cout<<"

Ma tran b la:

";b.display();

cout<<"

Tong hai ma tran la:

";(a+b).display();

cout<<"

Hieu hai ma tran la:

";(a-b).display();

cout<<"

Tich hai ma tran la:

";(a*b).display();

getch();

}

05-04

#include<iostream.h>

#include<conio.h>

class point{

private:

            int x,y;

public:

            point(int a=0,int b=0){x=a;y=b;}

            point(point &a){ x=a.x;y=a.y;}

            void display(){

                cout<<" Toa do: ("<<x<<" , "<<y<<")";

                              }

};//het lop

class circle:private point{

private:

            int r;

public:

            circle(int ox,int oy,int or):point(ox,oy){ r=or;}

            void display(){

                        cout<<"Tam: ";point::display();

                        cout<<"  Ban kinh: "<<r;

                              }

            float area(){ return(3.14*r*r);}

};//het lop

void main(){

int x,y,r;

cout<<"Nhap x,y,r: ";cin>>x>>y>>r;

circle c(x,y,r);

c.display();

cout<<"  Dien tich: "<<c.area();

getch();

}

06-04

#include<iostream.h>

#include<conio.h>

#include<string.h>

#include<stdio.h>

class mydate{

private:

            int d,m,y;

protected:

            mydate(int dd=1,int mm=1,int yy=1){ d=dd;m=mm;y=yy;}

            void display(){

                        cout<<d<<"-"<<m<<"-"<<y;

                              }

};//hetlop

class person:private mydate{

private:

            char name[30];

            char address[40];

            long int phone;

public:

            person(char *ten,int dd,int mm,int yy,char *dc,long int dt):mydate(dd,mm,yy){

                        strcpy(name,ten);

                        strcpy(address,dc);

                        phone=dt;

            }

            void outscreen(){

                        cout<<"

Name :"<<name;

                        cout<<"

Address: "<<address;

                        cout<<"

Phone: "<<phone;

                        cout<<"

Birthday: ";mydate::display();

                        cout<<"

";

                                    }

};//het lop

void main(){

char ten[30],dc[40];

int d,m,y;

long int dt;

person *p[3];

for(int i=0;i<3;i++){

            cout<<"

Nhap thong tin nguoi thu "<<i+1<<" : ";

            cout<<"Name: ";gets(ten);

            cout<<"Address: ";gets(dc);

            cout<<"Phone: ";cin>>dt;

            cout<<"Birthday( ngay, thang, nam): ";cin>>d>>m>>y;

            p[i]=new person(ten,d,m,y,dc,dt);

                            }

for(i=0;i<3;i++) p[i]->outscreen();

getch();

}

07-04

#include<iostream.h>

#include<conio.h>

class mytime{

private:

            int h,m,s;

protected:

            mytime(int hh=0,int mm=0,int ss=0){h=hh;m=mm;s=ss;}

            void settime(int hh,int mm, int ss){h=hh;m=mm;s=ss;}

            void display(){

                        cout<<h<<":"<<m<<":"<<s;

                              }

};//hetlop

class mydate{

private:

            int d,m,y;

protected:

            mydate(int dd=1,int mm=1,int yy=1){ d=dd;m=mm;y=yy;}

            void setdate(int dd,int mm,int yy){d=dd;m=mm;y=yy;}

            void display(){

                        cout<<d<<"-"<<m<<"-"<<y;

                              }

};//het lop

class datetime:protected mytime,protected mydate{

public:

            void setdatetime(int dd,int mm,int yy, int hh,int pp, int ss){

                        setdate(dd,mm,yy),settime(hh,pp,ss);

                                                                           }

            void display(){

                        cout<<"Ngay: ";mydate::display();

                        cout<<"  Gio: ";mytime::display();

                              }

};//het lop

void main(){

clrscr();

int a,b,c,d,e,f;

cout<<"

Nhap ngay, thang, nam, gio, phut, giay: ";cin>>a>>b>>c>>d>>e>>f;

datetime p;

p.setdatetime(a,b,c,d,e,f);

p.display();

getch();

}

08-04

X

#include<iostream.h>

#include<conio.h>

#include<string.h>

#include<stdio.h>

class printer{

private:

            char *sohieu;

            int soluong;

public:

            printer(char *sh,int sl){

                        sohieu=new char[strlen(sh)+1];

                        strcpy(sohieu,sh);

                        soluong=sl;

                                                }

            void nhapkho(int q){

                        soluong+=q;}

            void xuatkho(int q){

                        soluong-=q;}

            void display(){

                        cout<<"

So luong may in: "<<soluong;

                        cout<<"

So hieu: "<<sohieu;

                              }

};//het lop

class laser:public printer{

private:

            int dpi;

public:

            laser(char *sh,int sl,int d):printer(sh,sl){ dpi=d; }

            void display(){

                        printer::display();

                        cout<<"

So Dpi: "<<dpi;

                              }

};//het lop

class colorlaser:public laser{

private:

            int somau;

public:

            colorlaser(char *sh,int sl,int d,int m):laser(sh,sl,d){ somau=m;}

            void display(){

                        laser::display();

                        cout<<"

So mau: "<<somau;

                              }

};//het lop

void main(){

char sh[30];

int sl,d,m;

colorlaser *p[5];

for(int i=0;i<5;i++){

            cout<<"

Nhap thong tin may in thu "<<i<<" : ";

            cout<<"

nhap so hieu: ";gets(sh);

            cout<<"

nhap so luong: ";cin>>sl;

            cout<<"

nhap so Dpi: ";cin>>d;

            cout<<"

nhap so mau: ";cin>>m;

            p[i]=new colorlaser(sh,sl,d,m);

                                                }

p[2]->nhapkho(2);

p[4]->xuatkho(4);

p[2]->display();cout<<"

";

p[4]->display();

getch();

}

09-04

#include<iostream.h>

#include<conio.h>

#include<string.h>

#include<stdio.h>

class mytime{

private:

            int h,m,s;

protected:

            mytime(int hh=0,int mm=0,int ss=0){ h=hh;m=mm;s=ss;}

            void display(){

                        cout<<h<<":"<<m<<":"<<s;

                              }

};//het lop

class mydate{

private:

            int d,m,y;

protected:

            mydate(int dd=1,int mm=1,int yy=1){d=dd;m=mm;y=yy;}

            void display(){

                        cout<<d<<"-"<<m<<"-"<<y;

                              }

};//het lop

class myfile:private mydate,private mytime{

private:

            char *filename;

            int filesize;

public:

            myfile(int hh,int pp,int ss,int dd, int mm,int yy,char *ten,int size):mytime(hh,pp,ss),mydate(dd,mm,yy){

                        filename=new char[strlen(ten)+1];

                        strcpy(filename,ten);

                        filesize=size;

                                                                        }

            void display(){

                        cout<<"Ten tep: "<<filename<<" ,Kich thuoc: "<<filesize;

                        cout<<" ,Ngay: ";mydate::display();

                        cout<<" ,Gio: ";mytime::display();

                        cout<<"

";

                              }

};//het lop

void main(){

char ten[255];

int size,a,b,c,d,e,f;

myfile *p[3];

for(int i=0;i<3;i++){

            cout<<"

Nhap thong tin tep thu "<<i+1<<" : ";

            cout<<"

Ten tep: ";gets(ten);

            cout<<"

Kich thuoc: ";cin>>size;

            cout<<"

Ngay, thang, nam: ";cin>>a>>b>>c;

            cout<<"

Gio, phut, giay: ";cin>>d>>e>>f;

            p[i]=new myfile(d,e,f,a,b,c,ten,size);

                            }

for(i=0;i<3;i++)

p[i]->display();

getch();

}

10-04

#include<iostream.h>

#include<conio.h>

#include<string.h>

#include<stdio.h>

class mytime{

private:

            int h,m,s;

public:

            mytime(int hh=0,int mm=0,int ss=0){ h=hh;m=mm;s=ss;}

            void display(){

                        cout<<h<<":"<<m<<":"<<s;

                              }

            friend int operator>=(mytime &t1,mytime &t2){

                        if(t1.h>=t2.h) return 1;

                        if((t1.h==t2.h)&&(t1.m>=t2.m)) return 1;

                        if((t1.h==t2.h)&&(t1.m==t2.m)&&(t1.s>=t2.s)) return 1;

                        return 0;

                                                                            }

};//het lop

class mydate{

private:

            int d,m,y;

public:

            mydate(int dd=1,int mm=1,int yy=1){d=dd;m=mm;y=yy;}

            void display(){

                        cout<<d<<"-"<<m<<"-"<<y;

                              }

            friend int operator>=(mydate &d1,mydate &d2){

                        if(d1.y>=d2.y) return 1;

                        if((d1.y==d2.y)&&(d1.m>=d2.m)) return 1;

                        if((d1.y==d2.y)&&(d1.m==d2.m)&&(d1.d>=d2.d)) return 1;

                        return 0;

                                                                            }

};//het lop

class myfile:private mydate,private mytime{

private:

            char filename[255];

            int filesize;

public:

            myfile(int hh,int pp,int ss,int dd, int mm,int yy,char *ten,int size):mytime(hh,pp,ss),mydate(dd,mm,yy){

                        strcpy(filename,ten);

                        filesize=size;

                                                                        }

            void display(){

                        cout<<"Ten tep: "<<filename<<", Kich thuoc: "<<filesize;

                        cout<<", Ngay: ";mydate::display();

                        cout<<", Gio: ";mytime::display();

                        cout<<"

";

                              }

            friend int operator>=(myfile &f1,myfile &f2){

                        if(strcmp(f1.filename,f2.filename)>=0) return 1;

                        return 0;

                                                                            }

};//het lop

void main(){

myfile *p[5],*tmp;

char ten[255];

int size,ngay,thang,nam,gio,phut,giay;

for(int i=0;i<3;i++){

            cout<<"

Nhap doi tuong thu "<<i<<" : ";

            cout<<"

Nhap ten: ";gets(ten);

            cout<<"

Nhap kich thuoc: ";cin>>size;

            cout<<"

Nhap ngay, thang nam: ";cin>>ngay>>thang>>nam;

            cout<<"

Nhap gio, phut, giay: ";cin>>gio>>phut>>giay;

            p[i]=new myfile(gio,phut,giay,ngay,thang,nam,ten,size);

                            }

for(i=0;i<2;i++)

for(int j=i+1;j<3;j++)

    if(*p[i]>=*p[j]){

            tmp=p[i];

            p[i]=p[j];

            p[j]=tmp;

                         }

cout<<"Danh sach cac tep sau khi sap xep tang dan theo ten tep

";

for(i=0;i<3;i++) p[i]->display();

for(i=0;i<2;i++)

for(j=i+1;j<3;j++)

    if((mydate&)p[i]>=(mydate&)p[j]){

            (mydate&)tmp =(mydate&)p[i];

            p[i]=p[j];

            (mydate&)p[j]=(mydate&)tmp;

                         }

for(i=0;i<2;i++)

for(j=i+1;j<3;j++)

    if((mytime&)p[i]>=(mytime&)p[j]){

            (mytime&)tmp=(mytime&)p[i];

            p[i]=p[j];

            (mytime&)p[j]=(mytime&)tmp;

                         }

cout<<"

Danh sach cac tep sau khi sap xep tang dan theo ten tep

";

for(i=0;i<3;i++) p[i]->display();

getch();

}

11-04

#include<iostream.h>

#include<conio.h>

#include<string.h>

#include<stdio.h>

class sv{

protected:

            char lop[10];

            char hoten[30];

            sv(){}

            sv(char *t,char *l){

                        strcpy(lop,l);

                        strcpy(hoten,t);

                                       }

            void display(){

                        cout<<"

Ho ten: "<<hoten;

                        cout<<"

Lop: "<<lop;

                              }

};//het lop

class svsp:protected sv{

protected:

            float dtb;

            int hocbong;

            svsp(){}

            svsp(char *t,char *l,float d,int hb):sv(t,l){

            dtb=d;

            hocbong=hb;}

            void display(){

                        sv::display();

                        cout<<"

Diem trung binh: "<<dtb;

                        cout<<"

Hoc bong: "<<hocbong;

                              }

};//het lop

class svtc:protected sv{

protected:

            int hocphi;

            svtc(){}

            svtc(char *t,char *l,int hp):sv(t,l){

            hocphi=hp;}

            void display(){

                        sv::display();

                        cout<<"

Hoc phi: "<<hocphi;

                              }

};//het lop

class svcn:virtual public svsp,virtual public svtc{

public:

            svcn(char *t,char *l,float d,int hb,int hp):svsp(t,l,d,hb){

            hocphi=hp;

            }

            void display(){

                        svsp::display();

                        cout<<"

Hoc phi: "<<hocphi;

                              }

};//het lop

void main(){

clrscr();

char ten[20],lop[10];

float dtb;

int hb,hp;

svcn *sv[2];

for(int i=0;i<2;i++){

cout<<"nhap thong tin SV thu "<<i+1<<"

";

cout<<"ho ten: ";gets(ten);

cout<<"lop: ";gets(lop);

cout<<"dtb: ";cin>>dtb;

cout<<"hoc bong: ";cin>>hb;

cout<<"hoc phi: ";cin>>hp;

sv[i]=new svcn(ten,lop,dtb,hb,hp);

}

for(i=0;i<2;i++) sv[i]->display();

getch();

}

12-04

#include<iostream.h>

#include<conio.h>

#include<string.h>

#include<stdio.h>

class person{

protected:

            char name[30];

            char address[40];

            long int phone;

public:

            person(){}

            person(char *ten,char *dc,long int dt){

                        strcpy(name,ten);

                        strcpy(address,dc);

                        phone=dt;

                                                                  }

};//het lop

class officer:protected person{

protected:

            float salary;

public:

            officer(){}

            officer(char *ten,char *dc,long int dt,float sal):person(ten,dc,dt){

                        salary=sal;

                                                                                     }

};//het lop

class manager:protected officer{

private:

            float extra;

public:

            manager(char *ten,char *dc,long int dt,float sal,float pc):officer(ten,dc,dt,sal){

                        extra=pc;

                                                                                                                                     }

            void outscreen(){

                        cout<<"

=============================

";

                        cout<<"

Name: "<<name;

                        cout<<"

Address: "<<address;

                        cout<<"

Phone: "<<phone;

                        cout<<"

Salary: "<<salary;

                        cout<<"

Extra: "<<extra;

                                    }

};//het lop

void main(){

manager *p[5];

char ten[30],dc[40];

float sal,pc;

long int dt;

for(int i=0;i<5;i++){

            cout<<"

Nhap doi tuong thu "<<i+1<<"

";

            cout<<"ten:";gets(ten);

            cout<<"dia chi:";gets(dc);

            cout<<"dien thoai:";cin>>dt;

            cout<<"Luong: ";cin>>sal;

            cout<<"phu cap: ";cin>>pc;

            p[i]=new manager(ten,dc,dt,sal,pc);

                            }

for(i=0;i<5;i++) p[i]->outscreen();

getch();

}

13-04

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

#include<string.h>

class person{

protected:

            char name[30];

            char address[40];

            long int phone;

            person(){}

            person(char *ten,char *dc,long int dt){

                        strcpy(name,ten);

                        strcpy(address,dc);

                        phone=dt;

                                                                  }

            void display(){

                        cout<<"

Ho ten: "<<name;

                        cout<<"

Dia chi: "<<address;

                        cout<<"

Dien thoai: "<<phone;

            }

};//het lop

class officer:protected person{

protected:

            float salary;

            officer():person(){}

            officer(char *ten,char *dc,long int dt,float luong):person(ten,dc,dt){

            salary=luong;

            }

            void display(){

            person::display();

            cout<<"

Luong: "<<salary;

            }

};//het lop

class student:protected person{

protected:

            float fee;

            student():person(){}

            student(char *ten,char *dc,long int dt,float hp):person(ten,dc,dt){

            fee=hp;

            }

};//het lop

class offstudent:virtual public officer,virtual public student{

public:

            offstudent(char *ten,char *dc,long int dt,float luong,float hp):officer(ten,dc,dt,luong){

            fee=hp;

            }

            void outscreen(){

            officer::display();

            cout<<"

Hoc phi: "<<fee;

            }

};//het lop

void main(){

clrscr();

offstudent *p[5];

char ten[20],dc[30];

float l,hp;

long int dt;

for(int i=0;i<5;i++){

cout<<"nhap thong tin nguoi thu "<<i+1<<"

";

cout<<"ten:";gets(ten);

cout<<"dia chi: ";gets(dc);

cout<<"Dien thoai:";cin>>dt;

cout<<"Luong: ";cin>>l;

cout<<"Hoc phi: ";cin>>hp;

p[i]=new offstudent(ten,dc,dt,l,hp);

}

for(i=0;i<5;i++) p[i]->outscreen();

getch();

}

Bạn đang đọc truyện trên: Truyen2U.Pro

#lamletri