c语言之一个简单的《学生教师管理系统》小结记录(二)
2020-12-13 16:07
标签:break onclick make 表操作 core closed 一个 fopen eal 头文件包含学生结构体以及链表结构 本篇博文用来记录学生头/教师文件建立以及结构体链表创建及链表相关操作
首先是头文件的建立
1、学生结构体建立
1 /****定义学生信息******/
2
3 typedef struct studentInfo
4 {
5 int ID;
6 char name[16];
7 char password[16];
8 int age;
9 int classes;
10 char sex;
11 float math;
12 float chinese;
13 float clanguage;
14 }StuInfo;
2、链表结构建立
1 /*定义链表结构*/
2 typedef struct studentNode
3 {
4 struct studentInfo data;
5 struct studentNode *pNext;
6 }Node;
3、函数声明
1 /*创建链表结构并且申请空间*/
2 Node* makeNode();
3
4 /*学生信息初始化*/
5 StuInfo initData();
6
7 /*头插法*/
8 void headInsert(Node *pHead);
9
10 /*循环处理,将节点的数据域写入文件*/
11 void writeToFile(Node *pHead);
12
13 /*遍历链表*/
14 void showList(Node *pHead);
15
16 /*从文件中读取链表信息*/
17 Node* readFromFile(Node *pHead);
18
19 /*查找学生*/
20 Node* findnode(Node *pHead, const int ID);
21
22 /*录入查重*/
23 Node* findstu(Node *pHead, const int ID);
24
25 /*按班级遍历链表*/
26 void showClassList(Node *pHead, int classes);
27
28 /*按总成绩显示排序*/
29 void showClassSortList(Node *pHead, int classes);
30
31 /*修改学生信息初始化*/
32 StuInfo modinitData();
4、整个头文件
1 #ifndef __STUDENT_H_
2 #define __STUDENT_H_
3
4 #include
5、函数实现
1 /*创建链表节点*/
2 Node* makeNode()
3 {
4 Node *newnode = (Node *)malloc(NODE_LEN);//为新节点分配空间
5 if(NULL == newnode)//容错处理,如果分配失败再次申请空间
6 {
7 newnode = (Node *)malloc(NODE_LEN);
8 }
9 memset(&newnode->data, ‘\0‘, STU_LEN);
10 newnode->pNext = NULL;
11 return newnode;
12 }
1 /*学生信息初始化*/
2 StuInfo initData()
3 {
4 StuInfo userInfo;
5 Node *temp = makeNode();
6 Node *pHead = makeNode();
7 pHead = readFromFile(pHead);
8 memset(&userInfo, ‘\0‘, STU_LEN);
9 printf("\t\t\t? 请输入学号:");
10 scanf("%d",&userInfo.ID);
11 getchar();
12 temp = findstu(pHead,userInfo.ID);
13 while(temp != NULL)//防止学号重复
14 {
15 printf("\t\t\t\033[31m\033[1m学号重复,请重新录入\033[0m\033[33m\n");
16 printf("\n\t\t\t? 请输入学号:");
17 scanf("%d",&userInfo.ID);
18 getchar();
19 temp = findstu(pHead,userInfo.ID);
20 }
21 printf("\t\t\t? 请输入姓名:");
22 scanf("%s",userInfo.name);
23 getchar();
24 strncpy(userInfo.password,"000000",6);//初始密码为000000
25 printf("\t\t\t? 请输入年龄:");
26 scanf("%d",&userInfo.age);
27 getchar();
28 printf("\t\t\t? 请输入班级:");
29 scanf("%d",&userInfo.classes);
30 getchar();
31 printf("\t\t\t? 请输入性别:");
32 scanf("%c",&userInfo.sex);
33 getchar();
34 printf("\t\t\t? 请输入数学成绩:");
35 scanf("%f",&userInfo.math);
36 getchar();
37 printf("\t\t\t? 请输入语文成绩:");
38 scanf("%f",&userInfo.chinese);
39 getchar();
40 printf("\t\t\t? 请输入c语言成绩:");
41 scanf("%f",&userInfo.clanguage);
42 getchar();
43 temp = NULL;
44 pHead = NULL;
45 return userInfo;
46 }
1 /*修改学生信息*/
2 StuInfo modinitData(int stuID)//修改时学号可以和之前学号重复,但不能和其他人学号重复
3 {
4 StuInfo userInfo;
5 Node *temp = makeNode();
6 Node *pHead = makeNode();
7 pHead = readFromFile(pHead);
8 memset(&userInfo, ‘\0‘, STU_LEN);
9 printf("\t\t\t? 请输入学号:");
10 scanf("%d",&userInfo.ID);
11 getchar();
12 temp = findstu(pHead,userInfo.ID);
13 while((temp != NULL) && (userInfo.ID != stuID))
14 {
15 printf("\t\t\t\033[31m\033[1m学号重复,请重新录入\033[0m\033[33m\n");
16 printf("\n\t\t\t? 请输入学号:");
17 scanf("%d",&userInfo.ID);
18 getchar();
19 temp = findstu(pHead,userInfo.ID);
20 }
21 printf("\t\t\t? 请输入姓名:");
22 scanf("%s",userInfo.name);
23 getchar();
24 strncpy(userInfo.password,"000000",6);//初始密码为000000
25 printf("\t\t\t? 请输入年龄:");
26 scanf("%d",&userInfo.age);
27 getchar();
28 printf("\t\t\t? 请输入班级:");
29 scanf("%d",&userInfo.classes);
30 getchar();
31 printf("\t\t\t? 请输入性别:");
32 scanf("%c",&userInfo.sex);
33 getchar();
34 printf("\t\t\t? 请输入数学成绩:");
35 scanf("%f",&userInfo.math);
36 getchar();
37 printf("\t\t\t? 请输入语文成绩:");
38 scanf("%f",&userInfo.chinese);
39 getchar();
40 printf("\t\t\t? 请输入c语言成绩:");
41 scanf("%f",&userInfo.clanguage);
42 getchar();
43
44 return userInfo;
45 }
1 /*链表操作之头插法*/
2 void headInsert(Node *pHead)
3 {
4 if(NULL == pHead)
5 {
6 perror("the list head is NULL!\n");
7 }
8
9 Node *newnode = makeNode();
10 newnode->data = initData();//保存数据
11 newnode->pNext = pHead->pNext;//讲新的节点指针指向头结点的下一个节点
12 pHead->pNext = newnode;//头指针指向新节点
13 newnode = NULL;
14 }
1 /*遍历整个链表*/
2 void showList(Node *pHead)
3 {
4 if(NULL == pHead || NULL == pHead->pNext)
5 {
6 perror("the list is empty!");
7 return;
8 }
9 Node *temp = pHead->pNext;
10 printf("|-- ID --|---- name ----|");
11 printf("classes|-math-|chinese|clanguage|Total score|\n");
12 while(temp != NULL)
13 {
14 printf("| %-6d | %-12s | %-3d |",
15 temp->data.ID, temp->data.name, temp->data.classes);
16 printf("%-5.1f |%-5.1f | %-5.1f | %-5.1f |\n",
17 temp->data.math, temp->data.chinese,temp->data.clanguage,temp->data.math+temp->data.chinese+temp->data.clanguage);
18 temp = temp->pNext;//指向下一个节点
19 }
20 temp = NULL;
21 }
1 //按班级遍历链表
2 void showClassList(Node *pHead, int classes)
3 {
4 if(NULL == pHead || NULL == pHead->pNext)
5 {
6 perror("the list is empty!");
7 return;
8 }
9 Node *temp = pHead->pNext;
10 printf("|-- ID --|---- name ----|");
11 printf("classes|-math-|chinese|clanguage|Total score|\n");
12 while(temp != NULL)
13 {
14 if(classes == temp->data.classes)
15 {
16 printf("| %-6d | %-12s | %-3d |",
17 temp->data.ID, temp->data.name, temp->data.classes);
18 printf("%-5.1f |%-5.1f | %-5.1f | %-5.1f |\n",
19 temp->data.math, temp->data.chinese,temp->data.clanguage,temp->data.math+temp->data.chinese+temp->data.clanguage);
20 }
21 temp = temp->pNext;
22 }
23 temp = NULL;
24 }
1 //按总成绩显示排序
2 void showClassSortList(Node *pHead, int classes)
3 {
4 if(NULL == pHead || NULL == pHead->pNext)
5 {
6 perror("the list is empty!");
7 return;
8 }
9 Node *temp = pHead->pNext;
10 printf(" ? ? ? ? ? ? ? ? ? %d班成绩排名一览? ? ? ? ? ? ? ? ?\n\n",classes);
11 printf("| 姓名 | 学号 | 数学 | 语文 | C语言 | 总分 |\n");
12 while(temp != NULL)
13 {
14 if(classes == temp->data.classes)
15 {
16 printf("| %-12s | %-4d | %-5.1f | %-5.1f | %-5.1f | %-6.1f |\n",
17 temp->data.name, temp->data.ID,
18 temp->data.math, temp->data.chinese,temp->data.clanguage,
19 (temp->data.math + temp->data.chinese + temp->data.clanguage));
20 }
21 temp = temp->pNext;
22 }
23 temp = NULL;
24 }
1 //循环处理,将节点的数据域写入文件
2 void writeToFile(Node *pHead)
3 {
4 if(NULL == pHead || NULL == pHead->pNext)
5 {
6 perror("NO data to write...\n");
7 return;
8 }
9 FILE *fpw = fopen("student.txt", "w");//可读写方式
10 if(NULL == fpw)
11 {
12 perror("open file student,txt failed!\n");
13 return;
14 }
15
16 Node *temp = pHead->pNext;
17 while(NULL != temp)//循环写入
18 {
19 fwrite(&temp->data, STU_LEN, 1, fpw);
20 temp = temp->pNext;
21 }
22 system("clear");
23 puts("========文件保存成功!==========");
24 fclose(fpw);//关闭文件
25 return;
26 }
1 //链表的读文件
2 Node* readFromFile(Node *pHead)//从文件中将数据读取并且建立一个链表
3 {
4 if(NULL == pHead)
5 {
6 perror("the head is NULL!");
7 return NULL;
8 }
9
10 FILE *fpr = fopen("student.txt", "r");//以可读方式打开
11 if(NULL == fpr)
12 {
13 perror("Open file student.txt failed!");
14 return NULL;
15 }
16
17 Node *newnode = NULL;//新建节点
18 StuInfo stuInfo;//
19 memset(&stuInfo, ‘\0‘, STU_LEN);//空间初始化
20 while(fread(&stuInfo, STU_LEN, 1, fpr) > 0)//边读文件边建立链表
21 {
22 newnode = makeNode();
23 newnode->data = stuInfo;
24 newnode->pNext = pHead->pNext;
25 pHead->pNext = newnode;
26 }
27 fclose(fpr);//关闭文件
28 return pHead;//返回链表的头结点
29 }
1 /*查找学生*/
2 Node* findnode(Node *pHead, const int ID)//用于登录时显示登录信息
3 {
4 Node *p = makeNode();
5 p = pHead;
6 while(p != NULL)
7 {
8 if(ID == p->data.ID)
9 break;
10 p = p->pNext;
11 }
12 if(p == NULL)
13 {
14 printf("ID输入错误或此学生信息未录入\n");
15 }
16 return p;
17 }
完整student.c
/*student.c*/
#include "student.h"
#define STU_LEN sizeof(StuInfo)
#define NODE_LEN sizeof(Node)
/*void main()
{
Node *pHead = makeNode();
// for(int i = 0; i */
/*创建链表节点*/
Node* makeNode()
{
Node *newnode = (Node *)malloc(NODE_LEN);//为新节点分配空间
if(NULL == newnode)//容错处理,如果分配失败再次申请空间
{
newnode = (Node *)malloc(NODE_LEN);
}
memset(&newnode->data, ‘\0‘, STU_LEN);
newnode->pNext = NULL;
return newnode;
}
/*学生信息初始化*/
StuInfo initData()
{
StuInfo userInfo;
Node *temp = makeNode();
Node *pHead = makeNode();
pHead = readFromFile(pHead);
memset(&userInfo, ‘\0‘, STU_LEN);
printf("\t\t\t? 请输入学号:");
scanf("%d",&userInfo.ID);
getchar();
temp = findstu(pHead,userInfo.ID);
while(temp != NULL)//防止学号重复
{
printf("\t\t\t\033[31m\033[1m学号重复,请重新录入\033[0m\033[33m\n");
printf("\n\t\t\t? 请输入学号:");
scanf("%d",&userInfo.ID);
getchar();
temp = findstu(pHead,userInfo.ID);
}
printf("\t\t\t? 请输入姓名:");
scanf("%s",userInfo.name);
getchar();
strncpy(userInfo.password,"000000",6);//初始密码为000000
printf("\t\t\t? 请输入年龄:");
scanf("%d",&userInfo.age);
getchar();
printf("\t\t\t? 请输入班级:");
scanf("%d",&userInfo.classes);
getchar();
printf("\t\t\t? 请输入性别:");
scanf("%c",&userInfo.sex);
getchar();
printf("\t\t\t? 请输入数学成绩:");
scanf("%f",&userInfo.math);
getchar();
printf("\t\t\t? 请输入语文成绩:");
scanf("%f",&userInfo.chinese);
getchar();
printf("\t\t\t? 请输入c语言成绩:");
scanf("%f",&userInfo.clanguage);
getchar();
temp = NULL;
pHead = NULL;
return userInfo;
}
/*修改学生信息*/
StuInfo modinitData(int stuID)//修改时学号可以和之前学号重复,但不能和其他人学号重复
{
StuInfo userInfo;
Node *temp = makeNode();
Node *pHead = makeNode();
pHead = readFromFile(pHead);
memset(&userInfo, ‘\0‘, STU_LEN);
printf("\t\t\t? 请输入学号:");
scanf("%d",&userInfo.ID);
getchar();
temp = findstu(pHead,userInfo.ID);
while((temp != NULL) && (userInfo.ID != stuID))
{
printf("\t\t\t\033[31m\033[1m学号重复,请重新录入\033[0m\033[33m\n");
printf("\n\t\t\t? 请输入学号:");
scanf("%d",&userInfo.ID);
getchar();
temp = findstu(pHead,userInfo.ID);
}
printf("\t\t\t? 请输入姓名:");
scanf("%s",userInfo.name);
getchar();
strncpy(userInfo.password,"000000",6);//初始密码为000000
printf("\t\t\t? 请输入年龄:");
scanf("%d",&userInfo.age);
getchar();
printf("\t\t\t? 请输入班级:");
scanf("%d",&userInfo.classes);
getchar();
printf("\t\t\t? 请输入性别:");
scanf("%c",&userInfo.sex);
getchar();
printf("\t\t\t? 请输入数学成绩:");
scanf("%f",&userInfo.math);
getchar();
printf("\t\t\t? 请输入语文成绩:");
scanf("%f",&userInfo.chinese);
getchar();
printf("\t\t\t? 请输入c语言成绩:");
scanf("%f",&userInfo.clanguage);
getchar();
return userInfo;
}
/*链表操作之头插法*/
void headInsert(Node *pHead)
{
if(NULL == pHead)
{
perror("the list head is NULL!\n");
}
Node *newnode = makeNode();
newnode->data = initData();//保存数据
newnode->pNext = pHead->pNext;//讲新的节点指针指向头结点的下一个节点
pHead->pNext = newnode;//头指针指向新节点
newnode = NULL;
}
/*遍历整个链表*/
void showList(Node *pHead)
{
if(NULL == pHead || NULL == pHead->pNext)
{
perror("the list is empty!");
return;
}
Node *temp = pHead->pNext;
printf("|-- ID --|---- name ----|");
printf("classes|-math-|chinese|clanguage|Total score|\n");
while(temp != NULL)
{
printf("| %-6d | %-12s | %-3d |",
temp->data.ID, temp->data.name, temp->data.classes);
printf("%-5.1f |%-5.1f | %-5.1f | %-5.1f |\n",
temp->data.math, temp->data.chinese,temp->data.clanguage,temp->data.math+temp->data.chinese+temp->data.clanguage);
temp = temp->pNext;//指向下一个节点
}
temp = NULL;
}
//按班级遍历链表
void showClassList(Node *pHead, int classes)
{
if(NULL == pHead || NULL == pHead->pNext)
{
perror("the list is empty!");
return;
}
Node *temp = pHead->pNext;
printf("|-- ID --|---- name ----|");
printf("classes|-math-|chinese|clanguage|Total score|\n");
while(temp != NULL)
{
if(classes == temp->data.classes)
{
printf("| %-6d | %-12s | %-3d |",
temp->data.ID, temp->data.name, temp->data.classes);
printf("%-5.1f |%-5.1f | %-5.1f | %-5.1f |\n",
temp->data.math, temp->data.chinese,temp->data.clanguage,temp->data.math+temp->data.chinese+temp->data.clanguage);
}
temp = temp->pNext;
}
temp = NULL;
}
//按总成绩显示排序
void showClassSortList(Node *pHead, int classes)
{
if(NULL == pHead || NULL == pHead->pNext)
{
perror("the list is empty!");
return;
}
Node *temp = pHead->pNext;
printf(" ? ? ? ? ? ? ? ? ? %d班成绩排名一览? ? ? ? ? ? ? ? ?\n\n",classes);
printf("| 姓名 | 学号 | 数学 | 语文 | C语言 | 总分 |\n");
while(temp != NULL)
{
if(classes == temp->data.classes)
{
printf("| %-12s | %-4d | %-5.1f | %-5.1f | %-5.1f | %-6.1f |\n",
temp->data.name, temp->data.ID,
temp->data.math, temp->data.chinese,temp->data.clanguage,
(temp->data.math + temp->data.chinese + temp->data.clanguage));
}
temp = temp->pNext;
}
temp = NULL;
}
//循环处理,将节点的数据域写入文件
void writeToFile(Node *pHead)
{
if(NULL == pHead || NULL == pHead->pNext)
{
perror("NO data to write...\n");
return;
}
FILE *fpw = fopen("student.txt", "w");//可读写方式
if(NULL == fpw)
{
perror("open file student,txt failed!\n");
return;
}
Node *temp = pHead->pNext;
while(NULL != temp)//循环写入
{
fwrite(&temp->data, STU_LEN, 1, fpw);
temp = temp->pNext;
}
system("clear");
puts("========文件保存成功!==========");
fclose(fpw);//关闭文件
return;
}
//链表的读文件
Node* readFromFile(Node *pHead)//从文件中将数据读取并且建立一个链表
{
if(NULL == pHead)
{
perror("the head is NULL!");
return NULL;
}
FILE *fpr = fopen("student.txt", "r");//以可读方式打开
if(NULL == fpr)
{
perror("Open file student.txt failed!");
return NULL;
}
Node *newnode = NULL;//新建节点
StuInfo stuInfo;//
memset(&stuInfo, ‘\0‘, STU_LEN);//空间初始化
while(fread(&stuInfo, STU_LEN, 1, fpr) > 0)//边读文件边建立链表
{
newnode = makeNode();
newnode->data = stuInfo;
newnode->pNext = pHead->pNext;
pHead->pNext = newnode;
}
fclose(fpr);//关闭文件
return pHead;//返回链表的头结点
}
/*查找学生*/
Node* findnode(Node *pHead, const int ID)//用于登录时显示登录信息
{
Node *p = makeNode();
p = pHead;
while(p != NULL)
{
if(ID == p->data.ID)
break;
p = p->pNext;
}
if(p == NULL)
{
printf("ID输入错误或此学生信息未录入\n");
}
return p;
}
/*录入查重*/
Node* findstu(Node *pHead, const int ID)//不打印信息
{
Node *p = makeNode();
p = pHead;
while(p != NULL)
{
if(ID == p->data.ID)
break;
p = p->pNext;
}
return p;
}
6、教师
下一篇:XML、JSON、PB比较