考勤系统统计程序设计
毕业设计
1
C++中怎样使用文件保存全班每个学生的考勤信息,怎样可以实现,求解?
-
学生管理系统参考一下:
#include<stdio.h> #include<string.h> #include<stdlib.h> #define N 100 //最大输入 int n = 0; //总数 struct students { int num; //学号 char name[8]; //姓名 int chinese; //语文 int math; //数学 int english; //英语 int sum; //总成绩 int ave; //平均分 } student[N],student1; void count() { struct students stu1; FILE * fp; //定义一个文件指针fp fp = fopen("students.txt", "at+"); //以"追加"方式打开students文本文件 if (fp == NULL) //判断文件是否为空 printf("Cannot open file!"); n = 0; //初始化 while (1) { if (feof(fp)) //检测文件是否结束 break; fread( & stu1, sizeof(struct students), 1, fp); //读取文件 n++; //读取文件数 } n--; //关闭文件数 fclose(fp); //关闭文件 } /*成绩输入*/ void input() { int i = 0; //学生人数初始化// char c; FILE * fp; //定义一个文件指针fp// count(); //实时检测总数n// fp = fopen("students.txt", "at+"); //以"追加"方式打开students文本文件 do { printf("请输入学生的个人信息:\n"); printf("\n请输入第%d学生的信息:\n", n + 1); printf("请输入学生的学号:"); scanf("%d", &student[i].num); printf("请输入学生的姓名:"); scanf("%s", student[i].name); printf("请输入学生的语文成绩:"); scanf("%d", &student[i].chinese); printf("请输入学生的数学成绩:"); scanf("%d", &student[i].math); printf("请输入学生的英语成绩:"); scanf("%d", &student[i].english); student[i].sum = student[i].chinese + student[i].math + student[i].english; student[i].ave = student[i].sum / 3; fwrite( & student[i], sizeof(struct students), 1, fp); //以方式将其写入students文本文件 i++; n++; printf("\n\n\t\t\t\t是否继续输入(Y/N):"); getchar(); //提示是否继续,从键盘获得一个字符 scanf("%c", &c); } while ( c == 'y' || c == 'Y'); fclose(fp); } /*成绩输出*/ void output() { int i; char c; FILE * fp; //定义一个文件指针fp fp = fopen("students.txt", "rt"); //以"读取的" 方式打开students文本文件 system("cls"); //清屏// getchar(); printf("|#学号\t姓名\t语文\t数学\t英语\t总成绩\t平均分\t#|\n"); do { for (i = 0; i < n; i++) { fread(&student[i], sizeof(struct students), 1, fp); printf(" %d ", student[i].num); printf(" %s ", student[i].name); printf(" %d ", student[i].chinese); printf(" %d ", student[i].math); printf(" %d ", student[i].english); printf(" %d ", student[i].sum); printf(" %d \n", student[i].ave); } printf("\n\n\t\t\t\t是否继续其它操作(Y/N):"); //提示是否继续 scanf("%c", &c); c = getchar(); } while ( c == 'y' || c == 'Y'); fclose(fp); //关闭文件// } /*成绩修改*/ void xiugai() { int i, tp, num1; char c; FILE * fp; //定义一个文件指针fp// lb: printf("\n按学号修改\n\n"); printf("请输入要修改的学生学号\n"); scanf("%d", &num1); for (i = 0; i < n; i++) { if (num1 == student[i].num) { printf("要修改的学生信息为:\n"); printf("学号\t姓名\t语文成绩\t数学成绩\t英语成绩\t总成绩\t平均分\n"); printf(" %d ", student[i].num); printf(" %s ", student[i].name); printf(" %d ", student[i].chinese); printf(" %d ", student[i].math); printf(" %d ", student[i].english); printf(" %d ", student[i].sum); printf(" %d ", student[i].ave); do { printf("请选择要修改项:\n"); printf("1学号\n2姓名\n3语文成绩\n4数学成绩\n5总成绩\n6平均分\n"); scanf("%d", &tp); switch (tp) { case 1: { printf("请输入学号:\n"); scanf("%d", &student[i].num); goto la; } case 2: { printf("请输入姓名:\n"); scanf("%s", student[i].name); goto la; } case 3: { printf("请输入语文成绩:\n"); scanf("%d", &student[i].chinese); goto la; } case 4: { printf("请输入数学成绩:\n"); scanf("%d", &student[i].math); goto la; } case 5: { printf("请输入英语成绩:\n"); scanf("%d", &student[i].english); goto la; } case 6: { printf("请输入总成绩:\n"); scanf("%d", &student[i].sum); goto la; } case 7: { printf("请输入平均分:\n"); scanf("%d", &student[i].ave); goto la; } } } while ( tp < 8 ); la: printf("是否继续修改(y/n)\n"); //提示是否继续 getchar(); scanf("%c", &c); if (c == 'y' || c == 'Y') goto lb; } } fp = fopen("students.txt", "w+"); //以"读写" 方式打开students文本文件 for (i = 0; i < n; i++) fwrite( & student[i], sizeof(struct students), 1, fp); //以 方式将其写入student文本文件 fclose(fp); //关闭文件 } /*成绩查询*/ void query() { int querymode; //子菜单选择 int i, j = 0, findnum; char findname[20]; int findok, t = 0; //定义一个判断标志的整型变量findok char findct; //定义一个用于判断是否继续查找的字符变量findct FILE * fp; //定义一个文件指针fp count(); //实时检测总数 fp = fopen("students.txt", "r"); //以"只读" 方式打开students文本文件 system("cls"); //清屏 /*查询菜单*/ printf("\t\t成绩查询"); printf("\n\t1按姓名查询"); printf("\n\t2按学号查询"); printf("\n\t0返回上级菜单"); printf("\n请输入您的操作:"); scanf("%d", &querymode); /*查询操作*/ switch (querymode) { case 1: { printf("请输入要查询学生的姓名:\n"); getchar(); scanf("%s", findname); for (i = 0; i < N; i++) { fread( & student[i], sizeof(struct students), 1, fp); //以方式将其读入students 文本文件 if (strcmp(student[i].name, findname) == 0) //比较字符串 { j++; printf("\n查寻到的学生为:\n"); if (j == 1) printf("|#学号\t姓名\t语文\t数学\t英语\t总成绩\t平均分\t#|\n"); printf("%4d%s%d%d%d%d%d", student[i].num, student[i].name, student[i].chinese, student[i].math, student[i].english,student[i].sum, student[i].ave); } } if (j == 0) printf("\n>>>查无此人!"); //提示是否继续// printf("\n是否继续查找(Y/N)?"); getchar(); getchar(); } break; case 2: { printf("请输入要查询学生的学号:"); scanf("%d", &findnum); for (i = 0; i < n; i++) { fread( & student[i], sizeof(struct students), 1, fp); //以结构体students的方式从文件fp中读取,将其读入students文本文件,每次读入一个 学生的信息 if (student[i].num == findnum) //整型数字的比较/ { findok = 1; break; } else findok = 0; } if (!findok) printf("\n>>>查无此人!"); else { printf("\n查寻到的学生为:\n"); if (j == 1) printf("|学号\t姓名\t语文\t数学\t英语\t总成绩\t平均分\t|\n"); printf("%4d%s%d%d%d%d%d", student[i].num, student[i].name,student[i].chinese, student[i].math,student[i].english, student[i].sum, student[i].ave); } //提示是否继续// printf("\n是否继续查找(Y/N)?"); getchar(); findct = getchar(); //从键盘输入一个判断字符,并将它赋值给字符变量 findct,用于以下的if语句的判断 if (findct == 'Y' || findct == 'y') query(); } } fclose(fp); //关闭文件// } /*成绩排序*/ void paixu() { int i, j; FILE * fp; //定义一个文件指针fp// system("cls"); //清屏// count(); //实时检测总数n// for (i = 0; i < n - 1; i++) for (j = 0; j < n - 1 - i; j++) //按学号对学生信息进行排序 { if (student[j].num > student[j + 1].num) //整型数字的比较 { student1 = student[j]; student[j] = student[j + 1]; student[j + 1] = student1; } } fp = fopen("students.txt", "w+"); //以 "读写"方式打开students文本文件 for (i = 0; i < n; i++) fwrite( & student[i], sizeof(struct students), 1, fp); fclose(fp); //关闭文件// } /*退出系统*/ void tuichu() { char c; system("cls"); //清屏 printf("\n\n\t\t谢谢使用\n\n\n"); printf("\n\n\t\t制作人:唐璐 李泓娴\n"); getchar(); c = getchar(); //取用户输入,回显 } /*菜单*/ void main() { int i=1; //获取用户在菜单中的选择 while (i != 0) { printf("\t\t欢迎进入学生成绩管理系统\n"); printf("\t\t==============================\n"); printf("\t\t*选择操作 *\n"); printf("\t\t*1输入学生信息 *\n"); printf("\t\t*2输出学生信息 *\n"); printf("\t\t*3修改学生信息 *\n"); printf("\t\t*4查询学生信息 *\n"); printf("\t\t*5排序学生信息 *\n"); printf("\t\t*0退出系统 *\n"); printf("\t\t==============================\n"); scanf("%d", &i); system("cls"); //清屏 count(); //实时检测总数 switch (i) { case 1: input(); break; //输入学生信息 case 2: output(); break; //输出学生信息 case 3: xiugai(); output(); break; //修改学生信息 case 4: query(); break; //查询学生信息 case 5: paixu(); output(); break; //成绩排序 default: printf("输入有错\n"); //错误纠正 } } if (i == 0) tuichu(); //退出系统 }
-
把全班每个学生的考勤信息写入文件中就好了。参考一下。
ofstream os;//输出文件流操作对象
os.open("data.txt", ios::app); //以追加的方式建立并打开文件“data.txt”
// 学生考勤管理系统课程设计 #include<iostream> #include <iostream> #include <string> #include <iomanip> #include <vector> #include<fstream> #include<algorithm> #include<iomanip> 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; }
发表回复