C语言利用链表简单实现学生成绩管理系统

初学链表,加深自己对链表的认识与理解

#include
#include
#include
typedef struct student
{
	long num;
	char name[10];
	int C;
	int English;
	int Math;
	int sum;
	struct student *next; 
}Student;
//********************************************
Student *InitStudent()
{
	Student *p;
	p=(Student*)malloc(sizeof(Student));
	if (p!=NULL)
	{
		p->next=NULL;
		return p;
	}
	else
		return NULL;
}
//*******************************************
int create(Student *p)  //输入学生信息
{
	int flag;
	Student *s;
	while(flag)
	{
		s=(Student*)malloc(sizeof(Student));
		if(s==NULL)
		{
			printf("创建失败\n");
			return 0;
		}
		printf("输入学号:");
		scanf("%ld",&s->num);
		printf("输入姓名:");
		scanf("%s",s->name);
		printf("输入C语言成绩:");
		scanf("%d",&s->C);
		printf("输入英语成绩:");
		scanf("%d",&s->English);
		printf("输入数学成绩:");
		scanf("%d",&s->Math);
	//	n=s->num;
		s->sum=(s->C+s->English+s->Math);
		s->next=p->next;
		p->next=s;
		printf("是否继续输入信息:1/0\n");
		scanf("%d",&flag);
	}
	printf("输入完毕\n");
	return 1;
}
//*******************************************
Student *Find(Student *p,int x) //查询
{
	Student *q;
	q=p->next;
	while(q != NULL&&q->num != x)
		q=q->next;
	if(q!=NULL)
		return q;
	else
		return NULL;
}
//********************************************
int Modify(Student *p,int x)  //修改信息
{
	int flag,n;
	Student *q;
	q=p->next;
	while(q!=NULL&&q->num!=x)
		q=q->next;
	if(q!=NULL)
	{
		while(n)
		{
			printf("\n修改学科:1.C语言 2.英语 3.数学:");
			scanf("%d",&flag);
			if(flag==1)
			{
				getchar();
				printf("\n重新输入C语言成绩:");
				scanf("%d",&q->C);
			}
			else if(flag==2)
			{
				getchar();
				printf("\n重新输入英语成绩:");
				scanf("%d",&q->English);
			}
			else if(flag==3)
			{
				getchar();
				printf("\n重新输入数学成绩:");
				scanf("%d",&q->Math);
			}
			printf("\n是否继续修改成绩:1/0\n");
			scanf("%d",&n);
		}
		return 1;
	}
	return 0;
}
//****************************************************
Student *Sort(Student *p)//排序
{
	Student *a,*b,*t;
	int sum=0;
	char name[10];
	if(p->next==NULL);
	{
		printf("error!!!\n");
		return NULL;
	}
	for(a=p->next;a;a=a->next)
	{
		t=a;
		for(b=p->next;b;b=b->next)
		{
			if(t->sum >= a->sum)
			{
				strcpy(name,t->name);
				strcpy(t->name,a->name);
				strcpy(a->name,name);
				t=a;
			}
		}
	}
}
//**************************************************************
void print(Student *p)  //打印成绩
{
	Student *q;
	q = p;
	if(p->next == NULL)
		printf("无该学生成绩!");
	else
	{
		printf("结果为:\n");
		while(q->next!=NULL)
		{
			q=q->next;
			printf("|学号\t|姓名\t|C语言\t|英语\t|高数\t|总分\t|\n");
			printf("|%ld\t|%s\t|%d\t|%d\t|%d\t|%d\t|\n",q->num,q->name,q->C,q->English,q->Math,q->sum);
		}
	}
}
//***************************************************************
void Menu()
{
	int flag,x,y;
	Student *p,*q,*r;
	x=0;
	p=InitStudent();	
begin:
	printf("\n\t\t\t   学生成绩管理系统\n");
	printf("1.输入学生信息   2.修改学生信息   3.查询学生信息\n");
	printf("4.学生成绩排序   5.打印学生信息   \n");
	printf("\n请输入指令前的序号:");
	scanf("%d",&flag);
	switch(flag)
	{
		case 1:create(p);
			   //print(p);
			   goto begin;
		case 2:while(x==0)
			   {
				   printf("输入学号:");
				   scanf("%d",&x);
			   }
			   y=Modify(p,x);
			   if(y==1)
				   printf("修改成功!\n");
			   else
				   printf("修改失败!\n");
			   x=0;
			   goto begin;
		case 3:while(x==0)
			   {
				   printf("输入学生学号:");
				   scanf("%ld",&x);
			   }
			   q=Find(p,x);
			   if(q!=NULL)
			   {
				   printf("\n查询结果为:\n");
			       printf("|学号\t|姓名\t|C语言\t|英语\t|高数\t|总分\t|\n");
				   printf("|%ld\t|%s\t|%d\t|%d\t|%d\t|%d\t|\n",q->num,q->name,q->C,q->English,q->Math,q->sum);
			   }
			   else
				   printf("未查询到!\n");
			   x=0;
			   goto begin;
		case 4:r=Sort(p);
			   print(r);
			   goto begin;
		case 5:print(p);
			   goto begin;
	}
}
void main()
{
	Menu();
}

你可能感兴趣的:(C语言利用链表简单实现学生成绩管理系统)