1. Trang chủ
  2. » Công Nghệ Thông Tin

tuyển tập operator lập trình hướng đối tượng

21 1,4K 1

Đ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 21
Dung lượng 75 KB

Nội dung

operator phân số,số phức

operator Câu 1: void swap(int *a,int *b) { int c; c=*a; *a=*b; *b=c; } void main() { clrscr(); int a,b,i,j,n; int d[50]; cout<<"\nso phan tu cua day la n="; cin>>n; for(i=1;i<=n;i++) { 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<<d[i]<<"\t"; } 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++) { cout<<d[i]<<"\t"; } getch(); } Câu 2: struct PhanSo { int TuSo; int MauSo; }; PhanSo operator + (PhanSo a ,PhanSo b); PhanSo operator - (PhanSo a ,PhanSo b); 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) 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; 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) 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; cout<<"\nthuc:"<<endl; cin>>x; tam.thuc=x; cout<<"\nao:"<<endl; cin>>y; tam.ao=y; return tam; } void display(sophuc a) { cout<<"("<<a.thuc<<","<<a.ao<<")"<<endl; } sophuc operator + (sophuc a,sophuc b) { sophuc tam; tam.thuc=a.thuc+b.thuc; tam.ao=a.ao+b.ao; return tam; } sophuc operator - (sophuc a,sophuc b) { sophuc tam; tam.thuc=a.thuc-b.thuc; tam.ao=a.ao-b.ao; return tam; } sophuc operator * (sophuc a,sophuc b) { sophuc tam; tam.thuc= (a.thuc*b.thuc)-(a.ao*b.ao); tam.ao= (a.thuc*b.ao)+(b.thuc*a.ao); return tam; } sophuc operator / (sophuc a,sophuc b) { sophuc tam; if((b.thuc!=0.0)&&(b.ao!=0.0)) { tam.thuc= ((a.thuc*b.thuc)+(a.ao*b.ao))/((b.thuc*b.thuc)+ (b.ao*b.ao)); 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() { clrscr(); sophuc x,y,z,t,s,r; cout<<"\nso phuc x:"; x=lapsophuc(); cout<<"\nx"; display(x); cout<<"\nso phuc y:"; y=lapsophuc(); cout<<"\ny"; display(y); z=x+y; cout<<"\ncong hai so phuc:"; display(z); t=x-y; cout<<"\nhieu hai so phuc:"; display(t); s=x*y; cout<<"\ntich hai so phuc:"; display(s); r=x/y; cout<<"\nthuong hai so phuc:"; display(r); if(x==y) { cout<<"\nhai so phuc bang nhau";} else { cout<< "\nhai so phuc khong bang nhau"; } if(x!=y) { cout<<"\nhai so phuc khac nhau";} else { cout<<"\nhai so phuc khong khac nhau";} getch(); } Câu 4: class sinhvien { 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 :"; 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 { 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: 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) { . MauSo; }; PhanSo operator + (PhanSo a ,PhanSo b); PhanSo operator - (PhanSo a ,PhanSo b); PhanSo operator * (PhanSo a ,PhanSo b); PhanSo operator / (PhanSo. ,PhanSo b); int operator > (PhanSo a ,PhanSo b); int operator >=(PhanSo a ,PhanSo b); int operator < (PhanSo a ,PhanSo b); int operator <=(PhanSo

Ngày đăng: 20/12/2013, 11:58

TỪ KHÓA LIÊN QUAN

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

TÀI LIỆU LIÊN QUAN

w