0
点赞
收藏
分享

微信扫一扫

【第四天】c语言学生信息系统

眸晓 2022-03-25 阅读 51
c++c语言

main.cpp


#include"struct.h"
#include"menu_select.h"
#include"SORT.h"
#include"FILE_cin.h"
#include"CIN.h"
#include"COUT.h"
#include"CHANGE.h"
#include"EREASE.h"
#include"SEARCH.h"
int main()
{
	printf("1.输入\n");
	printf("2.修改\n");
	printf("3.删除\n");
	printf("4.查询\n");
	printf("5.显示学生信息并且统计\n");
	printf("6.排序\n");
	printf("7.退出菜单\n");
	char c;
	while (1)
	{
		c = getch();
		int ans = (int)c - 48;
    	menu_select(ans);	
	}
	return 0;
}

 CHANGE.h

#include<iostream>
#include<algorithm>
#include<vector>
#include<malloc.h>
#include<math.h>
#include<stdio.h>
#include<conio.h>
#include<windows.h>
using namespace std;
struct node;
void CHANGE()
{
	printf("请输入学生的学号:\n");
	int num, flag = 0;
	cin >> num;
	node* p1 = head;
	while (p1 != NULL)
	{
		if (p1->num == num)
		{
			flag++;
			printf("修改前信息如下:\n语文:%-10d高数:%-10d总分:%-10d\n", p1->Chinese, p1->Math, p1->Sum);
			printf("已经找到该学生,请输入修改后信息:\n");
			printf("语文:"); cin >> p1->Chinese;
			printf("高数:"); cin >> p1->Math;
			p1->Sum = p1->Math + p1->Chinese;
			printf("修改后信息如下:\n语文:%-10d高数:%-10d总分:%-10d\n", p1->Chinese, p1->Math, p1->Sum);
			break;
		}
		p1 = p1->next;
	}
	if (!flag) printf("未找到该学生:\n");
}

 CIN.h

#include<iostream>
#include<algorithm>
#include<vector>
#include<malloc.h>
#include<math.h>
#include<stdio.h>
#include<conio.h>
#include<windows.h>
struct node;
node* CIN(node* head)
{
	node* p1 = (node*)malloc(sizeof(node)), * p2 = head;
	if (p1 == NULL)
	{
		printf("NO MEMORY\n");
		exit(0);
	}
	if (head == NULL) head = p1;
	else
	{
		while (p2->next != NULL) p2 = p2->next;
		p2->next = p1;
	}
	printf("请输入学生的姓名,学号,语文和高数成绩\n");
	cin >> p1->name >> p1->num >> p1->Chinese >> p1->Math;
	p1->Sum = p1->Chinese + p1->Math;
	p1->next = NULL;//注意要把每次新开辟的节点的next值赋值为NULL
	return head;//返回头节点指向的地址
}

 COUT.h

#include<iostream>
#include<algorithm>
#include<vector>
#include<malloc.h>
#include<math.h>
#include<stdio.h>
#include<conio.h>
#include<windows.h>
using namespace std;
struct node;
void COUT(node* head)
{
	node* p1 = head;
	while (p1 != NULL)
	{
		printf("%s(%d):\n", p1->name, p1->num);
		printf("语文:%-10d高数:%-10d总分:%-10d\n", p1->Chinese, p1->Math, p1->Sum);
		p1 = p1->next;
	}
	int MAX1 = 0, MAX2 = 0, MIN1 = 10000, MIN2 = 10000, MAX3 = 0, MIN3 = 10000;
	double cnt1 = 0, cnt2 = 0, cnt3 = 0;
	node* p = head;
	while (p != NULL)
	{
		MIN1 = min(MIN1, p->Chinese);
		MIN2 = min(MIN2, p->Math);
		MAX1 = max(MAX1, p->Chinese);
		MAX2 = max(MAX2, p->Math);
		MIN3 = min(MIN3, p->Sum);
		MAX3 = max(MAX3, p->Sum);
		if (p->Chinese < 60) cnt1++;
		if (p->Math < 60) cnt2++;
		cnt3++;
		p = p->next;
	}
	printf("语文最高分:%-5d语文最低分:%-5d\n", MAX1, MIN1);
	printf("数学最高分:%-5d数学最低分:%-5d\n", MAX2, MIN2);
	printf("总分最高分:%-5d总分最低分:%-5d\n", MAX3, MIN3);
	printf("语文不及格率为:%.5f", cnt1 / cnt3); putchar('\n');
	printf("数学不及格率为:%.5f", cnt2 / cnt3); putchar('\n');

}

 EREASE.h

#include<iostream>
#include<algorithm>
#include<vector>
#include<malloc.h>
#include<math.h>
#include<stdio.h>
#include<conio.h>
#include<windows.h>
using namespace std;
#pragma warning (disable:4996)
struct node;
void EREASE()//注意删除非头节点和头节点为两种情况!!!!
{
	printf("请输入要删除的学生的学号:\n");
	int num, flag = 0;
	cin >> num;
	node* p1 = head;
	if (p1->num == num)//删除链表的头节点
	{
		p1 = p1->next;
		head = p1;
		printf("删除完毕\n");
		return;
	}
	while (p1->next != NULL)//删除的为非头节点
	{
		if (p1->next->num == num)
		{
			flag++;
			p1->next = p1->next->next;
			printf("删除完毕\n");
			break;
		}
		p1 = p1->next;
	}
	if (!flag) printf("未找到该学生\n");
}

 FILE_cin.h

#include<iostream>
#include<algorithm>
#include<vector>
#include<malloc.h>
#include<math.h>
#include<stdio.h>
#include<conio.h>
#include<windows.h>
using namespace std;
struct node;
void FILE_cin()
{
	FILE* fp;
	node* p1 = head;
	fp = fopen("学生成绩信息表.txt", "a");
	while (p1 != NULL)
	{
		fprintf(fp, "%s(%d)\n%10d%10d10%d\n", p1->name, p1->num, p1->Chinese, p1->Math, p1->Sum);
		p1 = p1->next;
	}
}

 menu_select.h

#include<iostream>
#include<algorithm>
#include<vector>
#include<malloc.h>
#include<math.h>
#include<stdio.h>
#include<conio.h>
#include<windows.h>
node* CIN(node* head);
void CHANGE();
void EREASE();
void COUT(node* head);
void SEARCH();
void FILE_cin();
void SORT(node* lis);
void menu_select(int ans)
{
	switch (ans)
	{
	case 1:int n;
		printf("请输入学生的数量:");
		cin >> n;
		for (int i = 1; i <= n; i++) head = CIN(head);
		printf("输入完毕\n");
		break;
	case 2:
		CHANGE();
		break;
	case 3:
		EREASE();
		break;
	case 4:
		SEARCH();
		break;
	case 5:
		printf("所有学生的信息如下:\n");
		COUT(head);
		break;
	case 6:
		SORT(head);
		break;
	case 7:
		system("cls");
		FILE_cin();
		printf("谢谢您的使用\n");
		exit(0);
	default:printf("请输入 1---7\n");
	}
}

 SEARCH.h

#include<iostream>
#include<algorithm>
#include<vector>
#include<malloc.h>
#include<math.h>
#include<stdio.h>
#include<conio.h>
#include<windows.h>
using namespace std;
struct node;
void SEARCH()
{
	int num;
	cin >> num;
	node* p1 = head;
	while (p1 != NULL)
	{
		if (p1->num == num)
		{
			printf("语文:%-10d高数:%-10d总分:%-10d\n", p1->Chinese, p1->Math, p1->Sum);
			return;
		}
		p1 = p1->next;
	}
	printf("未找到该学生\n");
}

#include<iostream>
#include<algorithm>
#include<vector>
#include<malloc.h>
#include<math.h>
#include<stdio.h>
#include<conio.h>
#include<windows.h>
struct node;
void swap_string(char *p1,char *p2)
{
    int t[11];
    strcpy(t,p1);
    strcpy(p1,p2);
    strcpy(p2,t);
}
node* partation(node* low, node* hi)
{
	int key = low->Sum;
	node* pslow = low;
	node* pquick = low->next;
	while (pquick != hi)
	{
		if (pquick->Sum < key)
		{
			pslow = pslow->next;
			swap(pslow->Sum, pquick->Sum);
			swap(pslow->Chinese, pquick->Chinese);
			swap(pslow->Math, pquick->Math);
			swap(pslow->num, pquick->num);
			swap_string(pslow->name, pquick->name);
		}
		pquick = pquick->next;
	}
	swap(pslow->Sum, low->Sum);
	swap(pslow->Chinese, low->Chinese);
	swap(pslow->Math, low->Math);
	swap(pslow->num, low->num);
	swap_string(pslow->name, low->name);
	return pslow;//pslow记录的是头节点最后所在的位置
}
void quicksort(node* low, node* hi)
{
	if (low == hi) return;
	node* par = partation(low, hi);
	quicksort(low, par);    //因为最后一个元素不处理
	quicksort(par->next, hi);
}
void SORT(node* lis)
{
	quicksort(lis, NULL);
}

 struct.h

#include<iostream>
#include<algorithm>
#include<vector>
#include<malloc.h>
#include<math.h>
#include<stdio.h>
#include<conio.h>
#include<windows.h>
using namespace std;
#pragma warning (disable:4996)
struct node
{
	int Chinese, Math, Sum, num;
	char name[11];
	node* next;
};
node* head = NULL;
举报

相关推荐

0 条评论