c语言之一个简单的《学生教师管理系统》小结记录(二)

本篇博文用来记录学生头/教师文件建立以及结构体链表创建及链表相关操作

首先是头文件的建立

头文件包含学生结构体以及链表结构

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 <stdlib.h>
 5 #include <stdio.h>
 6 #include <string.h>
 7 #include <malloc.h>
 8 
 9 #define STU_LEN sizeof(StuInfo)
10 #define NODE_LEN sizeof(Node)
11 
12 /****定义学生信息******/
13 
14 typedef struct studentInfo
15 {
16     int ID;
17     char name[16];
18     char password[16];
19     int age;
20     int classes;
21     char sex;
22     float math;
23     float chinese;
24     float clanguage;
25 }StuInfo;
26 
27 
28 /*定义链表结构*/
29 typedef struct studentNode
30 {
31     struct studentInfo data;
32     struct studentNode *pNext;
33 }Node;
34 
35 /*创建链表结构并且申请空间*/
36 Node* makeNode();
37 
38 /*学生信息初始化*/
39 StuInfo initData();
40 
41 /*头插法*/
42 void headInsert(Node *pHead);
43 
44 /*循环处理,将节点的数据域写入文件*/
45 void writeToFile(Node *pHead);
46 
47 /*遍历链表*/
48 void showList(Node *pHead);
49 
50 /*从文件中读取链表信息*/
51 Node* readFromFile(Node *pHead);
52 
53 /*查找学生*/ 
54 Node* findnode(Node *pHead, const int ID);
55 
56 /*录入查重*/
57 Node* findstu(Node *pHead, const int ID);
58 
59 /*按班级遍历链表*/
60 void showClassList(Node *pHead, int classes);
61 
62 /*按总成绩显示排序*/
63 void showClassSortList(Node *pHead, int classes);
64 
65 /*修改学生信息初始化*/
66 StuInfo modinitData();
67 
68 #endif
View Code

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 < 2; i++)
//    {
//        printf("=====第 %d 个学生的信息=====\n",i+1);
//        headInsert(pHead);
//    }
//    writeToFile(pHead);
    readFromFile(pHead);
    showList(pHead);
}*/

/*创建链表节点*/
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;
}
View Code

6、教师相关函数类似,直接放代码

teacher.h

 1 #ifndef __TEACHER_H_
 2 #define __TEACHER_H_
 3 /*teacher.h*/
 4 
 5 #include <stdlib.h>
 6 #include <stdio.h>
 7 #include <string.h>
 8 #include <malloc.h>
 9 
10 
11 /****定义教师信息******/
12 
13 typedef struct teacherInfo
14 {
15     int ID;//教师工号
16     char name[16];
17     char password[16];
18     int age;
19     int classes;
20     char sex;
21     char position[16]; //班主任 head, 数学math 语文 chinese, C语言 clanguage;
22 }TeaInfo;
23 
24 
25 /*定义教师链表结构*/
26 typedef struct teacherNode
27 {
28     struct teacherInfo data;
29     struct teacherNode *pNext;
30 }TeaNode;
31 
32 
33 
34 #define TEA_LEN sizeof(TeaInfo)
35 #define TEANODE_LEN sizeof(TeaNode)
36 
37 /*构建节点*/
38 TeaNode* makeTeaNode();
39 
40 /*初始化教师信息*/
41 TeaInfo initTeaData();
42 
43 /*教师头插法*/
44 void teaheadInsert(TeaNode *pHead);
45 
46 /*教师信息保存*/
47 void writeToTeaFile(TeaNode *pHead);
48 
49 /*教师信息遍历*/
50 void showTeaList(TeaNode *pHead);
51 
52 /*从文件中读取教师信息*/
53 TeaNode* readFromTeaFile(TeaNode *pHead);
54 
55 /*查找教师*/
56 TeaNode* findteanode(TeaNode *pHead, const int ID);
57 
58 /*录入查重*/
59 TeaNode* findtea(TeaNode *pHead, const int ID);
60 
61 /*修改初始化调用*/
62 TeaInfo modinitTeaData();
63 
64 #endif
View Code

teacher.c

  1 /*teacher.c*/
  2 
  3 
  4 #include "teacher.h"
  5 
  6 #define TEA_LEN sizeof(TeaInfo)
  7 #define TEANODE_LEN sizeof(TeaNode)
  8 
  9 
 10 /*void main()
 11 {
 12     TeaNode *pHead = makeTeaNode();
 13     //for(int i = 0; i < 2; i++)
 14     //{
 15     //    printf("=====第 %d 个教师的信息=====\n",i+1);
 16     //    teaheadInsert(pHead);
 17     //}
 18     //writeToTeaFile(pHead);
 19     pHead = readFromTeaFile(pHead);
 20     showTeaList(pHead);
 21 }*/
 22 
 23 /*申请空间*/
 24 TeaNode* makeTeaNode()
 25 {
 26     TeaNode *newnode = (TeaNode *)malloc(TEANODE_LEN);
 27     if(NULL == newnode)
 28     {
 29         newnode = (TeaNode *)malloc(TEANODE_LEN);
 30     }
 31     memset(&newnode->data, '\0', TEA_LEN);
 32     newnode->pNext = NULL;
 33     return newnode;
 34 }
 35 
 36 
 37 /*教师信息初始化*/
 38 TeaInfo initTeaData()
 39 {
 40     TeaInfo userInfo;
 41     TeaNode *temp = makeTeaNode();
 42     TeaNode *pTHead = makeTeaNode();
 43     pTHead = readFromTeaFile(pTHead);
 44     memset(&userInfo, '\0', TEA_LEN);
 45     printf("\n\t\t\t➢ 请输入工号:");
 46     scanf("%d",&userInfo.ID);
 47     getchar();
 48     temp = findtea(pTHead,userInfo.ID);
 49     while(temp != NULL)
 50     {
 51         printf("\t\t\t\033[31m\033[1m工号重复,请重新录入\033[0m\033[33m\n");
 52         printf("\n\t\t\t➢ 请输入工号:");
 53         scanf("%d",&userInfo.ID);
 54         getchar();
 55         temp = findtea(pTHead,userInfo.ID);
 56     }
 57     printf("\n\t\t\t➢ 请输入姓名:");
 58     scanf("%s",userInfo.name);
 59     getchar();
 60     strncpy(userInfo.password,"000000",6);//初始密码为000000
 61     printf("\n\t\t\t➢ 请输入年龄:");
 62     scanf("%d",&userInfo.age);
 63     getchar();
 64     printf("\n\t\t\t➢ 请输入班级:");
 65     scanf("%d",&userInfo.classes);
 66     getchar();
 67     printf("\n\t\t\t➢ 请输入性别:");
 68     scanf("%c",&userInfo.sex);
 69     getchar();
 70     printf("\n\t\t\t➢ 请输入职位:");
 71     scanf("%s",userInfo.position);
 72     getchar();
 73     return userInfo;
 74 }
 75 
 76 /*修改初始化调用*/
 77 TeaInfo modinitTeaData()
 78 {
 79     TeaInfo userInfo;
 80     memset(&userInfo, '\0', TEA_LEN);
 81     printf("\t\t\t➢ 请输入工号:");
 82     scanf("%d",&userInfo.ID);
 83     getchar();
 84     printf("\t\t\t➢ 请输入姓名:");
 85     scanf("%s",userInfo.name);
 86     getchar();
 87     strncpy(userInfo.password,"000000",6);//初始密码为000000
 88     printf("\t\t\t➢ 请输入年龄:");
 89     scanf("%d",&userInfo.age);
 90     getchar();
 91     printf("\t\t\t➢ 请输入班级:");
 92     scanf("%d",&userInfo.classes);
 93     getchar();
 94     printf("\t\t\t➢ 请输入性别:");
 95     scanf("%c",&userInfo.sex);
 96     getchar();
 97     printf("\t\t\t➢ 请输入职位:");
 98     scanf("%s",userInfo.position);
 99     getchar();
100     return userInfo;
101 }
102 
103 /*教师头插法*/
104 void teaheadInsert(TeaNode *pHead)
105 {
106     if(NULL == pHead)
107     {
108         perror("the  teacher list head is NULL!\n");
109     }
110     
111     TeaNode *newnode = makeTeaNode();
112     newnode->data = initTeaData();
113     newnode->pNext = pHead->pNext;
114     pHead->pNext = newnode;
115     newnode = NULL;
116 }
117 
118 /*遍历链表*/
119 void showTeaList(TeaNode *pHead)
120 {
121     if(NULL == pHead || NULL == pHead->pNext)
122     {
123         perror("the teacher list is empty!");
124         return;
125     }
126     TeaNode *temp = pHead->pNext;
127     int i = 1;
128     printf("-- ID -- |---- name ----|-- password --|"); 
129     printf("-age-|classes|sex|--position--|\n");
130     while(temp != NULL)
131     {
132         printf("| %-6d | %-12s | %-12s |",
133                 temp->data.ID, temp->data.name, temp->data.password);
134         printf(" %-3d |  %-3d  |%-3c| %-10s |\n",
135                 temp->data.age,temp->data.classes, temp->data.sex, temp->data.position);
136         i++;
137         temp = temp->pNext;
138     }
139     temp = NULL;
140     return;
141 }
142 
143 //循环处理,将节点的数据域写入文件
144 void writeToTeaFile(TeaNode *pHead)
145 {
146     if(NULL == pHead || NULL == pHead->pNext)
147     {
148         perror("NO teacher data to write...\n");
149         return;
150     }
151     FILE *fpw = fopen("teacher.txt", "w");
152     if(NULL == fpw)
153     {
154         perror("open file teacher.txt failed!\n");
155         return;
156     }
157 
158     TeaNode *temp = pHead->pNext;
159     while(NULL != temp)
160     {
161         fwrite(&temp->data, TEA_LEN, 1, fpw);
162         temp = temp->pNext;
163     }
164     system("clear");
165     puts("========文件保存成功!==========");
166     fclose(fpw);
167     return;
168 }
169 
170 //链表的读文件
171 TeaNode* readFromTeaFile(TeaNode *pHead)
172 {
173     if(NULL == pHead)
174     {
175         perror("the  teacher head is NULL!");
176         return NULL;
177     }
178 
179     FILE *fpr = fopen("teacher.txt", "r");
180     if(NULL == fpr)
181     {
182         FILE *fpr = fopen("teacher.txt", "w");
183     }
184     
185     TeaNode *newnode = NULL;
186     TeaInfo teaInfo;
187     memset(&teaInfo, '\0', TEA_LEN);
188     while(fread(&teaInfo, TEA_LEN, 1, fpr) > 0)
189     {
190         newnode = makeTeaNode();
191         newnode->data = teaInfo;
192         newnode->pNext = pHead->pNext;
193         pHead->pNext = newnode;
194     }
195     fclose(fpr);
196     return pHead;
197 }
198 
199 /*查找教师*/
200 TeaNode* findteanode(TeaNode *pHead, const int ID)
201 {
202     TeaNode *p = makeTeaNode();
203     p = pHead;
204     while(p != NULL)
205     {
206         if(ID == p->data.ID)
207         break;
208         p = p->pNext;
209     }
210     if(p == NULL)
211     {
212         printf("ID输入错误或此教师信息未录入\n");
213     }
214     return p;
215 }
216 
217 /*录入查重*/
218 TeaNode* findtea(TeaNode *pHead, const int ID)
219 {
220     TeaNode *p = makeTeaNode();
221     p = pHead;
222     while(p != NULL)
223     {
224         if(ID == p->data.ID)
225         break;
226         p = p->pNext;
227     }
228     return p;
229 }
View Code

所有执行模块函数在下一部分记录。。。。。

猜你喜欢

转载自www.cnblogs.com/lttdxuexizhilu/p/11617811.html
今日推荐