|
发表于 2020-12-21 09:23:38
|
显示全部楼层
本楼为最佳答案

- //Shibo
- //2020/12/20
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #define MAX_STUDENTS 1000
- typedef struct student{
- char name[15];
- char gen[4];
- char phone_number[11];
- int time;
- float temp;
- struct student* next;
- }student;
- /** Function */
- void common_list();
- void init(student* ptr);
- struct student* add_student(student* head);
- void searching(student* ptr);
- struct student* add_one_student(struct student *list);
- void file_out(student* ptr, FILE* fp);
- void free_function(struct student* list);
- int main(void){
- //File pointer.
- FILE* fp = fopen("output.txt","w");
- //Student pointer.
- struct student* head = NULL;
- int common = 0;
- while(common != 4){
- //Print menu.
- common_list();
- //Read common.
- scanf("%d",&common);
- switch(common){
- case 1:
- head = add_student(head);
- break;
- case 2:
- searching(head);
- break;
- case 3:
- file_out(head,fp);
- break;
- case 4:
- free_function(head);
- return 0;
- default:
- printf("错误输入\n");
- break;
- }
- }
- return 0;
- }
- void common_list(){
- printf("--------欢迎来到 COVID-19 监控系统-------\n");
- printf("1. 添加学生\n");
- printf("2. 查找学生\n");
- printf("3. 输出文件\n");
- printf("4. 退出系统\n\n");
- }
- struct student* add_student(student* head){
- printf("你想添加多少个学生?\n");
- int num_st;
- scanf("%d",&num_st);
- for(int i = 0; i<num_st; i++){
- printf("学生%d: \n",i+1);
- head = add_one_student(head);
- }
- return head;
- }
- struct student* add_one_student(struct student *list) {
- struct student *pNew = NULL;
- // Create the new node.
- pNew = malloc(sizeof(student));
- //Read in student information.
- printf("\t请输入学生姓名:");
- scanf("%s",pNew->name);
- printf("\t请输入学生性别: ");
- scanf("%s",pNew->gen);
- printf("\t请输入学生手机号:");
- scanf("%s",pNew->phone_number);
- printf("\t请输入学生进楼时间:(XX:XX)");
- scanf("%d",&pNew->time);
- printf("\t请输入学生体温:");
- scanf("%f",&pNew->temp);
- pNew->next = NULL;
- //printf("\n\n");
- // if list is empty then this becomes the first node.
- if (list == NULL)
- return pNew;
- // Insert to front (special case)
- if (pNew->time < list->time) {
- pNew->next = list;
- return pNew;
- }
- // Store front of the linked list
- struct student *current = list;
- // Iterate so that current points to the node BEFORE the one we want to do the insert.
- while (current->next != NULL && current->next->time < pNew->time)
- current = current->next;
- // Links new node to its successor (which is right after current).
- pNew->next = current->next;
- // Links current to the new node.
- current->next = pNew;
- // Return a pointer to the edited list.
- return list;
- }
- void searching(student* ptr){
- if(ptr == NULL){
- printf("空列表,请先输入学生信息\n\n\n");
- return;
- }
- printf("请输入搜索姓名: ");
- char name[20];
- scanf("%s",name);
- //Find the time.
- student* current = ptr;
- while(current!=NULL && strcmp(current->name,name)==1)
- current = current->next;
- if(current == NULL){
- printf("查无此人\n");
- return;
- }
- int time = current->time;
- //Go over.
- current = ptr;
- //Go through all node print out all people we want.
- while(current!=NULL){
- if(abs(time-current->time)<=3)
- printf("姓名;%-*s \t电话:%-*s \t进楼时间: %02d:%02d\n",6,current->name,11,current->phone_number,current->time/100,current->time%100);
- // i++ step.
- current = current->next;
- }
- }
- void file_out(student* ptr, FILE* fp){
- fprintf(fp,"--------欢迎来到 COVID-19 监控系统-------\n");
- student* current = ptr;
- //Go through all node print out all people we want.
- while(current!=NULL){
- //Output.
- fprintf(fp,"姓名;%-*s \t电话:%-*s \t进楼时间: %02d:%02d\n",6,current->name,11,current->phone_number,current->time/100,current->time%100);
- // i++ step.
- current = current->next;
- }
- }
- void free_function(struct student* list){
- if (list != NULL) {
- free_function(list->next);
- free(list);
- }
- }
复制代码
Test Case:
- 1
- 1
- 小甲鱼
- 男
- 188888888
- 1234
- 39.0
- 2
- 小甲鱼
- 1
- 3
- 黑夜
- 男
- 166666666
- 2359
- 35.4
- 康小泡
- 女
- 132323232
- 1200
- 37.0
- 小姨
- 女
- 1199
- 36.9
- 3
- 4
复制代码
Cmd:
- --------欢迎来到 COVID-19 监控系统-------
- 1. 添加学生
- 2. 查找学生
- 3. 输出文件
- 4. 退出系统
- 1
- 你想添加多少个学生?
- 1
- 学生1:
- 请输入学生姓名:小甲鱼
- 请输入学生性别: 男
- 请输入学生手机号:188888888
- 请输入学生进楼时间:(XX:XX)1234
- 请输入学生体温:39.0
- --------欢迎来到 COVID-19 监控系统-------
- 1. 添加学生
- 2. 查找学生
- 3. 输出文件
- 4. 退出系统
- 2
- 请输入搜索姓名: 小甲鱼
- 姓名;小甲鱼 电话:188888888 进楼时间: 12:34
- --------欢迎来到 COVID-19 监控系统-------
- 1. 添加学生
- 2. 查找学生
- 3. 输出文件
- 4. 退出系统
- 1
- 你想添加多少个学生?
- 3
- 学生1:
- 请输入学生姓名:黑夜
- 请输入学生性别: 男
- 请输入学生手机号:166666666
- 请输入学生进楼时间:(XX:XX)2359
- 请输入学生体温:35.4
- 学生2:
- 请输入学生姓名:康小泡
- 请输入学生性别: 女
- 请输入学生手机号:132323232
- 请输入学生进楼时间:(XX:XX)1200
- 请输入学生体温:37.0
- 学生3:
- 请输入学生姓名:小姨
- 请输入学生性别: 女
- 请输入学生手机号:1199
- 请输入学生进楼时间:(XX:XX)36.9
- 请输入学生体温:--------欢迎来到 COVID-19 监控系统-------
- 1. 添加学生
- 2. 查找学生
- 3. 输出文件
- 4. 退出系统
- 3
- --------欢迎来到 COVID-19 监控系统-------
- 1. 添加学生
- 2. 查找学生
- 3. 输出文件
- 4. 退出系统
- 4
- Process returned 0 (0x0) execution time : 2.690 s
- Press any key to continue.
复制代码
File Out:
- --------欢迎来到 COVID-19 监控系统-------
- 姓名;小姨 电话:1199 进楼时间: 00:36
- 姓名;康小泡 电话:132323232 进楼时间: 12:00
- 姓名;小甲鱼 电话:188888888 进楼时间: 12:34
- 姓名;黑夜 电话:166666666 进楼时间: 23:59
复制代码 |
|