1. Trang chủ
  2. » Luận Văn - Báo Cáo

Lập trình hướng đối tượng C++

22 440 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 22
Dung lượng 123 KB

Nội dung

Tài liệu tham khảo công nghệ thông tin Lập trình hướng đối tượng C++

Trang 1

Giáo viên hướng dẫn : Nguyễn Đức Hiển

Sinh viên thực hiện : Nguyễn Tiến Hoài Nam Lớp : 06I

Câu 1:

Trang 2

void swap(int *a,int *b){ int c;

void main(){

clrscr();int a,b,i,j,n;int d[50];

cout<<"\nso phan tu cua day la n=";cin>>n;

cout<<"nhap so d["<<i<<"]=";cin>>d[i];

cout<<"day duoc sap sep tang dan :\n";for(i=1;i<=n;i++)

for(j=i+1;j<n+1;j++) if(d[i]>d[j])

swap(&d[i],&d[j]);for(i=1;i<=n;i++){

cout<<"\nday duoc sap sep giam dan :\n";for(i=1;i<=n;i++)

for(j=i+1;j<n+1;j++) if(d[i]<d[j])

swap(&d[i],&d[j]);for(i=1;i<=n;i++){

Câu 2:

struct PhanSo {

int TuSo; int MauSo; };

PhanSo operator + (PhanSo a ,PhanSo b);PhanSo operator - (PhanSo a ,PhanSo b);

Trang 3

PhanSo operator * (PhanSo a ,PhanSo b);PhanSo operator / (PhanSo a ,PhanSo b); int operator > (PhanSo a ,PhanSo b); int operator >=(PhanSo a ,PhanSo b); int operator < (PhanSo a ,PhanSo b); int operator <=(PhanSo a ,PhanSo b); int operator ==(PhanSo a ,PhanSo b); int operator !=(PhanSo a ,PhanSo b);

PhanSo RutGonPS (PhanSo a);}

void main() {

clrscr(); PhanSo a,b,c;

cout<<"Tu So a="; cin>>a.TuSo; cout<<"MauSo a="; cin>>a.MauSo; cout<<"Tu So b="; cin>>b.TuSo; cout<<"MauSo b="; cin>>b.MauSo; cout<<"\na="<<a.TuSo<<"/"<<a.MauSo; cout<<"\nb="<<b.TuSo<<"/"<<b.MauSo; c=a+b;

c=RutGonPS(c);

cout<<"\na+b="<<c.TuSo <<"/"<<c.MauSo; c=a-b;

c=RutGonPS(c);

cout<<"\na-b="<<c.TuSo <<"/"<<c.MauSo; c=a*b;

c=RutGonPS(c);

cout<<"\na*b="<<c.TuSo <<"/"<<c.MauSo; c=a/b;

c=RutGonPS(c);

cout<<"\na/b="<<c.TuSo <<"/"<<c.MauSo; if (a==b)

cout<<"\nPS a bang PS b"; else cout<<"\nPS b khac PS a"; if (a!=b)

cout<<"\nPS a khac PS b"; else cout<<"\nPS b bang PS a"; if (a>b)

cout<<"\nPS a lon hon PS b"; else if(b>a)

Trang 4

cout<<"\nPS b lon hon PS a"; else cout<<"\na=b";

if (a>=b)

cout<<"\nPS a lon hon hoac bang PS b"; else cout<<"\nPS b lon hon hoac bang PS a"; if (a<b)

cout<<"\nPS a be hon PS b"; else if (b<a)

cout<<"\nPS b be hon PS a"; else cout<<"\nb=a"; if (a<=b)

cout<<"\nPS a be hon hoac bang PS b"; else cout<<"\nPS b be hon hoac bang PS a"; getch();

// Dinh Nghia Cac Ham

PhanSo operator + (PhanSo a ,PhanSo b) {

PhanSo c;

c.TuSo=a.TuSo*b.MauSo+a.MauSo*b.TuSo; c.MauSo=a.MauSo*b.MauSo;

return c; }

PhanSo operator - (PhanSo a ,PhanSo b) {

PhanSo c;

c.TuSo=a.TuSo*b.MauSo-a.MauSo*b.TuSo; c.MauSo=a.MauSo*b.MauSo;

return c; }

PhanSo operator * (PhanSo a ,PhanSo b) {

PhanSo c;

c.TuSo=a.TuSo*b.TuSo; c.MauSo=a.MauSo*b.MauSo; return c;

}

PhanSo operator / (PhanSo a ,PhanSo b) {

PhanSo c;

Trang 5

c.TuSo=a.TuSo*b.MauSo; c.MauSo=a.MauSo*b.TuSo; return c;

}

int operator > (PhanSo a ,PhanSo b) {

if(a.TuSo*b.MauSo>b.TuSo*a.MauSo) return 1;

else return 0; }

int operator >= (PhanSo a ,PhanSo b) {

if(a.TuSo*b.MauSo>=b.TuSo*a.MauSo) return 1;

else return 0; }

int operator < (PhanSo a ,PhanSo b) {

if(a.TuSo*b.MauSo<b.TuSo*a.MauSo) return 1;

else return 0; }

int operator <= (PhanSo a ,PhanSo b) {

if(a.TuSo*b.MauSo<=b.TuSo*a.MauSo) return 1;

else return 0; }

int operator == (PhanSo a ,PhanSo b) {

if(a.TuSo*b.MauSo==b.TuSo*a.MauSo) return 1;

else return 0; }

int operator != (PhanSo a ,PhanSo b) {

if(a.TuSo*b.MauSo!=b.TuSo*a.MauSo)

Trang 6

return 1; else return 0; }

PhanSo RutGonPS (PhanSo a){

int UCLN;

int n=abs(a.TuSo),m=abs(a.MauSo); while (n != 0 && m != 0)

if (n>m) n -= m; else m -= n; if (n != 0) UCLN= n; else

UCLN= m;

a.TuSo=a.TuSo/UCLN; a.MauSo=a.MauSo/UCLN; return a;

Câu 3:

struct sophuc{

double thuc; double ao;};

sophuc lapsophuc(){

int x,y;sophuc tam;

tam.ao=y;return tam;}

void display(sophuc a){

sophuc operator + (sophuc a,sophuc b){

Trang 7

sophuc tam;

tam.thuc=a.thuc+b.thuc; tam.ao=a.ao+b.ao; return tam;

tam.ao= ((b.thuc*a.ao)-(a.thuc*b.ao))/((b.thuc*b.thuc)+(b.ao*b.ao));

}

return tam; }

int operator == (sophuc a,sophuc b){

return ((a.thuc==b.thuc)&&(a.ao==b.ao));}

int operator != (sophuc a,sophuc b){

return ((a.thuc!=b.thuc)||(a.ao!=b.ao));}

void main(){

sophuc x,y,z,t,s,r;cout<<"\nso phuc x:";x=lapsophuc();

Trang 8

cout<<"\nso phuc y:";y=lapsophuc();

cout<<"\ncong hai so phuc:";display(z);

public: char *ten;

float toan , ly ,hoa ,dtb; void nhap();

void xuat();

friend void hoanvi(float *a,float *b); };

void sinhvien::nhap(){

cout<<"vui long nhap cac thong tin sau :\n"; cout<<"ten sinh vien :";

cin>>ten;

cout<<"\ndiem toan :";

Trang 9

cin>>toan;

cout<<"\ndiem ly :"; cin>>ly;

cout<<"\ndiem hoa :"; cin>>hoa;

dtb=(toan+ly+hoa)/3;}

void sinhvien::xuat(){

cout<<"\nten sinh vien :"<<ten;

cout<<"\ndiem trung binh :"<<dtb<<"\n";}

void main(){

clrscr(); int i,j;

sinhvien a[10];

for(i=0;i<=9;i++) //nhap thong tin 10 sinh vien {

a[i].nhap(); }

for(i=0;i<=9;i++) //sap xep theo diem trung binh giam dan for(j=i+1;j<=10;j++)

if((a[i].dtb)<(a[j].dtb)) {

float c; char *tam; c=a[i].dtb; tam=a[i].ten; a[i].dtb=a[j].dtb; a[i].ten=a[j].ten; a[j].dtb=c; a[j].ten=tam; }

cout<<"3 SV co diem trung binh cao nhat la : \n";

for(i=0;i<=2;i++) //in ra man hinh 3 sinh vien co dtb cao nhat a[i].xuat();

getch();}

Câu 5:

class hcnhat

Trang 10

private:

int chdai,chrong; public:

void nhap(); int chuvi(); int dientich(); void xuat();};

void hcnhat::nhap(){

cout<<"nhap chieu dai :"; cin>>chdai;

cout<<"nhap chieu rong :"; cin>>chrong;

int hcnhat::chuvi(){

return (chdai+chrong)*2;}

int hcnhat::dientich(){

return chdai*chrong;}

void hcnhat::xuat(){

cout<<"chieu dai la :"<<chdai; cout<<"\nchieu rong la :"<<chrong; cout<<"\nchu vi hcn la :"<<chuvi(); cout<<"\ndien tich hcn la :"<<dientich();}

void main(){

clrscr(); hcnhat a; a.nhap(); a.xuat(); getch();}

Câu 6:

class phanso{

private:

Trang 11

int tuso, mauso; public:

void nhap();

friend phanso rutgon(phanso p);

friend phanso cong(phanso p1,phanso p2); friend phanso tru(phanso p1, phanso p2); friend phanso nhan(phanso p1,phanso p2); friend phanso chia(phanso p1,phanso p2); void xuat();

void phanso::nhap(){

phanso p1,p2;

cout<<"nhap tu so cua ps a :"; cin>>p1.tuso;

cout<<"\nnhap mau so cua ps a :"; cin>>p1.mauso;

cout<<"\nnhap tu so cua ps b :"; cin>>p2.tuso;

cout<<"\nnhap mau so cua ps b :"; cin>>p2.mauso;

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.tuso,p.mauso); q.tuso=p.tuso/x;

q.mauso=p.mauso/x; return q;

phanso cong(phanso p1,phanso p2){

p1.tuso=p1.tuso*(p2.mauso) + p2.tuso*(p1.mauso); p1.mauso=p1.mauso*p2.mauso;

return rutgon(p1);

Trang 12

phanso tru(phanso p1,phanso p2){

p1.tuso=p1.tuso*p2.mauso - p2.tuso*p1.mauso; p1.mauso=p1.mauso*p2.mauso;

return rutgon(p1);}

phanso nhan(phanso p1,phanso p2){

p1.tuso=p1.tuso*p2.tuso;

p1.mauso=p1.mauso*p2.mauso; return rutgon(p1);

phanso chia(phanso p1,phanso p2){

p1.tuso=p1.tuso*p2.mauso; p1.mauso=p1.mauso*p2.tuso; return rutgon(p1);

void phanso::xuat(){

phanso p1,p2,c,t,n,ch; c=cong(p1,p2); t=tru(p1,p2); n=nhan(p1,p2); ch=chia(p1,p2);

cout<<"\na+b="<<c.tuso<<"/"<<c.mauso; cout<<"\na-b="<<t.tuso<<"/"<<t.mauso; cout<<"\na*b="<<n.tuso<<"/"<<n.mauso; cout<<"\na : b="<<ch.tuso<<"/"<<ch.mauso;}

void main(){

clrscr(); phanso a; a.nhap(); a.xuat(); getch();}

Câu 7:

class sophuc{

private:

Trang 13

double thuc;double ao; public:

void nhap() {

cout<<"\nthuc="; cin>>thuc;

cout<<"\nao="; cin>>ao;

}

void in(sophuc a) {

cout<<"("<<a.thuc<<","<<a.ao<<")"<<endl; }

sophuc operator + ( sophuc a) {

sophuc tam;

tam.thuc=this->thuc+a.thuc; tam.ao =this->ao+a.ao; return tam;

tam.ao=

return tam; }

Trang 14

int operator == (sophuc a) {

return ((this->thuc==a.thuc)&&(this->ao==a.ao)); }

int operator != (sophuc a) {

return ((this->thuc!=a.thuc)||(this->ao!=a.ao)); }

};void main(){

clrscr(); sophuc a,b,c;

cout<<"\nsophuc a"; a.nhap();

cout<<"\nsophuc b"; b.nhap();

c=a+b;

cout<<"\na+b=" ; c.in(c); c=a-b;

cout<<"\na-b="; c.in(c); c=a*b;

cout<<"\na*b="; c.in(c); c=a/b;

cout<<"\na/b="; c.in(c); if(a==b)

cout<<"\nhai so phuc bang nhau"; else

cout<<"\nhai so phuc khong bang nhau"; if(a!=b)

cout<<"\nhai so phuc khac nhau"; else

cout<<"\nhai so phuc khong khac nhau";getch();

Câu 8:

struct matran{

double mt[20][20];

Trang 15

int n;};

ostream& operator <<(ostream& os, const matran& x){

os<<setprecision(2)<<setiosflags(ios::showpoint); for(int i=1;i<=x.n;++i)

{

os<<"\n";

for(int j=1;j<=x.n;++j) os<<setw(6)<<x.mt[i][j]; }

os<<"\n"; return os;}

istream& operator>>(istream& is,matran& x){

cout<<"cap ma tran vuong:"; is>>x.n;

cout<<"nhap cac phan tu :\n"; for(int i=1;i<=x.n;++i)

for(int j=1;j<=x.n;++j) {

cout<<"a["<<i<<"]["<<j<<"]="; is>>x.mt[i][j];

}

return is;}

matran operator +(matran a, matran b){ matran c;

for(int i=1;i<=a.n;++i) for(int j= 1;j<=a.n;++j)

c.mt[i][j]= a.mt[i][j] + b.mt[i][j];return c;

matran operator -(matran a,matran b){ matran c;

int i,j; if(a.n!=b.n) {

cout<<"\nkhong thuc hien duoc phep tru vi 2 MT khong cung cap"; getch();

return a; }

else

for(i=1;i<=a.n;++i) for(j= 1;j<=a.n;++j)

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

Trang 16

return c;}

matran operator *(matran a, matran b){

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

cout<<"\nkhong thuc hien duoc phep nhan vi 2 MT khong cung cap"; getch();

return a; }

else {

matran c; int i,j,k;

for(i=1;i<=a.n;++i) for(j=1;j<=a.n;++j) {

c.mt[i][j]=0.0;

for(k=1;k<=a.n;++k)

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

return c; }

void main(){

clrscr();

matran a,b,c,d,e; int n,i,j;

cout<<"\nnhap ma tran A";cin>>a;cout<<"\nnhap ma tran B";cin>>b;c = a+b;

cout<<"\nket qua A + B :"; for(i=1;i<=a.n;++i)

for(j=1;j<=a.n;++j)

cout<<"\tc["<<i<<"]["<<j<<"]="<<c.mt[i][j];d=a-b;

cout<<"\nket qua A - B :"; for(i=1;i<=a.n;++i)

for(j=1;j<=a.n;++j)

cout<<"\td["<<i<<"]["<<j<<"]="<<d.mt[i][j];e=a*b;

cout<<"\nket qua A * B :"; for(i=1;i<=a.n;++i)

for(j=1;j<=a.n;++j)

cout<<"\te["<<i<<"]["<<j<<"]="<<e.mt[i][j];getch();

Trang 17

Câu 9:

class point {

private:

int x,y; // hoanh do va tung do public:

void nhap() {

cout<< "\nhoanh do va tung do cua diem:" ; cin>>x>>y;

}

void xuat() {

cout<<x<<","<<y; }

double do_dai(point n) {

return sqrt(pow(x-n.x,2)+pow(y-n.y,2)); }

}; void main() {

clrscr(); point a,b; double c;

cout<<"\ntoa do diem a :"; a.nhap();

cout<<"\ndiem a:"; a.xuat();

cout<<"\ntoa do diem b:"; b.nhap();

cout<<"\ndiem b:"; a.xuat();

c= a.do_dai(b);

cout<<" \ndo dai giua hai diem:"; cout<<c;

getch(); }

Câu 10:

class DIEM

Trang 18

int x,y;public :

DIEM(int x1,int y1){

DIEM(DIEM &d){

int operator[](int i){

if(i==1)return x;else return y;}

class DUONG_TRON:public DIEM{

int r,md;public:

DUONG_TRON(DIEM d,int r1, int md1):DIEM(d){

void ve(){

int getmd(){

return md;}

void ktdh(){

int mh=0,mode=0;

initgraph(&mh,&mode,"");

Trang 19

void main(){

DUONG_TRON dt(DIEM(100,100),80,MAGENTA);dt.ve();

Câu 11:

class DIEM{

private :

double x,y; public :

DIEM() {

x=y=0.0; }

DIEM(double x1,double y1) {

x=x1;y=y1; }

void in() {

cout<<"\nx="<<x<<" y="<<y; }

class HINH_TRON : public DIEM{

private :double r; public :

HINH_TRON() {

r=0.0; }

HINH_TRON(double x1,double y1,double r1):DIEM(x1,y1){

r=r1;}

double getR() {

return r; }

};

Trang 20

void main(){

HINH_TRON h(2.5,3.5,8); clrscr();

cout<<"\nHinh tron co tam :"; h.in();

cout<<"\nCo ban kinh ="<<h.getR(); getch();

Câu 12:

class nguoi{

private: char *ten; char *diachi; int ns; // nam sinh public:

nguoi() {

}

nguoi(char *ten1, char *diachi1, int ns1) {

int n=strlen(ten1);ten =new char[n+1];strcpy(ten,ten1);int m=strlen(diachi1);diachi =new char[m+1];strcpy(diachi,diachi1);ns=ns1;

}

~nguoi() {

if((ten!=NULL)&&(diachi!=NULL)){

delete ten;delete diachi;ns=0;

} }

void xuat() {

cout<<"\nho va ten:"<<ten<<"\nnam sinh:"<<ns<<"\ndiachi:"<<diachi; }

Trang 21

};

class mon_hoc{

private:

char*mon; int sotiet; public: mon_hoc() {

mon=NULL;sotiet=0; }

mon_hoc(char*mon1,int sotiet1) {

int n=strlen(mon1);mon=new char[n+1];strcpy(mon,mon1);sotiet=sotiet1; }

~mon_hoc() {

if(mon!=NULL)delete mon;sotiet=0; }

void xuat() {

cout<<"\nTen mon hoc:"<<mon<<"\nSo tiet hoc:"<<sotiet; }

giao_vien():nguoi(),mh() {}

giao_vien(char*ten1,char*diachi1,int ns1,char*mon1,int sotiet1):nguoi(ten1,diachi1,ns1),mh(mon1,sotiet1)

{}

~giao_vien() {}

void xuat() {

nguoi::xuat() ;mh.xuat(); }

};

Trang 22

void main(){

_♠The End♠

Ngày đăng: 01/12/2012, 08:41

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w