Demo entry 6782503

my

   

Submitted by Heiwater on Jan 17, 2019 at 07:43
Language: C. Code size: 4.6 kB.

#include<stdio.h>
#include<stdlib.h>

#define LEN sizeof(struct student)

int thenumber = 0;						//删除学生的学号
int theincertnumber=0;


struct student							//学生信息的结构体
{
	long num;
	float score;
	struct student*next;
};

struct student*head;
//struct student*myin;
int n;

struct student*creat(void)				//添加学生信息
{
	struct student*head;
	struct student *p1, *p2;
	n = 0;
	p1 = p2 = (struct student*)malloc(LEN);
	scanf("%ld,%f",&p1->num,&p2->score);
	head = NULL;
	while (p1->num!=0)
	{
		n = n + 1;
		if (n == 1) head = p1;
		else p2->next = p1;
		p2 = p1;
		p1 = (struct student*)malloc(LEN);
		scanf("%ld,%f",&p1->num, &p1->score);
	}
	p2->next = NULL;
	return (head);
};

void print(struct student*head)			//输出学生信息
{
	struct student*p;
	printf("\nNow,These %d records are :\n", n);
	p = head;
	if(head!= NULL)
		do
		{
			printf("%ld %5.1f\n", p->num, p->score);
			p = p->next;
		} while (p!=NULL);
}

void myaverage(struct student*head)		//求学生的平均分
{
	float sum=0,n=0;
	float nowmax=0;
	struct student*p,*pt;

	p = head;
	pt = head;

	if (head != NULL)
		do
		{
			sum = sum + p->score;
			if (p->score > nowmax)
			{
				nowmax = p->score;
				pt = p;
			}
			n++;
			p = p->next;
		} while (p != NULL);

	sum = sum / n;
	
	printf("所有人的平均成绩为 %f\n", sum);
	printf("最高分的学生为\n");
	printf("\nnum:%ld\nscore:%5.1f\n", pt->num, pt->score);
}

void mydelet(struct student*head)		//删除某个学生的信息
{
	struct student*p,*pt;
	p = head;
	pt = head;
	pt = p->next;

	if (head != NULL)
		while (p != NULL && pt != NULL)		//判断关系,不能为空不然指针指向空
		{
			if (p->num == thenumber)
			{
				head = pt;
			}

			if (pt->num == thenumber)
			{
				if (pt->next == NULL)
				{
					p->next = NULL;
					break;
				}
				p->next = pt->next;
			}
			p = p->next;
			pt = p->next;
		} 
}

void myincert(struct student*head)		//插入学生成绩
{
	struct student*p,*pt;
	struct student*myin;
	p = head;
	pt = head;

	myin = (struct student*)malloc(LEN);

	/*
	myin->num = 00000;
	myin->score = 00000;
	myin->next = NULL;
	*/
	
	printf("请输入你想插入的信息\n");
	scanf("%ld,%f", &myin->num, &myin->score);
	myin->next = NULL;
	
	if (head != NULL)
		do
		{
			if (p->num==theincertnumber)
			{
				pt = p->next;
				p->next = myin;
				myin->next = pt;
			}
			p = p->next;
		} while (p != NULL);

}

int main()
{
	//struct student*head;
	//head = creat();
	printf("****学生管理系统****\n\n\n\n");
	printf("****00****添加新的初始化成绩\n****11****输出所有的成绩\n****22****输出平均成绩和最优秀的学生\n****33****删除指定成绩\n****44****插入成绩\n\n****886****结束程序\n\n\n");
	for (;;)				//实现多次操作的死循环
	{
		int a;
		printf("输入相应指令\n");
		scanf("%d", &a);
		if (a == 00)		//添加新的成绩
		{
			printf("初始化输入成绩\n现在请输入成绩 格式为 学号,成绩\n以0,0结束\n");
			head = creat();
		}
		if (a == 11)		//输出所有成绩
			print(head);

		if (a == 22)		//输出平均成绩
			myaverage(head);

		if (a == 33)		//删除成绩
		{
			printf("请输入想要删除的成绩的学号\n");
			scanf("%d", &thenumber);
			mydelet(head);
		}

		if (a == 44)		//插入成绩
		{
			printf("插入成绩\n请输入想要插入后方的学号\n");
			scanf("%d", &theincertnumber);
			myincert(head);
		}
		
		if (a == 886)		//结束
			break;
	}
//	print(head);
//	printf("\nnum:%ld\nscore:%5.1f\n", pt->num, pt->score);
	return 0;
}

/*
	***************************************
	******实现思路**************************
	***************************************
	***************************************
	****删除实现的思路************************
	找到想要删除的数据所指向的数据
	将指向删除的数据的next和删除数据的next进行调换
	***************************************
	***************************************
	****遍历链表思路*************************
	利用链表的头指针遍历时p->next头指针
	找到自己需要的,没有继续
	结束时如果链表的头指针为NULL结束遍历
	if(head!= NULL)
		do
		{
			//******内容******
			p = p->next;
		} while (p!=NULL);
	***************************************
	***************************************
	****平均值思路***************************
	对维护的链表进行一次遍历
	遍历的途中将每一个学生的成绩进行记录
	并统计学生总数
	记录完成后将总成绩处以总数得到平均值
	***************************************
	***************************************
	****最好学生的思路***********************
	在进行遍历时设置一个对比值(初始值为零)
	比对比值大的存下来,并记录它的地址
	在最后遍历完之后剩下的就是最大的输出即可
	***************************************
	***************************************
	****插入实现思路*************************
	先创建一个空的student的结构体
	并将该结构体存进去
	进行一次遍历
	再找到需要插入的学生的学号
	将该学生的next指向插入的学生的首地址
	并将插入学生的next指向原本应该插入的地址
	***************************************
	***************************************
	//******  软工八班	*****
	//******  张水源	******
	//****18060208125	*****
*/

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).