您的位置:首页 > 编程语言 > C语言/C++

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

2017-10-14 19:47 591 查看
用C语言实现的学生管理系统

可以输入学生的学号,姓名,年龄,年级;

可以输入学生的学号,姓名,年龄,年级;

插入学生信息,默认插入到第一个

通过学号,对学生进行排序

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

#include <stdio.h>
#include <stdlib.h>
#include<malloc.h>
#include<string.h>
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-&
4000
gt;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语言 指针