#include <stdio.h>
#include <string.h>
#include <stdlib.h>
const char STU_MESSAGE[15] = "data1.txt";
const char STU_SEARCH[15] = "data2.txt";
const char STU_EXCEPTION[15] = "data3.txt";
const char STU_THREEDAY[15] = "data4.txt";
const char INSTI[13][30] = {{""}, {"通信学院"}, {"计算机学院"}, {"光电学院"}, {"通信学院"}, {"计算机学院"}, {"光电学院"}
, {"通信学院"}, {"计算机学院"}, {"光电学院"}, {"通信学院"}, {"计算机学院"}, {"光电学院"}};
typedef struct student
{
char id[20];
char name[20];
char institute[30];
char temperature[10];
int isCough;
int health;
char time[20];
char date[20];
}Stu;
int idIsTrue(char id[]);
int getInsti(char a, char b);
void inputStuMessage();
void getAll(const char *filename);
void print(Stu stu);
void alterStu();
void searchStu();
void countExceptionStu();
void menu();
int main()
{
int num;
menu();
printf("请输入操作: ");
while ( 1 )
{
scanf("%d", &num);
switch(num)
{
case 1:
inputStuMessage();
break;
case 2:
getAll(STU_MESSAGE);
break;
case 3:
alterStu();
break;
case 4:
searchStu();
break;
case 5:
countExceptionStu();
break;
case 6:
getAll(STU_EXCEPTION);
break;
case 7:
menu();
break;
case 8:
system("cls");
menu();
break;
case 9:
return 0;
break;
default:
printf("请正确输入!!!\n");
break;
}
printf("请输入操作: ");
}
}
void menu()
{
printf("\n***************************2020学生返校健康管理系统***************************\n\n");
printf("*******************************1.录入学生信息*******************************\n\n");
printf("*******************************2.查看所有信息*******************************\n\n");
printf("*******************************3.修改学生信息*******************************\n\n");
printf("*******************************4.查询学生信息*******************************\n\n");
printf("*******************************5.统计异常信息*******************************\n\n");
printf("*******************************6.查看异常信息*******************************\n\n");
printf("*******************************7.查看操作菜单*******************************\n\n");
printf("*******************************8.清除屏幕操作*******************************\n\n");
printf("*******************************9.退出管理系统*******************************\n\n");
}
int getInsti(char a, char b)
{
return (a - '0') + (b - '0');
}
int idIsTrue(char id[])
{
if ( strlen(id) != 9 ) return 0;
int year = 0, num = 0, number = 0;
int n = 4, i = 0;
while ( n -- )
{
year = year * 10 + id[i ++] - '0';
}
if ( year < 2016 || year > 2019 ) return 0;
n = 2;
while ( n -- )
{
num = num * 10 + id[i ++] - '0';
}
if ( num < 1 || num > 12 ) return 0;
n = 3;
while ( n -- )
{
number = number * 10 + id[i ++] - '0';
}
if ( number < 1 || num > 999 ) return 0;
return 1;
}
void inputStuMessage()
{
Stu stu = {0};
FILE *fp;
char c;
int loop = 1;
while ( 1 )
{
printf("请输入学生的各项信息: \n");
while ( 1 )
{
printf("学生学号: ");
scanf("%s", stu.id);
if ( idIsTrue(stu.id) ) break;
else printf("该学号信息无效, 请重新输入! \n");
}
printf("学生姓名: ");
scanf("%s", stu.name);
printf("学生体温: ");
scanf("%s", stu.temperature);
printf("是否咳嗽: ");
scanf("%d", &stu.isCough);
printf("时间: ");
scanf("%s", stu.time);
printf("日期: ");
scanf("%s", stu.date);
if ( stu.isCough || strcmp(stu.temperature, "37.3") >= 0 )
stu.health = 0;
else stu.health = 1;
strcpy(stu.institute, INSTI[getInsti(stu.id[4], stu.id[5])]);
fp = fopen("data1.txt", "a");
if ( fp == NULL )
{
printf("文件无法打开! \n");
exit(0);
}
if ( fwrite(&stu, sizeof(Stu), 1, fp) != 1 )
{
printf("无法录入! \n");
}
fclose(fp);
getchar();
printf("是否继续录入( y / n ): ");
scanf("%c", &c);
if ( c != 'y' ) break;
}
}
void getAll(const char *filename)
{
Stu stu = {0};
int pos;
FILE *fp = fopen(filename, "r");
for ( pos = 0; fread(&stu, sizeof(Stu), 1, fp) == 1; pos ++ )
print(stu);
fclose(fp);
}
void print(Stu stu)
{
printf("\n******************学生信息*******************\n");
printf("学号\t\t姓名\t学院\t\t体温\t是否咳嗽\t健康状态\t时间\t日期\n");
printf("%s\t%s\t%s\t%s\t%d\t\t%d\t\t%s\t%s\t\n",stu.id, stu.name, stu.institute, stu.temperature, stu.isCough, stu.health, stu.time, stu.date);
}
void alterStu()
{
char id[20];
FILE *fp = fopen("data1.txt", "r");
Stu stu = {0}, goalStu = {0}, stuList[50];
int isSearch = 0, num = 0, index, loop = 1;
while ( 1 )
{
printf("请输入要修改的学生的学号: ");
scanf("%s", id);
if ( idIsTrue(id) ) break;
else printf("该学号信息无效, 请重新输入! \n");
}
for (int pos = 0; fread(&stu, sizeof(Stu), 1, fp); pos ++)
{
if ( !strcmp(id, stu.id) )
{
isSearch = 1;
goalStu = stu;
num ++;
index = pos;
}
else
{
stuList[num ++] = stu;
}
}
fclose(fp);
if ( isSearch )
{
while ( loop )
{
int select;
printf("下列是可以选择的选项: \n");
printf("1. 姓名\n");
printf("2. 体温\n");
printf("3. 是否咳嗽\n");
printf("4. 时间\n");
printf("5. 日期\n");
printf("6. 退出\n");
printf("请输入要修改的选项: ");
scanf("%d", &select);
while ( select < 1 || select > 6 )
{
printf("没有此选项,请重新输入: ");
scanf("%d", &select);
}
switch (select)
{
case 1:
printf("学生姓名: ");
scanf("%s", goalStu.name);
break;
case 2:
printf("学生体温: ");
scanf("%s", goalStu.temperature);
break;
case 3:
printf("是否咳嗽: ");
scanf("%d", &goalStu.isCough);
break;
case 4:
printf("时间: ");
scanf("%s", goalStu.time);
break;
case 5:
printf("日期: ");
scanf("%s", goalStu.date);
break;
default:
loop = 0;
break;
}
}
if ( goalStu.isCough || strcmp(goalStu.temperature, "37.3") >= 0 )
goalStu.health = 0;
else goalStu.health = 1;
stuList[index] = goalStu;
fp = fopen("data1.txt", "w");
for ( int i = 0; i < num; i ++ )
fwrite(&stuList[i], sizeof(Stu), 1, fp);
fclose(fp);
}
else
{
printf("查无此人\n");
}
}
void searchStu()
{
Stu stu = {0};
FILE *fp, *fp2;
int searchType, isTrue = 1;
char searchId[20], searchName[20], searchDate[20];
while ( 1 )
{
printf("请输入查询方式, 1.按学号查询, 2.按姓名查询, 3.退出查询: ");
scanf("%d", &searchType);
switch(searchType)
{
case 1:
while ( 1 )
{
printf("请输入学生学号: ");
scanf("%s", searchId);
if ( idIsTrue(searchId) ) break;
else printf("该学号信息无效, 请重新输入! \n");
}
break;
case 2:
printf("请输入学生姓名: ");
scanf("%s", searchName);
break;
case 3:
return;
break;
default:
printf("输入错误内容!\n");
isTrue = 0;
break;
}
if ( !isTrue ) continue;
printf("请输入当前日期: ");
scanf("%s", searchDate);
fp = fopen("data1.txt", "r");
for (int pos = 0; fread(&stu, sizeof(Stu), 1, fp); pos ++)
{
if ( !strcmp(searchId, stu.id) || !strcmp(searchName, stu.name) )
{
break;
}
}
fclose(fp);
int year = 0, month = 0, day = 0;
for ( int i = 0; i < 4; i ++ ) year = year * 10 + searchDate[i] - '0';
for ( int i = 4; i < 6; i ++ ) month = month * 10 + searchDate[i] - '0';
for ( int i = 6; i < 8; i ++ ) day = day * 10 + searchDate[i] - '0';
fp2 = fopen("data2.txt", "a");
fp = fopen("data1.txt", "r");
printf("截止%d年%d月%d日该生的健康管理信息为: \n", year, month, day);
printf("学号: %s\n", stu.id);
printf("姓名: %s\n", stu.name);
printf("学院: %s\n", stu.institute);
for (int pos = 0; fread(&stu, sizeof(Stu), 1, fp); pos ++)
{
if ( (!strcmp(searchId, stu.id) || !strcmp(searchName, stu.name)) && strcmp(searchDate, stu.date) >= 0 )
{
fwrite(&stu, sizeof(Stu), 1, fp2);
printf("体温\t是否咳嗽\t健康状态\t时间\t日期\n");
printf("%s\t%d\t\t%d\t\t%s\t%s\t\n",stu.temperature, stu.isCough, stu.health, stu.time, stu.date);
}
}
fclose(fp);
fclose(fp2);
}
}
void countExceptionStu()
{
FILE *fp, *fp3;
char currentDate[20];
Stu stu = {0};
printf("请输入日期: ");
scanf("%s", currentDate);
fp = fopen("data1.txt", "r");
fp3 = fopen("data3.txt", "a");
for (int pos = 0; fread(&stu, sizeof(Stu), 1, fp); pos ++)
{
if ( !strcmp(stu.date, currentDate) && !stu.health )
{
fwrite(&stu, sizeof(Stu), 1, fp3);
}
}
printf("统计完成!\n");
fclose(fp);
fclose(fp3);
}
tql
6
修改为啥只需要学号,只需要学号就可以确定要修改是否咳嗽,不需要日期吗
为啥没有写data4的
牛啊!