班级档案管理系统(C语言)

班级档案管理系统文章目录



一、要求

班级档案管理系统对一个有N个学生的班级,通过该系统实现对该班级学生的基本信息进行录入、显示、修改、删除、保存等操作的管理。功能要求:(1)本系统采用一个包含N个数据的结构体数组,每个数据的结构应当包括:学号、姓名、性别、年龄、备注。(2)本系统显示这样的菜单:请选择系统功能项:.a学生基本信息录入b学生基本信息显示c学生基本信息保存d学生基本信息删除e学生基本信息修改(要求先输入密码)学生基本信息查询(1)按学号查询(2)按姓名查询(3)按性别查询(4)按年龄查询g.退出系统(3)执行一个具体的功能之后,程序将重新显示菜单。(4)将学生基本信息保存到文件中。(5)进入系统之前要先输入密码。


二、代码(三个.c文件,两个.h 文件)


1、test.c(main)

#include <stdio.h>
#include "Profile.h"

int login()
{
	printf("请输入登入密码:");
	char password[NUM] = { "\0" };
	int i = 3;
	//3次输入密码机会
	while (i)
	{
		scanf("%s", password);
		//密码为:wxy
		if (0 == strcmp(password, "wxy"))
		{
			printf("密码正确,欢迎进入系统\n");
			return 1;
		}
		else
		{
			if (i - 1 != 0)
			{
				printf("密码错误,还剩%d次机会,请重新输入密码:", i - 1);
			}
		}
		i--;

	}
	printf("密码三次全部输入错误,强制退出系统\n");
	return 0;
}

void menu()
{
	printf("请选择系统功能对应的数字:\n");
	printf("**********************************************\n");
	printf("***     1、信息录入        2、信息显示     ***\n");
	printf("***     3、信息保存        4、信息删除     ***\n");
	printf("***     5、信息修改        6、信息查询     ***\n");
	printf("***               0、退出系统              ***\n");
	printf("**********************************************\n");
}

int main()
{
	if (login())//登入密码
	{
		Profile pro;//学生档案
		InitPro(&pro);//初始化学生档案

		LoadFile(&pro);//加载文件信息到档案中
		ShowProfile(&pro);//显示加载的信息

		int input = 0;
		do
		{
			menu();//菜单打印
			scanf("%d", &input);
			switch (input)
			{
			case 0:
				printf("退出系统,文件自动保存\n");
				SaveProfile(&pro);
				break;
			case 1:
				printf("信息录入开始\n");
				InputProfile(&pro);
				SortProfile(&pro);//学号排序
				break;
			case 2:
				printf("信息显示开始\n");
				ShowProfile(&pro);
				break;
			case 3:
				printf("信息保存开始\n");
				SaveProfile(&pro);
				break;
			case 4:
				printf("信息删除开始\n");
				DeleteProfile(&pro);
				SortProfile(&pro);//学号排序
				break;
			case 5:
				printf("信息修改开始,为保证信息安全,");
				if (login())
				{
					ReviseProfile(&pro);
					SortProfile(&pro);//学号排序
				}
				else
				{
					printf("文件自动保存\n");
					SaveProfile(&pro);
					input = 0;
				}
				break;
			case 6:
				printf("信息查询开始\n");
				FindProfile(&pro);
				break;
			default:
				printf("输入错误,请重新输入\n");
				break;
			}
		} while (input);
	}
	return 0;
}

2、Profile.h

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

#define NUM 30
#define MAX 100

//学生信息
typedef struct Student
{
	char StudentNumber[NUM];//学号
	char name[NUM];
	char gender[NUM];//性别
	int age;
	char remark[MAX];//备注
}Student;

//档案
typedef struct Profile
{
	Student date[MAX];
	int count;
}Profile;

//初始化学生档案
void InitPro(Profile* ps);

//加载文件信息到数据中
void LoadFile(Profile* ps);

//按照学号排序学生档案
void SortProfile( Profile* ps);

//防止出现学号重复
int repeat(Profile* ps);

//录入学生信息
void InputProfile(Profile* ps);

//显示学生信息
void ShowProfile(const Profile* ps);

//保存学生信息到文件
void SaveProfile(const Profile* ps);

//删除学生信息
void DeleteProfile(Profile* ps);

//修改学生信息
void ReviseProfile(Profile* ps);

//查找学生信息
void FindProfile(const Profile* ps);

3、Profile.c

#include "Profile.h"
#include "FindProfile.h"

void InitPro(Profile* ps)
{
	assert(ps);//断言指针,防止指针为NULL
	ps->count = 0;
	//用memset将全部结构体Student初始化为0
	memset(ps->date, 0, sizeof(ps->date));
}

void LoadFile(Profile* ps)
{
	assert(ps);//断言指针,防止指针为NULL
	//打开文件
	FILE* pf = fopen("test.txt", "r");
	if (NULL == pf)//判断打开文件是否失败
	{
		perror("LoadFile");//将错误码转为错误信息
		return;
	}
	//加载数据
	printf("数据正在上传中,请等待\n");
	char ch[MAX] = "\0";
	fgets(ch, 100, pf);//消除第一行
	fgets(ch, 100, pf);//消除第二行

	//上传数据
	while (fscanf(pf, "%d %s %s %s %d %s", &ps->count,
		                                    ps->date[ps->count].StudentNumber,
		                                    ps->date[ps->count].name,
	                                     	ps->date[ps->count].gender,
		                                   &ps->date[ps->count].age,
		                                    ps->date[ps->count].remark) != EOF);
	//关闭文件
	fclose(pf);
	pf = NULL;
	printf("数据已同步,信息如下:\n");
}

int compare(const void* e1, const void* e2)//qsort中,函数比较
{
	return strcmp(((Profile*)e1)->date->StudentNumber, ((Profile*)e2)->date->StudentNumber);
}

void SortProfile(Profile* ps)
{
	if (ps->count <= 1)
	{
		return;
	}
	//快速排序qsort,学号是唯一的,比较学号
	qsort(ps, ps->count, sizeof(ps->date[0]), compare);
}

int repeat(Profile* ps)//防止出现学号重复
{
	assert(ps);//断言指针,防止指针为NULL
	int i = 0;
	for (i = 0; i < ps->count; i++)
	{
		if (0 == strcmp(ps->date[ps->count].StudentNumber, ps->date[i].StudentNumber))
		{
			return 1;
		}
	}
	return 0;
}

void InputProfile(Profile* ps)
{
	assert(ps);//断言指针,防止指针为NULL
	if (MAX == ps->count)
	{
		printf("学生信息数据满了\n");
		printf("学生信息输入失败\n");
		return;
	}
	int i = 0;
	printf("输入第%d个的学生信息\n", ps->count + 1);
	printf("请输入学号:");
	scanf("%s", ps->date[ps->count].StudentNumber);

	//防止出现学号重复
	while (repeat(ps))
	{
		printf("学号重复,请重新输入学号:");
		scanf("%s", ps->date[ps->count].StudentNumber);
	}
	 
	printf("请输入姓名:");
	scanf("%s", ps->date[ps->count].name);
	printf("请输入性别:");
	scanf("%s", ps->date[ps->count].gender);
	printf("请输入年龄:");
	scanf("%d", &ps->date[ps->count].age);
	printf("请备注:");
	scanf("%s", ps->date[ps->count].remark);
	ps->count++;
	printf("学生信息输入成功\n");
}

void ShowProfile(const Profile* ps)
{
	assert(ps);//断言指针,防止指针为NULL
	if (0 == ps->count)
	{
		printf("警告:信息显示错误,数据中无信息\n");
		return;
	}
	int i = 0;
	printf("\t\t\t\tXXXXX班级学生档案\n");
	printf("%-5s\t%-20s\t%-20s\t%-10s\t%-5s\t%-30s\n", "序号", "学号", "姓名", "性别", "年龄", "备注");
	for (i = 0; i < ps->count; i++)
	{
		printf("%-5d\t", i + 1);//打印序号
		printf("%-20s\t", ps->date[i].StudentNumber);
		printf("%-20s\t", ps->date[i].name);
		printf("%-10s\t", ps->date[i].gender);
		printf("%-5d\t", ps->date[i].age);
		printf("%-30s\n", ps->date[i].remark);
	}
}

void SaveProfile(const Profile* ps)
{
	assert(ps);//断言指针,防止指针为NULL
	//打开文件
	FILE* pf = fopen("test.txt", "w");
	if (NULL == pf)//判断打开文件是否失败
	{
		perror("SaveFile");//将错误码转为错误信息
		return;
	}
	//写文件
	int i = 0;
	fprintf(pf, "\t\t\t\tXXXXX班级学生档案\n");
	fprintf(pf, "%-5s\t%-20s\t%-20s\t%-10s\t%-5s\t%-30s\n", "序号", "学号", "姓名", "性别", "年龄", "备注");
	for (i = 0; i < ps->count; i++)
	{
		fprintf(pf, "%-5d\t", i + 1);//打印序号
		fprintf(pf, "%-20s\t", ps->date[i].StudentNumber);
		fprintf(pf, "%-20s\t", ps->date[i].name);
		fprintf(pf, "%-10s\t",ps->date[i].gender);
		fprintf(pf, "%-5d\t", ps->date[i].age);
		fprintf(pf, "%-30s\n", ps->date[i].remark);
	}
	//关闭文件
	fclose(pf);
	pf = NULL;
	printf("文件保存成功\n");
}

void DeleteProfile(Profile* ps)
{
	assert(ps);//断言指针,防止指针为NULL
	if (0 == ps->count)
	{
		printf("警告:信息显示错误,数据中无信息\n");
		return;
	}
	//查找学号
	int ret = FindStudentNumber(ps);
	if (-1 == ret)
	{
		printf("删除失败\n");
		return;
	}
	//删除
	printf("要删除的学生信息已找到\n");
	int i = 0;
	for (i = ret; i < ps->count - 1; i++)
	{
		ps->date[i] = ps->date[i + 1];
	}
	ps->count--;
	printf("删除成功\n");
}

void ReviseProfile(Profile* ps)
{
	assert(ps);//断言指针,防止指针为NULL
	if (0 == ps->count)
	{
		printf("警告:信息显示错误,数据中无信息\n");
		return;
	}
	//查找学号
	int ret = FindStudentNumber(ps);
	if (-1 == ret)
	{
		printf("修改失败\n");
		return;
	}
	//修改
	printf("要修改的学生信息已找到,请修改\n");
	printf("请输入要修改的学号:");
	scanf("%s", ps->date[ret].StudentNumber);
	printf("请输入要修改的姓名:");
	scanf("%s", ps->date[ret].name);
	printf("请输入要修改的性别:");
	scanf("%s", ps->date[ret].gender);
	printf("请输入要修改的年龄:");
	scanf("%d", &ps->date[ret].age);
	printf("请要修改的备注:");
	scanf("%s", ps->date[ret].remark);
	printf("修改成功\n");
}

void FindProfile(const Profile* ps)
{
	assert(ps);//断言指针,防止指针为NULL
	if (0 == ps->count)
	{
		printf("警告:信息查询错误,数据中无信息\n");
		return;
	}
	int input = 0;
	do
	{
		FindMenu();//子菜单
		scanf("%d", &input);
		switch (input)
		{
		case 0:
			printf("退出查询\n");
			break;
		case 1:
			printf("学号查询\n");
			FindStudentNumber(ps);
			break;
		case 2:
			printf("姓名查询\n");
			FindName(ps);
			break;
		case 3:
			printf("性别查询\n");
			FindGender(ps);
			break;
		case 4:
			printf("年龄查询\n");
			FindAge(ps);
			break;
		default:
			printf("输入错误,请重新输入\n");
			break;
		}
	} while (input);
}

4、FindProfile.h

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

#define NUM 30
#define MAX 100

//学生信息
typedef struct Student
{
	char StudentNumber[NUM];//学号
	char name[NUM];
	char gender[NUM];//性别
	int age;
	char remark[MAX];//备注
}Student;

//档案
typedef struct Profile
{
	Student date[MAX];
	int count;
}Profile;

//初始化学生档案
void InitPro(Profile* ps);

//加载文件信息到数据中
void LoadFile(Profile* ps);

//按照学号排序学生档案
void SortProfile( Profile* ps);

//防止出现学号重复
int repeat(Profile* ps);

//录入学生信息
void InputProfile(Profile* ps);

//显示学生信息
void ShowProfile(const Profile* ps);

//保存学生信息到文件
void SaveProfile(const Profile* ps);

//删除学生信息
void DeleteProfile(Profile* ps);

//修改学生信息
void ReviseProfile(Profile* ps);

//查找学生信息
void FindProfile(const Profile* ps);

5、FindProfile.c

#include "FindProfile.h"

void FindMenu()
{
	printf("请选择查询类型对应的数字:\n");
	printf("*********************************\n");
	printf("*** 1、学号查询   2、姓名查询 ***\n");
	printf("*** 3、性别查询   4、年龄查询 ***\n");
	printf("***        0、退出查询        ***\n");
	printf("*********************************\n");
}

void PrintStudent(const Profile* ps, int i)
{
	printf("%-5s\t%-20s\t%-20s\t%-10s\t%-5s\t%-30s\n", "序号", "学号", "姓名", "性别", "年龄", "备注");
	printf("%-5d\t", i + 1);//打印序号
	printf("%-20s\t", ps->date[i].StudentNumber);
	printf("%-20s\t", ps->date[i].name);
	printf("%-10s\t", ps->date[i].gender);
	printf("%-5d\t", ps->date[i].age);
	printf("%-30s\n", ps->date[i].remark);
}

int FindStudentNumber(const Profile* ps)
{
	assert(ps);//断言指针,防止指针为NULL
	int i = 0;
	char ch[NUM] = "\0";
	printf("请输入学号:");
	scanf("%s", ch);
	for (i = 0; i < ps->count; i++)
	{
		if ( 0 == strcmp(ps->date[i].StudentNumber,ch))
		{
			PrintStudent(ps, i);//打印查找到的学生信息
			return i;
		}
	}
	printf("学生信息不存在\n");
	return -1;
}

void  FindName(const Profile* ps)
{
	assert(ps);//断言指针,防止指针为NULL
	int i = 0, flag = 0;
	char ch[NUM] = "\0";
	printf("请输入姓名:");
	scanf("%s", ch);
	for (i = 0; i < ps->count; i++)
	{
		if (0 == strcmp(ch, ps->date[i].name))
		{
			flag = 1;
			PrintStudent(ps, i);//打印查找到的学生信息
		}
	}
	if (0 == flag)
	{
		printf("学生信息不存在\n");
	}
}

void FindGender(const Profile* ps)
{
	assert(ps);//断言指针,防止指针为NULL
	int i = 0, flag = 0;
	char ch[NUM] = "\0";
	printf("请输入性别:");
	scanf("%s", ch);
	for (i = 0; i < ps->count; i++)
	{
		if (0 == strcmp(ch, ps->date[i].gender))
		{
			flag = 1;
			PrintStudent(ps, i);//打印查找到的学生信息
		}
	}
	if (0 == flag)
	{
		printf("学生信息不存在\n");
	}
}

void FindAge(const Profile* ps)
{
	assert(ps);//断言指针,防止指针为NULL
	int num = 0, i = 0, flag = 0;
	printf("请输入年龄:");
	scanf("%d", &num);
	for (i = 0; i < ps->count; i++)
	{
		if (num == ps->date[i].age)
		{
			flag = 1;
			PrintStudent(ps, i);//打印查找到的学生信息
		}
	}
	if (0 == flag)
	{
		printf("学生信息不存在\n");
	}
}
  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

甘-

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

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

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

打赏作者

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

抵扣说明:

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

余额充值