学生信息管理

本文介绍了一个简单的学生信息管理系统的设计与实现,使用C语言进行编程。该系统包括学生信息的添加、显示、插入、删除、替换等功能,并能根据学号查找特定学生的信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define MaxSize 50
#define TRUE 1
#define FALSE 0
typedef int BOOL;

typedef struct student {
  char num[9];
  char name[9];
  char sex[5];
  float score;
}student;

typedef student T ;

typedef struct {
  int Size;
  int MaxList;
  T Element[MaxSize];
}List;

List* initial() {
    List *lst;
    lst=(List *)malloc(sizeof(List));
    return lst;
}

void CreateList(List *lst, int maxsize) {
   lst->Size = 0;
   lst->MaxList = maxsize;
}

BOOL Append(List *lst,T x)
{
  if(lst->Size > lst->MaxList) {
      printf("Out of Data!");
      return FALSE;
  }
  strcpy(lst->Element[lst->Size].num, x.num);

  strcpy(lst->Element[lst->Size].name, x.name);

  strcpy(lst->Element[lst->Size].sex, x.sex);

  lst->Element[lst->Size].score = x.score;
  lst->Size++;
  return TRUE;
}

BOOL IsFull(List* lst) {
   if(lst->Size==lst->MaxList)
        return TRUE;
   else
        return FALSE;
}
BOOL Insert(List* lst, int pos, T x)
 {
    int i;
   if ( IsFull(lst)){
       printf("Overflow"); return FALSE;
   }
   if ( pos<0 || pos > lst->Size){
       printf("Out of Bounds");
       return FALSE;
    }
   for ( i=lst->Size-1;  i>=pos;  i-- ) {
       lst->Element[i+1] = lst->Element[i];
   }
  strcpy(lst->Element[pos].num, x.num);  //字符;
  strcpy(lst->Element[pos].name, x.name);
  strcpy(lst->Element[pos].sex, x.sex);
  lst->Element[pos].score = x.score;
   lst->Size++;
   return TRUE;
 }

 BOOL Remove(List* lst, int pos) {
    int i;
    if(pos>=0 && pos <= lst->Size-1) {
        for(i = pos; i < lst->Size-1; i++ ) {
            lst->Element[i] = lst->Element[i+1];
        }
        lst->Size = lst->Size -1;
    }
    else {
        printf("要删除的位置不合法!\n");
    }
    return 0;
 }
 BOOL Replace(List* lst, int pos, T x) {
     if(pos >= 0 && pos <= lst->Size) {
        lst->Element[pos] = x;
        printf("替换成功!\n");
     }
     else {
        printf("输入的信息不合法\n");
     }
     return 0;
 }
 void Find(List* lst, int pos) {
    if(pos>=0 && pos <= lst->Size-1) {
        printf("find it!\n");
    }
 }

void output(List *lst, int j) {
    printf("%-10s%-10s%-10s%6f\n",lst->Element[j].num, lst->Element[j].name, lst->Element[j].sex,lst->Element[j].score);
}

 void Loc(List* lst, char *a) {
     int i;
     for(i = 0; i < lst->Size-1; i++) {
        if(!strcmp(lst->Element[i].num , a)) {
            output(lst, i);break;
        }
     }
 }

void Show(List *lst) {
  int i;
  printf("数据表中记录条数为:%d\n",lst->Size);
  printf("____________________________________________________________________\n");
  for(i=0; i < lst->Size; i++) {
      printf("%-10s%-10s%-10s%6f\n",lst->Element[i].num, lst->Element[i].name, lst->Element[i].sex,lst->Element[i].score);
      printf("____________________________________________________________________\n");
  }
}

void input( T*  stemp ) {
    printf("学号:");
    scanf("%s", stemp->num);
    printf("姓名:");
    scanf("%s", stemp->name);
    printf("性别:");
    scanf("%s", stemp->sex);
    printf("成绩:");
    scanf("%f", &stemp->score);
}

int main()
{
  List *L;
  int i, pos, maxsize;
  int loc = 0;
  char sf;
  char num[10];
  T*  stemp;
  stemp = (T *)malloc(sizeof(T));
  L = initial();
  maxsize = MaxSize;

  CreateList(L, maxsize);

  printf("请输入所选择的功能代码:\n");
  printf("1-Append   2-Show   3-Insert  4-Remove  5-Replace \n");
  printf("6-find  7-Locate 0-Exit\n");
  scanf("%d", &i);
  do{
    switch(i) {
      case 1:
          printf("请输入学生信息:\n");
          input(stemp);
          if (Append(L,*stemp)==TRUE) {
              printf("添加数据成功!");
          }
          else {
              printf("添加数据失败!");
          }
          break;
      case 2:
          Show(L);
          break;
      case 3:
          printf("您要在哪个位置插入记录?");
          scanf("%d",&pos);
          printf("请输入您要插入的学生记录:\n");
          printf("请输入学生信息:\n");
          input(stemp);
          Insert( L, pos, *stemp );
          Show(L);
          break;
      case 4:
           printf("请输入要删除学生的位置:\n");
           scanf("%d", &loc);
           Remove( L, loc );
           Show( L );
           break;
      case 5:
            printf("请输入您要替换的位置:\n");
            scanf("%d", &loc);
            input(stemp);
            Replace( L, loc, *stemp );
            Show(L);
            break;
      case 6:
            printf("请输入要查找第几个学生的信息:\n");
            scanf("%d", &loc);
            Find( L, loc-1 );
            output(L, loc-1);
            break;
      case 7:
            printf("请输入要查找学生的学号:\n");
            scanf("%s", num);
            Loc(L, num);
      default:break;
    }
      if (i!=0){
          printf("是否继续?(Y/N)");
          getchar();
          scanf("%c", &sf);
          if (sf=='N'||sf=='n') {
                break;
          }
          printf("请输入所选择的功能代码:\n");
          scanf("%d",&i);
      }
  }while(i);
  return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值