学生信息管理系统

下面是我用动态数组实现的学生管理系统,虽说写的差不多了,但是还是希望大家给我提些意见,看能不能更加优化,另外下面是我的一个设计思路:
1.因为学生信息记录的姓名、学号、系别都是用数组来实现的,还有本身用来存储每个学生信息也是用数组实现的,每个学生占一个元素,为了节省不必要的浪费空间,于是我们可以用动态分配的方式来实现这个愿望!
2.首先,我们用结构体的方式来实现姓名、学号、系别数组的动态分配,比如你要输入姓名:你就要输入你姓名的长度是多少,然后输入姓名,从而实现动态分配。
我用n来定义长度,用*pdata来指向数组首地址的指针,这样一个结构体来实现。应该很好理解。
3.另外我用一个简单类型的动态数组实现方式,就是说定义出指向数组的指针,你给出长度,我再用malloc来申请,就得到了一个是动态分配的数组,这是常见的一种实现方式。
4.大概就是这样的,我的理解,不知道有什么错误,望指正!
 
 
/*下面是我用动态数组实现的学生信息记录系统,我是尝试一下,不知道对不对*/
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#define Init_Array_Num 10 /*数组初始化大小,增量大小*/
#define TRUE  1
#define FALSE 0
typedef char Status;
typedef char elem_t;
/*定义动态数组的存储结构,用来修饰下面结构体中的学号、姓名、系等数组的*/
typedef struct struct_style{
    int n;           /*表示数组的位数*/
    elem_t *pdata;   /*该指针指向存放数据空间的首地址*/
}Array_t;
typedef struct student_type       /*用动态结构来分配存储学生信息的数组存储结构*/
{
    Array_t *num;   /*学号*/
    Array_t *name; /* 姓名*/
    int age; /*年龄*/
    Array_t *dep; /*系*/
}Stud;
int count=0;             /*定义一个计数器,用来存放学生记录信息的个数*/
Status initArray(Array_t * array,int size)
/*初始化,size<=0时采用默认值Init_Array_Num*/
{
    char sentence=FALSE;
    int initSize=(size>0) ? size :Init_Array_Num;
    array->pdata=(elem_t *)malloc(initSize*sizeof(elem_t));
    if (array->pdata!=NULL)
    {
        array->n=initSize;
        sentence=TRUE;
    }
    return sentence;     /*存储分配失败*/
}
Status destroyArray(Array_t *array )          /*销毁该数组*/
{
    free(array->pdata);
    array->pdata= NULL;
    return TRUE;
}

void add(Stud *a)
{                  /*添加新纪录*/
    int i;
    int size1,size2,size3;
    printf("Please input added information:\n");
    printf("(add)Age:\n");
    scanf("%d",&a[count].age);
    printf("(add)Number:\n");
    printf("Wait please! Input your number length :");
    scanf("%d",&size1);
    initArray(a[count].num,size1);     /*数组实现了num动态分配*/
    printf("your number:");
    scanf("%s",a[count].num->pdata);
    printf("(add)Name:\n");
    printf("Wait please! Input your name length:");
    scanf("%d",&size2);
    initArray(a[count].name,size2);      /*数组实现了name动态分配*/
    printf("your name:");
    scanf("%s",a[count].name->pdata);
    printf("(add)Department:\n");
    printf("Wait please! Input your name length:");
    scanf("%d",&size3);
    initArray(a[count].dep,size3);
    printf("your department:");
    scanf("%s",a[count].dep->pdata);
    /*输出学生信息*/
    printf("This is the %d student information:\n",count+1);
    printf("a[%d].age=",count);
    printf("%d\n",a[count].age);
    printf("a[%d].num[]=",count);
    printf("%s\n",a[count].num->pdata);
    printf("a[%d].name[]=",count);
    printf("%s\n",a[count].name->pdata);
    printf("a[%d].dep[]=",count);
    printf("%s\n",a[count].dep->pdata);
    count++;                /*学生记录数增加一个*/
}
void del(Stud *a)
{   /*以学号为关键字删除记录,a为需要进行删除的学生记录*/
    Stud del1;
    int i,j;
    int length;
    int compare1;    /*通过输入学号跟存储学号比较,判断删除记录是否存在*/
    int count1;    /*计数器二,用于查找到该学号,在进行删除操作*/
    Status sentence=TRUE;   /*判断删除操作是否成功!TURE or FALSE*/
    printf("(delete)Please input the number you want to delete and its length:\n");
    printf("Length:");
    scanf("%d",length);
    initArray(del1.num,length);
    scanf("%s",del1.num->pdata);
    for(count1=0;count1<count;count1++)
    {
     compare1=strcmp(a[count1].num->pdata,del1.num->pdata);
     if(compare1==0)       /*如果完全匹配*/
         {
           for(i=count1;i<=count;i++);
            {
              memset(a[i].num->pdata,'\0',strlen(a[i].num->pdata));  /*先清空,避免字符串长度不同问题*/
             strcpy(a[i].num->pdata,a[i+1].num->pdata);
            }
           destroyArray(a[count].num);          /*销毁数组,将最后一个销毁*/
           for(i=count1;i<=count;i++)
           {
             memset(a[i].name->pdata,'\0',strlen(a[i].name->pdata));
             strcpy(a[i].name->pdata,a[i+1].name->pdata);
            }
           destroyArray(a[count].name);        /*同上*/
           for(i=count1;i<=count;i++)
            {
                memset(a[i].dep->pdata,'\0',strlen(a[i].dep->pdata));
                strcpy(a[i].dep->pdata,a[i+1].dep->pdata);
           }
            destroyArray(a[count].dep);
           for(i=count1;i<=count;i++)
                     a[i].age=a[i+1].age;
           count--;           /*学生记录数少一个*/
           printf("Delete Succeed!\n");
           sentence=TRUE;
         }
         else
           sentence=FALSE;
     }
  if(sentence==FALSE)
     printf("Delete Failed!\n");
}
void update(Stud *a){   /*以学号为关键字修改记录信息*/
    int i,menu1;
    int compare;
    int length1,length2,length3;
    int count2;            /*计数器2,用来查找修改学生信息是否存在*/
    Stud modify;
    printf("(update)Please input the number you want to modify and its Length number:\n");
    printf("Length:");
    scanf("%d",&length1);
    initArray(modify.num,length1);
    scanf("%s",modify.num->pdata);
    for(count2=0;count2<=count;count2++)   /*在数组逐一查询*/
    {
      compare=strcmp(a[count2].num->pdata,modify.num->pdata);
         if(compare==0)       /*如果完全匹配,即需要修改的学生信息存在*/
         {
             while(1){                             /*用while结构实现子菜单选择功能*/
             printf("1. Modify the name.\n");
             printf("2. Modify the age.\n");
             printf("3. Modify the department.\n");
             printf("4. Quit the Modify.\n");        /*退出修改*/
             printf("Please input the your choose:");
             scanf("%d",&menu1);
             switch(menu1)
            {
             case 1:
               printf("Please input the name and its length number:");
               printf("Length:");
               scanf("%d",&length2);
               initArray(modify.name,length2);
               scanf("%s",modify.name->pdata);
               if(strlen(a[count2].name->pdata)<=strlen(modify.name->pdata)){
                     strcpy(a[count2].name->pdata,modify.name->pdata);
                }
               else{            /*清楚掉a[count2].name里多余的元素*/
                   strcpy(a[count2].name->pdata,modify.name->pdata);
                   for(i=strlen(a[count2].name->pdata);i<strlen(a[count2].name->pdata)-strlen(modify.name->pdata);i--)
                     a[count2].name->pdata[i]='\0';
                   }
               break;
             case 2:
               printf("Please input the age:");
               scanf("%d",&modify.age);
               a[count2].age=modify.age;
                break;
             case 3:
               printf("Please input the department and its length number:");
               printf("Length:");
               scanf("%d",&length3);
               initArray(modify.dep,length3);
               scanf("%s",modify.dep->pdata);
               if(strlen(a[count2].dep->pdata)<=strlen(modify.dep->pdata)){
                  strcpy(a[count2].dep->pdata,modify.dep->pdata);
                   }
               else{
                  for(i=strlen(a[count2].dep->pdata);i<strlen(a[count2].dep->pdata)-strlen(modify.dep->pdata);i--)
                    a[count2].dep->pdata[i]='\0';
                 }
               break;
             case 4:
               goto in;  /*跳转到下面的输出信息语句,in为行号*/
             }
           printf("\n");
         }
      }
  }
   /*goto的跳转语句在这里*/
    in:printf("This is the %d student information after you modified::\n",count2+1);
    printf("a[%d].age=",count2);
    printf("%d",a[count2].age);
    printf("a[%d].num[]=",count2);
    printf("%s",a[count2].num->pdata);
    printf("a[%d].name[]=",count2);
    printf("%s",a[count2].name->pdata);
    printf("a[%d].dep[]=",count2);
    printf("%s",a[count2].dep->pdata);
}
Status query(Stud *a){                  /*以学号或姓名为查询字段进行查询,*a为指向存储学生记录的数组的指针*/
    Stud find;
    int i,j,count3;                       /*计数器3*/
    int compare;
    int length;
    Status sentence=TRUE;
    printf("(query)Please input the number you want to find and its length number:\n");
    printf("Length:");
    scanf("%d",&length);
    scanf("%s",find.num->pdata);
    for(count3=0;count3<count;count3++)   /*在数组逐一查询*/
    {
      compare=strcmp(a[count3].num->pdata,find.num->pdata);
      if(compare==0)       /*如果完全匹配,则输出查询结果*/
         {
          printf("This is the result you find out:\n");    /*输出查找结果*/
          printf("a[%d].num[]=",count3);          /*输出所查找信息的学号*/
          printf("%s\n",a[count3].num->pdata);
          printf("a[%d].name[]=",count3);         /*输出所查找信息的姓名*/
          printf("%s\n",a[count3].name->pdata);
          printf("a[%d].age=",count3);   /*输出所查找信息的年龄*/
          printf("%d\n",a[count3].age);
          printf("a[%d].dep=",count);                 /*输出所查找信息的系别*/
          printf("%s\n",a[count3].dep->pdata);
          sentence=TRUE;
          printf("Find Succeed!\n");                       /*查找成功*/
         }
         else
           sentence=FALSE;
      }
     if(sentence==FALSE)
        printf("Find Failed!\n");                    /*没有查询内容*/
}
void main()
{
    int menu;
    /*另一种类型的动态数组*/
    Stud *STU;              /*用来存储学生记录的数组*/
    int studentNum;         /*用动态数组形式来分配可以存储的数目*/
    printf("Please input the student numbers you want to save:");
    scanf("%d",&studentNum);
    STU=(Stud *)malloc(studentNum*sizeof(Stud));
    while(1)
    {                        /*用while结构定义主菜单结构,供用户选择操作*/
        printf("------The Simple Student Information Management System v1.0------\n");
        printf("1. Add the student information in STU.\n");
        printf("2. Delete the student information in STU.\n");
        printf("3. Modify the student information in STU.\n");
        printf("4. Find the student information in STU.\n");
        printf("5. Quit the system.\n");
        printf("Please input your selection:");
        scanf("%d", &menu);
        if(menu==1||menu==2||menu==3||menu==4||menu==5)
        {
           printf("**We begin to operate now!\n");
         switch(menu)
        {
            case 1:
                  add(STU);
                  break;
            case 2:
                  del(STU);
                  break;
            case 3:
                  update(STU);
                  break;
            case 4:
                  query(STU);
                  break;
            case 5:
                  exit(1);
                  break;
         }
       }
       else
         printf("Sorry! Your input does not match,again please!\n");
    }
   getch();
}
 
 
 

 

你可能感兴趣的:(职场,休闲)