学生信息管理系统(链表)

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct stud_node{
	int num;
	char name[20];
	 int math,score,english,computer;
	 struct stud_node*next;
}; 
struct stud_node*Create_Stu_Doc();
struct stud_node*AmendDoc(struct stud_node*head,struct stud_node*p);
struct stud_node*DeleteDoc(struct stud_node*head,int num);
struct stud_node*Line (struct stud_node*head,struct stud_node*p);
void Foud(struct stud_node*head,struct stud_node*p);
void print_stu_node(struct stud_node*head);
int main()
{struct stud_node*head,*p;
int choice,num,english,computer,math,score,k;
char name[20];
int size=sizeof(struct stud_node);//计算结构体占多少字节,整形。

do
	{printf("1.添加信息		2.修改信息\n");
	printf("3.查询信息		4.删除信息\n");
	printf("5.数据排序		6.保存读取\n7.退出系统\n");
	printf("请输出你的选择(1~7):") ;
	scanf("%d",&choice);
	switch (choice){
		case 1:
				head=Create_Stu_Doc();
				break;
		case 2:
		        p=(struct stud_node*)malloc(size);
				printf("输入姓名:");
				scanf("%s",name);
				printf("输入修改后的学号:");
				scanf("%d",&num); 
				printf("输入修改后的数学成绩:");
				scanf("%d",&math);
				printf("输入修改后的英语成绩:");
				scanf("%d",&english);
				printf("输入修改后的计算机成绩:");
				scanf("%d",&computer) ;
				strcpy(p->name,name);
				p->num=num;
				p->math=math;
				p->english=english;
				p->computer=computer;
				p->score=p->computer+p->english+p->math+p->math;
				head=AmendDoc(head,p);
				break;
		case 3:p=(struct stud_node*)malloc(size);
			Foud(head,p);
			    	
				
				break;
		case 4:
				printf("输入学号:");
				scanf("%d",&num);
				head=DeleteDoc(head,num);
				break;
		case 5:
				p=head->next; 
				head=Line(head,p);
		case 6:
				print_stu_node(head);break;
		case 7:
			break;} 
	}while(choice!=7);
	
	return 0; 
}
 struct stud_node*Create_Stu_Doc()
	{int num,english,math,computer,score;
	char name[20];
	int size=sizeof(struct stud_node);
		struct stud_node*head,*tail,*p;
		head=tail=NULL;
		printf("intput num,name and math,english,computer: ");
		scanf("%d%s%d%d%d",&num,name,&math,&english,&computer);
		
		score=math+english+computer;
		while(num!=0){
			p=(struct stud_node*)malloc(size);//创建一个和结构体字节数相同的房间,并定义为储存结构体数据。如果成功则返回一个结构体指针。(实则就是一个地址)
			p->num=num;
			strcpy(p->name,name);
			p->computer=computer;
			p->math=math;
			p->english=english;
			p->next=NULL;
			p->score=p->english+p->math+p->computer;
			if(head==NULL)
			head=p;
			else 
			tail->next=p;
			tail=p;
			printf("intput 0 0 0 0 0 quit\n");
		printf("intput num,name and math,english,computer: ");
		scanf("%d%s%d%d%d",&num,name,&math,&english,&computer);
		}
		
		return head;
	}
	struct stud_node*AmendDoc(struct stud_node*head,struct stud_node*p) 
	{struct stud_node*tail;
	tail=head;
	while(tail!=NULL)
	{if(strcmp(p->name,tail->name)==0)
	{tail->num=p->num; 
	tail->math=p->math;
	tail->english=p->english;
	tail->computer=p->computer;
	tail->score=p->computer+p->english+p->math;
	break;
	}
	else
	tail=tail->next;
	
	}
	return head;
	}
	
    void	Foud(struct stud_node*head,struct stud_node*p)
	{int k;
	struct stud_node*tail;
				printf("1.按学号查询		2.按姓名查询\n3.全部显示		4.返回\n请输入你的选择:");
				scanf("%d",&k);
				switch(k){
					case 1:
							scanf("%d",&p->num);
							
							 break;
					case 2:
							scanf("%s",p->name);
							 
							 break;
					case 3:
						printf("输入姓名:");
						scanf("%s",p->name);
						printf("输入学号:");
						scanf("%d",&p->num); 
						printf("输入数学成绩:");
						scanf("%d",&p->math);
						printf("输入英语成绩:");
						scanf("%d",&p->english);
						printf("输入计算机成绩:");
						scanf("%d",&p->computer) ; break;
				    default:p=NULL ; break;}
				    if(k==1||k==3)
				{tail=head;
				    while(tail!=NULL)
				    {if(tail->num==p->num)
				    break;
				    else 
				    tail=tail->next;
					}}
					if(k==2)
					{tail=head;
					while(tail!=NULL)
					{if(strcmp(tail->name,p->name)==0)
					break;
					else tail=tail->next;
					}
					}if(p!=NULL)
					printf("%s\t%d\t%d\t%d\t%d\n",tail->name,tail->math,tail->english,tail->computer,tail->score);
					
					
	}
	struct stud_node*DeleteDoc(struct stud_node*head,int num)
	{
		struct stud_node*ptr1,*ptr2;
		while(head!=NULL&&head->num==num)
		{ptr2=head;
		head=head->next;
		free(ptr2);}
		if(head==NULL)
		return NULL;
		ptr1=head;
		ptr2=head->next;
		while(ptr2!=NULL)
		{if(ptr2->num==num){
			ptr1->next=ptr2->next;
			free(ptr2);
		}
		else ptr1=ptr2;
		ptr2=ptr1->next;
		}
		return head;
		}
	struct stud_node*Line (struct stud_node*head,struct stud_node*p)
	{struct stud_node*ptr1=head,*ptr2=p,*temp,*tail;
	int size=sizeof(struct stud_node);
		temp=(struct stud_node*)malloc(size);
	for(ptr1=head;ptr1!=NULL;ptr1=ptr1->next)
	for(ptr2=ptr1->next;ptr2!=NULL;ptr2=ptr2->next)
	{if(ptr1->score<ptr2->score)
	{strcpy(temp->name,ptr2->name);
	temp->math=ptr2->math;
	temp->english=ptr2->english;
	temp->computer=ptr2->computer;
	temp->score=ptr2->score;
	temp->num=ptr2->num;
	strcpy(ptr2->name,ptr1->name);
	ptr2->math=ptr1->math;
	ptr2->computer=ptr1->computer;
	ptr2->english=ptr1->english;
	ptr2->score=ptr1->score;
	ptr2->num=ptr1->num;
	strcpy(ptr1->name,temp->name);
	ptr1->math=temp->math;
	ptr1->computer=temp->computer;
	ptr1->english=temp->english;
	ptr1->score=temp->score;
	ptr1->num=temp->num;
	}
	}
	return head;	
	}
	void print_stu_node(struct stud_node*head)
	{struct stud_node*ptr;
	if(head==NULL){
		printf("\nno record\n");
		return;
	}
	printf("\nThe students' Records Are:\n ");
	for(ptr=head;ptr!=NULL;ptr=ptr->next)
	printf("%d\t%s\t%d\t%d\t%d\t%d\n",ptr->num,ptr->name,ptr->english,ptr->math,ptr->computer,ptr->score);
		}	

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值