• 大作业 :学生信息管理系统。。。


          一个简单学生信息管理系统。

       功能 : 

          1. 可以读写文件内容

          2.可以显示学生信息(只写了id   name   chinese  math   ave  五项信息)

          3.可以删除学生信息

          4.可以添加学生信息

      1.头文件

     1 #ifndef _HEAD_H_
     2 #define _HEAD_H_
     3 
     4 #include <stdio.h>
     5 #include <string.h>
     6 #include <stdlib.h>
     7 #include <errno.h>
     8 
     9 #define MAX_NAME 37
    10 #define MAX 1024
    11 #define MAX_DATA  51
    12 
    13 enum hex{show = 1, sort, add, my_delete, quite = 520};
    14 
    15 typedef struct students
    16 {
    17     char name[MAX_NAME];
    18     int seg;
    19     int id;
    20     float chinese;
    21     float math;
    22     float ave;
    23     struct students *next;
    24 }stu_t;
    25 
    26 int read_file(stu_t *);
    27 int write_file(stu_t *);
    28 void show_student_data(stu_t *);
    29 void sort_ave_data(stu_t *);
    30 void add_student_data(stu_t *, int);
    31 void delete_student_data(stu_t *);
    32 
    33 #endif
    头文件

      2.main函数

     1 //main.c
     2 #include "head.h"
     3 
     4 int main(int argc, char *argv[])
     5 {
     6     stu_t *head;
     7     int choose_num = 0, add_num = 0;
     8 
     9     head = calloc(1, sizeof(stu_t));
    10 
    11     if(-1 == read_file(head)) {
    12         printf("------------------------------------------------------------------------------
    ");
    13         fprintf(stderr,"No File or File no data!
    ");
    14         printf("------------------------------------------------------------------------------
    ");
    15     }
    16     else {
    17         printf("Read success!
    ");
    18         printf("---------------------------------Write_list_data------------------------------
    ");
    19     }
    20 
    21     while(1)
    22     {
    23         printf("==============================================================================
    ");
    24         printf("1.Display all student's info
    "
    25                 "2.Sort by average
    "
    26                 "3.Insert a new info
    "
    27                 "4.Delete a record
    ");
    28         printf("==============================================================================
    ");
    29         printf("PLease input your choice : ");
    30         scanf("%d", &choose_num);
    31         getchar();
    32 
    33         switch(choose_num)
    34         {
    35             case quite :
    36                 if(-1 == write_file(head)) {
    37                     printf("------------------------------------------------------------------------------
    ");
    38                     fprintf(stderr, "Write_file fail %s : %d : %s.
    ", __FILE__, __LINE__, strerror(errno));
    39                     printf("------------------------------------------------------------------------------
    ");
    40                     return -1;
    41                 }
    42                 else if(0 == write_file(head)) {
    43                     printf("------------------------------------------------------------------------------
    ");
    44                     printf("Write_File success!
    
    ");
    45                 }
    46 
    47                 printf("Good Lucky!
    ");
    48                 printf("------------------------------------------------------------------------------
    ");
    49 
    50                 free(head);
    51                 head = NULL;
    52 
    53                 return 0;
    54                 break;
    55 
    56             case show :
    57                 show_student_data(head);
    58                 break;
    59 
    60             case sort :
    61                 printf("--------------------------Before sort_data------------------------------------
    ");
    62                 show_student_data(head);
    63                 printf("--------------------------Before sort_data------------------------------------
    
    ");
    64                 sort_ave_data(head);
    65                 printf("--------------------------After sort_data-------------------------------------
    ");
    66                 show_student_data(head);
    67                 printf("--------------------------After sort_data-------------------------------------
    
    ");
    68                 break;
    69 
    70             case add :
    71                 printf("Enter the student_num : ");
    72                 scanf("%d", &add_num);
    73                 getchar();
    74 
    75                 add_student_data(head, add_num);
    76                 printf("--------------------------After add_data--------------------------------------
    ");
    77                 show_student_data(head);
    78                 printf("--------------------------After add_data--------------------------------------
    ");
    79 
    80                 break;
    81 
    82             case my_delete :
    83                 printf("--------------------------Before add_data-------------------------------------
    ");
    84                 show_student_data(head);
    85                 printf("--------------------------Before  add_data------------------------------------
    
    ");
    86                 printf("------------------------------------------------------------------------------
    ");
    87                 delete_student_data(head);
    88                 break;
    89 
    90             default :
    91                 printf("------------------------------------------------------------------------------
    ");
    92                 printf("Enter error.......
    ");
    93                 printf("------------------------------------------------------------------------------
    ");
    94                 break;
    95         }
    96     }
    97 
    98     return 0;
    99 }
    main

      3.文件操作

     1 //file.c
     2 #include "head.h"
     3 
     4 int read_file(stu_t *read_head)
     5 {
     6     FILE *fp_read;
     7     char buffer[MAX], itemp_data[MAX_DATA];
     8     stu_t *data, *itemp_head;
     9     int number = 0;
    10 
    11     if(NULL == (fp_read = fopen("student_data.txt", "r"))) {
    12         return -1;
    13     }
    14  
    15     itemp_head = read_head;
    16     while(fgets(buffer, MAX, fp_read) != NULL) 
    17     {
    18         number ++;
    19         data = calloc(1, sizeof(stu_t));
    20 
    21         data->seg = number;
    22 
    23         strcpy(itemp_data, strtok(buffer, " "));
    24         data->id = atoi(itemp_data);
    25 
    26         strcpy(itemp_data, strtok(NULL, " "));
    27         strcpy(data->name, itemp_data);
    28 
    29         strcpy(itemp_data, strtok(NULL, " "));
    30         data->chinese = atof(itemp_data);
    31 
    32         strcpy(itemp_data, strtok(NULL, " "));
    33         data->math = atof(itemp_data);
    34 
    35         strcpy(itemp_data, strtok(NULL, " 
    "));
    36         data->ave = atof(itemp_data);
    37 
    38         itemp_head->next = data;
    39         data->next = NULL;
    40         itemp_head = data;
    41     }
    42 
    43     printf("---------------------------------Write_list_data------------------------------
    ");
    44     printf("Totai student_data is %d
    
    ", number);
    45     return 0;
    46 }
    47 
    48 int write_file(stu_t *write_head)
    49 {
    50     FILE *fp_write;
    51     stu_t *itemp_head, *delete;
    52 
    53     if(NULL == (fp_write = fopen("student_data.txt", "w"))) {
    54         fprintf(stderr, "Error fo fopen %s : %d : %s.
    ", __FILE__, __LINE__, strerror(errno));
    55         return -1;
    56     }
    57 
    58     itemp_head = write_head;
    59     for( ; itemp_head->next; )
    60     {
    61         delete = itemp_head->next;
    62         fprintf(fp_write, "%d %s %.1f %.1f %.1f
    ", itemp_head->next->id, itemp_head->next->name, itemp_head->next->chinese, itemp_head->next->math, itemp_head->next->ave);
    63         itemp_head->next = itemp_head->next->next;
    64         free(delete);
    65     }
    66 
    67     return 0;
    68 }
    文件操作

      4.链表操作(使用的单链表)

      1 //list.c
      2 #include "head.h"
      3 
      4 void show_student_data(stu_t *show_head)
      5 {
      6     stu_t *itemp_head;
      7 
      8     if(NULL == show_head->next) {
      9         printf("------------------------------------------------------------------------------
    ");
     10         printf("The list is no data to show!
    ");
     11         printf("------------------------------------------------------------------------------
    ");
     12         return ;
     13     }
     14 
     15     printf("
    --------------------------Show data-----------------------------------------
    ");
     16     printf("seg	ID		NAME		CHINESE	MATH	AVERAGE
    ");
     17     itemp_head = show_head;
     18     for(; itemp_head->next; itemp_head = itemp_head->next)
     19     {
     20         printf("%d: 	%d		%s		%.1f	%.1f	%.1f
    ", itemp_head->next->seg, itemp_head->next->id, itemp_head->next->name, itemp_head->next->chinese, itemp_head->next->math, itemp_head->next->ave);
     21     }
     22     printf("
    --------------------------Show data-----------------------------------------
    
    ");
     23 
     24     return ;
     25 }
     26 
     27 void sort_ave_data(stu_t *sort_head)
     28 {
     29     stu_t *itemp_head , *sort_1, *sort_2;
     30 
     31     int change_id = 0;
     32     char change_name[MAX_NAME];
     33     float change_number = 0.0;
     34 
     35     itemp_head = sort_head;
     36     if(NULL == itemp_head->next) {
     37         printf("------------------------------------------------------------------------------
    ");
     38         printf("The list no data to sort!
    ");
     39         printf("------------------------------------------------------------------------------
    ");
     40         return ;
     41     }
     42 
     43     for(sort_1 = itemp_head->next;  sort_1; sort_1 = sort_1->next)
     44     {
     45         for(sort_2 = sort_1->next; sort_2; sort_2 = sort_2->next)
     46         {
     47             if(sort_1->ave > sort_2->ave) {
     48 
     49                 change_id = sort_1->id;
     50                 sort_1->id = sort_2->id;
     51                 sort_2->id = change_id;
     52 
     53                 change_number = sort_1->chinese;
     54                 sort_1->chinese = sort_2->chinese;
     55                 sort_2->chinese = change_number;
     56 
     57                 change_number = sort_1->math;
     58                 sort_1->math = sort_2->math;
     59                 sort_2->math = change_number;
     60 
     61                 change_number = sort_1->ave;
     62                 sort_1->ave = sort_2->ave;
     63                 sort_2->ave = change_number;
     64 
     65                 strcpy(change_name, sort_1->name);
     66                 strcpy(sort_1->name, sort_2->name);
     67                 strcpy(sort_2->name, change_name);
     68 
     69             }
     70         }
     71     }
     72 
     73     return ;
     74 }
     75 
     76 void add_student_data(stu_t *add_head, int add_number)
     77 {
     78     stu_t *itemp_head, *data, *end, *find, *find_1, *end_1;
     79     char buffer[MAX_DATA];
     80 
     81     int i = 0, number = 0;
     82 
     83     find_1 = add_head;
     84     if(find_1->next != NULL) {
     85         for( ; ; )
     86         {
     87             if(NULL == find_1->next) {
     88                 end_1 = find_1;
     89                 break;
     90             }
     91             find_1 = find_1->next;
     92         }
     93         number = end_1->seg;
     94     }
     95 
     96     printf("------------------------------------------------------------------------------
    ");
     97     printf("ID should more than %d.
    ", number);
     98     printf("------------------------------------------------------------------------------
    ");
     99 
    100     number = 0;
    101 
    102     itemp_head = add_head;
    103     for(i = 0; i != add_number; i ++)
    104     {
    105         data = calloc(1, sizeof(stu_t));
    106 
    107         printf("Enter id : ");
    108         fgets(buffer, MAX_DATA, stdin);
    109         if('
    ' == *(buffer + strlen(buffer) - 1)) {
    110             *(buffer + strlen(buffer) - 1) = '';
    111         }
    112         data->id = atoi(buffer);
    113 
    114         printf("Enter name : ");
    115         fgets(buffer, MAX_DATA, stdin);
    116         if('
    ' == *(buffer + strlen(buffer) - 1)) {
    117             *(buffer + strlen(buffer) - 1) = '';
    118         }
    119         strcpy(data->name, buffer);
    120 
    121         printf("Enter chinese_score : ");
    122         fgets(buffer, MAX_DATA, stdin);
    123         if('
    ' == *(buffer + strlen(buffer) - 1)) {
    124             *(buffer + strlen(buffer) - 1) = '';
    125         }
    126         data->chinese = atof(buffer);
    127 
    128         printf("Enter math_score : ");
    129         fgets(buffer, MAX_DATA, stdin);
    130         if('
    ' == *(buffer + strlen(buffer) - 1)) {
    131             *(buffer + strlen(buffer) - 1) = '';
    132         }
    133         data->math = atof(buffer);
    134 
    135         printf("Enter ave_score : ");
    136         fgets(buffer, MAX_DATA, stdin);
    137         if('
    ' == *(buffer + strlen(buffer) - 1)) {
    138             *(buffer + strlen(buffer) - 1) = '';
    139         }
    140         data->ave = atof(buffer);
    141 
    142         if(NULL == (itemp_head->next)) {
    143             number++;
    144             data->seg = number;
    145 
    146             itemp_head->next = data;
    147             data->next = NULL;
    148             itemp_head = data;
    149         }
    150         else {
    151             for(find = add_head; ; find = find->next)
    152             {
    153                 if(NULL == (find->next)) {
    154                     end = find;
    155                     break;
    156                 }
    157             }
    158             number = end->seg;
    159             data->seg = ++number;
    160             end->next = data;
    161             data->next = NULL;
    162         }
    163 
    164     }
    165     printf("------------------------------------------------------------------------------
    ");
    166     printf("Write_List Success!
    ");
    167     printf("------------------------------------------------------------------------------
    ");
    168 
    169     return ;
    170 }
    171 
    172 void delete_student_data(stu_t *delete_head)
    173 {
    174     stu_t *id_head, *name_head, *delete, *end, *find, *mobile;
    175     int choose_num = 0, delete_id = 0, flag = 0;
    176     char buffer[MAX_NAME];
    177     int number = 0;
    178 
    179     find = delete_head;
    180     if(NULL == find->next) {
    181         printf("------------------------------------------------------------------------------
    ");
    182         printf("The list no data to delete!
    ");
    183         printf("------------------------------------------------------------------------------
    ");
    184         return ;
    185     }
    186     else {
    187         for(; ;)
    188         {
    189             if(NULL == find->next) {
    190                 end = find;
    191                 break;
    192             }
    193             find = find->next;
    194         }
    195     }
    196 
    197     number = end->seg;
    198 
    199     while(1)
    200     {
    201         flag = 0;
    202 
    203         printf("==============================================================================
    ");
    204         printf("Please choose the way :
    ");
    205         printf("1.Delete by ID
    "
    206                 "2.Delete by name
    ");
    207         printf("==============================================================================
    ");
    208         printf("Your choose : ");
    209         scanf("%d", &choose_num);
    210         getchar();
    211 
    212         switch(choose_num)
    213         {
    214             case 520 :
    215                 printf("------------------------After delete_data-------------------------------------
    ");
    216                 show_student_data(delete_head);
    217                 printf("------------------------After delete_data-------------------------------------
    
    ");
    218                 printf("------------------------------------------------------------------------------
    ");
    219                 printf("Delete_quite :: Good Lucky!
    ");
    220                 printf("------------------------------------------------------------------------------
    ");
    221                 return ;
    222                 break;
    223 
    224             case 1 :
    225                 printf("------------------------------------------------------------------------------
    ");
    226                 printf("Your shoule choose the numbe included 1-%d: ", number);
    227                 printf("------------------------------------------------------------------------------
    ");
    228                 scanf("%d", &delete_id);
    229                 getchar();
    230 
    231                 id_head = delete_head;
    232                 for(; id_head->next; id_head = id_head->next)
    233                 {
    234                     if(delete_id == id_head->next->id) {
    235                         delete = id_head->next;
    236 
    237                         mobile = id_head->next->next;
    238                         for( ; mobile; mobile = mobile->next)
    239                         {
    240                             mobile->seg = mobile->seg - 1;
    241                         }
    242 
    243                         id_head->next = id_head->next->next;
    244                         free(delete);
    245 
    246                         flag = 1;
    247                         break;
    248                     }
    249                 }
    250                 if(0 == flag) {
    251                     printf("------------------------------------------------------------------------------
    ");
    252                     printf("No your want delete's id_data.
    ");
    253                     printf("------------------------------------------------------------------------------
    ");
    254                 }
    255                 break;
    256 
    257             case 2 :
    258                 printf("Enter Delete_name : ");
    259                 fgets(buffer, MAX_NAME, stdin);
    260                 if('
    ' == *(buffer + strlen(buffer) - 1)) {
    261                     *(buffer + strlen(buffer) - 1) = '';
    262                 }
    263 
    264                 name_head = delete_head;
    265                 for(; name_head->next; name_head = name_head->next)
    266                 {
    267                     if(0 == strcmp(buffer, name_head->next->name)) {
    268                         delete = name_head->next;
    269 
    270                         mobile = name_head->next->next;
    271                         for( ; mobile; mobile = mobile->next)
    272                         {
    273                             mobile->seg = mobile->seg - 1;
    274                         }
    275 
    276                         name_head->next = name_head->next->next;
    277                         free(delete);
    278 
    279                         flag = 1;
    280                         break;
    281                     }
    282                 }
    283                 if(0 == flag) {
    284                     printf("------------------------------------------------------------------------------
    ");
    285                     printf("No your want delete's name_data.
    ");
    286                     printf("------------------------------------------------------------------------------
    ");
    287                 }
    288                 break;
    289         }
    290         show_student_data(delete_head);
    291     }
    292 
    293     return ;
    294 }
    List

          终于改好了。。爽快。。。嘿嘿。

                          

                                                                          笨宝贝。想你!

     

  • 相关阅读:
    xcode debug
    ios 使用Quartz 2D画虚线
    ios UIGestureRecognizer
    ios 实现带矩形框的屏幕截图
    ios UISegemetedControl
    centos最小安装之后无法使用ifconfig
    无线LoRa智能远传水表
    智能水表188协议汇总
    无线LoRa远传智能水表
    静电除尘器除尘原理
  • 原文地址:https://www.cnblogs.com/cxw825873709/p/3274952.html
Copyright © 2020-2023  润新知