#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
struct Student
{
char studentid[10];
int age;
char sex[8];
char name[20];
int chinesesocre;
int mathsocre;
int englishsocre;
};
struct Node
{
struct Student student;
struct Node* Next;
};
struct Node* CreateHeadNode();
struct Node* CreateNode(struct Student);
//尾插法----------------------增
void InsertByTail(struct Node*, struct Student);
//以数据作为删除的参照--------删
void DeleteByData(struct Node*, char[10]);
//浏览全部信息----------------查
void PrintList(struct Node*);
//以学号找学生信息------------查
void SearchStudent(struct Node*, char[10]);
//先找到学生------------------改
void ModifyStudent(struct Node*, char[10]);
//排序
void BubbleSort(struct Node*);
//写入文件
void WriteFile(struct Node*, char*);
void ReadFile(struct Node*, char*);
//菜单
void menu();
int main()
{
int input;
struct Node* HeadNode = CreateHeadNode();
struct Student student = { 0 };
char studentid[10] = "";
char* filename = "studentsystem.bin";
ReadFile(HeadNode, filename);
while (1)
{
menu();
scanf_s("%d", &input);
switch (input)
{
case 0:
printf("退出成功\n");
WriteFile(HeadNode, filename);
exit(0);
break;
case 1:
printf("********************************************************************\n");
printf("\t\t\t 1.写入信息\n");
while (1)
{
printf("输入学生信息:");
scanf_s("%s %d %s %s %d %d %d",
student.studentid, 10,
&student.age,
student.sex, 8,
student.name, 20,
&student.chinesesocre,
&student.mathsocre,
&student.englishsocre);
InsertByTail(HeadNode, student);
while (getchar() != '\n');
printf("\n请输入y(继续)/n(退出):");
input = getchar();
if (input == 'n' || input == 'N')
break;
while (getchar() != '\n');
}
break;
case 2:
printf("********************************************************************\n");
printf("\t\t\t 2.浏览信息\n");
PrintList(HeadNode);
break;
case 3:
printf("********************************************************************\n");
printf("\t\t\t 3.删除信息\n");
printf("请输入删除学生学号:");
scanf_s("%s", studentid, 10);
DeleteByData(HeadNode, studentid);
break;
case 4:
printf("********************************************************************\n");
printf("\t\t\t 4.修改信息\n");
printf("请输入修改学生学号:");
scanf_s("%s", studentid, 10);
ModifyStudent(HeadNode, studentid);
break;
case 5:
printf("********************************************************************\n");
printf("\t\t\t 5.查找信息\n");
printf("请输入查找学生学号:");
scanf_s("%s", studentid, 10);
SearchStudent(HeadNode, studentid);
break;
case 6:
printf("********************************************************************\n");
printf("\t\t\t 6.信息排序\n");
BubbleSort(HeadNode);
break;
default:
printf("请选择0到6之间的数。\n");
break;
}
while (getchar() != '\n');
system("pause");
system("cls");
}
return 0;
}
struct Node* CreateHeadNode()
{
struct Node* HeadNode = (struct Node*)malloc(sizeof(struct Node));
assert(HeadNode);
HeadNode->Next = NULL;
return HeadNode;
}
struct Node* CreateNode(struct Student student)
{
struct Node* Node = (struct Node*)malloc(sizeof(struct Node));
assert(Node);
Node->student = student;
Node->Next = NULL;
return Node;
}
void InsertByTail(struct Node* HeadNode, struct Student student)
{
struct Node* newNode = CreateNode(student);
struct Node* Temp = HeadNode;
while (Temp->Next != NULL)
{
Temp = Temp->Next;
}
Temp->Next = newNode;
}
void DeleteByData(struct Node* HeadNode, char studentid[10])
{
struct Node* PreNode = HeadNode;
struct Node* CurNode = HeadNode->Next;
while (CurNode != NULL && (0 != strncmp(CurNode->student.studentid, studentid, 10)))
{
PreNode = CurNode;
CurNode = PreNode->Next;
}
PreNode->Next = CurNode->Next;
free(CurNode);
CurNode = NULL;
}
void PrintList(struct Node* HeadNode)
{
struct Node* Temp = HeadNode->Next;
printf("学号\t年龄\t性别\t姓名\t语文\t数学\t英语\n");
while (Temp != NULL)
{
printf("%s\t%d\t%s\t%s\t%d\t%d\t%d\n",
Temp->student.studentid,
Temp->student.age,
Temp->student.sex,
Temp->student.name,
Temp->student.chinesesocre,
Temp->student.mathsocre,
Temp->student.englishsocre);
Temp = Temp->Next;
}
}
void SearchStudent(struct Node* HeadNode, char studentid[10])
{
struct Node* Temp = HeadNode;
while (Temp->Next != NULL && (0 != strncmp(Temp->student.studentid, studentid, 10)))
{
Temp = Temp->Next;
}
printf("学号\t年龄\t性别\t姓名\t语文\t数学\t英语\n");
printf("%s\t%d\t%s\t%s\t%d\t%d\t%d\n",
Temp->student.studentid,
Temp->student.age,
Temp->student.sex,
Temp->student.name,
Temp->student.chinesesocre,
Temp->student.mathsocre,
Temp->student.englishsocre);
}
void ModifyStudent(struct Node* HeadNode, char studentid[10])
{
struct Node* Temp = HeadNode;
struct Student student = { 0 };
while (Temp->Next != NULL && (0 != strncmp(Temp->student.studentid, studentid, 10)))
{
Temp = Temp->Next;
}
printf("重新输入学生数据:\n");
scanf_s("%s %d %s %s %d %d %d",
student.studentid, 10,
&student.age,
student.sex, 8,
student.name, 20,
&student.chinesesocre,
&student.mathsocre,
&student.englishsocre);
Temp->student = student;
}
void BubbleSort(struct Node* HeadNode)
{
int input = 0;
printf("输入1(按学号大小排序) 2(按年龄大小排序):");
scanf_s("%d", &input);
switch (input)
{
case 1:
for (struct Node* i = HeadNode->Next; i != NULL; i = i->Next)
{
for (struct Node* j = HeadNode->Next; j != NULL; j = j->Next)
{
if (j->Next != NULL && 0 < (strncmp(j->student.studentid, j->Next->student.studentid, 10)))
{
struct Student temp = j->student;
j->student = j->Next->student;
j->Next->student = temp;
}
}
}
printf("按学号排序成功!\n");
break;
case 2:
for (struct Node* i = HeadNode->Next; i != NULL; i = i->Next)
{
for (struct Node* j = HeadNode->Next; j != NULL; j = j->Next)
{
if (j->Next != NULL && (j->student.age > j->Next->student.age))
{
struct Student temp = j->student;
j->student = j->Next->student;
j->Next->student = temp;
}
}
}
printf("按年龄排序成功!\n");
break;
}
}
void menu()
{
printf("********************************************************************\n");
printf("--------------------------------------------------------------------\n");
printf("\t\t\t 学生管理系统\n");
printf("********************************************************************\n");
printf("********************************************************************\n");
printf("\t\t\t 0.退出\n");
printf("\t\t\t 1.写入信息\n");
printf("\t\t\t 2.浏览信息\n");
printf("\t\t\t 3.删除信息\n");
printf("\t\t\t 4.修改信息\n");
printf("\t\t\t 5.查找信息\n");
printf("\t\t\t 6.信息排序\n");
printf("--------------------------------------------------------------------\n");
printf("********************************************************************\n");
printf("输入:");
}
void WriteFile(struct Node* HeadNode, char* filename)
{
FILE* fp = NULL;
struct Node* Temp = HeadNode->Next;
fp = fopen(filename, "w+");
if (fp == NULL)
{
printf("文件不存在");
return;
}
while (Temp != NULL)
{
fprintf(fp, "%s\t%d\t%s\t%s\t%d\t%d\t%d\n",
Temp->student.studentid,
Temp->student.age,
Temp->student.sex,
Temp->student.name,
Temp->student.chinesesocre,
Temp->student.mathsocre,
Temp->student.englishsocre);
Temp = Temp->Next;
}
fclose(fp);
}
void ReadFile(struct Node* HeadNode, char* filename)
{
struct Node* Temp = HeadNode;
struct Student student = { 0 };
FILE* fp = NULL;
fp = fopen(filename, "r");
if (fp == NULL)
return;
while (fscanf_s(fp, "%s\t%d\t%s\t%s\t%d\t%d\t%d\n",
student.studentid, 10,
&student.age,
student.sex, 8,
student.name, 20,
&student.chinesesocre,
&student.mathsocre,
&student.englishsocre) != EOF)
{
struct Node* newNode = CreateNode(student);
while (Temp->Next != NULL)
{
Temp = Temp->Next;
}
Temp->Next = newNode;
}
fclose(fp);
}
C语言实现文件操作的学生信息系统
最新推荐文章于 2022-07-26 21:45:17 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)