C语言综合实验1—学生信息管理系统

实验题目:学生信息管理系统

实验要求:用户可以选择1-7可以分别进行学生信息的查看、添加、删除,修改,计算平均成绩,保存,退出系统操作。

提示:用一个结构体类型表示学生信息

 1 typedef struct node /*定义结构体*/
 2 { 
 3     int num; //学号
 4     char name[15];//姓名
 5     char sex[9]; //性别
 6     int age;  //年龄
 7     int english; //英语成绩
 8     int math; //数学成绩
 9     int computer;//计算机成绩
10     int average; //平均成绩
11     struct node *next; //链表指针域
12 }student_info;

运行过程中采用一个结构体链表存储学生信息。退出系统时可以采用文件存储用户输入的信息,再次运行的时候从文件中读取。

完整C语言程序分为三个文件,student.h、student.cpp和main.c文件。具体实现如下所示:

student.h文件中定义学生信息结构体,操作函数声明。

 1 #ifndef STUDENT_HEAD
 2 #define STUDENT_HEAD
 3 
 4 typedef struct node /*定义结构体*/
 5 { 
 6     int num; //学号
 7     char name[15];//姓名
 8     char sex[9]; //性别
 9     int age;  //年龄
10     int english; //英语成绩
11     int math; //数学成绩
12     int computer;//计算机成绩
13     int average; //平均成绩
14     struct node *next; //链表指针域
15 }student_info;
16 
17 //学生信息链表
18 extern student_info* student_list;  //全局变量声明
19 
20 
21 //初始化函数声明
22 //初始化学生信息链表
23 void init_student_info_list();
24 //判断学生信息链表是否为空
25 int student_list_empty();
26 
27 //操作函数声明
28 //向学校信息表中添加学生信息记录
29 int add_student_info();
30 //根据学号删除学生信息
31 int delete_student_info(int num);
32 //根据学号修改学生信息
33 int modify_student_info(int num);
34 //根据学号查找学生信息
35 student_info* search_student_info(int num);
36 //输出每个学生的平均成绩
37 void display_average();
38 //显示所有学生信息
39 void display_student_info();
40 //将学生信息保存到文件
41 int save_file();
42 //从文件中读取学生信息
43 int read_file();
44 #endif

student.c文件中是对student.h文件中声明的函数进行定义,给出具体的实现代码。

  1 #include "student.h"
  2 #include <stdio.h>
  3 #include <string.h>
  4 #include <malloc.h>
  5 
  6 //初始化学生信息链表
  7 void init_student_info_list()
  8 {
  9     //学生信息链表头结点
 10     student_list = (student_info*)malloc(sizeof(student_info));
 11     student_list->next = NULL;
 12 }
 13 //判断学生信息链表是否为空
 14 int student_list_empty()
 15 {
 16     return student_list->next == NULL;
 17 }
 18 //操作函数实现
 19 //向学校信息表中添加学生信息记录
 20 int add_student_info()
 21 {
 22     student_info *pstu = (student_info*)malloc(sizeof(student_info));
 23     if(pstu == NULL)
 24     {
 25         printf("内存分配失败.\n");
 26         return 0;
 27     }
 28     printf("请按要求一次输入学生的信息.\n");
 29     printf("请输入学号: ");
 30     scanf("%d",&pstu->num);
 31     //判断该学号是否已经存在
 32     if(search_student_info(pstu->num) != NULL)
 33     {
 34         printf("该学号已经存在学生信息表中.\n");
 35         return 0;
 36     }
 37     printf("请输入姓名: ");
 38     getchar();
 39     gets(pstu->name);
 40     printf("请输入性别: ");
 41     scanf("%s",pstu->sex);
 42     printf("请输入年龄: ");
 43     scanf("%d",&pstu->age);
 44     printf("请输入英语成绩: ");
 45     scanf("%d",&pstu->english);
 46     printf("请输入数学成绩: ");
 47     scanf("%d",&pstu->math);
 48     printf("请输入计算机成绩: ");
 49     scanf("%d",&pstu->computer);
 50     pstu->average = (pstu->english + pstu->math + pstu->computer)/3;
 51     //每次从学生信息链表的头部插入;
 52     pstu->next = student_list->next;
 53     student_list->next = pstu;
 54     return 1;
 55 }
 56 //根据学号删除学生信息
 57 int delete_student_info(int num)
 58 {
 59     student_info *pstu;
 60     student_info *qstu;
 61     if(search_student_info(num) == NULL)
 62     {
 63         printf("不存在该学好为%d的学生信息.\n",num);
 64         return 0;
 65     }
 66     pstu = student_list->next;
 67     qstu = student_list;
 68     while(pstu->num != num)
 69     {
 70         qstu = pstu;
 71         pstu = pstu->next;
 72     }
 73     qstu->next = pstu->next;
 74     free(pstu);
 75     return 1;
 76 }
 77 //根据学号修改学生信息
 78 int modify_student_info(int num)
 79 {
 80     int choice;
 81     student_info *pstu = search_student_info(num);
 82     if(pstu == NULL)
 83     {
 84         printf("不存在该学好为%d的学生信息.\n",num);
 85         return 0;
 86     }
 87     printf("1.姓名 2.性别 3.年龄 4.英语成绩 5.数学成绩 6.计算机成绩.\n");
 88     printf("请选择修改的信息: ");
 89     scanf("%d",&choice);
 90     switch(choice)
 91     {
 92     case 1:
 93         printf("请输入新的姓名: ");
 94         getchar();
 95         gets(pstu->name);
 96         break;
 97     case 2:
 98         printf("请输入新的性别: ");
 99         scanf("%s",pstu->sex);
100         break;
101     case 3:
102         printf("请输入新的年龄: ");
103         scanf("%d",&pstu->age);
104         break;
105     case 4:
106         printf("请输入新的英语成绩: ");
107         scanf("%d",&pstu->english);
108         break;
109     case 5:
110         printf("请输入新的数学成绩: ");
111         scanf("%d",&pstu->math);
112         break;
113     case 6:
114         printf("请输入新的计算机成绩: ");
115         scanf("%d",&pstu->computer);
116         break;
117     default:
118         printf("请按提示要求操作.\n");
119     }
120     return 1;
121 }
122 //根据学号查找学生信息
123 student_info* search_student_info(int num)
124 {
125     student_info *pstu;
126     pstu = student_list->next;
127     while(pstu  && pstu->num != num)
128     {
129 
130         pstu = pstu->next;
131     }
132     return pstu;
133 }
134 //输出每个学生的平均成绩
135 void display_average()
136 {
137     student_info *pstu;
138     pstu = student_list->next;
139     while(pstu)
140     {
141         printf("学号为%d,姓名为%s的学生平均成绩为: %d\n",pstu->num,pstu->name,pstu->average);
142         pstu = pstu->next;
143     }
144 }
145 //显示所有学生信息
146 void display_student_info()
147 {
148     student_info *pstu;
149     pstu = student_list->next;
150     printf("所有学生信息如下所示.\n");
151     printf("学号\t姓名\t性别\t年龄\t英语\t数学\t计算机\t平均成绩.\n");
152     while(pstu)
153     {
154         printf("%d\t",pstu->num);
155         printf("%s\t",pstu->name);
156         printf("%s\t",pstu->sex);
157         printf("%d  \t",pstu->age);
158         printf("%d   \t",pstu->english);
159         printf("%d  \t",pstu->math);
160         printf("%d    \t",pstu->computer);
161         printf("%d\n",pstu->average);
162         pstu = pstu->next;
163     }
164 }
165 //将学生信息保存到文件
166 int save_file()
167 {
168     FILE *pfile;
169     student_info *pstu;
170     pfile = fopen("student.txt","w");
171     if(pfile == NULL)
172     {
173         printf("打开文件失败.\n");
174         return 0;
175     }
176     pstu = student_list->next;
177     while(pstu)
178     {
179         fprintf(pfile,"%5d%15s%9s%3d%4d%4d%4d%4d",pstu->num,pstu->name,pstu->sex,pstu->age,
180             pstu->english,pstu->math,pstu->computer,pstu->average);
181         pstu = pstu->next;
182     }
183     fclose(pfile);
184     return 1;
185 }
186 
187 //从文件中读取学生信息
188 int read_file()
189 {
190     FILE *pfile;
191     student_info *pstu;
192     pfile = fopen("student.txt","r");
193     if(pfile == NULL)
194     {
195         printf("打开文件失败.\n");
196         return 0;
197     }
198     while(!feof(pfile))
199     {
200         pstu = (student_info*)malloc(sizeof(student_info));
201         fscanf(pfile,"%5d%15s%9s%4d%4d%4d%4d%4d",&pstu->num,pstu->name,pstu->sex,&pstu->age,
202             &pstu->english,&pstu->math,&pstu->computer,&pstu->average);
203         pstu->average = (pstu->english + pstu->math + pstu->computer)/3;
204         //每次从学生信息链表的头部插入;
205         pstu->next = student_list->next;
206         student_list->next = pstu;
207     }
208     fclose(pfile);
209     return 1;
210 }

主函数main.c文件进行测试调用,如下所示:

  1 #include "student.h"
  2 #include <stdlib.h>
  3 #include <stdio.h>
  4 
  5 void menu();
  6 
  7 //学生信息链表
  8 student_info* student_list;
  9 
 10 //用户可以选择1-7可以分别进行学生信息的查看、添加、删除,修改,计算平均成绩,保存,退出系统操作。
 11 int main()
 12 {
 13     int choice;
 14     int num;
 15     printf("**************************\n");
 16     printf("欢迎使用学生信息管理系统\n");
 17     printf("**************************\n");
 18     printf("-----------------------------\n");
 19     init_student_info_list();
 20     if(read_file())
 21         printf("从文件中读取学生信息成功.\n");
 22     else
 23         printf("从文字中读取学生信息失败.\n");
 24     printf("-----------------------------\n");
 25     menu();
 26     while(1)
 27     {
 28         printf("请选择操作: ");
 29         scanf("%d",&choice);
 30         switch(choice)
 31         {
 32         case 1:
 33             if(student_list_empty())
 34                 printf("学生信息表为空,请先添加学生信息.\n");
 35             else
 36                 display_student_info();
 37             break;
 38         case 2:
 39             if(add_student_info())
 40                 printf("添加学生信息成功.\n");
 41             else
 42                 printf("添加学生信息失败.\n");
 43             break;
 44         case 3:
 45             if(student_list_empty())
 46                 printf("学生信息表为空,请先添加学生信息.\n");
 47             else
 48             {
 49                 printf("请输入要删除学生信息的学号: ");
 50                 scanf("%d",&num);
 51                 if(delete_student_info(num))
 52                     printf("成功删除该学号对应的学生信息.\n");
 53                 else
 54                     printf("删除失败.\n");
 55             }
 56             break;
 57         case 4:
 58             if(student_list_empty())
 59                 printf("学生信息表为空,请先添加学生信息.\n");
 60             else
 61             {
 62                 printf("请输入要修改学生信息的学号: ");
 63                 scanf("%d",&num);
 64                 if(modify_student_info(num))
 65                     printf("成功修改该学号对应的学生信息.\n");
 66                 else
 67                     printf("修改失败.\n");
 68             }
 69             break;
 70         case 5:
 71             if(student_list_empty())
 72                 printf("学生信息表为空,请先添加学生信息.\n");
 73             else
 74                 display_average();
 75             break;
 76         case 6:
 77             if(student_list_empty())
 78                 printf("学生信息表为空,请先添加学生信息.\n");
 79             else
 80                 if(save_file())
 81                     printf("保存学生信息成功.\n");
 82                 else
 83                     printf("保存学生信息失败.\n");
 84             break;
 85         case 0:
 86             printf("欢迎下次使用,再见.\n");
 87             system("pause");
 88             exit(0);
 89             break;
 90         default:
 91             printf("输入错误,请重新选择操作.\n");
 92         }
 93     }
 94     system("pause");
 95     return 0;
 96 }
 97 
 98 void menu()
 99 {
100     printf("1.查看学生信息.\n");
101     printf("2.添加学生信息.\n");
102     printf("3.删除学生信息.\n");
103     printf("4.修改学生信息.\n");
104     printf("5.输出平均成绩.\n");
105     printf("6.保存学生信息.\n");
106     printf("0.退出系统操作.\n");
107 }

程序执行结果如下所示:

(1)第一次执行没有学生信息,读取文件失败。

(2)以后执行先从文件中读取学生信息,然后进行操作。

posted @ 2013-05-06 19:46  Rabbit_Dale  阅读(7834)  评论(1编辑  收藏  举报