zoukankan      html  css  js  c++  java
  • 线性表的基本操作及其作用

    顺序表

    要求:

    定义一个包含学生信息(学号,姓名,成绩)的顺序表和链表,使其具有如下功能:

    (1) 根据指定学生个数,逐个输入学生信息;

    (2) 逐个显示学生表中所有学生的相关信息;

    (3) 根据姓名进行查找,返回此学生的学号和成绩;

    (4) 根据指定的位置可返回相应的学生信息(学号,姓名,成绩);

    (5) 给定一个学生信息,插入到表中指定的位置;

    (6) 删除指定位置的学生记录;

    (7) 统计表中学生个数。

    typedef struct
    {
        char stuID[ID_SIZE];        //学生学号
        char stuName[NAME_SIZE];    //学生姓名 
        double stuScore;        //学生成绩 
    } StuData;
    1 typedef  struct {
    2   Student  *elem;     //指向数据元素的基地址
    3   int  length;       //线性表的当前长度                                                           
    4  }SqList;

    完整代码:

      1 /*
      2  * 顺序表 
      3  * 一个简陋的学生信息管理程序 
      4  * Data: 10/13/2017  20:42
      5  */
      6 
      7 #include <stdio.h>
      8 #include <stdlib.h>
      9 #include <string.h>
     10 #include <unistd.h>
     11 #include <stddef.h>
     12 #include <errno.h>
     13 #include <inttypes.h>
     14 #define STU_NUM_MAX 100
     15 #define ID_SIZE 8
     16 #define NAME_SIZE 20
     17 
     18 typedef struct
     19 {
     20     char stuID[ID_SIZE];        //学生学号
     21     char stuName[NAME_SIZE];    //学生姓名 
     22     double stuScore;        //学生成绩 
     23 } StuData;
     24 typedef StuData* stuPtr;
     25 
     26 typedef struct
     27 {
     28     StuData *elem;        //指向动态分配的内存的首地址 
     29     int length;        //保存已存储的数据据元素的数目 
     30 } SqList;
     31 
     32 void welcome(int *p_choose);
     33 /*
     34  * 功能:输出欢迎界面,并提示用户执行相应的操作
     35  * 参数:指向choose的指针,通过指针改变变量choose的值,根据其值执行相应的操作
     36  * 返回值:无
     37  */
     38 
     39 void InitList(SqList *p_seq);
     40 /*
     41  * 功能:一次性分配所有的储存空间,初始化
     42  * 参数:SqList的指针
     43  * 返回值:无
     44  */
     45 
     46 void add(SqList *p_seq);
     47 /*
     48  * 功能:
     49  *
     50  *
     51  */
     52 
     53 stuPtr info_input(stuPtr info);
     54 /*
     55  * 功能:对数组赋值,其长度不超过len
     56  * 参数:stuID: 指向数组的指针   size_t
     57  * 返回值:传入的指针
     58  */
     59 
     60 void NodeDelete(SqList *p_seq, int locate);
     61 /*
     62  * 功能:删除指定序号的数据元素
     63  * 参数:p_seq: SqList的指针   locate: 序号(第几个)
     64  * 返回值:无
     65  */
     66 
     67 StuData *search(stuPtr p, size_t len, char *target);
     68 /*
     69  * 功能:根据指定的字符串遍历查找是否存在相应的ID or Name
     70  * 参数:p: 指向第一个顺序元素   len: 已存储的数据元素的长度   target: 需要查找的字符
     71  * 返回值:指向查找到的节点,不存在则返回NULL
     72  */
     73 
     74 void print(StuData *elem, size_t len);
     75 /*
     76  * 功能:打印一定长度的数据元素
     77  * 参数:elem: 指向某个数据元素   len: 需要打印多少个数据元素(长度)
     78  * 返回值:无
     79  */
     80 
     81 void save(FILE *stream, stuPtr p, size_t len);
     82 /*
     83  * 功能:将输入的信息保存到文件中
     84  * 参数:stream: 指定的文件输入流  p: 指向第一个数据元素   len: 数据元素的长度
     85  * 返回值:无
     86  */
     87 
     88 
     89 int main(void)
     90 {
     91     int choose;
     92     char ans = 'y';
     93     SqList L;
     94 
     95     InitList(&L);
     96     system("color 2F");
     97     while (1)
     98     {
     99         fflush(stdin);
    100         ans = 'y';
    101         welcome(&choose);
    102         switch (choose)
    103         {
    104             case 1:
    105             {
    106                 while (ans == 'y')
    107                 {
    108                     if (L.length >= STU_NUM_MAX)
    109                     {
    110                         printf("\a\n\tWarning: Memory is full!\n");
    111                         break;
    112                     }
    113                     else
    114                     {
    115                         //info_input(&info);
    116                         add(&L);
    117                         printf("\n\nAdd succeefully!               stu's num %u\n", L.length);
    118                         printf("Continue?[y]\n");
    119                         fflush(stdin);
    120                         ans = getchar( );
    121                         if (ans == '\n')
    122                         {
    123                             ans = 'y';
    124                         }
    125                         system("cls");
    126                     }
    127                 }
    128                 break;
    129             }
    130             case 2:
    131             {
    132                 int locate;
    133                 while (ans == 'y')
    134                 {
    135                     printf("Please enter the node number you want to delete: ");
    136                     scanf("%d", &locate);
    137                     NodeDelete(&L, locate);
    138                     printf("\a\n\n\t\tDelete Successfully\n");
    139                     printf("Continue?[y]");
    140                     fflush(stdin);
    141                     ans = getchar( );
    142                     if (ans == '\n')
    143                     {
    144                         ans = 'y';
    145                     }
    146                     system("cls");
    147                 }
    148                 break;
    149             }
    150             case 3:
    151             {
    152                 StuData *locate;
    153                 char target[NAME_SIZE];
    154                 while (ans == 'y')
    155                 {
    156                     printf("Please enter the ID/Name of the student you want to find: ");
    157                     scanf("%s", target);
    158                     locate = search(L.elem, L.length, target);
    159                     if (locate == NULL)
    160                     {
    161                         printf("\a\n\t\tSorry! There is no such person!\n");
    162                     }
    163                     else
    164                     {
    165                         printf("\aFind successfully!\n");
    166                         print(locate, 1);
    167                     }
    168                     printf("Continu?[y] ");
    169                     fflush(stdin);
    170                     ans = getchar( );
    171                     if (ans == '\n')
    172                     {
    173                         ans = 'y';
    174                     }
    175                     system("cls");
    176                 }
    177                 break;
    178             }
    179             case 4:
    180             {
    181                 printf("All of the stu's info are:\n\n");
    182                 print(L.elem, L.length);
    183                 getchar( );
    184                 getchar( );
    185                 system("cls");
    186                 break;
    187             }
    188             case 5:
    189             {
    190                 FILE *stream;
    191                 if ((stream = fopen("info.dat", "w+")) == NULL)
    192                 {
    193                     perror("\a\n\n\t\tSorry: Open fail!\n");
    194                     break;
    195                 }
    196                 else
    197                 {
    198                     save(stream, L.elem, L.length);
    199                     getchar( );
    200                     sleep(3);
    201                     fclose(stream);
    202                     system("cls");
    203                     break;
    204                 }
    205             }
    206             case 6:
    207             {
    208                 free(L.elem);
    209                 L.elem = NULL;
    210                 printf("\a\n\n\t\tBye Bye!\n\n");
    211                 sleep(2);
    212                 system("cls");
    213                 system("color 0F");
    214                 exit(0);
    215             }
    216             default :
    217             {
    218                 printf("\a\n\tSorry! I have not develop the function what you want!\n");
    219                 sleep(2);
    220                 system("cls");
    221                 break;
    222             }
    223         }
    224     }
    225     
    226 
    227     return 0;
    228 }
    229 
    230 void welcome(int *p_choose)
    231 {
    232     printf("\n\n\n                       WELCOME\n");
    233     printf("------------------------------------------------------\n");
    234     printf("--                1.增加指定学生信息\n");
    235     printf("--                2.删除指定位置信息\n");
    236     printf("--                3.按学号或姓名查找\n");
    237     printf("--                4.显示所有学生信息\n");
    238     printf("--                5.保存\n");
    239     printf("--                6.退出\n");
    240     printf("------------------------------------------------------\n");
    241     printf("请输入那想要执行的操作的序号: ");
    242     scanf("%d", p_choose);
    243     system("cls");
    244 }
    245 
    246 void InitList(SqList *p_seq)
    247 {
    248     p_seq->elem = (StuData *)malloc(STU_NUM_MAX*sizeof(StuData));
    249     if (p_seq->elem == NULL)
    250     {
    251         perror("\n\n\t\tError: memory may full");    //perror??????????????
    252         _exit(1);
    253     }
    254     else
    255     {
    256         p_seq->length = 0;
    257     }
    258 }
    259 
    260 void add(SqList *p_seq)
    261 {
    262     printf("Please enter information:\n");
    263     
    264     while (1)
    265     {
    266         printf("ID: ");
    267         scanf("%s", p_seq->elem[p_seq->length].stuID);
    268         if (strlen(p_seq->elem[p_seq->length].stuID) >= ID_SIZE)
    269         {
    270             printf("It's too long, enter again\n");
    271             sleep(1);
    272             system("cls");
    273         }
    274         else
    275         {
    276             break;
    277         }
    278     }
    279     while (1)
    280     {
    281         printf("Name: ");
    282         scanf("%s", p_seq->elem[p_seq->length].stuName);
    283         if (strlen(p_seq->elem[p_seq->length].stuName) >= NAME_SIZE)
    284         {
    285             printf("It's too long, enter again\n");
    286             sleep(1);
    287             system("cls");
    288         }
    289         else
    290         {
    291             break;
    292         }
    293     }
    294     while (1)
    295     {
    296         printf("Score: ");
    297         scanf("%lf", &p_seq->elem[p_seq->length].stuScore);
    298         if (p_seq->elem[p_seq->length].stuScore <0 || p_seq->elem[p_seq->length].stuScore > 100)
    299         {
    300             printf("The score is percentage system\n");
    301             sleep(1);
    302             system("cls");
    303         }
    304         else
    305         {
    306             break;
    307         }
    308     }
    309     p_seq->length++;
    310 }
    311 
    312 
    313 void NodeDelete(SqList *p_seq, int locate)
    314 {
    315     for (int i=locate; i<=p_seq->length; i++)
    316     {
    317         memccpy((p_seq->elem[i-1]).stuID, (p_seq->elem[i]).stuID, '\0', ID_SIZE);
    318         memccpy((p_seq->elem[i-1]).stuName, (p_seq->elem[i]).stuName, '\0', NAME_SIZE);
    319         (p_seq->elem[i-1]).stuScore = (p_seq->elem[i]).stuScore;
    320     }
    321     p_seq->length--;
    322 }
    323 
    324 
    325 stuPtr search(stuPtr p, size_t len, char *target)
    326 {
    327     for (unsigned i=0; i<len; i++)
    328     {
    329         if (strcmp(p[i].stuID, target) == 0)
    330         {
    331             return (p+i);
    332         }
    333         else if (strcmp(p[i].stuName, target)== 0)
    334         {
    335             return (p+i);
    336         }
    337     }
    338     return NULL;
    339 }
    340 
    341 
    342 void print(StuData *elem, size_t len)
    343 {
    344     printf("      ID                Name                Score\n");
    345     printf("    -----------------------------------------------\n");
    346     if (len <= 0)
    347     {
    348         printf("\a\n\t\tWarning: NO data exist!\n");
    349     }
    350     else
    351     {
    352         unsigned count = 0;
    353         for (unsigned i=0; i<len; i++)
    354         {
    355             printf("%3d-", ++count);
    356             printf(" %s\t\t", elem[i].stuID);
    357             printf(" %s\t\t", elem[i].stuName);
    358             printf(" %.2lf\n", elem[i].stuScore);
    359         }
    360         printf("    num: %lu\n", len);
    361     }
    362     printf("    -----------------------------------------------\n");
    363 }
    364 
    365 void save(FILE *stream, stuPtr p, size_t len)
    366 {
    367     if (len <= 0)
    368     {
    369         printf("\a\n\n\t\tSorry: No data exist!\n");
    370         sleep(2);
    371         return;
    372     }
    373     else
    374     {
    375         unsigned count = 0;
    376         fprintf(stream, "      ID                  Name                 Score\n");
    377         fprintf(stream, "    -------------------------------------------------\n");
    378         for (unsigned i=0; i<len; i++)
    379         {
    380             fprintf(stream, "%3d-", ++count);
    381             fprintf(stream, " %s\t\t", p[i].stuID);
    382             fprintf(stream, " %s\t\t", p[i].stuName);
    383             fprintf(stream, " %.2lf\n", p[i].stuScore);
    384         }
    385         fprintf(stream,"\n    -------------------------------------------------\n");
    386         printf("\a\n\n\tSave successfully!\n\n");
    387     }
    388 }
  • 相关阅读:
    springboot2.1.3使用jdbcTemplate
    httpclient4.5.2 Post请求支持http和https
    springboot2.1.3+spring-session2.1.4分库处理
    mysql查看当前实时连接数
    springboot2.1.3+Junit4 单元测试
    subprocess.Popen()详解
    matplotlib 设置图形大小时 figsize 与 dpi 的关系
    matplotlib之subplot
    matplotlib.pyplot.plot()参数详解
    plt.rcParams属性总结
  • 原文地址:https://www.cnblogs.com/lymboy/p/7750229.html
Copyright © 2011-2022 走看看