Ít Bài Tập OOP Cho Anh Em Tham Khảo!


Diễn đàn chia sẻ kiến thức, kinh nghiệm về IT và cuộc sống!
 
Trang ChínhGalleryTìm kiếmLatest imagesĐăng kýĐăng Nhập
Top posters
Sakura (1124)
Ít Bài Tập OOP Cho Anh Em Tham Khảo! Vote_lcapÍt Bài Tập OOP Cho Anh Em Tham Khảo! Voting_barÍt Bài Tập OOP Cho Anh Em Tham Khảo! Vote_rcap 
hotboy (705)
Ít Bài Tập OOP Cho Anh Em Tham Khảo! Vote_lcapÍt Bài Tập OOP Cho Anh Em Tham Khảo! Voting_barÍt Bài Tập OOP Cho Anh Em Tham Khảo! Vote_rcap 
Già Làng (373)
Ít Bài Tập OOP Cho Anh Em Tham Khảo! Vote_lcapÍt Bài Tập OOP Cho Anh Em Tham Khảo! Voting_barÍt Bài Tập OOP Cho Anh Em Tham Khảo! Vote_rcap 
con_ca_nho90 (289)
Ít Bài Tập OOP Cho Anh Em Tham Khảo! Vote_lcapÍt Bài Tập OOP Cho Anh Em Tham Khảo! Voting_barÍt Bài Tập OOP Cho Anh Em Tham Khảo! Vote_rcap 
that_true (154)
Ít Bài Tập OOP Cho Anh Em Tham Khảo! Vote_lcapÍt Bài Tập OOP Cho Anh Em Tham Khảo! Voting_barÍt Bài Tập OOP Cho Anh Em Tham Khảo! Vote_rcap 
theanhkkt (143)
Ít Bài Tập OOP Cho Anh Em Tham Khảo! Vote_lcapÍt Bài Tập OOP Cho Anh Em Tham Khảo! Voting_barÍt Bài Tập OOP Cho Anh Em Tham Khảo! Vote_rcap 
phamay (137)
Ít Bài Tập OOP Cho Anh Em Tham Khảo! Vote_lcapÍt Bài Tập OOP Cho Anh Em Tham Khảo! Voting_barÍt Bài Tập OOP Cho Anh Em Tham Khảo! Vote_rcap 
lovelonelyman (134)
Ít Bài Tập OOP Cho Anh Em Tham Khảo! Vote_lcapÍt Bài Tập OOP Cho Anh Em Tham Khảo! Voting_barÍt Bài Tập OOP Cho Anh Em Tham Khảo! Vote_rcap 
o0ovioletstaro0o (128)
Ít Bài Tập OOP Cho Anh Em Tham Khảo! Vote_lcapÍt Bài Tập OOP Cho Anh Em Tham Khảo! Voting_barÍt Bài Tập OOP Cho Anh Em Tham Khảo! Vote_rcap 
stevenhung (122)
Ít Bài Tập OOP Cho Anh Em Tham Khảo! Vote_lcapÍt Bài Tập OOP Cho Anh Em Tham Khảo! Voting_barÍt Bài Tập OOP Cho Anh Em Tham Khảo! Vote_rcap 
Âm - Dương lịch
Clock
Logo
11TH02 Pro!
Liên kết
Tin tức 60s
Tin công nghệ
Thời sự 24h
Game Moblie

Share
 

 Ít Bài Tập OOP Cho Anh Em Tham Khảo!

Xem chủ đề cũ hơn Xem chủ đề mới hơn Go down 
Tác giảThông điệp
Sakura

Ít Bài Tập OOP Cho Anh Em Tham Khảo! Stars7
Sakura

Thú CƯng : Ít Bài Tập OOP Cho Anh Em Tham Khảo! I-hate-Cats-icon
Nam Scorpio

Số bài viết : 1124
Điểm : 1688
Được cảm ơn : 35
Ngày sinh : 03/11/1990
Tham gia ngày : 16/03/2010
Tuổi : 33
Đến từ : Bình Dương
Ngề nghiệp : IT Student

Ít Bài Tập OOP Cho Anh Em Tham Khảo! Empty
Bài gửiTiêu đề: Ít Bài Tập OOP Cho Anh Em Tham Khảo!   Ít Bài Tập OOP Cho Anh Em Tham Khảo! I_icon_minitime24/9/2010, 20:21

Tình hình là do hôm nay mình mới bắt đầu ôn OOP nên lục lại mớ tài liệu và thấy mớ này, ai thik thì coi thêm nha!
1. Elip
Code:
include<conio.h>
#include<iostream>
using namespace std;
#include<string.h>
#include<stdio.h>


//------------------------------------------------------------------------

class elip{
protected:
int x,y;
int rng,rdoc;
public:
elip(int m,int n){set(m,n);};
void set(int m,int n)
{
if(m<0) m=1;
rng=m;
if(n<0) n=2;
rdoc=n;
};
friend istream& operator>>(istream &is,elip &B);
friend ostream& operator<<(ostream &os,elip B);

};

//------------------------------------------------------------------------

class hinhtron:public elip{
public:
hinhtron(int n):elip(n,n){};
friend istream& operator>>(istream &is,hinhtron &B);
friend ostream& operator<<(ostream &os,hinhtron B);

};

//------------------------------------------------------------------------

istream& operator>>(istream &is,elip &B)
{
cout<<"\n Nhap toa do tam elip (x;y): ";
is>>B.x>>B.y;
cout<<"\n Nhap kich thuoc elip : ";
cout<<"\n Nhap ban kinh ngang : ";
is>>B.rng;
cout<<"\n Nhap ban kinh doc : ";
is>>B.rdoc;
return is;
}

//------------------------------------------------------------------------

ostream& operator<<(ostream &os,elip B)
{
cout<<"\n Toa do tam elip : ";
os<<"("<<B.x<<";"<<B.y<<")";
cout<<"\n Ban kinh ngang :";
os<<B.rng;
cout<<"\n Ban kinh doc :";
os<<B.rdoc;
return os;
}

//------------------------------------------------------------------------

istream& operator>>(istream &is,hinhtron &B)
{
cout<<"\n Nhap toa do tam hinh tron (x,y) : ";
is>>B.x>>B.y;
cout<<"\n Nhap ban kinh hinh tron : ";
is>>B.rng;
return is;
}

//------------------------------------------------------------------------

ostream& operator<<(ostream &os,hinhtron B)
{
cout<<"\n Toa do tam hinh tron : ";
os<<"("<<B.x<<";"<<B.y<<")";
cout<<"\n Ban kinh hinh tron : ";
os<<B.rng;
return os;
}

//------------------------------------------------------------------------

void menu()
{
elip A(2,3);
hinhtron B(2);
int n,c,kq;
do{
system("cls");
cout<<"\n MENU CUA CHUONG TRINH ";
cout<<"\n-----------------*----------------";
cout<<"\n 1. Nhap elip ";
cout<<"\n 2. Xuat elip ";
cout<<"\n 3. Nhap hinh tron ";
cout<<"\n 4. Xuat hinh tron ";
cout<<"\n 0 .Thoat khoi chuong trinh";
cout<<"\n----------------(>.<)---------------";
cout<<"\n Lua chon cua ban la gi ??? :";
cin>>c;
switch(c)
{
case 1:
cin>>A;
cout<<"\n\n";
system(" PAUSE");
break;
case 2:
cout<<"\n elip vua nhap la :";
cout<<A;
cout<<"\n\n";
system(" PAUSE");
break;
case 3:
cin>>B;
cout<<"\n\n";
system(" PAUSE");
break;
case 4:
cout<<"\n hinh tron ban vua nhap la :";
cout<<B;
cout<<"\n\n";
system(" PAUSE");
break;
case 0:
exit(1);
break;
default:
cout<<"\n Ban da chon sai buoc,moi ban chon lai:";
cout<<"\n\n";
system(" PAUSE");
break;
}
}while(c!=0);
system("cls");
}
//------------------------------------------------------------------------
int main()
{
menu();
return 0;
}


//------------------------------------------------------------------------
Về Đầu Trang Go down
Sakura

Ít Bài Tập OOP Cho Anh Em Tham Khảo! Stars7
Sakura

Thú CƯng : Ít Bài Tập OOP Cho Anh Em Tham Khảo! I-hate-Cats-icon
Nam Scorpio

Số bài viết : 1124
Điểm : 1688
Được cảm ơn : 35
Ngày sinh : 03/11/1990
Tham gia ngày : 16/03/2010
Tuổi : 33
Đến từ : Bình Dương
Ngề nghiệp : IT Student

Ít Bài Tập OOP Cho Anh Em Tham Khảo! Empty
Bài gửiTiêu đề: Re: Ít Bài Tập OOP Cho Anh Em Tham Khảo!   Ít Bài Tập OOP Cho Anh Em Tham Khảo! I_icon_minitime24/9/2010, 20:22

2. List trên mảng:

Code:
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<math.h>
//Ư t??ng chính: tao 1 class node có du lieu la " int data". voi các hàm can` thie't
// Sau do tao 1 class list có p[100] ptu co kieu la "node".]
//Các buoc làm tuong tu voi list con tro cua struct
// De nhap dc list: minh can các hàm "them dau, them cuoi"
//Sau do nhap list thi ta cho them vao` cuoi cua danh sach, Zay. xong....
class node
{
int data;
public:
node(int temp=0){set(temp);}//Contructor
void set(int temp){data=temp;}
int getdata(){return data;}
~node(){} //Detructor
friend int operator >(node a,node b);
friend int operator <(node a,node b);
node taonode(int x){data=x;return*this;}// hàm dùng de tao ra 1 node moi
};
class list
{
node p[100];
int n;
public:
list(node q=0,int n1=0){set(q,n1);}
void set(node q,int n1){n=n1;p[0]=q;}
~list(){}

int getn(){return n;}
node operator()(int i){return p[i];}

friend istream &operator >>(istream& is,list &H);
friend ostream &operator <<(ostream& os,list H);

void themdau(int x);
void themcuoi(int x);
void thempt(int x,int y);
node timkiem(int x);

int kiemtrarong();
void sapxep();
};
int main()
{
list H;
cout<<"\nNhap danh sach,dung lai =0:";
cin>>H;
cout<<"\nDanh sach vua nhap:";
cout<<H;

int x,y;
cout<<"\nChen ptu ";
cin>>x;
cout<<"sau ptu :";
cin>>y;
H.thempt(x,y);
cout<<"\nList vua dc them la:";
cout<<H;
[You must be registered and logged in to see this link.]
//cout<<"\n"<<H;

H.sapxep();
cout<<"\nList vua dc sap xep la:";
cout<<H;
getch();
}
int list::kiemtrarong()
{
if(n!=0)
return 1;
else
return 0;
}
void list::themdau(int x)
{
node q;
q.taonode(x);

for(int i=n;i>0;i--)
p[i]=p[i-1];
p[0]=q;
n++;
}
void list::themcuoi(int x)
{
node q;
q.taonode(x);
p[n]=q;
n++;
}

node list::timkiem(int x)
{
node q;
int temp=NULL;
q.taonode(temp);
for(int i=0;i<n;i++)
if(p[i].getdata()==x)
{
q=p[i];
break;
}
return q;
}
void list ::thempt(int x,int y)
{
node q;
q=timkiem(y);
if(q.getdata()==NULL)
themcuoi(x);
else
{

int j=n;
while(p[j].getdata()!=q.getdata())
{
p[j]=p[j-1];
j--;
}
p[j+1].taonode(x);

n++;
}
}
istream &operator >>(istream& is,list &H)
{
int x;
int i=0;
cout<<"\n\tNhap ptu dau tien:";
is>>x;
while(x!=0)
{
H.themcuoi(x);

cout<<"\n\tNhap ptu thu "<<i+2<<":";
is>>x;
i++;
}
}
ostream &operator <<(ostream& os,list H)
{
for(int i=0;i<H.n;i++)
os<<H.p[i].getdata()<<" ";
}
int operator >(node a,node b)
{
if(a.getdata()>b.getdata())
return true;
return false;
}
int operator <(node a,node b)
{
if(a.getdata()<b.getdata())
return true;
return false;
}
void doi(node &a,node &b)
{
node tam;
tam=a;a=b;b=tam;
}
void list::sapxep()
{
for( int i=0;i<n-1;i++)
for( int j=i;j<n;j++)
if( p[j]<p[i])
doi(p[i],p[j]);
}
Về Đầu Trang Go down
Sakura

Ít Bài Tập OOP Cho Anh Em Tham Khảo! Stars7
Sakura

Thú CƯng : Ít Bài Tập OOP Cho Anh Em Tham Khảo! I-hate-Cats-icon
Nam Scorpio

Số bài viết : 1124
Điểm : 1688
Được cảm ơn : 35
Ngày sinh : 03/11/1990
Tham gia ngày : 16/03/2010
Tuổi : 33
Đến từ : Bình Dương
Ngề nghiệp : IT Student

Ít Bài Tập OOP Cho Anh Em Tham Khảo! Empty
Bài gửiTiêu đề: Re: Ít Bài Tập OOP Cho Anh Em Tham Khảo!   Ít Bài Tập OOP Cho Anh Em Tham Khảo! I_icon_minitime24/9/2010, 20:23

3. Nhân Viên:
Code:
#include <conio.h>
#include <iostream>
using namespace std;
#include <stdlib.h>
#include <string.h>

//------------------------------------------------------------------

class date
{
private:
int day;
int month;
int year;
public:
date(int d=1,int m=1,int y=1950){set(d,m,y);};
void set(int d,int m,int y)
{
if(d<1||d>31) d=1;
day=d;
if(m<1||m>12) m=1;
month=m;
if(y<1950||y>1992) y=1950;
year=y;
}
friend istream& operator>>(istream&is,date &a);
friend ostream& operator<<(ostream&os,date a);
};

//------------------------------------------------------------------

class nhanvien:public date{
private:
char name[30];
float cmnd;
char manv[10];
public:
nhanvien():date(1,1,1950){ strcpy(name,"Hello");
cmnd=11111;
strcpy(manv,"1111111");
};
char getn(){return *this->name;};
float getcm(){return cmnd;};
char getma(){return *this->manv;};
friend istream& operator>>(istream&is,nhanvien &a);
friend ostream& operator<<(ostream&os,nhanvien a);
int timcmnd(float x);
};

//------------------------------------------------------------------

class dsnv {
private:
nhanvien nv[50];
int n;
public:
friend istream& operator>>(istream&is,dsnv &a);
friend ostream& operator<<(ostream&os,dsnv a);
int timkiem(float x);
void themnv();
void xoanv(int m);
};

//------------------------------------------------------------------

istream& operator>>(istream&is,date &a)
{
cout<<"\n Nhap ngay sinh :";
is>>a.day;
while(a.day<0||a.day>31)
{
cout<<"\n Nhap lai ngay sinh : ";
is>>a.day;
}
cout<<"\n Nhap thang sinh :";
is>>a.month;
while(a.month<0||a.month>12)
{
cout<<"\n Nhap lai thang sinh : ";
is>>a.month;
}
cout<<"\n Nhap nam sinh :";
is>>a.year;
while(a.year<1950||a.year>1992)
{
cout<<"\n Nhan vien cua ban da qua gia de co the lam viec \n Hoac chua du tuoi lao dong theo quy dinh cua phap luat ";
cout<<"\n Moi ban nhap lai nam sinh cua nhan vien : ";
is>>a.year;
}
return is;
}

//------------------------------------------------------------------

ostream& operator<<(ostream&os,date a)
{
os<<a.day<<"/"<<a.month<<"/"<<a.year;
return os;
}


//------------------------------------------------------------------

istream& operator>>(istream&is,nhanvien &a)
{
cout<<"\n\n Nhap vao ten cua nhan vien : ";
fflush(stdin);
is.getline(a.name, sizeof(a.name));
is>>(date&)a;
cout<<"\n Nhap vao so chung minh nhan dan cua nhan vien : ";
is>>a.cmnd;
cout<<"\n Nhap vao ma nhan vien cua nhan vien : ";
fflush(stdin);
is.getline(a.manv, sizeof(a.manv));
return is;
}

//------------------------------------------------------------------

ostream& operator<<(ostream&os,nhanvien a)
{
cout<<"\n Ho ten :";
os<<a.name;
cout<<"\n Ngay,thang,nam sinh : ";
os<<(date&)a;
cout<<"\n So chung minh nhan dan : ";
os<<a.cmnd;
cout<<"\n Ma nhan vien : ";
os<<a.manv;
return os;
}

//------------------------------------------------------------------

istream& operator>>(istream&is,dsnv &a)
{
cout<<"\n Nhap vao so luong nhan vien cua cong ty : ";
is>>a.n;
cout<<"\n Nhap vao cac thong tin cua nhan vien : ";
for(int i=0;i<a.n;i++)
{
cout<<"\n Nhan vien thu "<<i+1<<" : ";
is>>a.nv[i];
}
return is;
}

//------------------------------------------------------------------

ostream& operator<<(ostream&os,dsnv a)
{
for(int i=0;i<a.n;i++)
{
cout<<"\n Nhan vien thu "<<i+1<<" : ";
cout<<a.nv[i];
}
return os;
}


//------------------------------------------------------------------

int dsnv::timkiem(float x)
{
int kq;
for(int i=0;i<n;i++)
{
kq=nv[i].timcmnd(x);
if(kq==1)
{
return 1;
break;
}
}
return 0;
}

//------------------------------------------------------------------

int nhanvien::timcmnd(float x)
{
if(cmnd==x) return 1;
return 0;
}

//------------------------------------------------------------------

void dsnv::themnv()
{
cin>>nv[n];
n++;
}

//------------------------------------------------------------------

void dsnv::xoanv(int m)
{
if(m>n) return;
else
{
for(int i=m-1;i<n;i++)
nv[i]=nv[i+1];
}
n--;
}

//------------------------------------------------------------------

void menu()
{
int n,c,kq;
char *k;
float x;
dsnv a;
nhanvien M;
do{
system("cls");
cout<<"\n MENU CUA CHUONG TRINH ";
cout<<"\n-----------------*----------------";
cout<<"\n 1. Nhap nhan vien ";
cout<<"\n 2. Xuat nhanvien ";
cout<<"\n 3. Tim kiem nhan vien theo so chung minh nhan dan ";
cout<<"\n 4. Them nhan vien moi ";
cout<<"\n 5. Loai bo mot nhan vien trong danh sach";
cout<<"\n 0 .Thoat khoi chuong trinh";
cout<<"\n----------------(>.<)---------------";
cout<<"\n Lua chon cua ban la gi ??? :";
cin>>c;
switch(c)
{
case 1:
cin>>a;
cout<<"\n\n";
system(" PAUSE");
break;
case 2:
cout<<"\n Cac nhan vien ban vua nhap la :";
cout<<a;
cout<<"\n\n";
system(" PAUSE");
break;
case 3:
cout<<"\n Nhap vao so cmnd cua nhan vien ban muon tim kiem : ";
cin>>x;
kq=a.timkiem(x);
if(kq==1) cout<<"\n Nhan vien nay co lam viec trong cong ty ";
if(kq==0) cout<<"\n Nhan vien nay khong co trong cong ty";
cout<<"\n\n";
system(" PAUSE");
break;
case 4:
cout<<"\n Bat dau them nhan vien :";
a.themnv();
cout<<"\n\n";
system(" PAUSE");
break;
case 5:
cout<<"\n Ban muon xoa nhan vien thu may cua cong ty :";
cin>>n;
a.xoanv(n);
cout<<"\n Danh sach nhan vien con lai la : ";
cout<<a;
cout<<"\n\n";
system(" PAUSE");
break;
case 0:
exit(1);
break;
default:
cout<<"\n Ban da chon sai buoc,moi ban chon lai:";
cout<<"\n\n";
system(" PAUSE");
break;
}
}while(c!=0);
system("cls");
}

//------------------------------------------------------------------

int main()
{
menu();
return 0;
}
Về Đầu Trang Go down
Sakura

Ít Bài Tập OOP Cho Anh Em Tham Khảo! Stars7
Sakura

Thú CƯng : Ít Bài Tập OOP Cho Anh Em Tham Khảo! I-hate-Cats-icon
Nam Scorpio

Số bài viết : 1124
Điểm : 1688
Được cảm ơn : 35
Ngày sinh : 03/11/1990
Tham gia ngày : 16/03/2010
Tuổi : 33
Đến từ : Bình Dương
Ngề nghiệp : IT Student

Ít Bài Tập OOP Cho Anh Em Tham Khảo! Empty
Bài gửiTiêu đề: Re: Ít Bài Tập OOP Cho Anh Em Tham Khảo!   Ít Bài Tập OOP Cho Anh Em Tham Khảo! I_icon_minitime24/9/2010, 20:23

4. Nhân Viên nữa:
Code:
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<iostream.h>
#include<stdlib.h>
#include<iomanip.h>

class nhanvien
{
char msnv[10];
char hoten[30];
char cmnd[10];
char namsinh[10];
public:
nhanvien(char* ms="",char* ht="",char* cm="",char* ns=""){set(ms,ht,cm,ns);}

friend istream& operator >>(istream& is,nhanvien &c);
friend ostream& operator <<(ostream& os,nhanvien c);

char* getms(){return msnv;}
char* getht(){return hoten;}
char* getcm(){return cmnd;}
char* getns(){return namsinh;}

void set(char* ms,char* ht,char* cm,char* ns)
{
strcpy(msnv,ms);
strcpy(hoten,ht);
strcpy(cmnd,cm);
strcpy(namsinh,ns);
}
};
class danhsach
{
nhanvien a[100];
int n;
public:

friend istream& operator >>(istream& is,danhsach &c);
friend ostream& operator <<(ostream& os,danhsach c);

int kiemtra(char kt[]);
void them();
void xoa(int t);
int sonv(){return n;}

};
int main()
{
danhsach b;
cin>>b;
system("cls");
cout<<"\nDanhsach cac nhan vien da nhap:";
cout<<b;
cout<<"\nNhap thong tin cua nhan vien can them:";
b.them();
system("cls");
cout<<"\nDanhsach nhanvien sau khi them:";
cout<<b;
char kt[10];
cout<<"\nNhap cmnd can kiem tra:";
gets(kt);
//system("cls");
int t=b.kiemtra(kt);
if(t!=-1)
{
cout<<"\nDa tim thay nhan vien co so cmnd nay la o vi tri thu "<<t+1;

}
else
cout<<"\nKo co nhan vien co cmnd nhu the.";
//system("cls");
cout<<"\nNhap cmnd cua nhan vien can xoa:";
char cmnd[10];
gets(cmnd);
t=b.kiemtra(cmnd);
if(t!=-1)
{
b.xoa(t);

}
else
cout<<"\nKo co nhan vien co cmnd nhu the.";

cout<<"\nDanhsach nv sau khi xoa.";
cout<<b;

getch();
}


istream& operator >>(istream& is,nhanvien &c)
{
fflush(stdin);
cout<<"\nNhap MSNV:";
is.getline(c.msnv,10);

cout<<"\nNhap ho ten:";
is.getline(c.hoten,30);

cout<<"\nNhap CMND:";
is.getline(c.cmnd,10);

cout<<"\nNhap nam sinh:";
is.getline(c.namsinh,10);

return is;
}
ostream& operator <<(ostream& os,nhanvien c)
{
cout<<"\n\tMSNV:";
os<<c.msnv;
cout<<"\n\tHo ten:";
os<<(c.hoten);
cout<<"\n\tCMND:";
os<<(c.cmnd);
cout<<"\n\tNam sinh:";
os<<(c.namsinh);
return os;
}
istream& operator >>(istream& is,danhsach &c)
{
cout<<"\nNhap so luong nhan vien can nhap:";
is>>c.n;
for(int i=0;i<c.n;i++)
{
cout<<"\nNhap nhan vien thu "<<i+1;
is>>c.a[i];
}
return is;
}
ostream& operator <<(ostream& os,danhsach c)
{
os<<endl<<endl<<setw(40)<<"DANH SACH CAC NHAN VIEN: \n ";

os<<endl<<endl<<setw(4)<<"STT"<<setw(10)<<"MSNV"<<setw(20)<<"Ho ten"<<setw(15)<<"CMND"<<setw(12)<<"Nam sinh"<<setw(10)<<endl;
for(int i=0;i<c.n;i++)
os<<endl<<setw(4)<<i+1<<setw(10)<<c.a[i].getms()<<setw(20)<<c.a[i].getht()<<setw(15)<<c.a[i].getcm()<<setw(12)<<c.a[i].getns()<<setw(10);
return os;
}
void danhsach::them()
{
cin>>a[n];
n++;
}
int danhsach::kiemtra(char kt[])
{
for(int i=0;i<n;i++)
{
if(strcmp(a[i].getcm(),kt)==0)
return i;
}

return -1;
}
void danhsach::xoa(int t)
{
for(int i=t;t<n;t++)
a[t]=a[t+1];
n--;
}
Về Đầu Trang Go down
Sakura

Ít Bài Tập OOP Cho Anh Em Tham Khảo! Stars7
Sakura

Thú CƯng : Ít Bài Tập OOP Cho Anh Em Tham Khảo! I-hate-Cats-icon
Nam Scorpio

Số bài viết : 1124
Điểm : 1688
Được cảm ơn : 35
Ngày sinh : 03/11/1990
Tham gia ngày : 16/03/2010
Tuổi : 33
Đến từ : Bình Dương
Ngề nghiệp : IT Student

Ít Bài Tập OOP Cho Anh Em Tham Khảo! Empty
Bài gửiTiêu đề: Re: Ít Bài Tập OOP Cho Anh Em Tham Khảo!   Ít Bài Tập OOP Cho Anh Em Tham Khảo! I_icon_minitime24/9/2010, 20:24

5. Phân Số, còn nhiều lắm!
#include
#include
#include

class phanso{
private:
int tu,mau;
int uscln(int a,int b);
void rutgon();
public:
phanso (int ts=0,int ms=1){set(ts,ms);}
void set(int ts,int ms){tu=ts; if (ms==0) ms=1; mau=ms;}
friend phanso operator +(phanso a,phanso b);
friend phanso operator - (phanso a,phanso b);
friend phanso operator * (phanso a,phanso b);
friend phanso operator / (phanso a,phanso b);
phanso operator -(){return phanso(-tu,mau);}

friend int operator ==(phanso a,phanso b);
friend int operator !=(phanso a,phanso b);
friend int operator >(phanso a,phanso b);
friend int operator <(phanso a,phanso b);
friend int operator >=(phanso a,phanso b);
friend int operator <=(phanso a,phanso b);

phanso operator += (phanso b){*this=*this+b; return *this;};
phanso operator -= (phanso b){*this=*this-b; return *this;};
phanso operator *= (phanso b){*this=*this*b; return *this;};
phanso operator /= (phanso b){*this=*this/b; return *this;};

friend istream &operator >>(istream &is,phanso &ps);
friend ostream &operator<<(ostream &os,phanso ps);

int gettu(){return tu;};
int getmau(){return mau;};
};

class mangphanso{
private:
phanso a[100];
int n;
public:
friend istream&operator >>(istream &is,mangphanso &mps);
friend ostream&operator<<(ostream &os,mangphanso mps);
phanso max();
phanso min();
void sapxeptang();
void sapxepgiam();
int kiemtra(phanso x);
phanso trungbinh();
void them(phanso y);
void xoa(int z);
};

int main()
{
mangphanso b;
cin>>b;
cout<<"\nmang phan so vua nhap la: ";
cout<
phanso x;
cout<<"nhap phan so can kiem tra: ";
cin>>x;
cout<if (b.kiemtra(x)==-1) cout<<"ko co trong mang";
else cout<<"vi tri phan tu x trong mang la: "<
cout<<"\nphan so lon nhat cua mang phan so la: "<cout<<"\nphan so nho nhat cua mang phan so la: "<cout<<"\nTrung binh cong cua mang la:"<
phanso y;
cout<<"\nNhap phan tu can them: ";
cin>>y;
b.them(y);
cout<<"\nmang vua dc them:";
cout<
cout<<"\nmang phan so da sap xep tang la:";
b.sapxeptang();
cout<cout<<"\nmang phan so da sap xep giam la:";
b.sapxepgiam();
cout<
int z;
cout<<"\nNhap vi tri can xoa:";
cin>>z;
b.xoa(z);
cout<<"\nMang phan so sau khi xoa la:";
cout<
getch();
return 1;
}
//nhap/xuat phan so
istream &operator>>(istream &is,phanso &ps)
{
cout<<"\nnhap tu: ";
is>>ps.tu;
cout<<"\nnhap mau: ";
is>>ps.mau;
while (!ps.mau)
{
cout<<"nhap lai mau so";
is>>ps.mau;
}
ps.rutgon();
return is;
}
ostream &operator<<(ostream &os,phanso ps)
{
os<if (ps.tu!=0&&ps.mau!=1)
os<<"/"<return os;
}

int phanso::uscln(int a,int b)
{
while (a!=b)
{
if (a>b) a=a-b;
else b=b-a;
}
return a;
}
void phanso::rutgon()
{ phanso kq;
int us,t;
t=tu%mau;
if (t==0) kq=t;
else
{
us=kq.uscln(abs(tu),abs(mau));
tu=tu/us;
mau=mau/us;
}
}
phanso operator +(phanso a,phanso b)
{
phanso kq;
kq.tu=a.tu*b.mau + a.mau*b.tu;
kq.mau=a.mau*b.mau;
kq.rutgon();
return kq;
}
phanso operator -(phanso a,phanso b)
{
return a +(-b);
}
phanso operator *(phanso a,phanso b)
{
phanso kq;
kq.tu=a.tu*b.tu;
kq.mau=a.mau*b.mau;
kq.rutgon();
return kq;
}
phanso operator /(phanso a,phanso b)
{
phanso kq;
kq.tu=a.tu*b.mau;
kq.mau=a.mau*b.tu;
kq.rutgon();
return kq;
}

int operator ==(phanso a,phanso b)
{
int c,d;
c=a.tu*b.mau;
d=b.tu*a.mau;
if (c==d)
return true;
return false;
}
int operator !=(phanso a,phanso b)
{
if (a==b)
return false;
return true;
}
int operator >(phanso a,phanso b)
{
int c,d;
c=a.tu*b.mau;
d=b.tu*a.mau;
if (c>d)
return true;
return false;
}
int operator <(phanso a,phanso b)
{
int c,d;
c=a.tu*b.mau;
d=b.tu*a.mau;
if (creturn true;
return false;
}
int operator >=(phanso a,phanso b)
{
if (a>b||a==b)
return true;
return false;
}
int operator <=(phanso a,phanso b)
{
if (areturn true;
return false;
}
//nhap/xuat mang phan so
istream &operator>>(istream &is,mangphanso &mps)
{
cout<<"nhap so phan tu: ";
is>>mps.n;
for (int i=0;i{
cout<<"\n nhap phan so thu "<is>>mps.a[i];
}
}
ostream &operator<<(ostream &os,mangphanso mps)
{
for (int i=0;ireturn os;
}
phanso mangphanso::max()
{
phanso M=a[0];
for (int i=0;iif (MM=a[i];
return M;
}
phanso mangphanso::min()
{
phanso m=a[0];
for (int i=0;iif (m>a[i])
m=a[i];
return m;
}
void swap (phanso &a,phanso &b)
{
phanso t;
t=a;
a=b;
b=t;
}
void mangphanso::sapxepgiam()
{
for (int i=0;ifor (int j=i;jif (a[i]swap(a[i],a[j]);
}
void mangphanso::sapxeptang()
{
for (int i=0;ifor (int j=i;jif (a[i]>a[j])
swap(a[i],a[j]);
}
int mangphanso::kiemtra(phanso x)
{
for (int i=0;iif (a[i]==x)
return i;
return -1;
}
phanso mangphanso::trungbinh()
{
phanso tong;
tong=a[0];
for (int k=1;ktong+=(a[k]);
tong=tong/n;
return tong;
}
void mangphanso::them(phanso x)
{
a[n]=x;
n++;
}
void mangphanso::xoa(int z)
{
for (int i=z;ia[i]=a[i+1];
n--;
}
Về Đầu Trang Go down
Sakura

Ít Bài Tập OOP Cho Anh Em Tham Khảo! Stars7
Sakura

Thú CƯng : Ít Bài Tập OOP Cho Anh Em Tham Khảo! I-hate-Cats-icon
Nam Scorpio

Số bài viết : 1124
Điểm : 1688
Được cảm ơn : 35
Ngày sinh : 03/11/1990
Tham gia ngày : 16/03/2010
Tuổi : 33
Đến từ : Bình Dương
Ngề nghiệp : IT Student

Ít Bài Tập OOP Cho Anh Em Tham Khảo! Empty
Bài gửiTiêu đề: Re: Ít Bài Tập OOP Cho Anh Em Tham Khảo!   Ít Bài Tập OOP Cho Anh Em Tham Khảo! I_icon_minitime24/9/2010, 20:25

6. thêm bài Phân Số:

Code:
#include<iostream.h>
#include<conio.h>
class PHANSO   
{
      int tuso;
      int mauso;
public:
        void nhap();
        void xuat();
        void rutgon();
        PHANSO cong(PHANSO b);
        PHANSO tru(PHANSO b);
        PHANSO nhan(PHANSO b);       
        PHANSO chia(PHANSO b);
};
void PHANSO :: nhap()
{
      cout<<"nhap tu so";
      cin>>tuso;
      cout<<"nhap mau so";
      cin>>mauso;
}   
void PHANSO::xuat()
{
      cout<<tuso<<"/"<<mauso<<"\n";;     
}

PHANSO PHANSO::cong(PHANSO b)
{
      PHANSO c;
      c.tuso=(mauso*b.tuso)+(b.mauso*tuso);
      c.mauso=mauso*b.mauso;
      c.rutgon();
      return c;
}
PHANSO PHANSO::tru(PHANSO b)
{
      PHANSO c;
      c.tuso=(tuso*b.mauso)-(mauso*b.tuso);
      c.mauso=mauso*b.mauso;
      c.rutgon();
      return c;
}
PHANSO PHANSO::nhan(PHANSO b)
{
      PHANSO c;
      c.tuso=tuso*b.tuso;
      c.mauso=mauso*b.mauso;
      c.rutgon();
      return c;
}
PHANSO PHANSO::chia(PHANSO b)
{
      PHANSO c;
      c.tuso=tuso*b.mauso;
      c.mauso=mauso*b.tuso;
      c.rutgon();
      return c;
}
int uscln(int a,int b)
{
    while(a!=b)
    {
                if(a>b)
                      a=a-b;
                else
                    b=b-a;
    }
    return a;
}
void PHANSO::rutgon()
{
      int d;
      d=uscln(tuso,mauso);
      tuso=tuso/d;
      mauso=mauso/d;

int main()
{
    PHANSO a,b,c;
    a.nhap();
    a.xuat();
    b.nhap();
    b.xuat();
    cout<<"cong phan so";
    c=a.cong(b);
    c.xuat();
    cout<<"tru phan so";
    c=a.tru(b);
    c.xuat();
    cout<<"nhan phan so";
    c=a.nhan(b);
    c.xuat();
    cout<<"chia phan so";
    c=a.chia(b);
    c.xuat();
    system("PAUSE");
    return 0;
    }
Về Đầu Trang Go down
Sakura

Ít Bài Tập OOP Cho Anh Em Tham Khảo! Stars7
Sakura

Thú CƯng : Ít Bài Tập OOP Cho Anh Em Tham Khảo! I-hate-Cats-icon
Nam Scorpio

Số bài viết : 1124
Điểm : 1688
Được cảm ơn : 35
Ngày sinh : 03/11/1990
Tham gia ngày : 16/03/2010
Tuổi : 33
Đến từ : Bình Dương
Ngề nghiệp : IT Student

Ít Bài Tập OOP Cho Anh Em Tham Khảo! Empty
Bài gửiTiêu đề: Re: Ít Bài Tập OOP Cho Anh Em Tham Khảo!   Ít Bài Tập OOP Cho Anh Em Tham Khảo! I_icon_minitime24/9/2010, 20:25

7. Tam giác:

Code:
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#include<stdio.h>

class diem
{
float x;
float y;
public:
diem(float h=0,float t=0){set(h,t);}
void set(float h,float t){x=h;y=t;}
float getx(){return x;}
float gety(){return y;}

float kc(diem b){return(sqrt((b.x-x)*(b.x-x)+(b.y-y)*(b.y-y)));}
friend diem operator -(diem a,diem b);
friend diem operator +(diem a,diem b);

friend istream &operator >>(istream &is,diem &b);
friend ostream &operator <<(ostream &os,diem b);
};
class tamgiac
{
diem a,b,c;
float a1,b1,c1;//do dai cac canh cua tam giac
public:
tamgiac(diem A=0,diem B=0,diem C=0){set(A,B,C);}
void set(diem A,diem B,diem C){a=A;b=B;c=C;a1=b.kc(c);b1=a.kc(c);c1=a.kc(b);}
diem geta(){return a;}
diem getb(){return b;}
diem getc(){return c;}
float geta1(){return a1;}
float getb1(){return b1;}
float getc1(){return c1;}

friend istream &operator >>(istream &is,tamgiac &b);
friend ostream &operator <<(ostream &os,tamgiac b);

float chuvi();
void tinhtien();
};
int main()
{
tamgiac T;
cout<<"\nNhap tam giac:";
cin>>T;
cout<<"\nCac thong tin cua tam giac vua nhap la:";
cout<<T;

T.tinhtien();
cout<<"\nThong tin tam giac vua dc tinh tien:";
cout<<T;

getch();
}
istream &operator >>(istream &is,diem &b)
{
cout<<"\n\t\tNhap hoanh do:";
is>>b.x;
cout<<"\n\t\tNhap tung do:";
is>>b.y;
}
ostream &operator <<(ostream &os,diem b)
{
os<<"("<<b.x<<","<<b.y<<")";
}
istream &operator >>(istream &is,tamgiac &b)
{
cout<<"\n\tNhap diem A:";
is>>b.a;
cout<<"\n\tNhap diem B:";
is>>b.b;
cout<<"\n\tNhap diem C:";
is>>b.c;

b.a1=b.b.kc(b.c);b.b1=b.a.kc(b.c);b.c1=b.a.kc(b.b);
}
ostream &operator <<(ostream &os,tamgiac b)
{
os<<"\nDiem A"<<b.a<<"\tDiem B"<<b.b<<"\tDiem C"<<b.c;
float cv=b.chuvi();
os<<"\nChu vi tam giac CV="<<cv;
}
float tamgiac::chuvi()
{
float cv=a1+b1+c1;
return cv;
}
void tamgiac::tinhtien()
{
diem p;
cout<<"\nNhap toa do can tinh tien:";
cin>>p;
a=a+p;b=b+p;c=c+p;

}
diem operator +(diem a,diem b)
{
diem c;
c.x=a.x+b.x;
c.y=a.y+b.y;
return c;
}
diem operator -(diem a,diem b)
{
diem c;
c.x=a.x-b.x;
c.y=a.y-b.y;
return c;
}

Ai thik nữa thì cứ pmm, mình post tiếp, mất công mọi người bảo là Spam nữa thì khổ em!
Về Đầu Trang Go down
that_true

Member Nhiệt Tình


Member Nhiệt Tình
that_true

Thú CƯng : Ít Bài Tập OOP Cho Anh Em Tham Khảo! Dolphin-icon
Nam Scorpio

Số bài viết : 154
Điểm : 187
Được cảm ơn : 0
Ngày sinh : 07/11/1988
Tham gia ngày : 17/09/2010
Tuổi : 35
Đến từ : venus
Ngề nghiệp : catch

Ít Bài Tập OOP Cho Anh Em Tham Khảo! Empty
Bài gửiTiêu đề: Re: Ít Bài Tập OOP Cho Anh Em Tham Khảo!   Ít Bài Tập OOP Cho Anh Em Tham Khảo! I_icon_minitime24/9/2010, 21:29

Đề nghị chú H vứt ngay cái avatar đó ngay >> ô nhiễm môi trường quá!
Xin cảnh báo : đây là forum học tập >> chú mà thế này không khéo kích thích các bạn nữ trong forum mất!!
Nguy hiểm quá!!
Về Đầu Trang Go down
Sakura

Ít Bài Tập OOP Cho Anh Em Tham Khảo! Stars7
Sakura

Thú CƯng : Ít Bài Tập OOP Cho Anh Em Tham Khảo! I-hate-Cats-icon
Nam Scorpio

Số bài viết : 1124
Điểm : 1688
Được cảm ơn : 35
Ngày sinh : 03/11/1990
Tham gia ngày : 16/03/2010
Tuổi : 33
Đến từ : Bình Dương
Ngề nghiệp : IT Student

Ít Bài Tập OOP Cho Anh Em Tham Khảo! Empty
Bài gửiTiêu đề: Re: Ít Bài Tập OOP Cho Anh Em Tham Khảo!   Ít Bài Tập OOP Cho Anh Em Tham Khảo! I_icon_minitime24/9/2010, 22:00

that_true đã viết:
Đề nghị chú H vứt ngay cái avatar đó ngay >> ô nhiễm môi trường quá!
Xin cảnh báo : đây là forum học tập >> chú mà thế này không khéo kích thích các bạn nữ trong forum mất!!
Nguy hiểm quá!!
Tại vì dạo này anh ít thấy Mem nữ nào vào quá nên làm vậy để kích thích thị trường thôi mừ!
hành động này phải được tuyên dương mới đúng chứ! Heheh!
Về Đầu Trang Go down
d0m3n.0f.d3ath

Member Năng Động


Member Năng Động
d0m3n.0f.d3ath

Thú CƯng : Ít Bài Tập OOP Cho Anh Em Tham Khảo! Panda-icon
Nam Cancer

Số bài viết : 109
Điểm : 118
Được cảm ơn : 0
Ngày sinh : 27/06/1990
Tham gia ngày : 23/09/2010
Tuổi : 33
Đến từ : 11TH02
Ngề nghiệp : Sinh Viên

Ít Bài Tập OOP Cho Anh Em Tham Khảo! Empty
Bài gửiTiêu đề: Re: Ít Bài Tập OOP Cho Anh Em Tham Khảo!   Ít Bài Tập OOP Cho Anh Em Tham Khảo! I_icon_minitime24/9/2010, 22:40

Pác H ! cứ chém thế ... Nhìn cái ảnh s3xy qúa đấy yêu cầu đổi ngay Neutral
Về Đầu Trang Go down
https://itworld.forumvi.net
d0m3n.0f.d3ath

Member Năng Động


Member Năng Động
d0m3n.0f.d3ath

Thú CƯng : Ít Bài Tập OOP Cho Anh Em Tham Khảo! Panda-icon
Nam Cancer

Số bài viết : 109
Điểm : 118
Được cảm ơn : 0
Ngày sinh : 27/06/1990
Tham gia ngày : 23/09/2010
Tuổi : 33
Đến từ : 11TH02
Ngề nghiệp : Sinh Viên

Ít Bài Tập OOP Cho Anh Em Tham Khảo! Empty
Bài gửiTiêu đề: Re: Ít Bài Tập OOP Cho Anh Em Tham Khảo!   Ít Bài Tập OOP Cho Anh Em Tham Khảo! I_icon_minitime24/9/2010, 22:40

Pác H ! cứ chém thế ... Nhìn cái ảnh s3xy qúa đấy yêu cầu đổi ngay Neutral
Về Đầu Trang Go down
https://itworld.forumvi.net
Sponsored content




Ít Bài Tập OOP Cho Anh Em Tham Khảo! Empty
Bài gửiTiêu đề: Re: Ít Bài Tập OOP Cho Anh Em Tham Khảo!   Ít Bài Tập OOP Cho Anh Em Tham Khảo! I_icon_minitime

Về Đầu Trang Go down
 

Ít Bài Tập OOP Cho Anh Em Tham Khảo!

Xem chủ đề cũ hơn Xem chủ đề mới hơn Về Đầu Trang 
Trang 1 trong tổng số 1 trang

 Similar topics

-
» Thêm một đề thi tham khảo môn HĐH
» đề thi LTDT(tham khảo)
» đi thích đi code thì vào đây tham khảo
» ai thích code thì có thể tham khảo thử cái này
» [Thông Báo] Đề tham khảo mạng máy tính bị sai quá trời!

Permissions in this forum:Bạn không có quyền trả lời bài viết
IT World! :: HỌC TẬP :: Học Kỳ IV :: Lập Trình Hướng Dối Tượng(OOP)-