功能描述:某实验室对学生每天的学习时间进行管理,记录其到达时间和其离开时间。两个时间之间的时间段作为其当天的学习时间。设计一个考勤管理系统。
功能要求:
1.信息录入功能,提示用户逐项输入学生信息,并检查输入信息的合法性(如时、分、秒的取值范围),同时以分为单位计算每位同学的学习时间;
2.考勤信息浏览功能,显示所有人的考勤情况;
3.排序功能,按学习时间长短进行排序并显示
4.使用菜单选择操作,方便用户查看不同任务的解决方法以及结果;
5.将处理后的数据信息保存到data.txt文件中。
// 学生考勤管理系统课程设计
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
class Student {
protected:
string name; //姓名
string number; //学号
int reachHour; //签到小时
int reachMinute; //签到分钟
int reachSecond; //签到秒数
int leaveHour; //签退小时
int leaveMinute; //签退分钟
int leaveSecond; //签退秒数
int time; //学习时长
int rank; //排名
public:
static int count;//总人数
/*构造函数
初始化变量*/
Student(){
//构造函数对数据成员初始化
name = " ";
number = " ";
reachHour =0;
reachMinute=0;
reachSecond=0;
leaveHour=0;
leaveMinute=0;
leaveSecond=0;
time=0;
rank=0;
}
//设置信息函数
bool set_name (string);
bool set_number (string);
bool set_reachHour (int);
bool set_reachMinute (int);
bool set_reachSecond (int);
bool set_leaveHour (int);
bool set_leaveMinute (int);
bool set_leaveSecond (int);
bool set_time (int);
bool set_rank (int);
//获取信息函数
string get_name();
string get_number();
int get_reachHour();
int get_reachMinute();
int get_reachSecond();
int get_leaveHour();
int get_leaveMinute();
int get_leaveSecond();
int get_time();
int get_rank();
};
int Student::count = 0;
//显示菜单函数
void ShowInterface() {
cout <<"显示内容"<< endl;
cout << endl;
cout << "********考勤管理系统统计********" << endl;
cout << " " << "请输入相应字母执行功能!" << endl;
cout << " " << "L:信息录入(Log in)" << endl;
cout << " " << "O:输出(Output)" << endl;
cout << " " << "E:退出(Exit)" << endl;
cout << "********************************" << endl;
cout << endl;
cout << endl;
}
//登记信息函数
Student* Registration() {
Student* pall = new Student[100]; //定义一个全局指针对象并动态分配空间
Student* temp; //定义动态空间对象
string info;
int timeIn = NULL;
temp = new Student;
cout << "输入学号" << endl;
cin >> info;//输入参数
temp->set_number(info);
cout << "输入姓名" << endl;
cin >> info;//输入参数
temp->set_name(info);
cout << "***到达时间(按提示依次录入时,分,秒)***" << endl;
cout << "到达整点" << endl;
cin >> timeIn;//输入参数
if (timeIn >= 0 && timeIn < 24) {
temp->set_reachHour(timeIn);
}
else {
cout << "输入有误,请重新输入" << endl;
cin >> timeIn;
temp->set_reachHour(timeIn);
}
cout << "到达分钟" << endl;
cin >> timeIn;
if (timeIn >= 0 && timeIn < 60) {
temp->set_reachMinute(timeIn);
}
else {
cout << "输入有误,请重新输入" << endl;
cin >> timeIn;
temp->set_reachMinute(timeIn);
}
cout << "到达秒数" << endl;
cin >> timeIn;
if (timeIn >= 0 && timeIn < 60) {
temp->set_reachSecond(timeIn);
}
else
{
cout << "输入有误,请重新输入" << endl;
cin >> timeIn;
temp->set_reachSecond(timeIn);
}
cout << "***离开时间(按提示依次录入时,分,秒)***" << endl;
cout << "离开整点" << endl;
cin >> timeIn;
if (timeIn >= 0 && timeIn < 24) {
temp->set_leaveHour(timeIn);
}
else {
cout << "输入有误,请重新输入" << endl;
cin >> timeIn;
temp->set_leaveHour(timeIn);
}
cout << "离开分钟" << endl;
cin >> timeIn;
if (timeIn >= 0 && timeIn < 60) {
temp->set_leaveMinute(timeIn);
}
else {
cout << "输入有误,请重新输入" << endl;
cin >> timeIn;
temp->set_leaveMinute(timeIn);
}
cout << "离开秒数" << endl;
cin >> timeIn;
if (timeIn >= 0 && timeIn < 60) {
temp->set_leaveSecond(timeIn);
}
else
{
cout << "输入有误,请重新输入" << endl;
cin >> timeIn;
temp->set_leaveSecond(timeIn);
}
cout << endl << "信息录入成功=^_^=" << endl;
return temp;
}
void ResearchTime(Student* temp) {
int reschT = (((temp->get_leaveHour()) * 60 + temp->get_leaveMinute()-(temp->get_reachHour()) * 60 + temp->get_reachMinute()));
temp->set_time(reschT);
int i = 0;
i += 1;
temp->set_rank(i);
}
void Sort() {
Student* pall = new Student[100]; //定义一个全局指针对象并动态分配空间
int count = Student::count;
Student temp;
//算法:选择排序法
for (int i = 0; i < count; i++) {
for (int j = 0; j < count; j++) {
if (pall[i].get_time() < pall[j].get_time()) {
temp = pall[i];
pall[i] = pall[j];
pall[j] = temp;
}
}
}
}
void save(Student* temp) {
ofstream os;//输出文件流操作对象
os.open("data.txt", ios::app); //以追加的方式建立并打开文件“data.txt”
os << endl;
Sort();
os << setw(2) << "学生学号:" << temp->get_number() << endl;
os << setw(2) << "学生姓名:" << temp->get_name() << endl;
os << setw(2) << "签到时间:" << setw(2) << setfill('0') << temp->get_reachHour();
os << ":" << setw(2) << setfill('0') << temp->get_reachMinute() << ":";
os << setw(2) << setfill('0') << temp->get_reachSecond() << endl;
os << setw(2) << "签退时间:" << setw(2) << setfill('0') << temp->get_leaveHour();
os << ":" << setw(2) << setfill('0') << temp->get_leaveMinute() << ":";
os << setw(2) << setfill('0') << temp->get_leaveSecond() << endl;
os << setw(2) << "学习时长:" << temp->get_time() << endl;
os << setw(2) << "时间排名:" << temp->get_rank() << endl;
temp++; //一个对象输出完成后,使对象指针指向下一个对象
os.close();//关闭文件
}
void show() {
filebuf* pbuf;
ifstream is;
long size;
char* buffer;
if (!is) {
cout << "读取不到" << endl;
}
//采用二进制读取整个文件
is.open("data.txt", ios::binary);
Sort();
// 获取filestr对应buffer对象的指针
pbuf = is.rdbuf();
// 调用buffer对象方法获取文件大小
size = pbuf->pubseekoff(0, ios::end, ios::in);
pbuf->pubseekpos(0, ios::in);
// 分配内存空间
buffer = new char[size];
// 获取文件内容
pbuf->sgetn(buffer, size);
is.close();
// 输出到标准输出
cout.write(buffer, size);
delete[]buffer;
/*
Student* pall = new Student[100]; //定义一个全局指针对象并动态分配空间
int count = Student::count;
Sort();
for (int i = 0; i < count; i++)
{
cout << pall[i].get_number() << " " << pall[i].get_name() << " ";
cout << pall[i].get_reachHour() << ":" << pall[i].get_reachMinute() << ":"
<< pall[i].get_reachSecond() << " ";
cout << pall[i].get_leaveHour() << ":" << pall[i].get_leaveMinute() << ":"
<< pall[i].get_leaveSecond() << " " << pall[i].get_time() << " ";
cout << i + 1 << endl; //显示对象的排名即排名多少第几次输出
}
*/
}
int main()
{
Student s;
Student* temp = NULL;
int i = 1;
char input = 'e';
input = 'i';
while (input != 'e') {
ShowInterface();
cin >> input;
input = tolower(input);//大写转义为小写
switch (input)
{
case 'l':
cout << "信息录入:" << endl;
temp = Registration();
ResearchTime(temp);
save(temp);
break;
case 'o':
show();
cout << endl << "信息读取成功^_^按提示信息进行其他操作" << endl;
break;
case'e':
cout << "谢谢使用,再见!" << endl;
system("pause");
exit(0);
break;
default:
cout << "输入有误" << endl;
break;
}
}
system("pause");
return 0;
}
bool Student::set_name(string a)
{
name = a;
count++;
return true;
}
bool Student::set_number(string b)
{
number = b;
return true;
}
bool Student::set_reachHour(int c)
{
reachHour = c;
return true;
}
bool Student::set_reachMinute(int d)
{
reachMinute = d;
return true;
}
bool Student::set_reachSecond(int e)
{
reachSecond = e;
return true;
}
bool Student::set_leaveHour(int f)
{
leaveHour = f;
return true;
}
bool Student::set_leaveMinute(int g)
{
leaveMinute = g;
return true;
}
bool Student::set_leaveSecond(int h)
{
leaveSecond = h;
return true;
}
bool Student::set_time(int i)
{
time = i;
return true;
}
bool Student::set_rank(int j)
{
rank = j;
return true;
}
string Student::get_name()
{
return name;
}
string Student::get_number()
{
return number;
}
int Student::get_reachHour()
{
return reachHour;
}
int Student::get_reachMinute()
{
return reachMinute;
}
int Student::get_reachSecond()
{
return reachSecond;
}
int Student::get_leaveHour()
{
return leaveHour;
}
int Student::get_leaveMinute()
{
return leaveMinute;
}
int Student::get_leaveSecond()
{
return leaveSecond;
}
int Student::get_time()
{
return time;
}
int Student::get_rank()
{
rank = count;
return rank;
}