其他分享
首页 > 其他分享> > 用C语言写学生成绩管理系统

用C语言写学生成绩管理系统

作者:互联网

源代码:

test.c文件

#define _CRT_SECURE_NO_WARNINGS 1

#include"student.h"

void menu()
{
	printf("-------------------------------------------------------------------\n");
	printf("||            ^^^***The information of student***^^^             ||\n");
	printf("===================================================================\n");
	printf("|| 1、Input the information         2、Put out the information   ||\n");
	printf("|| 3、Search for the information    4、Modify the information    ||\n");
	printf("|| 5、Show the max                  6、Delete the information    ||\n");
	printf("|| 7、Work out the total or average 8、Sort                      ||\n");
	printf("|| 9、Save the information          0、Exit the system           ||\n");
	printf("===================================================================\n");
	printf("    |   The user can make a choice from the fuction above:   |     \n");
}


int main()
{
	int input;
	ReadInformation();
	do
	{
		menu();
		printf("\n请输入您需要选择的操作:");
		scanf("%d", &input);
		switch (input)
		{
			case 1:
				InputInformation();
				printf("录入成功!\n");
				SaveInformation();
				break;
			case 2:
				PutInformation();
				break;
			case 3:
				SearchInformation();
				break;
			case 4:
				ModifyInformation();
				SaveInformation();
				break;
			case 5:
				ShowMax();
				break;
			case 6:
				DeleteInformation();
				SaveInformation();
				break;
			case 7:
				Average();
				break;
			case 8:
				Sort();
				break;
			case 9:
				SaveInformation();
				break;
			case 0:
				SaveInformation();
				printf("\n成功退出,欢迎下次使用!\n");
				break;
			default:
				printf("\n输入错误请重新输入!\n");
				break;
		}

	} while (input);
	
	return 0;
}

student.h文件

#define _CRT_SECURE_NO_WARNINGS 1
#pragma once

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

#define MAX_NAME 20
#define MAX_SEX 10
#define MAX_STU_CLASS 20
#define MAX_AVG 5


typedef struct stu_type
{
	char name[MAX_NAME];	//名字
	long num;				//学号
	int age;				//年龄
	char sex[MAX_SEX];		//性别
	char stu_class[MAX_STU_CLASS];	//班级
	double C;
	double M;
	double E;
	double S;
	double sum;
}student;
// 创建链表

typedef struct _Node
{
	student stu;	//学生
	struct _Node *pNext;	//指向下一个节点的指针
}Node;



//函数声明
void SaveInformation();		//保存学生信息
void ReadInformation();		//读取学生信息
void InputInformation();	//信息录入
void PutInformation();		//展示信息
void SearchInformation();	//查找信息
void ModifyInformation();	//修改信息
void ShowMax();				//显示最高分
void DeleteInformation();	//删除信息
void Average();				//平均分
void Sort();
void PrintfInfo(Node *p);//排序

student.c文件

#define _CRT_SECURE_NO_WARNINGS 1
#include"student.h"

//头结点
Node *g_phead = NULL;
Node *last = NULL;


//ok
void SaveInformation()
{
	Node *p=g_phead;
	FILE *pfwrite=fopen("student.dat","w");
	if (pfwrite == NULL)
	{
		printf("SaveInformation::%s", strerror(errno));
	}
	while (p)
	{
		fwrite(&(p->stu), sizeof(student), 1, pfwrite);
		p = p->pNext;
	}
	fclose(pfwrite);
	pfwrite = NULL;
}
//ok
void ReadInformation()
{
	student tem = {0};
	FILE *pfread=fopen("student.dat","r");
	if (pfread == NULL)
	{
		printf("ReadInformation::%s", strerror(errno));
		return;
	}
	while (fread(&tem, sizeof(student), 1, pfread))
	{
		Node *pNewNext = (Node*)malloc(sizeof(Node));
		pNewNext->pNext = NULL;
		pNewNext->stu = tem;
		if (g_phead == NULL)
		{
			g_phead = pNewNext;
		}
		else
		{
			pNewNext->pNext = g_phead;
			g_phead = pNewNext;
		}
	}
	
	fclose(pfread);
	pfread = NULL;
}	  //
//ok
void InputInformation()
{
	//创建链表

	//创建一个新结点
	Node *pNewNode = (Node*)malloc(sizeof(Node));
	pNewNode->pNext = NULL;
	//头插法
	if (g_phead == NULL)
	{
		g_phead = pNewNode;
	}
	else
	{
		pNewNode->pNext = g_phead;
		g_phead = pNewNode;
	}
	//输入学生信息
	printf("请输入学生信息\n");
	printf("请输入学生姓名:");
	scanf("%s",g_phead->stu.name);
	printf("请输入学生学号:");
	scanf("%ld", &g_phead->stu.num);
	printf("请输入学生年龄:");
	scanf("%d", &g_phead->stu.age);
	printf("请输入学生性别:");
	scanf("%s", g_phead->stu.sex);
	printf("请输入学生班级:");
	scanf("%s", g_phead->stu.stu_class);
	printf("请输入学生语文成绩:");
	scanf("%lf", &g_phead->stu.C);
	printf("请输入学生数学成绩:");
	scanf("%lf", &g_phead->stu.M);
	printf("请输入学生英语成绩:");
	scanf("%lf", &g_phead->stu.E);
	printf("请输入学生体育成绩:");
	scanf("%lf", &g_phead->stu.S);
	g_phead->stu.sum = g_phead->stu.C + g_phead->stu.M +
		g_phead->stu.E + g_phead->stu.S;

}
//ok
void PutInformation()
{
	Node *p = g_phead;
	printf("\n\n%-8s%-15s%-5s%-5s%-10s%-8s%-8s%-8s%-8s%-8s\n",
		"姓名", "学号", "年龄", "性别", "班级", "语文",
		"数学", "英语", "体育","总分");
	while (p != NULL)
	{
		PrintfInfo(p);
		p=p->pNext;
	}
	printf("\n\n");
}
//ok
void SearchInformation()
{
	int num;
	char stu_class[MAX_STU_CLASS];
	long n;
	char name[MAX_NAME];
	Node *p = g_phead;
	printf("请选择查找方式: 1、按班级 2、按学号 3、按姓名\n");
	if (p == NULL)
	{
		printf("提示:暂未存储任何信息!\n");
		return;
	}
	scanf("%d", &num);
	if (num == 1)
	{
		printf("请输入需要查找人的班级:");
		scanf("%s", &stu_class);
		printf("\n\n%-8s%-15s%-5s%-5s%-10s%-8s%-8s%-8s%-8s%-8s\n",
			"姓名", "学号", "年龄", "性别", "班级", "语文",
			"数学", "英语", "体育", "总分");
		while (p != NULL)
		{
			
			if (strcmp(p->stu.stu_class, stu_class) == 0)
			{
				PrintfInfo(p);
			}
			p = p->pNext;
		}
	}
	else if (num == 2)
	{
		printf("请输入需要查找人的学号:");
		scanf("%ld", &n);
		printf("\n\n%-8s%-15s%-5s%-5s%-10s%-8s%-8s%-8s%-8s%-8s\n",
			"姓名", "学号", "年龄", "性别", "班级", "语文",
			"数学", "英语", "体育", "总分");
		while (p != NULL)
		{
			
			if (p->stu.num == n)
			{				
				PrintfInfo(p);
			}
			p = p->pNext;
		}
	}
	else if (num == 3)
	{
		printf("请输入需要查找人的姓名:");
		scanf("%s", &name);
		printf("\n\n%-8s%-15s%-5s%-5s%-10s%-8s%-8s%-8s%-8s%-8s\n",
			"姓名", "学号", "年龄", "性别", "班级", "语文",
			"数学", "英语", "体育", "总分");
		while (p != NULL)
		{
			if (strcmp(p->stu.name,name)==0)
			{
				PrintfInfo(p);
			}
			p = p->pNext;
		}
	}
	else
	{
		printf("输入错误,该操作不存在!\n");
	}
}
//ok
void ModifyInformation()
{
	int cnt = 0;
	char name[MAX_NAME];
	printf("请输入需要修改人的姓名:");
	scanf("%s", &name);
	Node *p = g_phead;
	while (p != NULL)
	{
		if (strcmp(p->stu.name, name) == 0)
		{
			printf("请输入学生姓名:");
			scanf("%s", p->stu.name);
			printf("请输入学生学号:");
			scanf("%ld", &p->stu.num);
			printf("请输入学生年龄:");
			scanf("%d", &p->stu.age);
			printf("请输入学生性别:");
			scanf("%s", p->stu.sex);
			printf("请输入学生班级:");
			scanf("%s", p->stu.stu_class);
			printf("请输入学生语文成绩:");
			scanf("%lf", &p->stu.C);
			printf("请输入学生数学成绩:");
			scanf("%lf", &p->stu.M);
			printf("请输入学生英语成绩:");
			scanf("%lf", &p->stu.E);
			printf("请输入学生体育成绩:");
			scanf("%lf", &p->stu.S);
			p->stu.sum = p->stu.C + p->stu.M +
				p->stu.E + p->stu.S;
			cnt = 1;
		}
		p = p->pNext;
	}
	if (cnt == 1)
		printf("修改成功!\n");
	else
		printf("该姓名不存在!\n");

}

void ShowMax()
{
	int i;
	printf("各科最高分的人和各分数为:\n");
	printf("\n\n            %-8s%-15s%-5s%-5s%-10s%-8s%-8s%-8s%-8s%-8s\n",
		"姓名", "学号", "年龄", "性别", "班级", "语文",
		"数学", "英语", "体育", "总分");
	Node *p1 = g_phead, *p2 = g_phead, *p3 = g_phead, *p4 = g_phead, *p5 = g_phead;
	Node *p = g_phead;
	while (p)
	{
		if (p->stu.C>p1->stu.C)
		{
			p1 = p;
		}
		if (p->stu.M>p2->stu.M)
		{
			p2 = p;
		}
		if (p->stu.E>p3->stu.E)
		{
			p3 = p;
		}
		if (p->stu.S>p4->stu.S)
		{
			p4 = p;
		}
		if (p->stu.sum>p5->stu.sum)
		{
			p5 = p;
		}
		p = p->pNext;
	}
	printf("语文最高分:");
	PrintfInfo(p1);
	printf("数学最高分:");
	PrintfInfo(p2);
	printf("英语最高分:");
	PrintfInfo(p3);
	printf("体育最高分:");
	PrintfInfo(p4);
	printf("总分最高分:");
	PrintfInfo(p5);
}
//ok
void DeleteInformation()
{
	char name[MAX_NAME];
	Node *p1;
	printf("请输入需要删除人的姓名:");
	scanf("%s",name);
	if (g_phead == NULL)
	{
		printf("未存储任何信息!\n");
		return;
	}
	//是头结点
	if (strcmp(name, g_phead->stu.name) == 0)
	{
		p1 = g_phead;
		g_phead = g_phead->pNext;
		free(p1);
		p1 = NULL;
		printf("删除成功!\n");
		return;
	}

		//不是头结点
	Node *p=g_phead;
	Node *p2;
	while (p->pNext!= NULL)
	{
		if (strcmp(name, p->pNext->stu.name) == 0)
		{
			p2 = p->pNext;
			p->pNext = p2->pNext;
			free(p2);
			p2 = NULL;
			printf("删除成功!\n");
			return;
		}
		p = p->pNext;
	}
	printf("未找到该人,删除失败!\n");
	
}
//ok
void Average()
{
	double avg[MAX_AVG] = { 0 };
	Node *p = g_phead;
	int cnt=0,i;
	while (p)
	{
		avg[0] += p->stu.C;
		avg[1] += p->stu.M;
		avg[2] += p->stu.E;
		avg[3] += p->stu.S;
		avg[4] += p->stu.sum;
		cnt++;
		p = p->pNext;
	}
	for (i = 0; i < MAX_AVG; i++)
	{
		avg[i] /= cnt;
	}
	printf("语文平均分:%.2f\n", avg[0]);
	printf("数学平均分:%.2f\n", avg[1]);
	printf("英语平均分:%.2f\n", avg[2]);
	printf("体育平均分:%.2f\n", avg[3]);
	printf("总分平均分:%.2f\n", avg[4]);
}
//ok——保存的地方存在问题
void Sort()
{
	//按总分排序
	int s;
	Node *p1,*p2;

	student tem;
	//选择排序
	printf("请选择排序方式:1、按总分排序 2、按学号排序\n");
	scanf("%d", &s);
	if (s == 1 )
	{
		for (p1 = g_phead; p1 != NULL; p1 = p1->pNext)
		{
			for (p2 = p1->pNext; p2 != NULL; p2 = p2->pNext)
			{
				if (p1->stu.sum < p2->stu.sum)
				{
					tem = p1->stu;
					p1->stu = p2->stu;
					p2->stu = tem;
				}
			}
		}
	}
	else if (s== 2)
	{
		for (p1 = g_phead; p1 != NULL; p1 = p1->pNext)
		{
			for (p2 = p1->pNext; p2 != NULL; p2 = p2->pNext)
			{
				if (p1->stu.num > p2->stu.num)
				{
					tem = p1->stu;
					p1->stu = p2->stu;
					p2->stu = tem;
				}
			}
		}
	}
	else
	{
		printf("输入错误!");
	}
	
}

void PrintfInfo(Node *p)
{
	printf("%-8s%-15ld%-5d%-5s%-10s%-8.2f%-8.2f%-8.2f%-8.2f%-8.2f\n",
		p->stu.name,
		p->stu.num,
		p->stu.age,
		p->stu.sex,
		p->stu.stu_class,
		p->stu.C,
		p->stu.M,
		p->stu.E,
		p->stu.S,
		p->stu.sum);
}

遇到的问题:

1、链表的创建,有两个.c文件时,头结点创建在.h文件中有重复创建的错误。将头结点创建在一个.c文件中就能够解决这个问题。

2、输入问题,当进入选择菜单时候输入字符a、字符串、非数字就会输入错误进入死循环

解决办法:

A、可以使用清理输入流fflush(stdin),缺点:循环输入的话会变成只能输入一次

B、可以使用while()来执行里面的消除字符,但如果直接这样while(getchar())就会进入无限循环,所以我们可以让其接收一个终止条件,每次输入完我们都会按回车,那就让其接收'\n'换行符终止自己的循环。
        while (scanf("%d", &f) != 1)
        {
            puts("输入非法");
            while(getchar()!='\n');
        }

3、保存问题。当排完序之后再进行保存,保存的是倒叙,下次再次读取的时候就会倒叙输出。

 

标签:管理系统,scanf,8s%,C语言,stu,phead,printf,成绩,pNext
来源: https://blog.csdn.net/m0_56406829/article/details/117913981