其他分享
首页 > 其他分享> > 2021-06-03

2021-06-03

作者:互联网

#include <vector>
#include <stdio.h>
#pragma warning(disable:4996)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
void printList(struct xinxi* s, int m);
struct xinxi* createList(int m);
void chaxun(struct xinxi* s);
int xiugai(struct xinxi* s, int m);
int avera(struct xinxi* s, int m);
int cechu(struct xinxi* s);
int tianjia(struct xinxi* s);
void pingda80(struct xinxi* s, int m);
int paixu(struct xinxi* s, int m);
int caidan();
int caidan()
{
	printf("**********************\n");
	printf("1.学生基本信息录入\n");
	printf("2.学生基本信息输出\n");
	printf("3.学号查询学生信息\n");
	printf("4.学号修改学生信息\n");
	printf("5.输出学生平均成绩\n");
	printf("6.删除某学生信息\n");
	printf("7.添加某学生信息\n");
	printf("8.输出平均分大于80分的同学信息\n");
	printf("9.综合排名\n");
	printf("10.若退出,输入0");
	printf("**********************\n");
	printf("\n");
	return 0;
}
 
 
struct xinxi
{
	int xuehao;
	char xingming[10];
	float kemu[10];
	float ave;
	struct xinxi* next;
}xinxi1;
 
 
struct xinxi* createList(int m)
{
	struct xinxi* h, * q, * p;
	int i, n, j = 1, k;
	float zong = 0, aver;
	printf("请输入要输入的人数:");
	scanf("%d", &n);
	h = (struct xinxi*)malloc(sizeof(struct xinxi));
	if (h == NULL)
	{
		printf("动态内存分配失败\n");
		exit(0);
	}
	h->xuehao = 0;
	h->xingming[0] = '\0';
	h->kemu[0] = 0;
	h->ave = 0;
	h->next = NULL;
	q = h;
	for (i = 1; i <= n; i++)
	{
		p = (struct xinxi*)malloc(sizeof(struct xinxi));
		if (p == NULL)
		{
			printf("动态内存分配失败\n");
			exit(0);
		}
		printf("请输入第%d位学生信息:\n", i);
		printf("学号:  ");
		scanf("%d", &p->xuehao);
		printf("姓名:  ");
		scanf("%s", p->xingming);
		for (j = 1; j <= m; j++)
		{
			printf("第%d个成绩:  ", j);
			scanf("%f", &p->kemu[j]);
		}
		zong = 0;
		for (k = 1; k <= m; k++)
		{
			zong = p->kemu[k] + zong;
		}
		aver = zong / m;
		p->ave = aver;
		p->next = NULL;
		q->next = p;
		q = p;
	}
	return(h);
}
 
void printList(struct xinxi* s, int m)
{
	struct xinxi* t;
	int i;
	t = s->next;
	printf("学号	姓名	成绩\n");
	while (t != NULL)
	{
		printf("%d		%s	", t->xuehao, t->xingming);
		for (i = 1; i <= m; i++)
		{
			printf("第%d个成绩:%f	", i, t->kemu[i]);
		}
		printf("\n");
		t = t->next;
	}
}
 
void chaxun(struct xinxi* s, int m)
{
	struct xinxi* t;
	int a, b, i;
	printf("输入要查询的学号: ");
	scanf("%d", &a);
	t = s->next;
	while (t != NULL)
	{
		b = t->xuehao;
		if (a == b)
		{
			printf("学号:%d  姓名:%s  ", t->xuehao, t->xingming);
			for (i = 1; i <= m; i++)
			{
				printf("第%d个成绩:%f	", i, t->kemu[i]);
			}
			break;
		}
		t = t->next;
	}
}
 
int xiugai(struct xinxi* s, int m)
{
	struct xinxi* t;
	int a, b, i, j, k;
	float zong = 0, aver;
	printf("输入要修改的学号: ");
	scanf("%d", &a);
	t = s->next;
	while (t != NULL)
	{
		b = t->xuehao;
		if (a == b)
		{
			printf("请输入学生信息\n");
			printf("学号:  ");
			scanf("%d", &t->xuehao);
			printf("姓名:  ");
			scanf("%s", t->xingming);
			for (j = 1; j <= m; j++)
			{
				printf("第%d个成绩:  ", j);
				scanf("%f", &t->kemu[j]);
			}
			for (k = 1; k <= m; k++)
			{
				zong = t->kemu[k] + zong;
			}
			aver = zong / m;
			t->ave = aver;
			printf("\n修改过的学生信息为:\n");
			printf("学号:%d  姓名:%s  ", t->xuehao, t->xingming);
			for (i = 1; i <= m; i++)
			{
				printf("第%d个成绩:%f	", i, t->kemu[i]);
			}
			printf("平均成绩为:  %f", t->ave);
			break;
		}
		else
			printf("未查到此人!");
		t = t->next;
	}
	return 0;
}
 
 
int avera(struct xinxi* s, int m)
{
	struct xinxi* t;
	int i;
	t = s->next;
	while (t != NULL)
	{
		printf("学号为:%d的平均成绩为: %f\n", t->xuehao, t->ave);
		t = t->next;
	}
	return 0;
}
 
int cechu(struct xinxi* s)
{
	struct xinxi* p, * q=NULL;
	char xing[10];
	printf("输入要删除信息的学生姓名:");
	scanf("%s", &xing);
	if (s->next == NULL)
		printf("此链表是一个空链表\n");
	else
	{
		p = s->next;
		while (strcmp(p->xingming, xing) != 0 && p->next != NULL)
		{
			q = p;
			p = p->next;
		}
		if (strcmp(p->xingming, xing) == 0)
		{
			if (p == s->next)
				s->next = p->next;
			else if (p->next != NULL)
				q->next = p->next;
			else
				q->next = NULL;
			printf("学生%s的信息已被删除", xing);
			free(p);
		}
		else
			printf("没有找到要删除的学生!");
	}
	return 0;
}
 
 
int tianjia(struct xinxi* s, int m)
{
	struct xinxi* op, * t;
	int i;
	op = (struct xinxi*)malloc(sizeof(struct xinxi));
	t = s->next;
	printf("输入要插入的学生信息:");
	printf("学号:");
	scanf("%d", &op->xuehao);
	printf("姓名:");
	scanf("%s", op->xingming);
	for (i = 1; i <= m; i++)
	{
		printf("第%d个成绩:", i);
		scanf("%f", &op->kemu[i]);
	}
	s->next = op;
	op->next = t;
	return 0;
}
 
 
void pingda80(struct xinxi* s, int m)
{
	struct xinxi* t;
	int i;
	t = s->next;
	printf("学号	姓名	成绩\n");
	while (t != NULL)
	{
		if (t->ave >= 80.0)
		{
			printf("%d		%s	", t->xuehao, t->xingming);
			for (i = 1; i <= m; i++)
			{
				printf("第%d个成绩:%f	", i, t->kemu[i]);
			}
			printf("\n");
		}
		t = t->next;
	}
}
 
 
int paixu(struct xinxi* s, int m)
{
	struct xinxi* t, * p, * q, * qt;
	int n = 0, i, k;
	t = s->next;
	while (t != NULL)
	{
		n++;
		t = t->next;
	};
	for (i = 1; i <= n; i++)
	{
		p = s->next;
		q = s->next;
		while (p != NULL)
		{
			p = p->next;
			if (q->ave < p->ave)
			{
				xinxi1.xuehao = q->xuehao;
				strcpy(xinxi1.xingming, q->xingming);
				for (k = 1; k <= m; k++)
				{
					xinxi1.kemu[i] = q->kemu[i];
				}
				xinxi1.ave = q->ave;
 
 
				q->xuehao = p->xuehao;
				strcpy(q->xingming, p->xingming);
				for (k = 1; k <= m; k++)
				{
					q->kemu[i] = p->kemu[i];
				}
				q->ave = p->ave;
 
				p->xuehao = xinxi1.xuehao;
				strcpy(p->xingming, xinxi1.xingming);
				for (k = 1; k <= m; k++)
				{
					p->kemu[i] = xinxi1.kemu[i];
				}
				p->ave = xinxi1.ave;
			}
			q = q->next;
		}
	}
	qt = s->next;
	printf("学号	姓名	成绩\n");
	while (t != NULL)
	{
		printf("%d		%s	", qt->xuehao, qt->xingming);
		for (i = 1; i <= m; i++)
		{
			printf("第%d个成绩:%f	", i, qt->kemu[i]);
		}
		printf("\n");
		t = t->next;
	}
	return 0;
}
 
 
 
int main()
{
	struct xinxi* head = new xinxi();
	int m, qa;
	printf("输入要输入的科目数量:");
	scanf("%d", &m);
	caidan();
	printf("请选择你要进行的操作:");
	scanf("%d", &qa);
	while (qa != 0)
	{
 
		switch (qa)
		{
		case 1:	head = createList(m); break;
		case 2: printList(head, m); break;
		case 3: chaxun(head, m); break;
		case 4: xiugai(head, m); break;
		case 5: avera(head, m); break;
		case 6: m = cechu(head); break;
		case 7: m = tianjia(head, m); break;
		case 8: pingda80(head, m); break;
		case 9: paixu(head, m); break;
		}
		printf("\n");
		printf("请继续你的操作,若退出,请输入0:   ");
		scanf("%d", &qa);
	}
}
 

标签:03,06,struct,int,next,xinxi,2021,printf,xuehao
来源: https://blog.csdn.net/technologist_32/article/details/117534838