Friday, May 13, 2016

Nhập một danh sách thời gian rồi sắp xếp danh sách thời gian đã nhập theo chiều tăng dần

//Họ Tên: Trần Văn Linh
//MaSV:581597
//Lớp:K58QLTT
--------------------------------------------------------------------------------------------------------------------------
//thoigian.cpp
#include<iostream>

using namespace std;

//Khai bao lop
class thoigian
{
private:
int gio;
int phut;
int giay;
public:
void nhap();
void hien();
int operator>(thoigian &t);
};
//Chuong trinh chinh
int main()
{
  thoigian t[10];
int i,j;
int n;

cout<<"Nhap so luong thoi gian.";
cin>>n;
for(i=0;i<n;i++)
t[i].nhap();
       
        //Sắp xếp
for(i=0;i<n;i++)
for(j=n-1;j>i;j--)
if(t[j-1]>t[j])
{
thoigian temp=t[j];
t[j]=t[j-1];
t[j-1]=temp;
}

cout<<"Danh sach thoi gian da nhap:";
for(i=0;i<n;i++)
t[i].hien();

return 0;
}
//Dinh nghia ham
void thoigian::nhap()
{
cout<<"Nhap gio:";
cin>>gio;
cout<<"Nhap phut:";
cin>>phut;
  cout<<"Nhap giay:";
cin>>giay;
}
//-------------
void thoigian::hien()
{
cout<<gio<<":"<<phut<<":"<<giay<<endl;
}
//----------
int thoigian::operator>(thoigian &t)
{
if(gio>t.gio)
return 1;
else if(gio==t.gio&&phut>t.phut)
return 1;
else if(gio==t.gio&&phut==t.phut&&giay>t.giay)
return 1;
return 0;
}

0 comments:

Post a Comment