宿舍管理系统

利用控制台实现宿舍人员管理;

包括多种功能,查找,插入,删除,排序,打印等。

  1 #include "stdafx.h"
  2 #include"stdio.h" 
  3 #include"string.h"  
  4 #include"stdlib.h" 
  5 #include"conio.h"                                                 /*头文件*/  
  6 #define MAX 100 
  7 
  8 typedef struct  {
  9     int year;
 10     int month;
 11     int day;
 12 }DATE;
 13 typedef struct   {
 14     int num;                   /*学号*/
 15     char name[20];             /*姓名*/                               /*定义结构体*/
 16     char sex[6];                  /*性别*/
 17     int age;                   /*年龄*/
 18     DATE birthday;             /*出生年月*/
 19     char sushe[30];          /*宿舍号*/
 20     char zhuanye[50];          /*专业*/
 21     char classes[10];          /*班级*/
 22     char chuanghao[50];           /*床号*/
 23 }STUDENTS;   /*读取信息*/
 24 
 25 int read_file(STUDENTS students[]) {
 26     FILE *fp;
 27     int  i = 0;
 28     if ((fp = fopen("stu.txt", "rt")) == NULL)   {
 29         printf("\n\n*****库存文件不存在!请创建");                    /*创建文件*/
 30         return 0;
 31     }
 32     while ((fread(&students[i], sizeof(STUDENTS), 1, fp)) == 1)                /*成功创建*/
 33     {
 34         i++;
 35     }
 36     fclose(fp);
 37     return i;
 38 }  /*保存信息*/
 39 void save_file(STUDENTS students[], int sum) {
 40     FILE  *fp;
 41     int i;
 42     if ((fp = fopen("stu.txt", "wb")) == NULL)    {
 43         printf("读文件错误!\n");
 44         return;
 45     }
 46     for (i = 0; i<sum; i++)
 47         if (fwrite(&students[i], sizeof(STUDENTS), 1, fp) != 1)
 48             printf("写文件错误!\n");                       /*学生信息保存到文件中*/
 49     fclose(fp);
 50 }  /*输入模块*/
 51 int input(STUDENTS students[])    {
 52     int i = 0, t;
 53     /*定义要输入学生的个数*/
 54     system("cls");
 55     printf("\n\n        录入学生个数(最多%d个)", MAX);
 56     scanf("%d", &t);
 57     printf("        ------------------------\n");
 58     do    {  // printf("\n       第%d个学生信息",i+1);   /*输入学生的基本信息*/   
 59         printf("\n       第%d个学生的学号", i + 1);    scanf("%d", &students[i].num);
 60         if (students[i].num == 0)
 61             break;
 62         printf("\n       学生姓名:");
 63         scanf("%s", students[i].name);
 64         printf("\n       学生性别:");
 65         scanf("%s", students[i].sex);
 66         printf("\n       学生年龄:");
 67         scanf("%d", &students[i].age);
 68         printf("\n       学生出生年月日:");
 69         scanf("%d %d %d", &students[i].birthday.year, &students[i].birthday.month, &students[i].birthday.day);
 70         printf("\n       学生宿舍:");
 71         scanf("%s", students[i].sushe);
 72         printf("\n       学生专业:");
 73         scanf("%s", students[i].zhuanye);
 74         printf("\n       学生班级:");
 75         scanf("%s", students[i].classes);
 76         printf("\n       学生床号:");
 77         scanf("%s", students[i].chuanghao);
 78         i++;
 79     } while (i<t);
 80     save_file(students, t);                                    /*保存输入的学生信息*/
 81     printf("\n          %d个学生信息输入完毕!   \n", t);
 82     _getch();
 83     return t;                                                  /*返回输入学生的个数值*/
 84 }   /*输出模块*/
 85 void output(STUDENTS students[], int sum)   {
 86     int i;
 87     system("cls");
 88     printf("num    name    sex  age   year-month-day   sushe   zhuanye   classes   chuanghao\n");
 89     printf("                \n");
 90     for (i = 0; i<sum; i++)                                                                 /*输出学生的信息*/
 91     {
 92         printf("%5d  %5s  %5s  %5d  %5d %5d %5d  %5s  %5s  %5s  %5s\n",
 93             students[i].num, students[i].name, students[i].sex, students[i].age, students[i].birthday.year,
 94             students[i].birthday.month, students[i].birthday.day, students[i].sushe,
 95             students[i].zhuanye, students[i].classes, students[i].chuanghao);
 96         printf("                                       \n");
 97     }
 98     _getch();
 99     save_file(students, sum);
100 }  /*添加信息模块*/
101 int append(STUDENTS  students[], int sum) {
102     int i = sum, flag = 0;                               /*从已经输入的学生尾部添加*/
103     char choice;
104     sum = read_file(students);
105     system("cls");   printf("\n\n     添加学生信息     \n");
106     do  {
107         if (i<MAX)/*已经输入的学生个数和要添加的学生个数之和要小于定义的学生个数最大值*/
108         {
109             printf("\n       新学生学号:");
110             scanf("%d", &students[i].num);
111             printf("\n       学生姓名:");
112             scanf("%s", students[i].name);
113             printf("\n       学生性别:");            /*输入添加学生的基本信息*/
114             scanf("%s", students[i].sex);
115             printf("\n       学生年龄:");
116             scanf("%d", &students[i].age);
117             printf("\n       学生出生年月日:");
118             scanf("%d %d %d", &students[i].birthday.year, &students[i].birthday.month, &students[i].birthday.day);
119             printf("\n       学生宿舍号:");
120             scanf("%s", students[i].sushe);
121             printf("\n       学生专业:");
122             scanf("%s", students[i].zhuanye);
123             printf("\n       学生班级:");
124             scanf("%s", students[i].classes);
125             printf("\n       床号:");
126             scanf("%s", students[i].chuanghao);
127             i++;
128         }
129         printf("\n        继续添加吗?(Y/N)");
130         choice = _getch();
131         if (choice == 'Y' || choice == 'y')    {
132             flag = 1;
133             printf("\n      继续!\n");                     /*是否要继续添加*/
134         }
135         else flag = 0;
136     } while (flag == 1);
137     printf("\n              按任意键返回主菜单!");
138     _getch();
139     save_file(students, i);                             /*保存添加的学生信息到原文件*/
140     return i;                                          /*返回文件中学生个数值*/
141 }  /*修改信息模块*/
142 void modify(STUDENTS  students[], int sum) {    //
143     FILE *fp;
144     int i, choice, flag, modify_num;
145     sum = read_file(students);                                             /*读文件*/
146     do    {
147         system("cls");
148         printf("\n            输入要修改的学生学号:");
149         scanf("%d", &modify_num);                                /*输入要修改的学生学号*/
150         for (i = 0; i<sum; i++)
151             if (students[i].num == modify_num)       {
152                 printf("\n             学生信息       \n");
153                 printf("num      name      sex  age  year-month-day  sushe  zhuanye  class  chuanghao\n");
154                 printf("                                          \n");
155                 printf("%5d  %5s  %5s  %5d  %5d %5d %5d  %5s  %5s  %5s  %5s\n",
156                     students[i].num, students[i].name, students[i].sex, students[i].age, students[i].birthday.year, students[i].birthday.month,
157                     students[i].birthday.day, students[i].sushe, students[i].zhuanye, students[i].classes, students[i].chuanghao);
158                 printf("\n                要修改哪一项?\n");
159                 printf("\n                1.学生学号 \n");
160                 printf("\n                2.学生姓名 \n");
161                 printf("\n                3.学生性别 \n");
162                 printf("\n                4.学生年龄 \n");
163                 printf("\n                5.学生出生年月日 \n");
164                 printf("\n                6.学生宿舍号 \n");
165                 printf("\n                7.学生专业 \n");
166                 printf("\n                8.学生班级 \n");
167                 printf("\n                9.学生床号 \n");
168                 printf("\n                请选择(1-9):");
169                 scanf("%d", &choice);
170                 switch (choice)    {
171                 case 1:printf("\n         输入修改后的学号:");
172                     scanf("%d", &students[i].num);  break;
173                 case 2:printf("\n         输入修改后的姓名:");
174                     scanf("%s", students[i].name);  break;
175                 case 3:printf("\n         输入修改后的性别:");
176                     scanf("%s", students[i].sex);   break;           /*选择要修改学生的项目*/
177                 case 4:printf("\n         输入修改后的年龄:");
178                     scanf("%d", &students[i].age);  break;
179                 case 5:printf("\n         输入修改后的出生年月:");
180                     scanf("%d %d %d", &students[i].birthday.year, &students[i].birthday.month, &students[i].birthday.day);
181                     break;
182                 case 6:printf("\n         输入修改后的宿舍号:");
183                     scanf("%s", students[i].sushe);   break;
184                 case 7:printf("\n         输入修改后的专业:");
185                     scanf("%s", students[i].zhuanye);   break;
186                 case 8:printf("\n         输入修改后的班级:");
187                     scanf("%s", students[i].classes);   break;
188                 case 9:printf("\n         输入修改后的床号:");
189                     scanf("%s", students[i].chuanghao);   break;
190                 }
191                 printf("\n          学生信息表      \n");
192                 printf("num      name      sex  age  year-month-day  sushe  zhuanye  classes  chuanghao\n");
193                 printf("                                          \n");
194                 printf("%5d  %5s  %5s  %5d  %5d %5d %5d  %5s  %5s  %5s  %5s\n",
195                     students[i].num, students[i].name, students[i].sex, students[i].age, students[i].birthday.year,
196                     students[i].birthday.month, students[i].birthday.day, students[i].sushe,
197                     students[i].zhuanye, students[i].classes, students[i].chuanghao);
198                 break;                 /*输出修改后的学生信息*/
199             }
200         if (i == sum)     {
201             printf("\n           该学生不存在!");
202             _getch();
203         }
204         printf("\n\n            继续修改吗?(Y/N)");
205         choice = _getch();
206         if (choice == 'Y' || choice == 'y')     {
207             flag = 1;
208             printf("\n          继续!\n");
209         }                                                                              /*是否要继续修改*/
210         else flag = 0;
211     } while (flag == 1);
212     printf("\n               按任意键返回主菜单!");
213     _getch();
214     save_file(students, sum);                                /*保存修改后的信息*/
215 }       /*删除信息模块*/
216 int del(STUDENTS students[], int sum)  {
217     int i, del_num;
218     printf("请输入要删除的学生学号");
219     scanf("%d", &del_num);                           /*输入要删除的学生学号*/
220     for (i = 0; i<sum; i++)     {
221         if (del_num == students[i].num)       {
222             for (; i<sum - 1; i++)
223                 students[i].num = students[i + 1].num;
224             printf("删除成功");
225         }
226     }
227     _getch();
228     save_file(students, sum - 1);            /*保存删除后的学生信息到原文件*/
229     return (sum - 1);
230 }
231 /*查询信息模块*/
232 void inquire(STUDENTS students[], int sum)  {
233     FILE *fp;
234     int i, low=0,mid,high,choice, flag, choice2, inquire_num;
235     char inquire_name[20],inquire_chuanghao[50];
236     sum = read_file(students);                              /*读文件*/
237     printf("%d\n", sum);
238     printf("\n                1.按姓名查询 \n");
239     printf("\n                2.按学号查询 \n");
240     printf("\n                3.按床号查询 \n");
241     printf("\n                请选择(1-3):");
242     scanf("%d", &choice2);
243     switch (choice2)    {
244     case 1:
245         do      {
246             system("cls");
247             printf("\n            输入要查询的学生姓名:");
248             scanf("%s", &inquire_name);                                /*输入要查询的学生姓名*/
249             for (i = 0; i < sum; i++)
250             if (strcmp(students[i].name,inquire_name)==0)      {
251                 printf("\n             此学生信息为:       \n");
252                 printf("num      name      sex  age  year-month-day  sushe  zhuanye  classes  chuanghao\n");
253                 printf("                                          \n");
254                 printf("%5d  %5s  %5s  %5d  %d%d%d  %5s  %5s  %5s  %5s\n",
255                     students[i].num, students[i].name, students[i].sex, students[i].age, students[i].birthday.year,
256                     students[i].birthday.month, students[i].birthday.day, students[i].sushe,
257                     students[i].zhuanye, students[i].classes, students[i].chuanghao);
258                 break;                                     /*输出要查询的学生信息*/
259             }
260             if (i == sum)      {
261                 printf("\n    无此学生信息    \n");
262                 _getch();
263             }
264             printf("\n\n      是否继续?(Y/N)");
265             choice = _getch();
266             if (choice == 'Y' || choice == 'y')      {
267                 flag = 1;
268                 printf("\n      继续!\n");                  /*是否要继续*/
269             }
270             else flag = 0;
271         } while (flag == 1);     break;
272 
273     case 2:
274         do      {
275             system("cls");
276             printf("\n            输入要查询的学生学号:");
277             scanf("%d", &inquire_num);   /*输入要查询的学生学号*/
278             high = sum - 1;
279             while (low <= high){
280                 mid = (low + high) / 2;
281                 if (students[mid].num < inquire_num){ low = mid + 1; }
282                 else if (students[mid].num > inquire_num) { high = mid - 1; }
283                 else { i = mid; break; }
284             }
285                 printf("\n             此学生信息为:       \n");
286                 printf("num      name      sex  age  year-month-day  sushe  zhuanye  classes  chuanghao\n");
287                 printf("                                          \n");
288                 printf("%5d  %5s  %5s  %5d  %d%d%d  %5s  %5s  %5s  %5s\n",
289                     students[i].num, students[i].name, students[i].sex, students[i].age, students[i].birthday.year,
290                     students[i].birthday.month, students[i].birthday.day, students[i].sushe,
291                     students[i].zhuanye, students[i].classes, students[i].chuanghao);
292                 break;                                     /*输出要查询的学生信息*/
293             
294             if (i == sum)      {
295                 printf("\n    无此学生信息    \n");
296                 _getch();
297             }
298             printf("\n\n      是否继续?(Y/N)");
299             choice = _getch();
300             if (choice == 'Y' || choice == 'y')      {
301                 flag = 1;
302                 printf("\n      继续!\n");                  /*是否要继续*/
303             }
304             else flag = 0;
305         } while (flag == 1);     break;
306     case 3:
307         do      {
308             system("cls");
309             printf("\n            输入要查询的学生床号:");
310             scanf("%s", &inquire_chuanghao);                                /*输入要查询的学生床号*/
311             for (i = 0; i < sum; i++)
312             if (strcmp(students[i].chuanghao,inquire_chuanghao)==0)      {
313                 printf("\n             此学生信息为:       \n");
314                 printf("num      name      sex  age  year-month-day  sushe  zhuanye  classes  chuanghao\n");
315                 printf("                                          \n");
316                 printf("%5d  %5s  %5s  %5d  %d%d%d  %5s  %5s  %5s  %5s\n",
317                     students[i].num, students[i].name, students[i].sex, students[i].age, students[i].birthday.year,
318                     students[i].birthday.month, students[i].birthday.day, students[i].sushe,
319                     students[i].zhuanye, students[i].classes, students[i].chuanghao);
320                 break;                                     /*输出要查询的学生信息*/
321             }
322             if (i == sum)      {
323                 printf("\n    无此学生信息    \n");
324                 _getch();
325             }
326             printf("\n\n      是否继续?(Y/N)");
327             choice = _getch();
328             if (choice == 'Y' || choice == 'y')      {
329                 flag = 1;
330                 printf("\n      继续!\n");                  /*是否要继续*/
331             }
332             else flag = 0;
333         } while (flag == 1);     break;
334     }
335 
336 
337     printf("\n      按任意键返回主菜单!");
338     _getch();
339 }  /*统计信息模块*/
340 void count(STUDENTS  students[], int sum)
341 {
342     int i, count = 0;
343     char s[10];
344     printf("请输入要统计的班级的学生的班级号!");
345     scanf("%s", s);                               /*按输入的班级号统计此班的总学生人数*/
346     for (i = 0; i<sum; i++)
347         if (strcmp(s, students[i].classes) == 0)
348             count++;
349     printf("%s班的总人数为%d人", s, count);
350     _getch();         save_file(students, sum);
351 }   /*排行信息模块*/
352 void  sort(STUDENTS  students[], int sum)  {
353     int i, j, t;
354     system("cls");
355     printf("\n        学生信息排行\n");
356     printf("\n        按学号排行\n");
357     printf("                      \n");
358     for (i = 0; i<sum - 1; i++)                          /*按学号用冒泡法升序排列学生信息*/
359         for (j = i + 1; j<sum; j++)    {
360             if (students[i].num>students[j].num)        {
361                 students[10] = students[i];
362                 students[i]= students[j];
363                 students[j] =students[10];            }
364         }
365     printf("                      \n");
366     printf("num      name      sex  age  year-month-day  sushe  zhuanye  classes  chuanghao\n");
367     for (i = 0; i<sum; i++)       printf("%5d  %5s  %5s  %5d  %d%d%d  %5s  %5s  %5s  %5s\n",
368         students[i].num, students[i].name, students[i].sex, students[i].age, students[i].birthday.year, students[i].birthday.month,
369         students[i].birthday.day, students[i].sushe, students[i].zhuanye, students[i].classes, students[i].chuanghao);     /*输出排序后的学生信息*/
370     _getch();
371     save_file(students, sum);                           //保存排序后的学生信息到文件中
372 }
373 
374 int main()                                                            /*主函数*/
375 {
376     STUDENTS students[MAX];
377     int choice, sum;
378     sum = read_file(students);
379     if (!sum)   {
380         printf("录入基本库存信息!\n");
381         _getch();
382         sum = input(students);                                     /*录入学生信息*/
383     }
384     do     {
385         system("cls");
386         printf("\n\n\n        学生宿舍管理系统            \n\n");
387         printf("              1.添加学生信息\n\n");
388         printf("              2.修改学生信息\n\n");
389         printf("              3.删除学生信息\n\n");
390         printf("              4.查询学生信息\n\n");
391         printf("              5.统计学生信息\n\n");
392         printf("              6.学生排序\n\n");
393         printf("              7.打印学生信息\n\n");
394         printf("              0.退出系统\n\n");
395         printf("                请选择(0-7):");
396         scanf("%d", &choice);
397         switch (choice)                          /*用switch选择要调用的各模块函数并执行*/
398         {
399         case 1: sum = append(students, sum); break;
400         case 2: modify(students, sum); break;
401         case 3: sum = del(students, sum); break;
402         case 4: inquire(students, sum); break;
403         case 5: count(students, sum); break;
404         case 6:sort(students, sum);   break;
405         case 7:output(students, sum); break;
406         case 0:                     break;          /*执行完各模块后按0退出系统*/
407         }
408     } while (choice != 0);
409     save_file(students, sum);                      /*保存执行完各模块后的学生信息*/
410 }
View Code

 

posted @ 2015-09-07 19:01  Wei_java  阅读(691)  评论(1编辑  收藏  举报