main.c
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include <string.h>
#include <stdlib.h>
#include "Sort.h"
#include"swap.h"
enum DATA {
A = '1',
B,
C,
D,
E,
F,
G='0'
};
int main()
{
extern Node* const Head;
Read(Head);
while (1)
{
view();
char flag {};
scanf("%c", &flag);
system("cls");
switch (flag)
{
case A:
Add();
system("pause");
break;
case B:
Delete(Head);
system("pause");
break;
case C:
Update(Head);
system("pause");
break;
case D:
Select(Head);
system("pause");
break;
case E:
sort_list_by_core(Head);
system("pause");
break;
case F:
Dayin(Head);
system("pause");
break;
case G:
Exit();
system("pause");
break;
default:
break;
}
}
return 1;
}
Sort.c
#include<stdio.h>
#include <string.h>
#include <stdlib.h>
#include"Sort.h"
Node* init()
{
Node* head = NULL;
head=(Node*)malloc(sizeof(Node));
head->next = NULL;
memset(&head->stu, 0, sizeof(struct Student));
return head;
}
Node* const Head = init();
//视图
void view()
{
printf("0、退出\n");
printf("1、录入\n");
printf("2、删除\n");
printf("3、修改\n");
printf("4、查找\n");
printf("5、排序\n");
printf("6、打印\n");
printf("请输入功能,输入一个字符\n");
}
//添加
void Add()
{
Node* T = Head;
while (T->next!=NULL)
{
T = T->next;
}
int num = 0;
printf("请输入添加学生人数\n");
scanf("%d", &num);
for(int i=0;i<num;i++)
{
Node* node = init();
printf("请输入第%d位学生姓名:\n",i+1);
scanf("%s", node->stu.name);
printf("请输入第%d位学生年龄:\n",i+1);
scanf("%d", &node->stu.age);
printf("请输入第%d位学生成绩:\n",i+1);
scanf("%f", &node->stu.core);
T->next = node;
T = T->next;
printf("录入成功\n");
}
Save(Head);
Dayin(Head);
}
void Delete(Node * T)
{
char name[20]{};
printf("请输入要删除的姓名\n");
scanf("%s", &name);
while (T->next!=NULL)
{
if (strcmp(T->next->stu.name, name) == 0)
{
Node* node = T->next;
T->next = T->next->next;
free(node);
Save(Head);
printf("删除成功\n");
return;
}
}
printf("未找到该学生\n");
}
void Dayin(Node *T)
{
if (T->next == NULL)
{
printf("暂无学生信息\n");
return;
}
printf("姓名\t年龄\t成绩\t\n");
while (T->next!=NULL)
{
T = T->next;
printf("%s\t%d\t%.2f\t\n", T->stu.name, T->stu.age, T->stu.core);
}
return;
}
void Update(Node* T)
{
char name[20]{};
printf("请输入要修改的学生姓名\n");
scanf("%s", name);
while (T->next!=NULL)
{
T = T->next;
if (strcmp(T->stu.name, name) == 0)
{
printf("请输入修改后的姓名:\n");
scanf("%s", T->stu.name);
printf("请输入修改后的年龄:\n");
scanf("%d", &T->stu.age);
printf("请输入修改后的成绩:\n");
scanf("%f", &T->stu.core);
Save(Head);
printf("修改成功\n");
return;
}
}
printf("未找到该学生\n");
}
void Select(Node* T)
{
char name[20]{};
printf("请输入要查找的学生姓名\n");
scanf("%s", name);
while (T->next != NULL)
{
T = T->next;
if (strcmp(T->stu.name, name) == 0)
{
printf("姓名\t年龄\t成绩\t\n");
printf("%s\t%d\t%.2f\t\n", T->stu.name, T->stu.age, T->stu.core);
return;
}
}
printf("未找到该学生\n");
}
void Exit()
{
exit(0);
}
void Save(Node* T)
{
FILE* pf = NULL;
pf = fopen("student.txt", "w");
if (pf == NULL)
{
printf("文件保存失败");
return;
}
else {
while (T->next != NULL)
{
T = T->next;
fprintf(pf, "%d %s %f\n", T->stu.age, T->stu.name, T->stu.core);
}
fclose(pf);
pf = NULL;
return;
}
}
void Read(Node* T)
{
FILE* fp;
fp = fopen("student.txt", "r");
if (fp == NULL)
{
return;
}
char line[256]{};
P: if (fgets(line,256,fp)!=NULL)
{
Node* node = init();
sscanf(line,"%d%s%f", &node->stu.age, node->stu.name, &node->stu.core);
T->next = node;
T = T->next;
goto P;
}
fclose(fp);
return;
}
Sort.h
#pragma once
#ifndef _SORT_H_ //防止文件重复引用
#define _SORT_H_
typedef struct Student {
char name[20];
int age;
float core;
};
typedef struct node {
struct Student stu;
struct node *next;
}Node;
extern Node* const Head;
Node* init();
void Add();
void view();
void Delete(Node* T);
void Dayin(Node* T);
void Update(Node* T);
void Select(Node* T);
void Save(Node* T);
void Read(Node* T);
void Exit();
#endif // !_SORT_H_
swap.c
#define _CRT_SECURE_NO_WARNINGS
#include"Sort.h"
#include"swap.h"
#include <stdio.h>
void swap(Student* a, Student* b)
{
Student temp = *a;
*a = *b;
*b = temp;
}
void sort_list_by_core(Node* head) {
Node* i = head;
Node* j = NULL;
for (; i != NULL; i = i->next) {
for (j = i->next; j != NULL; j = j->next) {
if (i->stu.core > j->stu.core) {
swap(&(i->stu), &(j->stu));
}
}
}
}
swap.h
#pragma once
#ifndef _SWAP_
#define _SWAP_
void swap(Student* a, Student* b);
void sort_list_by_core(Node* head);
#endif // !1