#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Node *list=NULL;
struct Student
{
char num[10];
char name[20];
char academy[30];
float temperature;
int cough;
int health;
char time[10];
char date[10];
};
//链表节点
struct Node
{
struct Student data;
struct Node *next;
};
//创建头节点
struct Node *createList()
{
struct Node *head=(struct Node *)malloc(sizeof(struct Node));
head->next=NULL;
return head;
}
//创建节点
struct Node *createNode(struct Student data)
{
struct Node *newNode=(struct Node *)malloc(sizeof(struct Node));
newNode->data=data;
newNode->next=NULL;
return newNode;
}
//插入链表 尾插法
void InsertNode(struct Node *head,struct Student data)
{
struct Node *newNode=createNode(data);
static struct Node *tail=NULL;
if(head->next==NULL)
{
head->next=newNode;
}
else
{
tail->next=newNode;
}
tail=newNode;
}
//读取文件
void readFile(char *fileName,struct Node *list)
{
FILE *fp=fopen(fileName,"r");
if(fp==NULL)
{
//文件为空
fp=fopen(fileName,"w");
}
struct Student student;
while(fscanf(fp,"%s\t%s\t%s\t%f\t\t%d\t%d\t%s\t%s",
student.num,student.name,student.academy,&student.temperature,
&student.cough,&student.health,student.time,student.date)!=EOF)
{
InsertNode(list,student);
memset(&student,0,sizeof(student));
}
fclose(fp);
}
//写文件
void saveFile(char *fileName,struct Node *list)
{
FILE *fp=fopen(fileName,"w");
struct Node *start=list->next;
while(start!=NULL)
{
fprintf(fp,"%s\t%s\t%s\t%.2f\t\t%d\t%d\t%s\t%s\n",
start->data.num,start->data.name,start->data.academy,
start->data.temperature,start->data.cough,start->data.health,
start->data.time,start->data.date);
start=start->next;
}
fclose(fp);
}
//打印
void Print(struct Node *head)
{
struct Node *start=head->next;
printf("学号\t\t姓名\t学院\t\t温度\t是否咳嗽\t是否健康\t时间\t日期\n");
while(start!=NULL)
{
printf("%s\t%s\t%s\t%.2f\t\t%d\t%d\t\t%s\t%s\n",
start->data.num,start->data.name,start->data.academy,start->data.temperature,start->data.cough,start->data.health,
start->data.time,start->data.date);
start=start->next;
}
}
//增加信息
struct Student Input()
{
struct Student student;
char AcadamyNum[5];
printf("请录入信息:\n");
do
{
printf("学生学号(9位):");
scanf("%s",student.num);
}
while(strlen(student.num)!=9); //学号有九位
printf("学生姓名:");
scanf("%s",student.name);
printf("温度:");
scanf("%f",&student.temperature);
printf("是否咳嗽(0:不咳嗽,1:咳嗽):");
scanf("%d",&student.cough);
printf("时间(xx:xx):");
scanf("%s",student.time);
do
{
printf("日期(8位):");
scanf("%s",student.date);
}
while(strlen(student.date)!=8);
//根据温度和咳嗽判断是否健康
if(student.temperature<37.3&&student.cough==0)
{
//温度低于37.3 且不咳嗽
student.health=1;
}
else
{
student.health=0;
}
//根据学院编号查找学院
//裁剪出学院编号
AcadamyNum[0]=student.num[4];
AcadamyNum[1]=student.num[5];
AcadamyNum[2]='\0';
if(!strcmp(AcadamyNum,"01"))
{
strcat(student.academy,"通信学院");
}
else if(!strcmp(AcadamyNum,"02"))
{
strcat(student.academy,"计算机学院");
}
else if(!strcmp(AcadamyNum,"03"))
{
strcat(student.academy,"光电学院");
}
else if(!strcmp(AcadamyNum,"04"))
{
strcat(student.academy,"自动化学院");
}
else if(!strcmp(AcadamyNum,"05"))
{
strcat(student.academy,"经管学院");
}
else if(!strcmp(AcadamyNum,"06"))
{
strcat(student.academy,"研究生学院");
}
else if(!strcmp(AcadamyNum,"07"))
{
strcat(student.academy,"外语学院");
}
else if(!strcmp(AcadamyNum,"08"))
{
strcat(student.academy,"安法学院");
}
else if(!strcmp(AcadamyNum,"09"))
{
strcat(student.academy,"体育学院");
}
else if(!strcmp(AcadamyNum,"10"))
{
strcat(student.academy,"传媒学院");
}
else if(!strcmp(AcadamyNum,"11"))
{
strcat(student.academy,"先进制造学院");
}
else if(!strcmp(AcadamyNum,"12"))
{
strcat(student.academy,"马克思主义学院");
}
else
{
printf("学生学号输入错误\n");
}
return student;
//插入节点
//InsertNode(list,student);
}
//打印单个节点信息
void PrintOneNode(struct Node *OneNode)
{
struct Node *temp=OneNode;
printf("%s\t%s\t%s\t%.2f\t\t%d\t%d\t\t%s\t%s\n",
temp->data.num,temp->data.name,temp->data.academy,temp->data.temperature,temp->data.cough,temp->data.health,
temp->data.time,temp->data.date);
}
//查找节点
struct Node *SearchNode(struct Node *list,char *target,char *date)
{
struct Node *temp=list->next;
if(temp==NULL)
{
return NULL;
}
else
{
while(strcmp(temp->data.num,target)||strcmp(temp->data.date,date))//根据学号和日期查询
{
temp=temp->next;
if(temp==NULL)
{
return temp;
}
}
return temp;
}
}
//根据学号和姓名查询 xx日期前的数据
struct Node *StaticNode(struct Node *list,char *target,char *date)
{
struct Node *temp=list->next;
if(temp==NULL)
{
return NULL;
}
else
{
while((strcmp(temp->data.num,target)&&strcmp(temp->data.name,target))||(strcmp(temp->data.date,date)>0))
{
temp=temp->next;
if(temp==NULL)
{
return temp;
}
}
return temp;
}
}
//查找信息
void Search()
{
char num[20],date[20],filename[60];
struct Node *staticList=createList();
printf("请输入学号或姓名:");
scanf("%s",num);
do
{
printf("请输入当前日期(8位):");
scanf("%s",date);
}
while(strlen(date)!=8);
struct Node *temp=StaticNode(list,num,date);
if(temp==NULL)
{
printf("未查找到该生\n");
}
else
{
printf("学号\t\t姓名\t学院\t\t温度\t是否咳嗽\t是否健康\t时间\t日期\n");
while(temp!=NULL)
{
PrintOneNode(temp);
InsertNode(staticList,temp->data);
temp=StaticNode(temp,num,date);
}
}
saveFile("data2.txt",staticList);
}
//修改信息
void Update()
{
char num[20],target[20],date[20];
struct Student student;
printf("请输入学号:");
scanf("%s",num);
do
{
printf("请输入日期(8位):");
scanf("%s",date);
}
while(strlen(date)!=8);
struct Node *temp=SearchNode(list,num,date);
if(temp==NULL)
{
printf("未查找到该生\n");
}
else
{
printf("学号\t\t姓名\t学院\t\t温度\t是否咳嗽\t是否健康\t时间\t日期\n");
PrintOneNode(temp);
student=Input();
temp->data=student;
}
}
//删除节点
void DeleteNode(struct Node *head,char *num,char *date)
{
struct Node *previous=head;
struct Node *posNode=head->next;
if(posNode==NULL)
{
printf("链表为空,无删除信息\n");
return;
}
else
{
while(strcmp(posNode->data.num,num)||strcmp(posNode->data.date,date))//根据学号和日期删除
{
previous=posNode;
posNode=posNode->next;
if(posNode==NULL)
{
printf("未搜索到删除信息\n");
return;
}
}
previous->next=posNode->next;
free(posNode);
}
}
void Delete()
{
char num[20],date[20];
printf("请输入学号:");
scanf("%s",num);
do
{
printf("请输入日期(8位):");
scanf("%s",date);
}
while(strlen(date)!=8);
DeleteNode(list,num,date);
}
//统计某日的异常学生数据
void StaticAbnormal()
{
struct Node *AbnormalList=createList();
struct Node *start=list->next;
char date[20];
do
{
printf("请输入日期(8位):");
scanf("%s",date);
}
while(strlen(date)!=8);
while(start!=NULL)
{
if(start->data.health!=1&&!strcmp(start->data.date,date))
{
InsertNode(AbnormalList,start->data);
}
start=start->next;
}
saveFile("data3.txt",AbnormalList);
}
//系统界面
void keydown()
{
int key;
struct Student student;
scanf("%d",&key);
switch(key)
{
case 1:
printf("===========1.录入信息===========\n");
student=Input();
//插入节点
InsertNode(list,student);
saveFile("student.txt",list);
break;
case 2:
printf("===========2.修改功能===========\n");
Update();
saveFile("student.txt",list);
break;
case 3:
printf("===========3.删除功能===========\n");
Delete();
saveFile("student.txt",list);
break;
case 4:
printf("===========4.查找功能===========\n");
Search();
break;
case 5:
printf("===========5.浏览功能===========\n");
Print(list);
break;
case 6:
printf("===========6.统计当天异常学生===========\n");
StaticAbnormal();
break;
}
}
void Menu()
{
printf("===========[学生健康管理系统]===========\n");
printf("===========1.录入信息===========\n");
printf("===========2.修改功能===========\n");
printf("===========3.删除功能===========\n");
printf("===========4.查找功能===========\n");
printf("===========5.浏览功能===========\n");
printf("===========6.统计当天异常学生===\n");
printf("================================\n");
printf("请输入(1-5):");
}
int main()
{
list=createList();
readFile("student.txt",list);
while(1)
{
Menu();
keydown();
system("pause");
system("cls");
}
return 0;
}
学生健康登记系统
最新推荐文章于 2024-11-12 20:24:31 发布