#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//定义全局变量 单链表
struct Node *list=NULL;
struct Student
{
char num[20];//学号
char name[20];//姓名
char id[20];//身份证号
char academy[20];//学院
char major[40];//专业
char analoy[10];//学生类型
char car[10];//交通工具
char touch[4];//是否接触
char cough[4];//是否咳嗽
char date[10];//日期
float temp;//温度
};
struct Node
{
struct Student data;
struct Node *next;
};
//创建头指针
struct Node *createList()
{
struct Node *head=(struct Node *)malloc(sizeof( struct Node));
if(head==NULL)
{
printf("内存申请失败\n");
exit(0);
}
head->next=NULL;
return head;
}
//创建节点
struct Node *createNode(struct Student data)
{
struct Node *newNode=(struct Node *)malloc(sizeof( struct Node));
if(newNode==NULL)
{
printf("内存申请失败\n");
exit(0);
}
newNode->data=data;
newNode->next=NULL;
return newNode;
}
//插入节点
void InsertNode(struct Node *list,struct Student data)
{
//头插法
//定义插入节点
struct Node *student=createNode(data);
student->next=list->next;
list->next=student;
}
//读取文件
void readFile(char *fileName,struct Node *list)
{
FILE *fp=fopen(fileName,"r");
if(fp==NULL)
{
FILE *fp=fopen(fileName,"w");
}
struct Student example;
while(fscanf(fp,"%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%f",
example.num,example.name,example.id,example.academy,example.major,example.analoy,example.car,example.touch,example.cough,example.date,&example.temp)!=EOF)
{
InsertNode(list,example);
memset(&example,0,sizeof(example));
}
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%s\t%s\t%s\t%s\t%s\t%s\t%s\t%.2f\n",
start->data.num,start->data.name,start->data.id,start->data.academy,start->data.major,start->data.analoy,start->data.car,start->data.cough,start->data.touch,start->data.date,start->data.temp);
start=start->next;
}
fclose(fp);
}
//按学院保存文件 单个节点
void saveAcademy(struct Student data)
{
FILE *fp=NULL;
if(!strcmp(data.academy,"计算机学院"))
{
fp=fopen("计算机学院.txt","a");
}
else if(!strcmp(data.academy,"自动化学院"))
{
fp=fopen("自动化学院.txt","a");
}
else if(!strcmp(data.academy,"通信学院"))
{
fp=fopen("通信学院.txt","a");
}
else if(!strcmp(data.academy,"光电学院"))
{
fp=fopen("光电学院.txt","a");
}
else if(!strcmp(data.academy,"软件学院"))
{
fp=fopen("软件学院.txt","a");
}
fprintf(fp,"%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%.2f\n",
data.num,data.name,data.id,data.academy,data.major,data.analoy,data.car,data.cough,data.touch,data.date,data.temp);
fclose(fp);
}
void Input(struct Node *list)
{
struct Student data;
printf("请输入学号:");
scanf("%s",data.num);
printf("请输入姓名:");
scanf("%s",data.name);
printf("请输入身份证号:");
scanf("%s",data.id);
printf("请输入学院,:");
scanf("%s",data.academy);
printf("请输入专业:");
scanf("%s",data.major);
printf("请输入学生类型:");
scanf("%s",data.analoy);
printf("请输入交通工具:");
scanf("%s",data.car);
printf("是否接触:");
scanf("%s",data.touch);
printf("是否咳嗽:");
scanf("%s",data.cough);
printf("请输入体温:");
scanf("%f",&data.temp);
printf("请输入返校日期:");
scanf("%s",data.date);
/*printf("学号:%s,姓名:%s,身份证号:%s,学院:%s,专业:%s,学生类型:%s,交通工具:%s,是否接触:%s,是否咳嗽:%s,返校日期:%s,体温:%.2f",
data.num,data.name,data.id,data.academy,data.major,data.analoy,data.car,data.touch,data.cough,data.date,data.temp);*/
InsertNode(list,data);
saveFile("student.txt",list);
saveAcademy(data);
}
//打印全部信息
void Print(struct Node *list)
{
struct Node *start=list->next;
while(start!=NULL)
{
printf("学号:%s,姓名:%s,身份证号:%s,学院:%s,专业:%s,学生类型:%s,交通工具:%s,是否接触:%s,是否咳嗽:%s,返校日期:%s,体温:%.2f\n",
start->data.num,start->data.name,start->data.id,start->data.academy,start->data.major,start->data.analoy,start->data.car,start->data.touch,start->data.cough,start->data.date,start->data.temp);
start=start->next;
}
}
//打印单个节点信息
void PrintOneNode(struct Node *OneNode)
{
struct Node *start=OneNode;
printf("学号:%s,姓名:%s,身份证号:%s,学院:%s,专业:%s,学生类型:%s,交通工具:%s,是否接触:%s,是否咳嗽:%s,返校日期:%s,体温:%.2f\n",
start->data.num,start->data.name,start->data.id,start->data.academy,start->data.major,start->data.analoy,start->data.car,start->data.touch,start->data.cough,start->data.date,start->data.temp);
}
//查找节点
struct Node *SearchNode(struct Node *list,char *target)
{
struct Node *temp=list->next;
if(temp==NULL)
{
return temp;
}
else
{
while(strcmp(temp->data.num,target)&&strcmp(temp->data.name,target)&&strcmp(temp->data.id,target))
{
temp=temp->next;
if(temp==NULL)
{
return temp;
}
}
return temp;
}
}
//查找相应信息
void Find(struct Node *list)
{
char target[40];
printf("请输入学号或姓名或身份证号:");
scanf("%s",target);
if(SearchNode(list,target)==NULL)
{
printf("未查找到相应信息\n");
}
else
{
PrintOneNode(SearchNode(list,target));
}
}
//通过学号排序
struct Node *SortNode(struct Node *head)
{
struct Student temp;
struct Node *start=head->next;
struct Node *Max=NULL;
while(start!=NULL)
{
Max=start;
struct Node *right=start->next;
while(right!=NULL)
{
if(strcmp(Max->data.num,right->data.num)>0)
{
Max=right;
}
right=right->next;
}
if(strcmp(Max->data.num,start->data.num))
{
temp=start->data;
start->data=Max->data;
Max->data=temp;
}
start=start->next;
}
return head;
}
//排序
void Sort()
{
char Acadamy[20],temp[20];
struct Node *head=createList();//返回的是头指针 头指针为空
printf("请输入学院名:");
scanf("%s",Acadamy);
strcpy(temp,Acadamy);
strcat(Acadamy,".txt");
readFile(Acadamy,head);
head=SortNode(head);
Print(head);
saveFile(strcat(temp,"排序结果.txt"),head);
}
//更新
void Update()
{
char Acadamy[20],num[20],temp[20];
struct Node *head=createList();//返回的是头指针 头指针为空
struct Node *acadamy=createList();
printf("请输入要修改信息的学院:");
scanf("%s",Acadamy);
strcpy(temp,Acadamy);
strcat(temp,".txt");
readFile(temp,acadamy);
strcat(Acadamy,"排序结果.txt");
readFile(Acadamy,head);
printf("请输入要修改信学生的学号:");
scanf("%s",num);
struct Node *result=SearchNode(head,num);
struct Node *resultA=SearchNode(acadamy,num);
struct Node *resultL=SearchNode(list,num);
if(result==NULL)
{
printf("未找到该生信息");
}
else
{
PrintOneNode(result);
struct Student data;
printf("请开始修改信息:\n");
printf("请输入学号:");
scanf("%s",data.num);
printf("请输入姓名:");
scanf("%s",data.name);
printf("请输入身份证号:");
scanf("%s",data.id);
printf("请输入学院,:");
scanf("%s",data.academy);
printf("请输入专业:");
scanf("%s",data.major);
printf("请输入学生类型:");
scanf("%s",data.analoy);
printf("请输入交通工具:");
scanf("%s",data.car);
printf("是否接触:");
scanf("%s",data.touch);
printf("是否咳嗽:");
scanf("%s",data.cough);
printf("请输入体温:");
scanf("%f",&data.temp);
printf("请输入返校日期:");
scanf("%s",data.date);
result->data=data;
resultA->data=data;
resultL->data=data;
printf("=========修改成功=======\n");
Print(head);
saveFile(Acadamy,head);
saveFile(temp,acadamy);
saveFile("student.txt",list);
}
}
//系统界面
void systemMenu()
{
printf("===========[学生进校信息管理系统]===========\n");
printf("===========1.录入信息===========\n");
printf("===========2.排序功能===========\n");
printf("===========3.查找功能===========\n");
printf("===========4.修改功能===========\n");
printf("===========5.浏览功能===========\n");
printf("================================\n");
printf("请输入(1-5):");
}
void keydown()
{
int key;
scanf("%d",&key);
switch(key)
{
case 1:
printf("===========1.录入信息===========\n");
//输入信息
Input(list);
break;
case 2:
printf("===========2.排序功能===========\n");
Sort();
break;
case 3:
printf("===========3.查找功能===========\n");
Find(list);
break;
case 4:
printf("===========4.修改功能===========\n");
Update();
break;
case 5:
printf("===========5.浏览功能===========\n");
Print(list);
break;
}
}
int main()
{
//创建链表
list=createList();
readFile("student.txt",list);
while(1)
{
systemMenu();
keydown();
system("pause");
system("cls");
}
}
(C语言实现)返校信息登记系统
最新推荐文章于 2022-11-11 16:59:40 发布