以链表为基础的学生信息小系统

以链表为基础的学生信息小系统

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<Windows.h>
void print(struct List* phead);
void AddAtEnd(struct List** pphead, char *id,char *name,int chinese,int math,int english);
void AddAtFirst(struct List** pphead, char *id, char *name, int chinese, int math, int english);
void DeleteAtEnd(struct List** pphead);
void DeleteAtFirst(struct List** pphead);
void research(struct List* phead);
void Insert(struct List** pphead);
void DeleteAtId(struct List** pphead);
void my_sort(struct List** pphead);
void sway(struct List** tail_1, struct List** tail_2);
void method_1(struct List** pphead);
void method_2(struct List** pphead);
void title();
struct List {
	char Id[20];
	char Name[10];
	int Chinese;
	int Math;
	int English;
	double Average;
	struct List* next;
};
int main()
{
	struct List* phead = NULL;
	while (1)
	{
		title();
		int n;
		scanf("%d", &n);
		getchar();
		switch (n)
		{
		case 0:
			printf("确认退出? Y/N\n");
			char a;
			scanf("%c", &a);
			if(a=='Y')
				return 0;
			break;
		case 1:
			method_1(&phead);
			break;
		case 2:
			method_2(&phead);
			break;
		case 3:
			DeleteAtFirst(&phead);
			break;
		case 4:
			DeleteAtEnd(&phead);
			break;
		case 5:
			Insert(&phead);
			break;
		case 6:
			research(phead);
			break;
		case 7:
			DeleteAtId(&phead);
			break;
		case 8:
			my_sort(&phead);
			break;
		case 9:
			print(phead);
			break;
		default:
			printf("输入错误\n");
		}
		Sleep(1000);
		printf("任意按键退出\n");
		getchar();
		getchar();  
	}
	return 0;
}

void title()
{
	printf("*****************************************************************\n");
	printf("*****************************************************************\n");
	printf("***                                                           ***\n");
	printf("***                                                           ***\n");
	printf("***                  学生管理链表系统                         ***\n");
	printf("***                                                           ***\n");
	printf("***                  头部插入        请按  1                  ***\n");
	printf("***                  尾部插入        请按  2                  ***\n");
	printf("***                  删除第一个      请按  3                  ***\n");
	printf("***                  删除最后一个    请按  4                  ***\n");
	printf("***                  在特定序号插入  请按  5                  ***\n");
	printf("***                  按照ID查找      请按  6                  ***\n");
	printf("***                  删除某个特定ID  请按  7                  ***\n");
	printf("***                  平均成绩排序    请按  8                  ***\n");
	printf("***                  输出全部        请按  9                  ***\n");
	printf("***                  退出            请按  0                  ***\n");
	printf("***                                                           ***\n");
	printf("*****************************************************************\n");
	printf("*****************************************************************\n");
}
void method_1(struct List**pphead)
{
	char id[20];
	char name[10];
	int c, m, e,num;
	printf("输入几位信息:\n");
	scanf("%d", &num);
	printf("请以此输入学生信息:\nId\t\tName\tChinese\tMath\tEnglish\n");
	for (int i = 0; i < num; i++)
	{
		scanf("%s %s %d %d %d", id, name, &c, &m, &e);
		AddAtFirst(pphead, id, name, c, m, e);
		printf("添加完成\n");
	}
}

void method_2(struct List** pphead)
{
	char id[20];
	char name[10];
	int c, m, e, num;
	printf("输入几位信息:\n");
	scanf("%d", &num);
	printf("请以此输入学生信息:\nId\t\tName\tChinese\tMath\tEnglish\n");
	for (int i = 0; i < num; i++)
	{
		scanf("%s %s %d %d %d", id, name, &c, &m, &e);
		AddAtEnd(pphead, id, name, c, m, e);
		printf("添加完成");
	}
}


void print(struct List* phead) {
	const char* arr[] = { "Id","Name","Chinese","Math","English","Average" };
	printf("\n%-15s%-10s%-8s%-8s%-8s%s\n", arr[0], arr[1], arr[2], arr[3], arr[4], arr[5]);
	if (phead == NULL) {
		printf("NULL\n");
		return;
	}
	while (phead->next != NULL) {
		printf("%-15s%-10s%-8d%-8d%-8d%.2lf\n",phead->Id,phead->Name,phead->Chinese,phead->Math,phead->English,phead->Average);
		phead = phead->next;
	}
	printf("%-15s%-10s%-8d%-8d%-8d%.2lf\n", phead->Id, phead->Name, phead->Chinese, phead->Math,phead->English, phead->Average);
}


void AddAtFirst(struct List** pphead, char* id, char* name, int chinese, int math, int english)
{
	struct List* new_num = (struct List*)malloc(sizeof(struct List));
	strcpy(new_num->Id,id);
	strcpy(new_num->Name, name);
	new_num->Chinese = chinese;
	new_num->Math = math;
	new_num->English = english;
	new_num->Average = (chinese + math + english) / 3.0;
	new_num->next = *pphead;
	*pphead = new_num;
}


void AddAtEnd(struct List** pphead, char *id, char* name, int chinese, int math, int english)
{
	struct List* tail = *pphead;
	struct List* new_num = (struct List*)malloc(sizeof(struct List));
	strcpy(new_num->Id, id);
	strcpy(new_num->Name, name);
	new_num->Chinese = chinese;
	new_num->Math = math;
	new_num->English = english;
	new_num->Average = (chinese + math + english) / 3.0;
	new_num->next = NULL;
	if (*pphead == NULL) {
		*pphead = new_num;
		return;
	}
	while (tail->next != NULL)
		tail = tail->next;
	tail->next = new_num;
}

void DeleteAtFirst(struct List** pphead) {
	if (*pphead == NULL)
		return;
	else if ((*pphead)->next == NULL)
	{
		(*pphead) == NULL;
		return;
	}
	else
		(*pphead) = (*pphead)->next;
}



void DeleteAtEnd(struct List** pphead) {
	struct List* tail_1 = *pphead;
	if (tail_1 == NULL)
		return;
	else if (tail_1->next == NULL) {
		*pphead = NULL;
		return;
	}
	else {
		tail_1 = tail_1->next;
	}
	struct List* tail_2 = *pphead;
	while (tail_1->next != NULL)
	{
		tail_1 = tail_1->next;
		tail_2 = tail_2->next;
	}
	tail_2->next = NULL;
}
void Insert(struct List** pphead) {
	printf("插入位置为:");
	char id[20], name[10];
	int chinese, math,  english,  loc;
	scanf("%d",&loc);
	printf("请以此输入学生信息:\nId\t\tName\tChinese\tMath\tEnglish\n");
	scanf("%s %s %d %d %d", id, name, &chinese, &math, &english);
	struct List* tail = *pphead;
	struct List* new_num = (struct List*)malloc(sizeof(struct List));
	strcpy(new_num->Id, id);
	strcpy(new_num->Name, name);
	new_num->Chinese = chinese;
	new_num->Math = math;
	new_num->English = english;
	new_num->Average = (chinese + math + english) / 3.0;
	new_num->next = NULL;
	if (loc == 1) {
		new_num->next = *pphead;
		*pphead = new_num;
		return;
	}
	int i = 1;
	while (tail->next != NULL)
	{
		i++;
		if (loc == i) {
			new_num->next = tail->next;
			tail->next = new_num;
			break;
		}
		tail = tail->next;
	}
	if (1 + i == loc) {
		tail->next = new_num;
		return;
	}
	else if (loc == i)
		return;
	printf("对不起,插入失败,这个链表最多没有那么多元素\n");
}
void DeleteAtId(struct List** pphead)
{
	printf("输入删除学生ID:\n");
	char id[20];
	scanf("%s", id);
	struct List* tail_1 = *pphead;
	if (tail_1 == NULL)
	{
		printf("无该id");
		return;
	}
	else if ((tail_1->next == NULL) && (strcmp(tail_1->Id, id) == 0))
	{
		*pphead = NULL;
		return;
	}
	else
		tail_1 = tail_1->next;
	struct List* tail_2 = *pphead;
	while (tail_1->next != NULL)
	{
		tail_1 = tail_1->next;
		tail_2 = tail_2->next;
		if (strcmp(tail_1->Id, id) == 0)
		{
			tail_2->next = tail_1;
			printf("\n删除成功:%-15s%-10s%-4d%-4d%-4d%.2lf\n", tail_1->Id, tail_1->Name, tail_1->Chinese,tail_1->Math, tail_1->English, tail_1->Average);
			return ;
		}
	}
	if (strcmp(tail_1->Id, id) == 0)
	{
		printf("%-15s%-10s%-4d%-4d%-4d%.2lf\n删除成功", tail_1->Id, tail_1->Name, tail_1->Chinese, tail_1->Math, tail_1->English, tail_1->Average);
		tail_2->next = NULL;
	}
	else
		printf("删除失败,无该id");
	return;
}

void research(struct List* phead) 
{
	printf("输入查找学生ID:\n");
	char id[20];
	scanf("%s", id);
	int tail = 0;
	if (phead != NULL)
	{
		//除最后一位以外的遍历查找。
		while (phead->next != NULL)
		{
			tail++;
			if (strcmp(id,phead->Id)==0)
			{ 
				printf("在第%d位\n%-15s%-10s%-4d%-4d%-4d%.2lf\n",tail, phead->Id, phead->Name, phead->Chinese, phead->Math, phead->English, phead->Average);
				return ;
			}	
			phead = phead->next;
		}
		if (strcmp(id, phead->Id) == 0)
			printf("在第%d位\n%-15s%-10s%-4d%-4d%-4d%.2lf\n", tail+1, phead->Id, phead->Name, phead->Chinese, phead->Math, phead->English, phead->Average);
	}
	printf("无该学生信息");
	return;
}

void my_sort(struct List** pphead)
{
	struct List* tail_1 = *pphead;
	if ((tail_1 == NULL) || (tail_1->next == NULL))
		return;
	tail_1 = tail_1->next;
	struct List* tail_2 = *pphead;
	struct List* tail_3 = *pphead;
	for (; tail_3->next != NULL;)
	{
		tail_2 = *pphead;
		tail_1 = tail_2->next;
		for (; tail_1->next != NULL;)
		{
			if (tail_2->Average < tail_1->Average)
				sway(&tail_1, &tail_2);
			tail_1 = tail_1->next;
			tail_2 = tail_2->next;
		}
		if (tail_2->Average < tail_1->Average)
			sway(&tail_1, &tail_2);
		tail_3 = tail_3->next;
	}
}
void sway(struct List** tail_1, struct List** tail_2)
{
	char str[20];
	strcpy(str, (*tail_1)->Id);
	strcpy((*tail_1)->Id, (*tail_2)->Id);
	strcpy((*tail_2)->Id, str);

	strcpy(str, (*tail_1)->Name);
	strcpy((*tail_1)->Name, (*tail_2)->Name);
	strcpy((*tail_2)->Name, str);

	int x = (*tail_1)->Chinese;
	(*tail_1)->Chinese = (*tail_2)->Chinese;
	(*tail_2)->Chinese = x;

	x = (*tail_1)->Math;
	(*tail_1)->Math = (*tail_2)->Math;
	(*tail_2)->Math = x;

	x = (*tail_1)->English;
	(*tail_1)->English = (*tail_2)->English;
	(*tail_2)->English = x;

	double b= (*tail_1)->Average;
	(*tail_1)->Average = (*tail_2)->Average;
	(*tail_2)->Average = b;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

「 25' h 」

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值