PAT-1006. Sign In and Sign Out (25)(一)

2014-11-24 01:07:53 · 作者: · 浏览: 10
分析:用c++的类来做,定义俩个重载运算符即可。简单题。
只是因为 vc6.0的bug,在使用友元函数定义重载运算符时可能会出错,这时头部不能这么写:
[cpp]
#include
#include
using namespace std;
应该要改成如下:
[cpp]
#include
#include
using std::cin;
using std::cout;
using std::endl;
using std::string;
题目描述:
At the beginning of every day, the first person who signs in the computer room will unlock the door, and the last one who signs out will lock the door. Given the records of signing in's and out's, you are supposed to find the ones who have unlocked and locked the door on that day.
Input Specification:
Each input file contains one test case. Each case contains the records for one day. The case starts with a positive integer M, which is the total number of records, followed by M lines, each in the format:
ID_number Sign_in_time Sign_out_time
where times are given in the format HH:MM:SS, and ID number is a string with no more than 15 characters.
Output Specification:
For each test case, output in one line the ID numbers of the persons who have unlocked and locked the door on that day. The two ID numbers must be separated by one space.
Note: It is guaranteed that the records are consistent. That is, the sign in time must be earlier than the sign out time for each person, and there are no two persons sign in or out at the same moment.
Sample Input:
3
CS301111 15:30:28 17:00:10
SC3021234 08:00:00 11:25:25
CS301133 21:45:00 21:58:40
Sample Output:
SC3021234 CS301133
参考代码:
[cpp]
//#include
//using namespace std;
#include
using std::cin;
using std::cout;
using std::endl;
using std::string;
#include
class Time
{
public:
Time(){};
int hour;
int min;
int sec;
void set(int h,int m,int s);
friend bool operator > (Time &t1,Time &t2);
friend bool operator < (Time &t1,Time &t2);
//friend void operator = (Time &t1,Time &t2);
};
/*void operator = (Time &t1,Time &t2)
{
t1.hour = t2.hour;
t1.min = t2.min;
t1.sec = t2.sec;
}*/
bool operator > (Time &t1,Time &t2)
{
if(t1.hour > t2.hour) return true;
else if(t1.hour == t2.hour)
{
if(t1.min > t2.min) return true;
else if(t1.min == t2.min)
{
if(t1.sec > t2.sec) return true;
else return false;
}
else return false;
}
else return false;
}
bool operator < (Time &t1,Time &t2)
{
if(t1.hour < t2.hour) return true;
else if(t1.hour == t2.hour)
{
if(t1.min < t2.min) return true;
else if(t1.min == t2.min)
{
if(t1.sec < t2.sec) return true;
else return false;
}
else return false;
}
else return false;
}
void Time::set(int h,int m,int s)
{
hour = h;
min = m;
sec = s;
}
class Record
{
public:
Record(){};
void set(string s);
string num;
Time enter;
Time out;
};
void Record::set(string s)
{
num = s;
}
int main()
{
int N;
string num;
int h,m,s;
int flag_in,flag_out;
Time t1,t2;
t1.set(23,59,59); t2.set(0,0,0);
char t;
cin>>N;
Record * R = new Record[N];
int i;
for(i=0; i
{
cin>>num;
R[i].num = num; //设置编号
cin>>h>>t>>m>>t>>s; //进入时间
R[i].enter.set(h,m,s);
if(R[i].e