Tải bản đầy đủ (.docx) (4 trang)

Ví dụ về Các lớp sắp xếp

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (57.33 KB, 4 trang )

. Ví dụ về Các lớp sắp xếp
Trong mục này trình bầy 2 chương trình minh hoạ cách
dùng các lớp nói trên. Chương trình thứ nhất minh hoạ cách
sử dụng các lớp trong tệp C_SORT.H để sắp xếp một dẫy thí
sinh theo thứ tự giảm và thứ tự tăng của tổng điểm. Chương
trình thứ hai minh hoạ cách dùng các lớp trong C_SORT.H để
sắp xếp một dẫy số nguyên theo chiều tăng và chiều giảm.
Chương trình 1
//CT10-08
// Lop co so truu tuong
// Lop sort
#include "c_sort.h"
class TS
{
private:
char ht[25];
int sobd;
float td;
public:
float get_td()
{
return td;
}
void nhap()
{
cout << "\nHo ten: " ;
fflush(stdin);
gets(ht);
cout << "So bao danh: " ;
cin >> sobd;
cout << "Tong diem: " ;


cin >> td;
}
void xuat()
{
cout << "\nHo ten: " << ht;
cout << "\nSo bao danh: " << sobd;
cout << "\nTong diem: " <<
setiosflags(ios::showpoint)
<< setprecision(1)<<setw(5)<<
td;
}
};
int ss_tong_diem_giam(void *ts1, void *ts2)
{
return ( ((TS*)ts1)->get_td() > ((TS*)ts2)->get_td()) ;
}
int ss_tong_diem_tang(void *ts1, void *ts2)
{
return ( ((TS*)ts1)->get_td() < ((TS*)ts2)->get_td()) ;
}
void main()
{
TS t[100];
sort *sa;
544 545
int n,i;
clrscr();
cout << "\nSo thi sinh: ";
cin >> n;
for(i=1; i<=n; ++i) t[i].nhap();

for(i=1; i<=n; ++i) t[i].xuat();
getch();
cout << "\n\nSap xep giam theo tong diem - PP Select
Sort" ;
sa= new select_sort;
sa->sapxep( t+1,n,sizeof(TS),ss_tong_diem_giam);
for(i=1; i<=n; ++i) t[i].xuat();
delete sa;
getch();
cout << "\n\nSap xep tang theo tong diem - PP Select
Sort";
sa= new select_sort;
sa->sapxep( t+1,n,sizeof(TS),ss_tong_diem_tang);
for(i=1; i<=n; ++i) t[i].xuat();
delete sa;
getch();
cout << "\n\nSap xep giam theo tong diem - PP Quick
Sort" ;
sa= new quick_sort;
sa->sapxep( t+1,n,sizeof(TS),ss_tong_diem_giam);
for(i=1; i<=n; ++i) t[i].xuat();
delete sa;
getch();
cout << "\n\nSap xep tang theo tong diem - PP Quick
Sort" ;
sa= new quick_sort;
sa->sapxep( t+1,n,sizeof(TS),ss_tong_diem_tang);
for(i=1; i<=n; ++i) t[i].xuat();
delete sa;
getch();

cout << "\n\nSap xep giam theo tong diem - PP Heap
Sort" ;
sa= new heap_sort;
sa->sapxep( t+1,n,sizeof(TS),ss_tong_diem_giam);
for(i=1; i<=n; ++i) t[i].xuat();
delete sa;
getch();
cout << "\n\nSap xep tang theo tong diem - PP Heap
Sort" ;
sa= new heap_sort;
sa->sapxep( t+1,n,sizeof(TS),ss_tong_diem_tang);
for(i=1; i<=n; ++i) t[i].xuat();
delete sa;
getch();
}
Chương trình 2
//CT10-09
// Lop co so truu tuong
// Lop sort
#include "c_sort.h"
int ss_tang(void *i1,void *i2)
546 547
{
return *((int*)i1) < *((int*)i2);
}
int ss_giam(void *i1,void *i2)
{
return *((int*)i1) > *((int*)i2);
}
void main()

{
int i,n;
struct time t1,t2;
int b[20],a[20], k, tg, sec, hund;
n=10;
sort *s[3];
select_sort ss;
quick_sort qs;
heap_sort hs;
s[0]=&ss; s[1]=&qs; s[2]=&hs;
clrscr();
srand(5000);
for(i=1;i<=n;++i)
b[i]=rand();
cout<<"\nDay ban dau\n ";
for(i=1;i<=n;++i) cout <<b[i]<<" ";
cout<<"\n\nCac day tang sap xep theo ";
cout << "select_sort, quick_sort, heap_sort\n";
for(k=0; k<3; ++k)
{
for(i=1;i<=n;++i)
a[i]=b[i];
s[k]->sapxep (a+1,n,sizeof(int),ss_tang);
//In
for(i=1;i<=n;++i) cout <<a[i]<<" ";
cout<<"\n";
}
cout<<"\n\nCac day giam sap xep theo ";
cout << "select_sort, quick_sort, heap_sort\n";
for(k=0; k<3; ++k)

{
for(i=1;i<=n;++i)
a[i]=b[i];
s[k]->sapxep (a+1,n,sizeof(int),ss_giam);
//In
for(i=1;i<=n;++i) cout <<a[i]<<" ";
cout << "\n";
}
getch();
}
548 549

×