只完成功能,未经调试,你可以自行调试
程序代码:
/*
1.阅读完此注释后请删除.
2.自定义path,path2,path3,不支持中文,新建一个空白文档ARecord.txt
3.把本txt重命名为xxx.c
4.在TC上运行程序
5.本程序没有实现功能5-统计信息,正因不完美才完美
6.TC不支持中文,所以本程序运行时显示的都是英文,具体含义查看英文单词对应意思或者看注释
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char path[200] = "ARecord.txt"; //均改为自己电脑TC\BIN目录下,文件名字不修改
char path2[200] = "temp.txt";
char path3[200] = "backup.txt";
struct Student { //个人信息
char name[30]; //学生姓名
char id[30]; //学号
char clazz[30]; //班级
char major[30]; //专业
char phone[11]; //电话
int age; //年龄
char birth[30]; //生日
char dormitory[30]; //宿舍
char QQ[30];
}stu[10] = { //初始化学生信息
{ "Xiao Ming","3113000000","Class 1","software","18819000000",20,"1996-01-01","712","12345678" },
{ "Na Ying","3113000001","Class 1","software","18819000001",21,"1996-10-01","512","123456781" },
{ "Rose","3113000002","Class 1","software","18819000002",20,"1996-02-01","712","123456782" },
{ "Jack","3113000003","Class 1","software","18819000003",20,"1996-12-01","712","123456783" },
{ "Hu LuWa","3113000004","Class 1","software","18819000004",20,"1996-02-01","502","123456784" },
{ "Wang Feng","3113000005","Class 1","software","18819000005",20,"1996-12-01","611","123456785" },
{ "Zhou Jielun","3113000006","Class 1","software","18819000006",20,"1996-05-01","533","123456786" },
{ "Hei Jialun","3113000007","Class 1","software","18819000007",20,"1996-03-19","713","123456787" },
{ "Na Polun","3113000008","Class 1","software","18819000008",20,"1996-03-29","712","123456788" },
{ "MO Tianlun","3113000009","Class 1","software","18819000009",20,"1996-04-01","712","123456789" }
};
struct lesson { //课程信息
char id[30]; //课程编号
char name[30]; //课程名称
float score; //学分
char teacher[30]; //任课老师
char time[30]; //上课时间
int num; //上课次数
char address[30]; //上课地点
}les[3] = { //初始花课程信息
{ "1000","Higher Mathematics",4.0,"Teacher 1","14:40-16:15",6,"teaching building 1-201" },
{ "1001","English",3.0,"Teacher 2","10:25-12:00",7,"teaching building 4-202" },
{ "1002","Application Design",4.0,"Teacher 3","16:15-18:00",8,"teaching building 5-501" }
};
struct KaoQin { //考勤登记信息
struct Student st; //学生信息
struct lesson ln; //课程信息
int classnum; //第几节课
enum Statuss { late, normal, absent, early }status; //违纪状态
}KQ;
void showStudent(struct Student stu[], int length) //查看学生表的信息
{
char name_id[30];
int i = 0, isFlag = 0;
printf("Please enter Student Id or Student Name(enter 'all' to show all):");
fflush(stdin);
gets(name_id);
for (i = 0; i<length; i++)
{
if (strcmp(name_id, "all") == 0)
{
if (!isFlag)
{
printf("\nSTUDENT NAME || ID || CLASS || MAJOR || PHONE || AGE || BIRTH || DORMITORY || QQ\n");
isFlag = 1;
}
printf("%s || %s || %s || %s || %s || %d || %s || %s || %s\n", stu[i].name, stu[i].id, stu[i].clazz, stu[i].major, stu[i].phone, stu[i].age, stu[i].birth, stu[i].dormitory, stu[i].QQ);
}
else if (strcmp(stu[i].name, name_id) == 0 || strcmp(stu[i].id, name_id) == 0)
{
if (!isFlag)
{
printf("\nSTUDENT NAME || ID || CLASS || MAJOR || PHONE || AGE || BIRTH || DORMITORY || QQ\n");
}
printf("%s || %s || %s || %s || %s || %d || %s || %s || %s\n", stu[i].name, stu[i].id, stu[i].clazz, stu[i].major, stu[i].phone, stu[i].age, stu[i].birth, stu[i].dormitory, stu[i].QQ);
isFlag = 1;
}
}
if (!isFlag)
{
printf("Message no found.\n");
}
}
int deleteMessage() //根据学生姓名或学号删除考勤信息
{
char ack2, name_id[30];
int i = 0, isFlag = 0;
struct KaoQin KQ3, KQ4;
FILE *fp2, *fp3;
fp2 = fopen(path, "r");
fp3 = fopen(path2, "w");
fclose(fp3);
fp3 = fopen(path2, "a");
printf("Please enter Student Id or Student Name to delete:");
fflush(stdin);
gets(name_id);
while (1)
{
//fseek(fp2,i*sizeof(struct KaoQin),0);
fread(&KQ3, sizeof(struct KaoQin), 1, fp2);
if (feof(fp2)) break;
if (strcmp(KQ3.st.name, name_id) == 0 || strcmp(KQ3.st.id, name_id) == 0)
{
printf("StudentName:%s StudentId:%s Class:%s lessonId:%s lessonName:%s Breach:%d BreachTime:%d Time:%s Address:%s\n", KQ3.st.name, KQ3.st.id, KQ3.st.clazz, KQ3.ln.id, KQ3.ln.name, KQ3.status, KQ3.classnum, KQ3.ln.time, KQ3.ln.address);
printf("Are u sure delete this one?(Y/N) ");
fflush(stdin);
ack2 = getchar();
if (ack2 == 'Y' || ack2 == 'y')
{
}
else
{
fwrite(&KQ3, sizeof(struct KaoQin), 1, fp3);
printf("Cancel Delete.\n");
}
isFlag = 1;
}
else
{
fwrite(&KQ3, sizeof(struct KaoQin), 1, fp3);
}
}
if (!isFlag)
{
printf("Message no found.\n");
}
fclose(fp3);
fclose(fp2);
remove(path);
rename(path2, path);
printf("Delete success.\n");
printf("Enter number 0 to go back:");
scanf("%d", &i);
if (i == 0) return 0;
else return 1;
}
void showKaoQin() //查看考勤表的信息
{
char name_id[30];
int i, isFlag = 0;
FILE *fp;
struct KaoQin KQ2;
fp = fopen(path, "r");
printf("Please enter Student Id or Student Name(enter 'all' to show all):");
fflush(stdin);
gets(name_id);
while (1)
{
//fseek(fp,i*sizeof(struct KaoQin),0);
fread(&KQ2, sizeof(struct KaoQin), 1, fp);
if (feof(fp)) break;
if (strcmp(name_id, "all") == 0)
{
printf("StudentName:%s StudentId:%s Class:%s lessonId:%s lessonName:%s Breach:%d BreachTime:%d Time:%s Address:%s\n", KQ2.st.name, KQ2.st.id, KQ2.st.clazz, KQ2.ln.id, KQ2.ln.name, KQ2.status, KQ2.classnum, KQ2.ln.time, KQ2.ln.address);
isFlag = 1;
}
else if (strcmp(KQ2.st.name, name_id) == 0 || strcmp(KQ2.st.id, name_id) == 0)
{
printf("StudentName:%s StudentId:%s Class:%s lessonId:%s lessonName:%s Breach:%d BreachTime:%d Time:%s Address:%s\n", KQ2.st.name, KQ2.st.id, KQ2.st.clazz, KQ2.ln.id, KQ2.ln.name, KQ2.status, KQ2.classnum, KQ2.ln.time, KQ2.ln.address);
isFlag = 1;
}
//i++;
}
fclose(fp);
if (!isFlag)
{
printf("Message no found.\n");
}
}
void showLesson(struct lesson les[], int len) //查看lesson的信息
{
char name_id[30];
int i, isFlag = 0;
printf("Please enter Lesson Name or Teacher Name(enter 'all' to show all):");
fflush(stdin);
gets(name_id);
for (i = 0; i<len; i++)
{
if (strcmp(name_id, "all") == 0)
{
if (!isFlag)
{
printf("\nLESSON ID || NAME || SCORE || TEACHER || TIME || NUM || ADDRESS\n");
isFlag = 1;
}
printf("%s || %s || %2.1f || %s || %s || %d || %s\n", les[i].id, les[i].name, les[i].score, les[i].teacher, les[i].time, les[i].num, les[i].address);
}
else if (strcmp(les[i].name, name_id) == 0 || strcmp(les[i].teacher, name_id) == 0)
{
if (!isFlag)
{
printf("\nLESSON ID || NAME || SCORE || TEACHER || TIME || NUM || ADDRESS\n");
}
printf("%s || %s || %2.1f || %s || %s || %d || %s\n", les[i].id, les[i].name, les[i].score, les[i].teacher, les[i].time, les[i].num, les[i].address);
isFlag = 1;
}
}
if (!isFlag)
{
printf("Message no found.\n");
}
}
int show(struct Student stu[], int length, struct lesson les[], int len) //实现查看的功能
{
int a, i = 0;
printf("Please choose:\n1.Query Student Message 2.Query KaoQin 3.Query Lesson 0.Go Back\n");
scanf("%d", &a);
switch (a)
{
case 1:
showStudent(stu, length);
break;
case 2:
showKaoQin();
break;
case 3:
showLesson(les, len);
break;
case 0:
default:;
}
printf("Enter number 0 to go back:");
scanf("%d", &i);
if (i == 0) return 0;
else return 1;
}
void save(struct KaoQin KQ) //保存一条考勤记录信息到ARecord.txt
{
FILE *fp;
if ((fp = fopen(path, "a")) == NULL)
{
printf("Can not open file.\n");
return;
}
if (fwrite(&KQ, sizeof(struct KaoQin), 1, fp) != 1)
{
printf("File write error.\n");
}
printf("File write ok.\n");
fclose(fp);
}
int add(struct KaoQin KQ, struct Student stu[], int length, struct lesson les[], int len) //输入考勤记录信息
{
char student_name_id[30], lesson_name_id[30], ack;
int classnum, i, isFlag1 = 0, isFlag2 = 0, isFlag3 = 0, status_num;
do {
printf("Please enter Student Name or Student Num:");
fflush(stdin);
gets(student_name_id);
for (i = 0; i<length; i++)
{
if (strcmp(stu[i].name, student_name_id) == 0 || strcmp(stu[i].id, student_name_id) == 0)
{
KQ.st = stu[i];
isFlag1 = 1;
}
}
if (!isFlag1)
{
printf("Message no found.\n");
}
} while (!isFlag1);
do {
printf("Please enter lesson Name or lesson Id:");
fflush(stdin);
gets(lesson_name_id);
for (i = 0; i<len; i++)
{
if (strcmp(les[i].name, lesson_name_id) == 0 || strcmp(les[i].id, lesson_name_id) == 0)
{
KQ.ln = les[i];
isFlag2 = 1;
}
}
if (!isFlag2)
{
printf("Message no found.\n");
}
} while (!isFlag2);
do {
printf("Please enter a num as Student Status(1.late,2.normal,3.absent,4.early):");
scanf("%d", &status_num);
if (status_num == 1 || status_num == 2 || status_num == 3 || status_num == 4)
{
KQ.status = (enum Statuss)status_num;
isFlag3 = 1;
}
if (!isFlag3) {
printf("Error Enter.\n");
}
} while (!isFlag3);
printf("Please enter classnum:");
scanf("%d", &classnum);
KQ.classnum = classnum;
printf("save the record?(Y/N) ");
fflush(stdin);
ack = getchar();
if (ack == 'Y' || ack == 'y') {
save(KQ);
}
printf("Enter number 0 to go back:");
scanf("%d", &i);
if (i == 0) return 0;
else return 1;
}
int count() //显示某生考勤信息
{
char name_id[30];
int i, isFlag = 0;
int latecount = 0, normalcount = 0, absentcount = 0, earlycount = 0;
FILE *fp;
struct KaoQin KQ2;
fp = fopen(path, "r");
printf("Please enter Student Id or Student Name:or class Name:");
fflush(stdin);
gets(name_id);
while (1)
{
//fseek(fp,i*sizeof(struct KaoQin),0);
fread(&KQ2, sizeof(struct KaoQin), 1, fp);
if (feof(fp)) break;
if (strcmp(KQ2.st.name, name_id) == 0 || strcmp(KQ2.st.id, name_id) == 0)
{
printf("Please enter Lesson Name(enter 'all' to show all):");
fflush(stdin);
gets(name_id);
//for(i=0;i<len;i++)
{
if (strcmp(name_id, "all") == 0)
{
if (!isFlag)
{
printf("\nLESSON ID || NAME || SCORE || TEACHER || TIME || NUM || ADDRESS\n");
isFlag = 1;
}
printf("%s || %s || %2.1f || %s || %s || %d || %s\n", KQ2.ln.id, KQ2.ln.name, KQ2.ln.score, KQ2.ln.teacher, KQ2.ln.time, KQ2.ln.num, KQ2.ln.address);
switch (KQ2.classnum)
{
case 0:
latecount++;
break;
case 1:
normalcount++;
break;
case 2:
absentcount++;
break;
case 3:
earlycount++;
break;
}
}
else if (strcmp(KQ2.ln.name, name_id) == 0)
{
if (!isFlag)
{
printf("\nLESSON ID || NAME || SCORE || TEACHER || TIME || NUM || ADDRESS\n");
}
printf("%s || %s || %2.1f || %s || %s || %d || %s\n", KQ2.ln.id, KQ2.ln.name, KQ2.ln.score, KQ2.ln.teacher, KQ2.ln.time, KQ2.ln.num, KQ2.ln.address);
switch (KQ2.classnum)
{
case 0:
latecount++;
break;
case 1:
normalcount++;
break;
case 2:
absentcount++;
break;
case 3:
earlycount++;
break;
}
isFlag = 1;
}
}
}
else if (strcmp(KQ2.st.clazz, name_id) == 0)
{
if (!isFlag)
{
printf("\nLESSON ID || NAME || SCORE || TEACHER || TIME || NUM || ADDRESS\n");
isFlag = 1;
}
printf("%s || %s || %2.1f || %s || %s || %d || %s\n", KQ2.ln.id, KQ2.ln.name, KQ2.ln.score, KQ2.ln.teacher, KQ2.ln.time, KQ2.ln.num, KQ2.ln.address);
switch (KQ2.classnum)
{
case 0:
latecount++;
break;
case 1:
normalcount++;
break;
case 2:
absentcount++;
break;
case 3:
earlycount++;
break;
}
}
//i++;
}
fclose(fp);
if (!isFlag)
{
printf("Message no found.\n");
}
else
{
printf("\nlate || normal || absent || early\n");
printf("\n%d || %d || %d || %d \n", latecount, normalcount, absentcount, earlycount);
}
}
int copy() //备份功能
{
char ack2, name_id[30];
int i = 0;
struct KaoQin KQ3;
FILE *fp2, *fp3;
fp2 = fopen(path, "r");
fp3 = fopen(path3, "w");
fclose(fp3);
fp3 = fopen(path3, "a");
while (1)
{
fseek(fp2, i*sizeof(struct KaoQin), 0);
fread(&KQ3, sizeof(struct KaoQin), 1, fp2);
fwrite(&KQ3, sizeof(struct KaoQin), 1, fp3);
i++;
if (!feof(fp2)) break;
}
printf("Back up success.\n");
fclose(fp3);
fclose(fp2);
printf("Enter number 0 to go back:");
scanf("%d", &i);
if (i == 0) return 0;
else return 1;
}
int main() //主函数
{
int a, length = sizeof(stu) / sizeof(stu[0]), i = 0;
int len = sizeof(les) / sizeof(les[0]);
printf("--------------------------------------------------------------------------------\n");
printf("-------------------------Welcome to StudentManager System-----------------------\n");
printf("--------------------------------------------------------------------------------\n");
while (!i)
{
printf("Please choose:\n");
printf("1.add 2.show 3.delete 4.count 5.copy\n");
scanf("%d", &a);
switch (a)
{
case 1: //添加一条考勤记录
i = add(KQ, stu, length, les, len);
break;
case 2: //查看信息(学生信息/课程信息/考勤信息)
i = show(stu, length, les, len);
break;
case 3: //删除一条考勤记录
i = deleteMessage();
break;
case 4: //未实现的功能5
i = count();
break;
case 5: //备份
i = copy();
break;
default:
i = 1;
printf("select error");
}
}
return 0;
}