C++编程练习-员工上下班

// 输入N个员工,每个员工输出ID号,上班时间,下班时间,
// 第一行输出最早到的员工的ID和上班时间
// 第二行输出最迟走的员工的ID和下班时间
// 第三行输出工作时间最久的员工的ID和上班时间

#include<iostream>
using namespace std;

// 定义时间结构体
struct time{
	int hour;
	int min;
	int sec;
};
// 定义员工结构体 
struct member{
	 char id[50];
	 struct time t1, t2;
	 int w_time;   // 工作时间
};

// 比较最早上班的
bool judge_first(member m1,member m2) {  
	if (m1.t1.hour != m2.t1.hour)
		return m1.t1.hour < m2.t1.hour;
	else if (m1.t1.min != m2.t1.min)
		return m1.t1.min < m2.t1.min;
	else
		return m1.t1.sec < m2.t1.sec;
}

// 比较最晚下班的
bool judge_last(member m1, member m2) {
	if (m1.t1.hour != m2.t1.hour)
		return m1.t1.hour > m2.t1.hour;
	else if (m1.t1.min != m2.t1.min)
		return m1.t1.min > m2.t1.min;
	else
		return m1.t1.sec > m2.t1.sec;
}
// 工作时间最长的
bool judge_long(member m1, member m2) {
	return m1.w_time > m2.w_time;
}

int main() {
	cout << "输入员工数:";
	int N = 0;
	cin >> N;
	cout << "输入每个员工的ID号,上班时间和下班时间" << endl;
	member mem[20]{};   // 假设有20个员工


	for (int i = 0; i < N; i++) {
		cin >> mem[i].id;
		scanf("%2d:%2d:%2d", &mem[i].t1.hour, &mem[i].t1.min, &mem[i].t1.sec);
		scanf("%2d:%2d:%2d", &mem[i].t2.hour, &mem[i].t2.min, &mem[i].t2.sec);

		mem[i].w_time = (mem[i].t2.hour - mem[i].t1.hour) * 3600 
			+ (mem[i].t2.min - mem[i].t1.min) * 60 
			+ mem[i].t2.sec - mem[i].t1.sec;
	}
	member tmp[3] = { mem[0], mem[0] ,mem[0] };
	for (int i = 0; i < N - 1; i++) {
		if (!judge_first(tmp[0], mem[i + 1]))
			tmp[0] = mem[i + 1];
		if (!judge_last(tmp[1], mem[i + 1]))
			tmp[1] = mem[i + 1];
		if (!judge_long(tmp[2], mem[i + 1]))
			tmp[2] = mem[i + 1];
	}
	printf("最早到的员工:%s  上班时间:%.2d:%.2d:%.2d\n", tmp[0].id, tmp[0].t1.hour, tmp[0].t1.min, tmp[0].t1.sec);
	printf("最晚走的员工:%s  下班时间:%.2d:%.2d:%.2d\n", tmp[1].id, tmp[1].t2.hour, tmp[1].t2.min, tmp[1].t2.sec);
	printf("工作时间最长的员工:%s  上班时间:%.2d:%.2d:%.2d\n", tmp[2].id, tmp[2].t1.hour, tmp[2].t1.min, tmp[2].t1.sec);

	system("pause");
	return 0;
}

输出测试:

猜你喜欢

转载自blog.csdn.net/MARS_098/article/details/114342862