用C语言实现的学生管理系统

用C语言实现的学生管理系统

  1. 可以输入学生的学号,姓名,年龄,年级;
  2. 可以输入学生的学号,姓名,年龄,年级;
  3. 插入学生信息,默认插入到第一个
  4. 通过学号,对学生进行排序

由于自己学C语言指针时,不太认真,所以有这个作业时,遇到了很多困难,不过最后还是写出来了,很高兴

#include 
#include 
#include
#include
struct student *head;


typedef struct student{
    char sno[10];
    char name[10];
    int sage;
    int grade;
    struct student *next;
};

//链表初始化
struct student * create(int n){
    struct student *h,*m,*q;
    h=(struct student*)malloc(sizeof(struct student));
    m=h;
    int i=1;
    for(i=1;i<=n;i++){
          q=(struct student*)malloc(sizeof(struct student));
          scanf("%s%s%d%d",q->sno,q->name,&q->sage,&q->grade);
          m->next=q;
          m=m->next;
    }
    m->next=NULL;
    return h;
}
//删除学生信息
struct student * del(head){
    struct student *p;
    p=(struct student*)malloc(sizeof(struct student));
    p=head;
    char out[10];
    printf("请输入要删除学生姓名的学号\n");
    scanf("%s",out);
    while(p->next!=NULL&&strcmp(out, (p->next)->sno) != 0){
        p=p->next;
    }
    p->next=(p->next)->next;
    return head;
}
//打印学生信息
void print(head){
    struct student *p;
    p=(struct student*)malloc(sizeof(struct student));
    p=head;
    while(p->next!=NULL){
        printf("%s  %s  %d  %d\n",(p->next)->sno,(p->next)->name,(p->next)->sage,(p->next)->grade);
        p=p->next;
    }
}
//插入学生信息
struct student* insert(head){
    struct student *q,*p;
    q=(struct student*)malloc(sizeof(struct student));
    p=(struct student*)malloc(sizeof(struct student));
    //q->next=(struct student*)malloc(sizeof(struct student));
    p=head;
    printf("请输入要插入的学生信息\n");
    scanf("%s   %s   %d   %d",q->sno,q->name,&q->sage,&q->grade);
    q->next=p->next;
    p->next=q;
    return head;
}
//查找学生信息
struct student * search(head){
    struct student *p;
    p=(struct student*)malloc(sizeof(struct student));
    p=head;
    char out[10];
    printf("请输入要查找的学生姓名的学号\n");
    scanf("%s",out);
    while(p->next!=NULL&&strcmp(out, (p->next)->sno) != 0){
        p=p->next;
    }
    printf("学号,姓名,年龄,年级\n");
    printf("%s%s%d%d",p->next->sno,p->next->name,p->next->sage,p->next->grade);
    //p->next=(p->next)->next;
    return head;
}
//排序学生信息
struct student * sort(head){
    struct student *p,*temp,*q,*h;
    p=(struct student*)malloc(sizeof(struct student));
    temp=(struct student*)malloc(sizeof(struct student));
    q=(struct student*)malloc(sizeof(struct student));
    h=(struct student*)malloc(sizeof(struct student));;
    //p=head->next;
    //min=p->next;    //
    //p=temp;
    h=head;
    head=q;
    while(h->next!=NULL){
        p=h->next;
        temp=h;
    while(p->next!=NULL){
        if(strcmp((p->next)->sno,(temp->next)->sno)<0){
                temp=p; //暂时最小的前一个节点
        }
        p=p->next;
    }
    //printf("%s",(temp->next)->sno);
    q->next=temp->next;
    temp->next=(temp->next)->next;
    q=q->next;
    }
    return head;
};

int main()
{
    int n;
    int i;
    int j=1;
    printf("请输入学生个数\n");
    scanf("%d",&n);
    printf("学号    姓名    年龄    年级\n");
    head=(struct student*)malloc(sizeof(struct student));
    head=create(n);

    printf("1删除信息    2插入信息    3查找信息    4排序信息    5打印信息\n");
    while(j){
            scanf("%d",&i);
    switch(i){
        case 1:{head=del(head);break;}
        case 2:{head=insert(head);break;}
        case 3:{head=search(head);break;}
        case 4:{head=sort(head);break;}
        case 5:{print(head);break;}
    }
    }
    return 0;
}

你可能感兴趣的:(c语言)