一、开发任务
开发任务的具体要求
实现平台是VisualStudio2022,大多数同学用的应该是devc++,区别主要体现在VisualStudio中scanf需要用scanf_s替代,gets需要用gets_s替代,还有等等之类的安全性替代。在搬运代码的时候要注意这一点。
第22题学生基本信息管理(难度系数:5级)——C语言编程模拟实现一个学生基本信息管理系统。
本来想淘宝买,要价400还不如自己写一个。还没看着免费发布的,可能是别人写的质量比较高。我的这个系统里边还有些问题,分数打个勉强及格吧。
二、需求分析
某学校学生管理部门在新生入学时,会登记每个学生的个人基本信息,以便今后提供给教务处、学生所在系部、毕业工作指导委员会等部门使用。学生信息管理系统的基本功能是实现学生信息的录入、修改、删除、查询、统计。
*录入功能:*
能够录入八位学号(4位入学年份+1位系别+1位班级+2位班级学号)、姓名、性别、籍贯(精确到市)、高考各科成绩(有新高考和旧高考两种模式)、民族、专业、家庭收入
*修改功能:*
能够实现对文件中任一数据的修改,并且修改需符合和录入一样的格式
*删除功能:*
能够实现对文件中任一行数据的删除。
*查询功能:*
1、按学号查询某个学生的基本信息;
2、按班级号查询该班全体学生的基本信息;
3、按年级查询该年级全体学生基本信息;
4、按专业查询全体学生基本信息;
*统计功能:*
1、按班级统计高考课程各科最高、最低、平均入学成绩,以及每个人的总分;
2、按入校时间统计每个专业入学人数;
3、按给定班级号统计该班生源所属省市人数;
4、按给定年级统计不同少数民族学生人数;
5、按所学专业统计不同年级男女生人数;
6、按给定年级统计各个班级中家庭收入低于该班级全体同学家庭平均收入的学生人数。
*程序中有如下性能:*
(1)学号不能不能超过位数,入学年份也不能在在校学生入学学年范围之外,性别只能输入两种,高考模式也只有两种,高考各科成绩不能超过满分也不能负数。
(2)为了防止管理系统被破坏,编辑操作必须是有特定口令的人才可以完成。查询和浏览则不用口令。
三、程序设计
针对需求做如下设计:
(1)数据结构设计
学生信息管理系统中的信息结构是:姓名、八位学号(4位入学年份+1位系别+1位班级+2位班级学号)、性别(W、M)、系别、籍贯、高考模式、高考成绩、民族、专业、家庭人数、家庭收入
typedef struct INFO
{
char name[N]; //姓名
char num[N]; //学号
char sex[N]; //性别
char dep[N]; //系别
char province[N];//籍贯
char mode[N];//高考模式
char score[N][N]; //高考成绩
char ethnic[N];//民族
char pro[N];//专业
int familyNum; //家庭人数
int fincome; //家庭收入
float t;
} Info;
//为编辑操作设计的口令是一个长度为6的字符串。其中取值假设是”12356”
char e[6] = ”123456”;
(2)模块设计
本任务主要算法是实现对于文件信息的查找、修改、删除、统计。
增加一行新数据
将信息读入结构体数组中,遍历结构体写入文件。
修改某个数据
以姓名为标识符读取对应行的数据,确认需要修改的内容及其结果,重新将读取指针指向该行开头,将更改内容后的数据行写入文件。
删除一个已有数据
以姓名为标识符读取对应行的数据,将数据全部存到结构体数组中,并确认需要删除的行。
将删除行对应的结构体数组位置之后的数据向前移动一个单位长度,覆盖删除行的数据,重置文件读取指针到文件头,重新遍历写入结构体数组。
以上三个操作是编辑学生基本信息,对此每次选择该功能时,则要求“输入密码”,如果正确,则允许编辑,否则,则不允许。
查询学生基本信息
允许用学号、姓名、专业、班级查询学生信息管理系统中的学生信息。
统计数据公开
1.按学号查询某个学生的基本信息
2.按班级号查询该班全体学生的基本信息
3.按年级查询该年级全体学生基本信息
4.按专业查询全体学生基本信息
5.按班级查询高考成绩相关信息
6.按班级统计该班生源所属省市人数
7.按年级统计不同少数民族学生人数
8.按专业统计不同年级男女生人数
9.按年级统计各班低于平均收入的同学
10.按年级统计专业人数
(1)void Check_Information() //查看学生基本信息
/*从文件读取输出,不作赘述。
贴代码如下:*/
void Check_Information()
{
FILE* fp;
int i = 0;
if ((fopen_s(&fp,"C:\\Users\\80958\\Desktop\\test00.txt", "r")) == NULL)
printf("打开失败!");
system("cls");
printf("\t\t********************************************\n");
printf("\t\t* *\n");
printf("\t\t* 学 生 信 息 管 理 系 统 *\n");
printf("\t\t* 查看学生基本信息 *\n");
printf("\t\t* *\n");
printf("\t\t********************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("\t姓名\t学号\t性别\t系别\t籍贯\t高考模式\t高考成绩\t 民族\t专业\t家庭人数\t家庭收入\n");
printf("-------------------------------------------------------------------------------\n");
while (!feof(fp))
{
fscanf_s(fp, "%10s %10s %s %10s %10s %s ", stu[i].name,10, stu[i].num,10, stu[i].sex,3, stu[i].dep,10, stu[i].province,10, stu[i].mode,3);
if (!atoi(stu[i].mode)) {
for (int t = 0; t < 4; t++)
fscanf_s(fp, "%s", stu[i].score[t],5);
}
else for (int t = 0; t < 9; t++)fscanf_s(fp, "%s", stu[i].score[t],5);
fscanf_s(fp, "%10s %10s %d %d", stu[i].ethnic,10, stu[i].pro,10, &stu[i].familyNum, &stu[i].fincome);
printf("%10s %10s %s %10s %10s %s ", stu[i].name, stu[i].num, stu[i].sex, stu[i].dep, stu[i].province, stu[i].mode);
if (!atoi(stu[i].mode)) {
for (int t = 0; t < 4; t++)printf("%s ", stu[i].score[t]);
}
else for (int t = 0; t < 9; t++)printf("%s ", stu[i].score[t]);
printf("%10s %10s %d %d", stu[i].ethnic, stu[i].pro, stu[i].familyNum, stu[i].fincome);
printf("\n-------------------------------------------------------------------------------\n");
i++;
}
printf("按任意键返回主菜单!");
_getch();
fclose(fp);
}
(2)void Menu_Information_Handing(); //学生基本信息处理
其中有4个case,分别为录入、修改、删除和返回菜单功能,具体操作在函数中详细介绍

(3)void Input_Information(); //录入学生信息
1、将用户输入读入结构体数组中,具有较严格边界检查;
2、遍历结构体数组将数据写入文件。
贴代码如下:
void Input_Information()
{
FILE* fp;
char t;
int i, n, m, z; int r;
char s[N];
time_t now;
struct tm e;
time(&now);
localtime_s(&e, &now);
fopen_s(&fp,"C:\\Users\\80958\\Desktop\\test00.txt", "a+");
do
{
printf("\n********************************************\n");
printf("请输入要录入的人数:");
scanf_s("%s", s,5);
getchar();
n = atoi(s);
while (n <= 0)
{
fflush(stdin);
printf("输入有误,请再次输入正确人数,或输入-1直接停止:");
scanf_s("%s", s,5);
n = atoi(s);
if (n == -1)break;
}
printf("\n 请按以下对应信息正确录入!\n");
for (i = 0; i < n; i++)
{
system("cls");
//清空输入缓冲区
printf("-------------------------------------------------------------------------------\n");
printf("请输入第%d 名学生姓名:", i + 1);
fflush(stdin);
gets_s(stu[i].name);
fflush(stdin);
printf("请输入第%d 名学生学号(8 位):", i + 1);
gets_s(stu[i].num);
while ((strlen(stu[i].num) != 8) || ((atoi(stu[i].num) / 10000) > e.tm_year + 1900)|| ((atoi(stu[i].num) / 10000) < e.tm_year + 1896))
{
printf("输入有误,请输入 8 位学号数:");
gets_s(stu[i].num);
}
fflush(stdin);
printf("请输入第%d 名学生性别([男].M/[女].W):", i + 1);
scanf_s("%s",stu[i].sex,5);
while (strcmp(stu[i].sex, "M") && strcmp(stu[i].sex, "W"))
{
fflush(stdin);
printf("输入有误,请按要求正确输入([男].M/[女].W):");
scanf_s("%s", stu[i].sex,5);
}
getchar();
printf("请输入第%d 名学生系别(1~4):", i + 1);
gets_s(stu[i].dep);
m = atoi(stu[i].dep);
while ((m == 0 || m > 4) || strlen(stu[i].dep) != 1)
{
fflush(stdin);
printf("输入有误,请按要求正确输入系别(1~4):");
gets_s(stu[i].dep);
m = atoi(stu[i].dep);
}
fflush(stdin);
printf("请输入第%d 名学生籍贯(精确到市):", i + 1);
gets_s(stu[i].province);
fflush(stdin);
printf("请输入第%d 名学生的高考模式(0_旧高考,1_新高考):", i + 1);
gets_s(stu[i].mode);
while (strcmp(stu[i].mode, "0") && strcmp(stu[i].mode, "1"))
{
fflush(stdin);
printf("输入有误,请按要求正确输入高考模式(0_旧高考,1_新高考):");
gets_s(stu[i].mode);
}
fflush(stdin);
if (!atoi(stu[i].mode)) {
printf("请输入第%d 名学生高考成绩:\n语文\t数学\t英语\t综合\n", i + 1);
for (int t = 0; t < 4; t++) {
do
{
scanf_s("%s", stu[i].score[t],20);
if((atoi(stu[i].score[t]) > 150 && t < 3) || (atoi(stu[i].score[t]) > 300 && t == 3) || (atoi(stu[i].score[t]) < 0))
printf("输入有误,请按要求正确输入第%d科成绩", t + 1);
}while((atoi(stu[i].score[t]) > 150 && t < 3) || (atoi(stu[i].score[t]) > 300 && t == 3) || (atoi(stu[i].score[t]) < 0));
}
}
else {
printf("请输入第%d 名学生高考成绩,没考的科目写零分:\n语文\t数学\t英语\t政治\t历史\t地理\t生物\t物理\t化学\n", i + 1);
do {
for (int t = 0; t < 9; t++) {
do
{
scanf_s("%s", stu[i].score[t], 20);
if ((atoi(stu[i].score[t]) > 150) || (atoi(stu[i].score[t]) < 0))
printf("输入有误,请按要求正确输入第%d科成绩", t + 1);
} while ((atoi(stu[i].score[t]) > 150) || (atoi(stu[i].score[t]) < 0));
}
z = 0;
for (int t = 0; t < 9; t++) {
z += atoi(stu[i].score[t]);
}
} while (z > 750);
}
fflush(stdin);
printf("请输入第%d 名学生民族(要写族):", i + 1);
scanf_s("%s",stu[i].ethnic,20);
fflush(stdin);
printf("请输入第%d 名学生专业(写全称):", i + 1);
scanf_s("%s", stu[i].pro,20);
fflush(stdin);
printf("请输入第%d 名学生家庭人数:", i + 1);
scanf_s("%d",&stu[i].familyNum);
fflush(stdin);
printf("请输入第%d 名学生家庭收入:", i + 1);
scanf_s("%d", &stu[i].fincome);
getchar();
fprintf(fp, "%10s %10s %s %10s %10s %s ", stu[i].name, stu[i].num, stu[i].sex, stu[i].dep,stu[i].province, stu[i].mode);
if (!atoi(stu[i].mode)) {
for (int t = 0; t < 4; t++)
fprintf(fp, "%s ", stu[i].score[t]);
}
else for (int t = 0; t < 9; t++)fprintf(fp, "%s ", stu[i].score[t]);
fprintf(fp, "%10s %10s %d %d\n", stu[i].ethnic, stu[i].pro,stu[i].familyNum, stu[i].fincome);
}
printf("-------------------------------------------------------------------------------\n");
printf("是否继续输入?([Y].是/[任意键].否)\n");
t = _getch();
} while (t == 'Y' || t == 'y');
printf("-------------------------------------------------------------------------------\n");
printf("信息已保存,按任意键返回主菜单!");
_getch();
fclose(fp);
}
(4)void Modify_Information(); //修改学生基本信息
1、以姓名为标识符遍历文件,找到对应学生
2、将对应学生的数据读入结构体,输入修改内容对待修改数据进行覆盖
3、通过fseek将文件读取指针返回本行开头
4、重新写入数据
贴代码如下:
void Modify_Information()
{
FILE* fp;
char str[N];
char t;
int i = -1, x = 0;
int j, m;
time_t now;
struct tm e;
time(&now);
localtime_s(&e, &now);
fopen_s(&fp,"C:\\Users\\80958\\Desktop\\test00.txt", "r+");
printf("-------------------------------------------------------------------------------\n");
printf("\t姓名\t学号\t性别\t系别\t籍贯\t高考模式\t高考成绩\t\t\t民族\t专业\t家庭人数\t家庭收入\n");
printf("-------------------------------------------------------------------------------\n");
while (!feof(fp))
{
i++;
fscanf_s(fp, "%10s %10s %s %10s %10s %s ", stu[i].name, 10, stu[i].num, 10, stu[i].sex, 3, stu[i].dep, 10, stu[i].province, 10, stu[i].mode, 3);
if (!atoi(stu[i].mode)) {
for (int t = 0; t < 4; t++)
fscanf_s(fp, "%s", stu[i].score[t], 5);
}
else for (int t = 0; t < 9; t++)fscanf_s(fp, "%s", stu[i].score[t], 5);
fscanf_s(fp, "%10s %10s %d %d", stu[i].ethnic, 10, stu[i].pro, 10, &stu[i].familyNum, &stu[i].fincome);
printf("%10s %10s %s %10s %10s %s ", stu[i].name, stu[i].num, stu[i].sex, stu[i].dep, stu[i].province, stu[i].mode);
if (!atoi(stu[i].mode)) {
for (int t = 0; t < 4; t++)
printf("%s", stu[i].score[t]);
}
else for (int t = 0; t < 9; t++)
printf("%s", stu[i].score[t]);
printf("%10s %10s %d %d", stu[i].ethnic, stu[i].pro, stu[i].familyNum, stu[i].fincome);
printf("\n-------------------------------------------------------------------------------\n");
}
do
{
printf("-------------------------------------------------------------------------------\n");
printf("请输入要修改信息的学生姓名:");
fflush(stdin);
scanf_s("%s",str,10);
getchar();
for (j = 0; j < (i + 1); j++)
{
if (strcmp(str, stu[j].name) == 0)
{
printf("----------------------------------------------------------------------------------------\n");
printf("\t姓名\t学号\t性别\t系别\t籍贯\t高考模式\t高考成绩\t\t\t民族\t专业\t家庭人数\t家庭收入\n");
printf("----------------------------------------------------------------------------------------\n");
printf("%10s %10s %s %10s %10s %s ", stu[j].name, stu[j].num, stu[j].sex, stu[j].dep, stu[j].province, stu[j].mode);
if (!atoi(stu[j].mode)) {
for (int t = 0; t < 4; t++)printf("%s", stu[j].score[t]);
}
else for (int t = 0; t < 9; t++)printf("%s", stu[j].score[t]);
printf("%10s %10s %d %d", stu[j].ethnic, stu[j].pro, stu[j].familyNum, stu[j].fincome);
printf("\n-------------------------------------------------------------------------------\n");
printf("1.姓名2.学号3.性别4.系别5.籍贯6.高考模式7.高考成绩8.民族9.专业10.家庭人数11.家庭收入\n输入要修改的信息:");
scanf_s("%d", &x);
printf("-------------------------------------------------------------------------------\n");
system("cls");
switch (x)
{
case 1:
fflush(stdin);
printf("修改该学生姓名:");
scanf_s("%10s",stu[j].name,10);
getchar();
printf("-------------------------------------------------------------------------------\n");
fseek(fp, j * 96, 0);
fprintf(fp, "%10s %10s %s %10s %10s %s ", stu[j].name, stu[j].num, stu[j].sex, stu[j].dep, stu[j].province, stu[j].mode);
if (!atoi(stu[j].mode)) {
for (int t = 0; t < 4; t++)fprintf(fp, "%s ", stu[j].score[t]);
}
else for (int t = 0; t < 9; t++)fprintf(fp, "%s ", stu[j].score[t]);
fprintf(fp, "%10s %10s %d %d\n", stu[j].ethnic, stu[j].pro, stu[j].familyNum, stu[j].fincome);
break;
case 2:
fflush(stdin);
printf("修改该学生学号:");
scanf_s("%10s",stu[j].num,10);
getchar();
while ((strlen(stu[j].num) != 8) || ((atoi(stu[j].num) / 10000) > e.tm_year + 1900)|| ((atoi(stu[j].num) / 10000) < e.tm_year + 1896))
{
fflush(stdin);
printf("输入有误,请输入 8 位学号数:");
scanf_s("%10s", stu[j].num, 10);
getchar();
}
printf("-------------------------------------------------------------------------------\n");
fseek(fp, j * 96, 0);
fprintf(fp, "%10s %10s %s %10s %10s %s ", stu[j].name, stu[j].num, stu[j].sex, stu[j].dep, stu[j].province, stu[j].mode);
if (!atoi(stu[j].mode)) {
for (int t = 0; t < 4; t++)fprintf(fp, "%s ", stu[j].score[t]);
}
else for (int t = 0; t < 9; t++)fprintf(fp, "%s ", stu[j].score[t]);
fprintf(fp, "%10s %10s %d %d\n", stu[j].ethnic, stu[j].pro, stu[j].familyNum, stu[j].fincome);
break;
case 3:
fflush(stdin);
printf("修改该学生性别:");
scanf_s("%s", stu[i].sex,3);
getchar();
while (strcmp(stu[j].sex, "M") && strcmp(stu[j].sex, "W"))
{
fflush(stdin);
printf("输入有误,请按要求正确输入([男].M/[女].W):");
scanf_s("%s", stu[i].sex,3);
}
printf("-------------------------------------------------------------------------------\n");
fseek(fp, j * 96, 0);
fprintf(fp, "%10s %10s %s %10s %10s %s ", stu[j].name, stu[j].num, stu[j].sex, stu[j].dep, stu[j].province, stu[j].mode);
if (!atoi(stu[j].mode)) {
for (int t = 0; t < 4; t++)fprintf(fp, "%s ", stu[j].score[t]);
}
else for (int t = 0; t < 9; t++)fprintf(fp, "%s ", stu[j].score[t]);
fprintf(fp, "%10s %10s %d %d\n", stu[j].ethnic, stu[j].pro, stu[j].familyNum, stu[j].fincome);
break;
case 4:
fflush(stdin);
printf("修改该学生系别:");
scanf_s("%s",stu[j].dep,10);
m = atoi(stu[j].dep);
while ((m == 0 || m > 4) || strlen(stu[j].dep) != 1)
{
fflush(stdin);
printf("输入有误,请按要求正确输入系别(1~4):");
scanf_s("%s", stu[j].dep, 10);
m = atoi(stu[j].dep);
}
printf("-------------------------------------------------------------------------------\n");
fseek(fp, j * 96, 0);
fprintf(fp, "%10s %10s %s %10s %10s %s ", stu[j].name, stu[j].num, stu[j].sex, stu[j].dep, stu[j].province, stu[j].mode);
if (!atoi(stu[j].mode)) {
for (int t = 0; t < 4; t++)fprintf(fp, "%s ", stu[j].score[t]);
}
else for (int t = 0; t < 9; t++)fprintf(fp, "%s ", stu[j].score[t]);
fprintf(fp, "%10s %10s %d %d\n", stu[j].ethnic, stu[j].pro, stu[j].familyNum, stu[j].fincome);
break;
case 5:
fflush(stdin);
printf("修改该学生籍贯:");
scanf_s("%10s",stu[j].province,10);
printf("-------------------------------------------------------------------------------\n");
fseek(fp, j * 96, 0);
fprintf(fp, "%10s %10s %s %10s %10s %s ", stu[j].name, stu[j].num, stu[j].sex, stu[j].dep, stu[j].province, stu[j].mode);
if (!atoi(stu[j].mode)) {
for (int t = 0; t < 4; t++)fprintf(fp, "%s ", stu[j].score[t]);
}
else for (int t = 0; t < 9; t++)fprintf(fp, "%s ", stu[j].score[t]);
fprintf(fp, "%10s %10s %d %d\n", stu[j].ethnic, stu[j].pro, stu[j].familyNum, stu[j].fincome);
break;
case 6:
fflush(stdin);
printf("修改该学生高考模式(0_旧高考1_新高考):");
scanf_s("%3s",stu[j].mode,3);
getchar();
m = atoi(stu[j].mode);
while (strcmp(stu[j].mode, "0") && strcmp(stu[j].mode, "1"))
{
fflush(stdin);
printf("输入有误,请按要求正确输入高考模式(0_旧高考,1_新高考):");
scanf_s("%3s", stu[j].mode, 3);
getchar();
m = atoi(stu[j].mode);
}
if (!atoi(stu[j].mode)) {
printf("请输入学生高考成绩,没考的科目写零分:\n语文\t数学\t英语\t综合\n");
for (int t = 0; t < 4; t++) {
do
{
scanf_s("%s",stu[j].score[t],10);
if ((atoi(stu[j].score[t]) > 150 && t < 3) || (atoi(stu[j].score[t]) > 300 && t == 3) || (atoi(stu[j].score[t]) < 0))
printf("输入有误,请按要求正确输入第%d科成绩", t + 1);
} while ((atoi(stu[j].score[t]) > 150 && t < 3) || (atoi(stu[j].score[t]) > 300 && t == 3) || (atoi(stu[j].score[t]) < 0));
}
}
else {
printf("请输入学生高考成绩,没考的科目写零分:\n语文\t数学\t英语\t政治\t历史\t地理\t生物\t物理\t化学\n");
for (int t = 0; t < 9; t++) {
do
{
scanf_s(stu[j].score[t]);
getchar();
if ((atoi(stu[j].score[t]) > 150) || (atoi(stu[j].score[t]) < 0))
printf("输入有误,请按要求正确输入第%d科成绩", t + 1);
} while ((atoi(stu[j].score[t]) > 150) || (atoi(stu[j].score[t]) < 0));
}
}
printf("-------------------------------------------------------------------------------\n");
fseek(fp, j * 96, 0);
fprintf(fp, "%10s %10s %s %10s %10s %s ", stu[j].name, stu[j].num, stu[j].sex, stu[j].dep, stu[j].province, stu[j].mode);
if (!atoi(stu[j].mode)) {
for (int t = 0; t < 4; t++)fprintf(fp, "%s ", stu[j].score[t]);
}
else for (int t = 0; t < 9; t++)fprintf(fp, "%s ", stu[j].score[t]);
fprintf(fp, "%10s %10s %d %d\n", stu[j].ethnic, stu[j].pro, stu[j].familyNum, stu[j].fincome);
break;
case 7:
fflush(stdin);
if (!atoi(stu[j].mode)) {
printf("请输入学生高考成绩,没考的科目写零分:\n语文\t数学\t英语\t综合\n");
for (int t = 0; t < 4; t++) {
do
{
scanf_s("%s", stu[j].score[t], 10);
if ((atoi(stu[j].score[t]) > 150 && t < 3) || (atoi(stu[j].score[t]) > 300 && t == 3) || (atoi(stu[j].score[t]) < 0))
printf("输入有误,请按要求正确输入第%d科成绩", t + 1);
} while ((atoi(stu[j].score[t]) > 150 && t < 3) || (atoi(stu[j].score[t]) > 300 && t == 3) || (atoi(stu[j].score[t]) < 0));
}
}
else {
printf("请输入学生高考成绩,没考的科目写零分:\n语文\t数学\t英语\t政治\t历史\t地理\t生物\t物理\t化学\n");
for (int t = 0; t < 9; t++) {
do
{
scanf_s(stu[j].score[t]);
getchar();
if ((atoi(stu[j].score[t]) > 150) || (atoi(stu[j].score[t]) < 0))
printf("输入有误,请按要求正确输入第%d科成绩", t + 1);
} while ((atoi(stu[j].score[t]) > 150) || (atoi(stu[j].score[t]) < 0));
}
}
printf("-------------------------------------------------------------------------------\n");
fseek(fp, j * 96, 0);
fprintf(fp, "%10s %10s %s %10s %10s %s ", stu[j].name, stu[j].num, stu[j].sex, stu[j].dep, stu[j].province, stu[j].mode);
if (!atoi(stu[j].mode)) {
for (int t = 0; t < 4; t++)fprintf(fp, "%s ", stu[j].score[t]);
}
else for (int t = 0; t < 9; t++)fprintf(fp, "%s ", stu[j].score[t]);
fprintf(fp, "%10s %10s %d %d\n", stu[j].ethnic, stu[j].pro, stu[j].familyNum, stu[j].fincome);
break;
case 8:
fflush(stdin);
printf("修改该学生民族:");
scanf_s("%10s",stu[j].ethnic,10);
printf("-------------------------------------------------------------------------------\n");
fseek(fp, j * 96, 0);
fprintf(fp, "%10s %10s %s %10s %10s %s ", stu[j].name, stu[j].num, stu[j].sex, stu[j].dep, stu[j].province, stu[j].mode);
if (!atoi(stu[j].mode)) {
for (int t = 0; t < 4; t++)fprintf(fp, "%s ", stu[j].score[t]);
}
else for (int t = 0; t < 9; t++)fprintf(fp, "%s ", stu[j].score[t]);
fprintf(fp, "%10s %10s %d %d\n", stu[j].ethnic, stu[j].pro, stu[j].familyNum, stu[j].fincome);
break;
case 9:
fflush(stdin);
printf("修改该学生专业:");
scanf_s("%10s",stu[j].pro,10);
printf("-------------------------------------------------------------------------------\n");
fseek(fp, j * 96, 0);
fprintf(fp, "%10s %10s %s %10s %10s %s ", stu[j].name, stu[j].num, stu[j].sex, stu[j].dep, stu[j].province, stu[j].mode);
if (!atoi(stu[j].mode)) {
for (int t = 0; t < 4; t++)fprintf(fp, "%s ", stu[j].score[t]);
}
else for (int t = 0; t < 9; t++)fprintf(fp, "%s ", stu[j].score[t]);
fprintf(fp, "%10s %10s %d %d\n", stu[j].ethnic, stu[j].pro, stu[j].familyNum, stu[j].fincome);
break;
case 10:
fflush(stdin);
printf("修改该学生家庭人数:");
scanf_s("%d",&stu[j].familyNum);
printf("-------------------------------------------------------------------------------\n");
fseek(fp, j * 96, 0);
fprintf(fp, "%10s %10s %s %10s %10s %s ", stu[j].name, stu[j].num, stu[j].sex, stu[j].dep, stu[j].province, stu[j].mode);
if (!atoi(stu[j].mode)) {
for (int t = 0; t < 4; t++)fprintf(fp, "%s ", stu[j].score[t]);
}
else for (int t = 0; t < 9; t++)fprintf(fp, "%s ", stu[j].score[t]);
fprintf(fp, "%10s %10s %d %d\n", stu[j].ethnic, stu[j].pro, stu[j].familyNum, stu[j].fincome);
break;
case 11:
fflush(stdin);
printf("修改该学生家庭收入:");
scanf_s("%d", &stu[j].fincome);
printf("-------------------------------------------------------------------------------\n");
fseek(fp, j * 96, 0);
fprintf(fp, "%10s %10s %s %10s %10s %s ", stu[j].name, stu[j].num, stu[j].sex, stu[j].dep, stu[j].province, stu[j].mode);
if (!atoi(stu[j].mode)) {
for (int t = 0; t < 4; t++)fprintf(fp, "%s ", stu[j].score[t]);
}
else for (int t = 0; t < 9; t++)fprintf(fp, "%s ", stu[j].score[t]);
fprintf(fp, "%10s %10s %d %d\n", stu[j].ethnic, stu[j].pro, stu[j].familyNum, stu[j].fincome);
break;
default:
printf("输入有误!\n");
}
break;
}
}
fflush(stdin);
if (j > i) printf("查无此人!\n");
printf("-------------------------------------------------------------------------------\n");
printf("是否继续输入?([Y].是/[任意键].否)\n");
t = _getch();
} while (t == 'Y' || t == 'y');
fclose(fp);
}
(5)void Delete_Information(); //删除学生基本信息
1、以姓名为标识符遍历文件,将数据存入结构体数组,并找到对应学生
2、将结构体数组中该学生之后的数据都向前移动一个单位长度,覆盖被删除的数据
3、遍历结构体数组将数据写入文件
贴代码如下:
void Delete_Information()
{
FILE* fp;
char str[N];
char t, ch;
int i = 0;
int j, n;
fopen_s(&fp,"C:\\Users\\80958\\Desktop\\test00.txt", "r+");
printf("--------------------------------------------------------------------------------------\n");
printf("\t姓名\t学号\t性别\t系别\t籍贯\t高考模式\t高考成绩\t\t\t民族\t专业\t家庭人数\t家庭收入\n");
printf("--------------------------------------------------------------------------------------\n");
while (!feof(fp))
{
fscanf_s(fp, "%10s %10s %s %10s %10s %s ", stu[i].name,10, stu[i].num, 10, stu[i].sex,3, stu[i].dep, 10, stu[i].province, 10, stu[i].mode,3);
if (!atoi(stu[i].mode)) {
for (int t = 0; t < 4; t++)
fscanf_s(fp, "%5s", stu[i].score[t],5);
}
else for (int t = 0; t < 9; t++)
fscanf_s(fp, "%s", stu[i].score[t],5);
fscanf_s(fp, "%10s %10s %d %d", stu[i].ethnic, 10, stu[i].pro, 10, &stu[i].familyNum, &stu[i].fincome);
printf("%10s %10s %s %10s %10s %s ", stu[i].name, stu[i].num, stu[i].sex, stu[i].dep, stu[i].province, stu[i].mode);
if (!atoi(stu[i].mode)) {
for (int t = 0; t < 4; t++)printf("%s ", stu[i].score[t]);
}
else for (int t = 0; t < 9; t++)printf("%s ", stu[i].score[t]);
printf("%10s %10s %d %d", stu[i].ethnic, stu[i].pro, stu[i].familyNum, stu[i].fincome);
printf("\n-------------------------------------------------------------------------------\n");
i++;
}
fclose(fp);
do
{
printf("-------------------------------------------------------------------------------\n");
printf("请输入要删除信息的学生姓名:");
fflush(stdin);
gets_s(str);
for (j = 0; j < i; j++)
{
if (strcmp(str, stu[j].name) == 0)
{
printf("-------------------------------------------------------------------------------\n");
printf(" 姓名\t学号\t性别\t系别\t籍贯\t高考模式\t高考成绩\t\t\t民族\t专业\t家庭人数\t家庭收入\n");
printf("-------------------------------------------------------------------------------\n");
printf("%10s %10s %s %10s %10s %s ", stu[j].name, stu[j].num, stu[j].sex, stu[j].dep, stu[j].province, stu[j].mode);
if (!atoi(stu[j].mode)) {
for (int t = 0; t < 4; t++)printf("%s", stu[j].score[t]);
}
else for (int t = 0; t < 9; t++)printf("%s", stu[j].score[t]);
printf("%10s %10s %d %d", stu[j].ethnic, stu[j].pro, stu[j].familyNum, stu[j].fincome);
printf("\n-------------------------------------------------------------------------------\n");
n = j + 1;
break;
}
}
fflush(stdin);
if (j == i) printf("查无此人!\n");
if (j < i)
{
printf("确定删除?([Y].是/[任意键].否)");
ch = _getch();
if (ch == 'Y' || ch == 'y')
{
for (j = n; j < i; j++)
{
stu[j - 1]=stu[j]; //将删除的学生后续的学生信息向前移动
}
memset(&stu[i - 1], 0, 95); //最后一个学生不可以和倒数第二个内容一致?做清除操作
}
printf("\n-------------------------------------------------------------------------------\n");
printf("删除成功!");
}
fopen_s(&fp,"C:\\Users\\80958\\Desktop\\test00.txt", "w+");
for (j = 0; j < i - 1; j++)
{
fprintf(fp, "%10s %10s %s %10s %10s %s ", stu[j].name, stu[j].num, stu[j].sex, stu[j].dep, stu[j].province, stu[j].mode);
if (!atoi(stu[j].mode)) {
for (int t = 0; t < 4; t++)fprintf(fp, "%s ", stu[j].score[t]);
}
else for (int t = 0; t < 9; t++)fprintf(fp, "%s ", stu[j].score[t]);
fprintf(fp, "%10s %10s %d %d\n", stu[j].ethnic, stu[j].pro, stu[j].familyNum, stu[j].fincome);
}
fflush(stdin);
printf("\n-------------------------------------------------------------------------------\n");
printf("\n 是否继续输入?([Y].是/[任意键].否)");
t = _getch();
} while (t == 'Y' || t == 'y');
fclose(fp);
}
(6)void Menu_Servefor_pub();//统计信息公开
其中有十种统计功能,前五种非常简单不再赘述,算法重复的也不分开说明。
*6、按班级统计该班生源所属省市人数*
A.遍历该班级同学,以字符串数组provinceALL[N]形式记录下所有存在的省市
B.另设置一个整型数组countProvince[N]存放对应省市的人数,将provinceALL[i]与countProvince[i]对应起来。设置整型局部变量temp=0,遍历文件,每当出现本次遍历还没出现过的省市时,就将省市名称复制进provinceALL[temp]并将countProvince[temp]++,temp++。;
C.输出统计结果。
贴代码如下:
case 6:
fflush(stdin);
printf("请输入查找的班级(4 位):");
gets_s(str);
while (strlen(str) != 4)
{
printf("输入有误,请输入 4 位班级:");
gets_s(str);
}
printf("\t姓名\t学号\t性别\t系别\t籍贯\t高考模式\t高考成绩\t 民族\t专业\t家庭人数\t家庭收入\n");
for (int p = 0; p < i; p++) {
clas = (atoi(stu[p].num) / 100) % 10000;
if (clas == atoi(str)) {
printf("%10s %10s %s %10s %10s %s ", stu[p].name, stu[p].num, stu[p].sex, stu[p].dep, stu[p].province, stu[p].mode);
if (!atoi(stu[p].mode)) {
for (int t = 0; t < 4; t++)printf("%s ", stu[p].score[t]);
}
else for (int t = 0; t < 9; t++)printf("%s ", stu[p].score[t]);
printf("%10s %10s %d %d\n", stu[p].ethnic, stu[p].pro, stu[p].familyNum, stu[p].fincome);
}
}
for (int p = 0; p < i; p++) {
clas = (atoi(stu[p].num) / 100) % 10000;
if (clas == atoi(str)) {
flag = 0;
for (int q = 0; q <= temp; q++) {
if (!strcmp(provinceALL[q], stu[p].province)) {
countprovince[q]++;
flag = 1;
}
}
if (flag == 0) {
strcpy_s(provinceALL[temp], stu[p].province);
countprovince[temp]++;
temp = p + 1;
}
}
}
while (countprovince[c]) {
printf("来自%s的有%d人\n", provinceALL[c], countprovince[c]);
c++;
}
printf("按任意键退出...");
_getch();
break;
*按年级统计不同少数民族学生人数与第六种大部分相似,不作重复说明。*
*8、按专业统计不同年级男女生人数*
A.遍历该专业同学,以整型数组numTime[N]形式记录下所有存在的年级,将年级个数赋给temp;
B.遍历temp次文件,设置整型变量boy,girl,在每次遍历的过程中记录下某年级的男生和女生人数,分别赋给numTime[1]和numTime[2];
C.输出统计结果。
贴代码如下:
fflush(stdin);
printf("请输入查找的专业:");
gets_s(str);
printf("\t姓名\t学号\t性别\t系别\t籍贯\t高考模式\t高考成绩\t 民族\t专业\t家庭人数\t家庭收入\n");
for (int p = 0; p < i; p++) {
if (!strcmp(str, stu[p].pro)) {
printf("%10s %10s %s %10s %10s %s ", stu[p].name, stu[p].num, stu[p].sex, stu[p].dep, stu[p].province, stu[p].mode);
if (!atoi(stu[p].mode)) {
for (int t = 0; t < 4; t++)printf("%s ", stu[p].score[t]);
}
else for (int t = 0; t < 9; t++)printf("%s ", stu[p].score[t]);
printf("%10s %10s %d %d\n", stu[p].ethnic, stu[p].pro, stu[p].familyNum, stu[p].fincome);
flag = 0;
for (int q = 0; q <= temp; q++) {
c = atoi(stu[p].num) / 10000;
if (numTime[q][0] == c) {
flag = 1;
}
}
if (flag == 0) {
numTime[temp][0] = c;
temp++;
}
}
}
flag = 0;
for (int p = 0; p < i; p++) {
if (!strcmp(str, stu[p].pro)) {
for (int q = 0; q < temp; q++) {
if ((atoi(stu[p].num) / 10000) == numTime[q][0]) {
if (!strcmp(stu[p].sex, "M")) {
numTime[q][1]++;
}
if (!strcmp(stu[p].sex, "W")) {
numTime[q][2]++;
}
}
}
}
}
c = 0;
while (c
*9、按年级统计各班低于平均收入的同学*
A.以处理第八种功能的方法记录下本年级所有的班级及其人数,将班级个数赋给temp;
B.遍历该年级同学,分别计算出每个班级平均收入,将每个同学和本班平均收入作比较并作计数;
C.输出统计结果。
贴代码如下:
fflush(stdin);
printf("请输入查找的年级(4 位):");
gets_s(str);
while (strlen(str) != 4)
{
printf("输入有误,请输入 4 位年份:");
gets_s(str);
}
temp = 0;
for (int p = 0; p < i; p++) {
c = atoi(stu[p].num) / 10000;
_itoa_s(c, TEMP, 10);
if (!strcmp(str,TEMP)) {
printf("%10s %10s %s %10s %10s %s ", stu[p].name, stu[p].num, stu[p].sex, stu[p].dep, stu[p].province, stu[p].mode);
if (!atoi(stu[p].mode)) {
for (int t = 0; t < 4; t++)printf("%s ", stu[p].score[t]);
}
else for (int t = 0; t < 9; t++)printf("%s ", stu[p].score[t]);
printf("%10s %10s %d %d\n", stu[p].ethnic, stu[p].pro, stu[p].familyNum, stu[p].fincome);
flag = 0;
for (int q = 0; q <= temp; q++) {
c = (atoi(stu[p].num) / 100)%10000;
if (numClas[q][0] == c) {
flag = 1;
}
}
if (flag == 0) {
numClas[temp][0] = c;
temp++;
}
}
}
flag = 0;
for (int p = 0; p < i; p++) {
flag = atoi(stu[p].num) / 10000;
_itoa_s(flag, TEMP,10);
if (!strcmp(str, TEMP)) {
for (int q = 0; q < temp; q++) {
if (((atoi(stu[p].num) / 100)%10000) == numClas[q][0]) {
numClas[q][1]++;
s_income[q] += stu[p].fincome;
}
}
}
}
c = 0; boy = 0;
while (c < temp) {
s_income[c] = s_income[c] / numClas[c][1];
for (int count = 0, q=0;q
五、设计小结
本任务设计的代码完成基本功能,但至少包含几个缺点:
(1)删除功能没有规避重名的情况
(2)口令输入的时候可以被看见,如果输入的时候显示******就更好了
(3)可以做一个图形化界面,比较有系统的感觉
(4)还有很多问题比如第一个功能总是会多打出来一行然后多打两个0,其实挺不好的。
完整代码折叠如下,需要自取。
#include
#include
#include
#include
#include
#include
# define N 100
typedef struct INFO
{
char name[N]; //姓名
char num[N]; //学号
char sex[N]; //性别
char dep[N]; //系别
char province[N];//籍贯
char mode[N];//高考模式
char score[N][N]; //高考成绩
char ethnic[N];//民族
char pro[N];//专业
int familyNum; //家庭人数
int fincome; //家庭收入
float t;
} Info;
void menu();
void Check_Information(); //查看学生基本信息
void Menu_Information_Handing(); //学生基本信息处理
void Input_Information(); //录入学生信息
void Modify_Information(); //修改学生基本信息
void Delete_Information(); //删除学生基本信息
void Menu_Servefor_pub();//统计信息公开
Info stu[100] = { "-","-","-","-","-","-","-","-","-",0,0}; //存储学生基本及比赛全部信息
Info stu2[100]; //作为另外一组 Info 结构体数组,暂存学生信息
int main()
{
char s[N]; //将 n 整型转化为字符串 s[N]
int n = 0; //选择菜单项目参数
char t; //判断是否继续的参数
char e[N];
while (1)
{
menu();
printf("请输入上述对应数字以进入相应界面:");
scanf_s("%s", s, 4);
n = atoi(s); //将输入的字符串转换为整型数
while (n <= 0 || n > 4)
{
fflush(stdin);
printf("输入有误,请再次输入数字 1~4:");
scanf_s("%s", s, 5);
n = atoi(s); //将输入的字符串转换为整型数
}
switch (n)
{
case 1:
getchar();
Check_Information();
break;
case 2:
printf("请输入口令:");
scanf_s("%s", e, 10);
if (!strcmp(e, "123456")) {
getchar();
Menu_Information_Handing();
}
break;
case 3:
getchar();
Menu_Servefor_pub();
break;
case 4:
system("cls");
printf("\n\n\n\n\n\n\n\n\n\t\t 退出学生信息管理系统?{(Y)、是 /(任意键)、否}\n");
t = _getch();
if (t == 'Y' || t == 'y')
{
system("cls");
printf("\n\n\n\n\n\n\n\n\n\t\t\t 退出管理系统,感谢您的使用!\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
exit(0);
}
break;
}
}
return 0;
}
void Menu_Servefor_pub() {
char s[N];
int n = 0; int i=0; int c = 0;
int m; int temp=0;
int clas;
FILE* fp;
char str[1024];
int max[N] = { 0 }, min[N] = {999,999,999,999,999,999,999,999,999};
int maxnew[N] = { 0 }, minnew[N] = { 999,999,999,999,999,999,999,999,999 };
int sum, count, countprovince[N]={0};
char provinceALL[N][N]; char mark[N] = "\0";
int countethnic[N] = { 0 }; char TEMP[N];
char ethnicALL[N][N]; char sexual[N][N]; char Time[N][N];
float avg; int flag; char proALL[N][N];
int numTime[N][N]; int boy = 0, girl = 0;
int numClas[N][N]; int s_income[N];
time_t now;
struct tm e;
time(&now);
localtime_s(&e, &now);
for (int s = 0; s < 100; s++) {
s_income[s] = 0;
}
for (int s = 0; s < 100; s++) {
numClas[s][1] = 0;
numClas[s][2] = 0;
}
for (int s = 0; s < 100; s++) {
numTime[s][1] = 0;
numTime[s][2] = 0;
}
if ((fopen_s(&fp, "C:\\Users\\80958\\Desktop\\test00.txt", "r")) == NULL)
printf("打开失败!");
system("cls");
printf("\t\t********************************************\n");
printf("\t\t* * * * * * * * * * * * * * * * * * * * * **\n");
printf("\t\t*\t* 学 生 信 息 管 理 系 统 * *\n");
printf("\t\t* 统计信息公开 *\n");
printf("\t\t* * * *\n");
printf("\t\t* 1.按学号查询某个学生的基本信息 *\n");
printf("\t\t* 2.按班级号查询该班全体学生的基本信息 *\n");
printf("\t\t* 3.按年级查询该年级全体学生基本信息 *\n");
printf("\t\t* 4.按专业查询全体学生基本信息 *\n");
printf("\t\t* 5.按班级查询高考成绩相关信息 *\n");
printf("\t\t* 6.按班级统计该班生源所属省市人数 *\n");
printf("\t\t* 7.按年级统计不同少数民族学生人数 *\n");
printf("\t\t* 8.按专业统计不同年级男女生人数 *\n");
printf("\t\t* 9.按年级统计各班低于平均收入的同学 *\n");
printf("\t\t* 10.按年级统计专业人数 *\n");
printf("\t\t* * * *\n");
printf("\t\t********************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("请输入上述对应数字以进入相应界面(系统将忽略小数位):");
scanf_s("%s", s, 5);
getchar();
n = atoi(s); //将输入的字符串转换为整型数
while (n <= 0 || n > 10)//加强鲁棒性
{
fflush(stdin);
printf("输入有误,请再次输入数字 1~10:");
scanf_s("%s", s, 5);
getchar();
n = atoi(s);
}
while (!feof(fp))//遍历读取文件
{
fscanf_s(fp, "%10s %10s %s %10s %10s %s ", stu[i].name, 10, stu[i].num, 10, stu[i].sex, 3, stu[i].dep, 10, stu[i].province, 10, stu[i].mode, 3);
if (!atoi(stu[i].mode)) {
for (int t = 0; t < 4; t++)
fscanf_s(fp, "%s", stu[i].score[t], 5);
}
else for (int t = 0; t < 9; t++)fscanf_s(fp, "%s", stu[i].score[t], 5);
fscanf_s(fp, "%10s %10s %d %d", stu[i].ethnic, 10, stu[i].pro, 10, &stu[i].familyNum, &stu[i].fincome);
i++;
}
fclose(fp);
switch (n)
{
case 1:
fflush(stdin);
printf("请输入查找的学生学号(8 位):");
gets_s(str);
while ((strlen(str) != 8) || ((atoi(str) / 10000) > e.tm_year + 1900)|| ((atoi(str) / 10000) < e.tm_year + 1896))
{
printf("输入有误,请输入 8 位学号数:");//加强鲁棒性
gets_s(str);
}
printf("\t姓名\t学号\t性别\t系别\t籍贯\t高考模式\t高考成绩\t 民族\t专业\t家庭人数\t家庭收入\n");
for (int p = 0; p < i; p++) {
if (!strcmp(stu[p].num, str)) {
printf("%10s %10s %s %10s %10s %s ", stu[p].name, stu[p].num, stu[p].sex, stu[p].dep, stu[p].province, stu[p].mode);
if (!atoi(stu[p].mode)) {
for (int t = 0; t < 4; t++)printf("%s ", stu[p].score[t]);
}
else for (int t = 0; t < 9; t++)printf("%s ", stu[p].score[t]);
printf("%10s %10s %d %d\n", stu[p].ethnic, stu[p].pro, stu[p].familyNum, stu[p].fincome);
}
}
printf("按任意键退出...");
_getch();
break;
case 2:
fflush(stdin);
printf("请输入查找的班级(4 位):");
gets_s(str);
while (strlen(str) != 4)//加强鲁棒性
{
printf("输入有误,请输入 4 位班级号:");
gets_s(str);
}
printf("\t姓名\t学号\t性别\t系别\t籍贯\t高考模式\t高考成绩\t 民族\t专业\t家庭人数\t家庭收入\n");
for (int p = 0; p < i; p++) {
clas = (atoi(stu[p].num) / 100) % 10000;
if (clas == atoi(str)) {
printf("%10s %10s %s %10s %10s %s ", stu[p].name, stu[p].num, stu[p].sex, stu[p].dep, stu[p].province, stu[p].mode);
if (!atoi(stu[p].mode)) {
for (int t = 0; t < 4; t++)printf("%s ", stu[p].score[t]);
}
else for (int t = 0; t < 9; t++)printf("%s ", stu[p].score[t]);
printf("%10s %10s %d %d\n", stu[p].ethnic, stu[p].pro, stu[p].familyNum, stu[p].fincome);
}
}
printf("按任意键退出...");
_getch();
break;
case 3:
fflush(stdin);
printf("请输入查找的年级(4 位):");//加强鲁棒性
gets_s(str);
while (strlen(str) != 4)
{
printf("输入有误,请输入 4 位年级号:");
gets_s(str);
}
printf("\t姓名\t学号\t性别\t系别\t籍贯\t高考模式\t高考成绩\t 民族\t专业\t家庭人数\t家庭收入\n");
for (int p = 0; p < i; p++) {
clas = atoi(stu[p].num) / 10000;
if (clas == atoi(str)) {
printf("%10s %10s %s %10s %10s %s ", stu[p].name, stu[p].num, stu[p].sex, stu[p].dep, stu[p].province, stu[p].mode);
if (!atoi(stu[p].mode)) {
for (int t = 0; t < 4; t++)printf("%s ", stu[p].score[t]);
}
else for (int t = 0; t < 9; t++)printf("%s ", stu[p].score[t]);
printf("%10s %10s %d %d\n", stu[p].ethnic, stu[p].pro, stu[p].familyNum, stu[p].fincome);
}
}
printf("按任意键退出...");
_getch();
break;
case 4:
fflush(stdin);
printf("请输入查找的专业:");//加强鲁棒性
gets_s(str);
printf("\t姓名\t学号\t性别\t系别\t籍贯\t高考模式\t高考成绩\t 民族\t专业\t家庭人数\t家庭收入\n");
for (int p = 0; p < i; p++) {
if (!strcmp(str, stu[p].pro)) {
printf("%10s %10s %s %10s %10s %s ", stu[p].name, stu[p].num, stu[p].sex, stu[p].dep, stu[p].province, stu[p].mode);
if (!atoi(stu[p].mode)) {
for (int t = 0; t < 4; t++)printf("%s ", stu[p].score[t]);
}
else for (int t = 0; t < 9; t++)printf("%s ", stu[p].score[t]);
printf("%10s %10s %d %d\n", stu[p].ethnic, stu[p].pro, stu[p].familyNum, stu[p].fincome);
}
}
printf("按任意键退出...");
_getch();
break;
case 5:
fflush(stdin);
printf("请输入查找的班级(4 位):");
gets_s(str);
while (strlen(str) != 4)
{//加强鲁棒性
printf("输入有误,请输入 4 位班级号:");
gets_s(str);
}
printf("\t姓名\t学号\t高考成绩 \n");
for (int p = 0; p < i; p++) {
clas = (atoi(stu[p].num) / 100) % 10000;
if (clas == atoi(str)) {
if (!atoi(stu[p].mode)) {
printf("%s %s ", stu[p].name, stu[p].num);
sum = 0;
for (int t = 0; t < 4; t++) {
printf("%s ", stu[p].score[t]);
sum += atoi(stu[p].score[t]);
}
printf("%d\n", sum);
}
else {
sum = 0;
for (int t = 0; t < 9; t++) {
printf("%s ", stu[p].score[t]);
sum += atoi(stu[p].score[t]);
}
printf("%d\n", sum);
}
}
}
sum = 0; count = 0;
for (int p = 0; p < i; p++) {
clas = (atoi(stu[p].num) / 100) % 10000;
if (clas == atoi(str)) {
if (!atoi(stu[p].mode)) {
for (int t = 0; t < 4; t++) {
if (max[t] < atoi(stu[p].score[t]))max[t] = atoi(stu[p].score[t]);
if (min[t] > atoi(stu[p].score[t]))min[t] = atoi(stu[p].score[t]);
sum += atoi(stu[p].score[t]);
}
}
else {
for (int t = 0; t < 9; t++) {
if (max[t] < atoi(stu[p].score[t]))maxnew[t] = atoi(stu[p].score[t]);
if (min[t] > atoi(stu[p].score[t]))minnew[t] = atoi(stu[p].score[t]);
sum += atoi(stu[p].score[t]);
}
}
count++;
}
}
if (count == 0)printf("没这个班的学生");
else {
avg = sum / count;
printf("本功能初始化为999\n");
printf("这个班旧高考科目最高分分别为语文%d数学%d英语%d综合%d\n", max[0], max[1], max[2], max[3]);
printf("这个班旧高考科目最低分分别为语文%d数学%d英语%d综合%d\n", min[0], min[1], min[2], min[3]);
printf("这个班新高考科目最高分分别为语文%d数学%d英语%d政%d史%d地%d生%d物%d化%\n", maxnew[0], maxnew[1], maxnew[2], maxnew[3], maxnew[4], maxnew[5], maxnew[6], maxnew[7], maxnew[8]);
printf("这个班新高考科目最低分分别为语文%d数学%d英语%d政%d史%d地%d生%d物%d化%d\n", minnew[0], minnew[1], minnew[2], minnew[3], minnew[4], minnew[5], minnew[6], minnew[7], minnew[8]);
printf("这个班学生平均总分为%f", avg);
}
printf("按任意键退出...");
_getch();
break;
case 6:
fflush(stdin);
printf("请输入查找的班级(4 位):");
gets_s(str);
while (strlen(str) != 4)
{
printf("输入有误,请输入 4 位班级:");
gets_s(str);
}
printf("\t姓名\t学号\t性别\t系别\t籍贯\t高考模式\t高考成绩\t 民族\t专业\t家庭人数\t家庭收入\n");
for (int p = 0; p < i; p++) {
clas = (atoi(stu[p].num) / 100) % 10000;
if (clas == atoi(str)) {
printf("%10s %10s %s %10s %10s %s ", stu[p].name, stu[p].num, stu[p].sex, stu[p].dep, stu[p].province, stu[p].mode);
if (!atoi(stu[p].mode)) {
for (int t = 0; t < 4; t++)printf("%s ", stu[p].score[t]);
}
else for (int t = 0; t < 9; t++)printf("%s ", stu[p].score[t]);
printf("%10s %10s %d %d\n", stu[p].ethnic, stu[p].pro, stu[p].familyNum, stu[p].fincome);
}
}
for (int p = 0; p < i; p++) {
clas = (atoi(stu[p].num) / 100) % 10000;
if (clas == atoi(str)) {
flag = 0;
for (int q = 0; q <= temp; q++) {
if (!strcmp(provinceALL[q], stu[p].province)) {
countprovince[q]++;
flag = 1;
}
}
if (flag == 0) {
strcpy_s(provinceALL[temp], stu[p].province);
countprovince[temp]++;
temp = p + 1;
}
}
}
while (countprovince[c]) {
printf("来自%s的有%d人\n", provinceALL[c], countprovince[c]);
c++;
}
printf("按任意键退出...");
_getch();
break;
case 7:
fflush(stdin);
printf("请输入查找的年级(4 位):");
gets_s(str);
while (strlen(str) != 4)
{
printf("输入有误,请输入 4 位年份:");
gets_s(str);
}
printf("\t姓名\t学号\t性别\t系别\t籍贯\t高考模式\t高考成绩\t 民族\t专业\t家庭人数\t家庭收入\n");
for (int p = 0; p < i; p++) {
clas = atoi(stu[p].num) / 10000;
if (clas == atoi(str)) {
printf("%10s %10s %s %10s %10s %s ", stu[p].name, stu[p].num, stu[p].sex, stu[p].dep, stu[p].province, stu[p].mode);
if (!atoi(stu[p].mode)) {
for (int t = 0; t < 4; t++)printf("%s ", stu[p].score[t]);
}
else for (int t = 0; t < 9; t++)printf("%s ", stu[p].score[t]);
printf("%10s %10s %d %d\n", stu[p].ethnic, stu[p].pro, stu[p].familyNum, stu[p].fincome);
}
}
for (int p = 0; p < i; p++) {
clas = atoi(stu[p].num) / 10000;
if (clas == atoi(str)) {
flag = 0;
for (int q = 0; q <= temp; q++) {
if (!strcmp(ethnicALL[q], stu[p].ethnic)) {
countethnic[q]++;
flag = 1;
}
}
if (flag == 0) {
strcpy_s(ethnicALL[temp], stu[p].ethnic);
countethnic[temp]++;
temp = p + 1;
}
}
}
while (countethnic[c]) {
printf("%s的有%d人\n", ethnicALL[c], countethnic[c]);
c++;
}
printf("按任意键退出...");
_getch();
break;
case 8:
fflush(stdin);
printf("请输入查找的专业:");
gets_s(str);
printf("\t姓名\t学号\t性别\t系别\t籍贯\t高考模式\t高考成绩\t 民族\t专业\t家庭人数\t家庭收入\n");
for (int p = 0; p < i; p++) {
if (!strcmp(str, stu[p].pro)) {
printf("%10s %10s %s %10s %10s %s ", stu[p].name, stu[p].num, stu[p].sex, stu[p].dep, stu[p].province, stu[p].mode);
if (!atoi(stu[p].mode)) {
for (int t = 0; t < 4; t++)printf("%s ", stu[p].score[t]);
}
else for (int t = 0; t < 9; t++)printf("%s ", stu[p].score[t]);
printf("%10s %10s %d %d\n", stu[p].ethnic, stu[p].pro, stu[p].familyNum, stu[p].fincome);
flag = 0;
for (int q = 0; q <= temp; q++) {
c = atoi(stu[p].num) / 10000;
if (numTime[q][0] == c) {
flag = 1;
}
}
if (flag == 0) {
numTime[temp][0] = c;
temp++;
}
}
}
flag = 0;
for (int p = 0; p < i; p++) {
if (!strcmp(str, stu[p].pro)) {
for (int q = 0; q < temp; q++) {
if ((atoi(stu[p].num) / 10000) == numTime[q][0]) {
if (!strcmp(stu[p].sex, "M")) {
numTime[q][1]++;
}
if (!strcmp(stu[p].sex, "W")) {
numTime[q][2]++;
}
}
}
}
}
c = 0;
while (c 4)
{
fflush(stdin);
printf("输入有误,请再次输入数字 1~4:");
scanf_s("%s", s,5);
n = atoi(s);
}
switch (n)
{
case 1:
Input_Information();
break;
case 2:
Modify_Information();
break;
case 3:
Delete_Information();
break;
case 4:
break;
}
}
/*函数功能:录入学生信息*/
void Input_Information()
{
FILE* fp;
char t;
int i, n, m, z; int r;
char s[N];
time_t now;
struct tm e;
time(&now);
localtime_s(&e, &now);
fopen_s(&fp,"C:\\Users\\80958\\Desktop\\test00.txt", "a+");
do
{
printf("\n********************************************\n");
printf("请输入要录入的人数:");
scanf_s("%s", s,5);
getchar();
n = atoi(s);
while (n <= 0)
{
fflush(stdin);
printf("输入有误,请再次输入正确人数,或输入-1直接停止:");
scanf_s("%s", s,5);
n = atoi(s);
if (n == -1)break;
}
printf("\n 请按以下对应信息正确录入!\n");
for (i = 0; i < n; i++)
{
system("cls");
//清空输入缓冲区
printf("-------------------------------------------------------------------------------\n");
printf("请输入第%d 名学生姓名:", i + 1);
fflush(stdin);
gets_s(stu[i].name);
fflush(stdin);
printf("请输入第%d 名学生学号(8 位):", i + 1);
gets_s(stu[i].num);
while ((strlen(stu[i].num) != 8) || ((atoi(stu[i].num) / 10000) > e.tm_year + 1900)|| ((atoi(stu[i].num) / 10000) < e.tm_year + 1896))
{
printf("输入有误,请输入 8 位学号数:");
gets_s(stu[i].num);
}
fflush(stdin);
printf("请输入第%d 名学生性别([男].M/[女].W):", i + 1);
scanf_s("%s",stu[i].sex,5);
while (strcmp(stu[i].sex, "M") && strcmp(stu[i].sex, "W"))
{
fflush(stdin);
printf("输入有误,请按要求正确输入([男].M/[女].W):");
scanf_s("%s", stu[i].sex,5);
}
getchar();
printf("请输入第%d 名学生系别(1~4):", i + 1);
gets_s(stu[i].dep);
m = atoi(stu[i].dep);
while ((m == 0 || m > 4) || strlen(stu[i].dep) != 1)
{
fflush(stdin);
printf("输入有误,请按要求正确输入系别(1~4):");
gets_s(stu[i].dep);
m = atoi(stu[i].dep);
}
fflush(stdin);
printf("请输入第%d 名学生籍贯(精确到市):", i + 1);
gets_s(stu[i].province);
fflush(stdin);
printf("请输入第%d 名学生的高考模式(0_旧高考,1_新高考):", i + 1);
gets_s(stu[i].mode);
m = atoi(stu[i].mode);
while (strcmp(stu[i].mode, "0") && strcmp(stu[i].mode, "1"))
{
fflush(stdin);
printf("输入有误,请按要求正确输入高考模式(0_旧高考,1_新高考):");
gets_s(stu[i].dep);
m = atoi(stu[i].dep);
}
fflush(stdin);
if (!atoi(stu[i].mode)) {
printf("请输入第%d 名学生高考成绩:\n语文\t数学\t英语\t综合\n", i + 1);
for (int t = 0; t < 4; t++) {
do
{
scanf_s("%s", stu[i].score[t],20);
if((atoi(stu[i].score[t]) > 150 && t < 3) || (atoi(stu[i].score[t]) > 300 && t == 3) || (atoi(stu[i].score[t]) < 0))
printf("输入有误,请按要求正确输入第%d科成绩", t + 1);
}while((atoi(stu[i].score[t]) > 150 && t < 3) || (atoi(stu[i].score[t]) > 300 && t == 3) || (atoi(stu[i].score[t]) < 0));
}
}
else {
printf("请输入第%d 名学生高考成绩,没考的科目写零分:\n语文\t数学\t英语\t政治\t历史\t地理\t生物\t物理\t化学\n", i + 1);
do {
for (int t = 0; t < 9; t++) {
do
{
scanf_s("%s", stu[i].score[t], 20);
if ((atoi(stu[i].score[t]) > 150) || (atoi(stu[i].score[t]) < 0))
printf("输入有误,请按要求正确输入第%d科成绩", t + 1);
} while ((atoi(stu[i].score[t]) > 150) || (atoi(stu[i].score[t]) < 0));
}
z = 0;
for (int t = 0; t < 9; t++) {
z += atoi(stu[i].score[t]);
}
} while (z > 750);
}
fflush(stdin);
printf("请输入第%d 名学生民族(要写族):", i + 1);
scanf_s("%s",stu[i].ethnic,20);
fflush(stdin);
printf("请输入第%d 名学生专业(写全称):", i + 1);
scanf_s("%s", stu[i].pro,20);
fflush(stdin);
printf("请输入第%d 名学生家庭人数:", i + 1);
scanf_s("%d",&stu[i].familyNum);
fflush(stdin);
printf("请输入第%d 名学生家庭收入:", i + 1);
scanf_s("%d", &stu[i].fincome);
getchar();
fprintf(fp, "%10s %10s %s %10s %10s %s ", stu[i].name, stu[i].num, stu[i].sex, stu[i].dep,stu[i].province, stu[i].mode);
if (!atoi(stu[i].mode)) {
for (int t = 0; t < 4; t++)
fprintf(fp, "%s ", stu[i].score[t]);
}
else for (int t = 0; t < 9; t++)fprintf(fp, "%s ", stu[i].score[t]);
fprintf(fp, "%10s %10s %d %d\n", stu[i].ethnic, stu[i].pro,stu[i].familyNum, stu[i].fincome);
}
printf("-------------------------------------------------------------------------------\n");
printf("是否继续输入?([Y].是/[任意键].否)\n");
t = _getch();
} while (t == 'Y' || t == 'y');
printf("-------------------------------------------------------------------------------\n");
printf("信息已保存,按任意键返回主菜单!");
_getch();
fclose(fp);
}
/*函数功能:修改学生基本信息*/
void Modify_Information()
{
FILE* fp;
char str[N];
char t;
int i = -1, x = 0;
int j, m;
time_t now;
struct tm e;
time(&now);
localtime_s(&e, &now);
fopen_s(&fp,"C:\\Users\\80958\\Desktop\\test00.txt", "r+");
printf("-------------------------------------------------------------------------------\n");
printf("\t姓名\t学号\t性别\t系别\t籍贯\t高考模式\t高考成绩\t\t\t民族\t专业\t家庭人数\t家庭收入\n");
printf("-------------------------------------------------------------------------------\n");
while (!feof(fp))
{
i++;
fscanf_s(fp, "%10s %10s %s %10s %10s %s ", stu[i].name, 10, stu[i].num, 10, stu[i].sex, 3, stu[i].dep, 10, stu[i].province, 10, stu[i].mode, 3);
if (!atoi(stu[i].mode)) {
for (int t = 0; t < 4; t++)
fscanf_s(fp, "%s", stu[i].score[t], 5);
}
else for (int t = 0; t < 9; t++)fscanf_s(fp, "%s", stu[i].score[t], 5);
fscanf_s(fp, "%10s %10s %d %d", stu[i].ethnic, 10, stu[i].pro, 10, &stu[i].familyNum, &stu[i].fincome);
printf("%10s %10s %s %10s %10s %s ", stu[i].name, stu[i].num, stu[i].sex, stu[i].dep, stu[i].province, stu[i].mode);
if (!atoi(stu[i].mode)) {
for (int t = 0; t < 4; t++)
printf("%s", stu[i].score[t]);
}
else for (int t = 0; t < 9; t++)
printf("%s", stu[i].score[t]);
printf("%10s %10s %d %d", stu[i].ethnic, stu[i].pro, stu[i].familyNum, stu[i].fincome);
printf("\n-------------------------------------------------------------------------------\n");
}
do
{
printf("-------------------------------------------------------------------------------\n");
printf("请输入要修改信息的学生姓名:");
fflush(stdin);
scanf_s("%s",str,10);
getchar();
for (j = 0; j < (i + 1); j++)
{
if (strcmp(str, stu[j].name) == 0)
{
printf("----------------------------------------------------------------------------------------\n");
printf("\t姓名\t学号\t性别\t系别\t籍贯\t高考模式\t高考成绩\t\t\t民族\t专业\t家庭人数\t家庭收入\n");
printf("----------------------------------------------------------------------------------------\n");
printf("%10s %10s %s %10s %10s %s ", stu[j].name, stu[j].num, stu[j].sex, stu[j].dep, stu[j].province, stu[j].mode);
if (!atoi(stu[j].mode)) {
for (int t = 0; t < 4; t++)printf("%s", stu[j].score[t]);
}
else for (int t = 0; t < 9; t++)printf("%s", stu[j].score[t]);
printf("%10s %10s %d %d", stu[j].ethnic, stu[j].pro, stu[j].familyNum, stu[j].fincome);
printf("\n-------------------------------------------------------------------------------\n");
printf("1.姓名2.学号3.性别4.系别5.籍贯6.高考模式7.高考成绩8.民族9.专业10.家庭人数11.家庭收入\n输入要修改的信息:");
scanf_s("%d", &x);
printf("-------------------------------------------------------------------------------\n");
system("cls");
switch (x)
{
case 1:
fflush(stdin);
printf("修改该学生姓名:");
scanf_s("%10s",stu[j].name,10);
getchar();
printf("-------------------------------------------------------------------------------\n");
fseek(fp, j * 96, 0);
fprintf(fp, "%10s %10s %s %10s %10s %s ", stu[j].name, stu[j].num, stu[j].sex, stu[j].dep, stu[j].province, stu[j].mode);
if (!atoi(stu[j].mode)) {
for (int t = 0; t < 4; t++)fprintf(fp, "%s ", stu[j].score[t]);
}
else for (int t = 0; t < 9; t++)fprintf(fp, "%s ", stu[j].score[t]);
fprintf(fp, "%10s %10s %d %d\n", stu[j].ethnic, stu[j].pro, stu[j].familyNum, stu[j].fincome);
break;
case 2:
fflush(stdin);
printf("修改该学生学号:");
scanf_s("%10s",stu[j].num,10);
getchar();
while ((strlen(stu[j].num) != 8) || ((atoi(stu[j].num) / 10000) > e.tm_year + 1900)|| ((atoi(stu[j].num) / 10000) < e.tm_year + 1896))
{
fflush(stdin);
printf("输入有误,请输入 8 位学号数:");
scanf_s("%10s", stu[j].num, 10);
getchar();
}
printf("-------------------------------------------------------------------------------\n");
fseek(fp, j * 96, 0);
fprintf(fp, "%10s %10s %s %10s %10s %s ", stu[j].name, stu[j].num, stu[j].sex, stu[j].dep, stu[j].province, stu[j].mode);
if (!atoi(stu[j].mode)) {
for (int t = 0; t < 4; t++)fprintf(fp, "%s ", stu[j].score[t]);
}
else for (int t = 0; t < 9; t++)fprintf(fp, "%s ", stu[j].score[t]);
fprintf(fp, "%10s %10s %d %d\n", stu[j].ethnic, stu[j].pro, stu[j].familyNum, stu[j].fincome);
break;
case 3:
fflush(stdin);
printf("修改该学生性别:");
scanf_s("%s", stu[i].sex,3);
getchar();
while (strcmp(stu[j].sex, "M") && strcmp(stu[j].sex, "W"))
{
fflush(stdin);
printf("输入有误,请按要求正确输入([男].M/[女].W):");
scanf_s("%s", stu[i].sex,3);
}
printf("-------------------------------------------------------------------------------\n");
fseek(fp, j * 96, 0);
fprintf(fp, "%10s %10s %s %10s %10s %s ", stu[j].name, stu[j].num, stu[j].sex, stu[j].dep, stu[j].province, stu[j].mode);
if (!atoi(stu[j].mode)) {
for (int t = 0; t < 4; t++)fprintf(fp, "%s ", stu[j].score[t]);
}
else for (int t = 0; t < 9; t++)fprintf(fp, "%s ", stu[j].score[t]);
fprintf(fp, "%10s %10s %d %d\n", stu[j].ethnic, stu[j].pro, stu[j].familyNum, stu[j].fincome);
break;
case 4:
fflush(stdin);
printf("修改该学生系别:");
scanf_s("%s",stu[j].dep,10);
m = atoi(stu[j].dep);
while ((m == 0 || m > 4) || strlen(stu[j].dep) != 1)
{
fflush(stdin);
printf("输入有误,请按要求正确输入系别(1~4):");
scanf_s("%s", stu[j].dep, 10);
m = atoi(stu[j].dep);
}
printf("-------------------------------------------------------------------------------\n");
fseek(fp, j * 96, 0);
fprintf(fp, "%10s %10s %s %10s %10s %s ", stu[j].name, stu[j].num, stu[j].sex, stu[j].dep, stu[j].province, stu[j].mode);
if (!atoi(stu[j].mode)) {
for (int t = 0; t < 4; t++)fprintf(fp, "%s ", stu[j].score[t]);
}
else for (int t = 0; t < 9; t++)fprintf(fp, "%s ", stu[j].score[t]);
fprintf(fp, "%10s %10s %d %d\n", stu[j].ethnic, stu[j].pro, stu[j].familyNum, stu[j].fincome);
break;
case 5:
fflush(stdin);
printf("修改该学生籍贯:");
scanf_s("%10s",stu[j].province,10);
printf("-------------------------------------------------------------------------------\n");
fseek(fp, j * 96, 0);
fprintf(fp, "%10s %10s %s %10s %10s %s ", stu[j].name, stu[j].num, stu[j].sex, stu[j].dep, stu[j].province, stu[j].mode);
if (!atoi(stu[j].mode)) {
for (int t = 0; t < 4; t++)fprintf(fp, "%s ", stu[j].score[t]);
}
else for (int t = 0; t < 9; t++)fprintf(fp, "%s ", stu[j].score[t]);
fprintf(fp, "%10s %10s %d %d\n", stu[j].ethnic, stu[j].pro, stu[j].familyNum, stu[j].fincome);
break;
case 6:
fflush(stdin);
printf("修改该学生高考模式(0_旧高考1_新高考):");
scanf_s("%3s",stu[j].mode,3);
getchar();
m = atoi(stu[j].mode);
while (strcmp(stu[j].mode, "0") && strcmp(stu[j].mode, "1"))
{
fflush(stdin);
printf("输入有误,请按要求正确输入高考模式(0_旧高考,1_新高考):");
scanf_s("%3s", stu[j].mode, 3);
getchar();
m = atoi(stu[j].mode);
}
if (!atoi(stu[j].mode)) {
printf("请输入学生高考成绩,没考的科目写零分:\n语文\t数学\t英语\t综合\n");
for (int t = 0; t < 4; t++) {
do
{
scanf_s("%s",stu[j].score[t],10);
if ((atoi(stu[j].score[t]) > 150 && t < 3) || (atoi(stu[j].score[t]) > 300 && t == 3) || (atoi(stu[j].score[t]) < 0))
printf("输入有误,请按要求正确输入第%d科成绩", t + 1);
} while ((atoi(stu[j].score[t]) > 150 && t < 3) || (atoi(stu[j].score[t]) > 300 && t == 3) || (atoi(stu[j].score[t]) < 0));
}
}
else {
printf("请输入学生高考成绩,没考的科目写零分:\n语文\t数学\t英语\t政治\t历史\t地理\t生物\t物理\t化学\n");
for (int t = 0; t < 9; t++) {
do
{
scanf_s(stu[j].score[t]);
getchar();
if ((atoi(stu[j].score[t]) > 150) || (atoi(stu[j].score[t]) < 0))
printf("输入有误,请按要求正确输入第%d科成绩", t + 1);
} while ((atoi(stu[j].score[t]) > 150) || (atoi(stu[j].score[t]) < 0));
}
}
printf("-------------------------------------------------------------------------------\n");
fseek(fp, j * 96, 0);
fprintf(fp, "%10s %10s %s %10s %10s %s ", stu[j].name, stu[j].num, stu[j].sex, stu[j].dep, stu[j].province, stu[j].mode);
if (!atoi(stu[j].mode)) {
for (int t = 0; t < 4; t++)fprintf(fp, "%s ", stu[j].score[t]);
}
else for (int t = 0; t < 9; t++)fprintf(fp, "%s ", stu[j].score[t]);
fprintf(fp, "%10s %10s %d %d\n", stu[j].ethnic, stu[j].pro, stu[j].familyNum, stu[j].fincome);
break;
case 7:
fflush(stdin);
if (!atoi(stu[j].mode)) {
printf("请输入学生高考成绩,没考的科目写零分:\n语文\t数学\t英语\t综合\n");
for (int t = 0; t < 4; t++) {
do
{
scanf_s("%s", stu[j].score[t], 10);
if ((atoi(stu[j].score[t]) > 150 && t < 3) || (atoi(stu[j].score[t]) > 300 && t == 3) || (atoi(stu[j].score[t]) < 0))
printf("输入有误,请按要求正确输入第%d科成绩", t + 1);
} while ((atoi(stu[j].score[t]) > 150 && t < 3) || (atoi(stu[j].score[t]) > 300 && t == 3) || (atoi(stu[j].score[t]) < 0));
}
}
else {
printf("请输入学生高考成绩,没考的科目写零分:\n语文\t数学\t英语\t政治\t历史\t地理\t生物\t物理\t化学\n");
for (int t = 0; t < 9; t++) {
do
{
scanf_s(stu[j].score[t]);
getchar();
if ((atoi(stu[j].score[t]) > 150) || (atoi(stu[j].score[t]) < 0))
printf("输入有误,请按要求正确输入第%d科成绩", t + 1);
} while ((atoi(stu[j].score[t]) > 150) || (atoi(stu[j].score[t]) < 0));
}
}
printf("-------------------------------------------------------------------------------\n");
fseek(fp, j * 96, 0);
fprintf(fp, "%10s %10s %s %10s %10s %s ", stu[j].name, stu[j].num, stu[j].sex, stu[j].dep, stu[j].province, stu[j].mode);
if (!atoi(stu[j].mode)) {
for (int t = 0; t < 4; t++)fprintf(fp, "%s ", stu[j].score[t]);
}
else for (int t = 0; t < 9; t++)fprintf(fp, "%s ", stu[j].score[t]);
fprintf(fp, "%10s %10s %d %d\n", stu[j].ethnic, stu[j].pro, stu[j].familyNum, stu[j].fincome);
break;
case 8:
fflush(stdin);
printf("修改该学生民族:");
scanf_s("%10s",stu[j].ethnic,10);
printf("-------------------------------------------------------------------------------\n");
fseek(fp, j * 96, 0);
fprintf(fp, "%10s %10s %s %10s %10s %s ", stu[j].name, stu[j].num, stu[j].sex, stu[j].dep, stu[j].province, stu[j].mode);
if (!atoi(stu[j].mode)) {
for (int t = 0; t < 4; t++)fprintf(fp, "%s ", stu[j].score[t]);
}
else for (int t = 0; t < 9; t++)fprintf(fp, "%s ", stu[j].score[t]);
fprintf(fp, "%10s %10s %d %d\n", stu[j].ethnic, stu[j].pro, stu[j].familyNum, stu[j].fincome);
break;
case 9:
fflush(stdin);
printf("修改该学生专业:");
scanf_s("%10s",stu[j].pro,10);
printf("-------------------------------------------------------------------------------\n");
fseek(fp, j * 96, 0);
fprintf(fp, "%10s %10s %s %10s %10s %s ", stu[j].name, stu[j].num, stu[j].sex, stu[j].dep, stu[j].province, stu[j].mode);
if (!atoi(stu[j].mode)) {
for (int t = 0; t < 4; t++)fprintf(fp, "%s ", stu[j].score[t]);
}
else for (int t = 0; t < 9; t++)fprintf(fp, "%s ", stu[j].score[t]);
fprintf(fp, "%10s %10s %d %d\n", stu[j].ethnic, stu[j].pro, stu[j].familyNum, stu[j].fincome);
break;
case 10:
fflush(stdin);
printf("修改该学生家庭人数:");
scanf_s("%d",&stu[j].familyNum);
printf("-------------------------------------------------------------------------------\n");
fseek(fp, j * 96, 0);
fprintf(fp, "%10s %10s %s %10s %10s %s ", stu[j].name, stu[j].num, stu[j].sex, stu[j].dep, stu[j].province, stu[j].mode);
if (!atoi(stu[j].mode)) {
for (int t = 0; t < 4; t++)fprintf(fp, "%s ", stu[j].score[t]);
}
else for (int t = 0; t < 9; t++)fprintf(fp, "%s ", stu[j].score[t]);
fprintf(fp, "%10s %10s %d %d\n", stu[j].ethnic, stu[j].pro, stu[j].familyNum, stu[j].fincome);
break;
case 11:
fflush(stdin);
printf("修改该学生家庭收入:");
scanf_s("%d", &stu[j].fincome);
printf("-------------------------------------------------------------------------------\n");
fseek(fp, j * 96, 0);
fprintf(fp, "%10s %10s %s %10s %10s %s ", stu[j].name, stu[j].num, stu[j].sex, stu[j].dep, stu[j].province, stu[j].mode);
if (!atoi(stu[j].mode)) {
for (int t = 0; t < 4; t++)fprintf(fp, "%s ", stu[j].score[t]);
}
else for (int t = 0; t < 9; t++)fprintf(fp, "%s ", stu[j].score[t]);
fprintf(fp, "%10s %10s %d %d\n", stu[j].ethnic, stu[j].pro, stu[j].familyNum, stu[j].fincome);
break;
default:
printf("输入有误!\n");
}
break;
}
}
fflush(stdin);
if (j > i) printf("查无此人!\n");
printf("-------------------------------------------------------------------------------\n");
printf("是否继续输入?([Y].是/[任意键].否)\n");
t = _getch();
} while (t == 'Y' || t == 'y');
fclose(fp);
}
/*函数功能:删除学生基本信息*/
void Delete_Information()
{
FILE* fp;
char str[N];
char t, ch;
int i = 0;
int j, n;
fopen_s(&fp,"C:\\Users\\80958\\Desktop\\test00.txt", "r+");
printf("--------------------------------------------------------------------------------------\n");
printf("\t姓名\t学号\t性别\t系别\t籍贯\t高考模式\t高考成绩\t\t\t民族\t专业\t家庭人数\t家庭收入\n");
printf("--------------------------------------------------------------------------------------\n");
while (!feof(fp))
{
fscanf_s(fp, "%10s %10s %s %10s %10s %s ", stu[i].name,10, stu[i].num, 10, stu[i].sex,3, stu[i].dep, 10, stu[i].province, 10, stu[i].mode,3);
if (!atoi(stu[i].mode)) {
for (int t = 0; t < 4; t++)
fscanf_s(fp, "%5s", stu[i].score[t],5);
}
else for (int t = 0; t < 9; t++)
fscanf_s(fp, "%s", stu[i].score[t],5);
fscanf_s(fp, "%10s %10s %d %d", stu[i].ethnic, 10, stu[i].pro, 10, &stu[i].familyNum, &stu[i].fincome);
printf("%10s %10s %s %10s %10s %s ", stu[i].name, stu[i].num, stu[i].sex, stu[i].dep, stu[i].province, stu[i].mode);
if (!atoi(stu[i].mode)) {
for (int t = 0; t < 4; t++)printf("%s ", stu[i].score[t]);
}
else for (int t = 0; t < 9; t++)printf("%s ", stu[i].score[t]);
printf("%10s %10s %d %d", stu[i].ethnic, stu[i].pro, stu[i].familyNum, stu[i].fincome);
printf("\n-------------------------------------------------------------------------------\n");
i++;
}
fclose(fp);
do
{
printf("-------------------------------------------------------------------------------\n");
printf("请输入要删除信息的学生姓名:");
fflush(stdin);
gets_s(str);
for (j = 0; j < i; j++)
{
if (strcmp(str, stu[j].name) == 0)
{
printf("-------------------------------------------------------------------------------\n");
printf(" 姓名\t学号\t性别\t系别\t籍贯\t高考模式\t高考成绩\t\t\t民族\t专业\t家庭人数\t家庭收入\n");
printf("-------------------------------------------------------------------------------\n");
printf("%10s %10s %s %10s %10s %s ", stu[j].name, stu[j].num, stu[j].sex, stu[j].dep, stu[j].province, stu[j].mode);
if (!atoi(stu[j].mode)) {
for (int t = 0; t < 4; t++)printf("%s", stu[j].score[t]);
}
else for (int t = 0; t < 9; t++)printf("%s", stu[j].score[t]);
printf("%10s %10s %d %d", stu[j].ethnic, stu[j].pro, stu[j].familyNum, stu[j].fincome);
printf("\n-------------------------------------------------------------------------------\n");
n = j + 1;
break;
}
}
fflush(stdin);
if (j == i) printf("查无此人!\n");
if (j < i)
{
printf("确定删除?([Y].是/[任意键].否)");
ch = _getch();
if (ch == 'Y' || ch == 'y')
{
for (j = n; j < i; j++)
{
stu[j - 1]=stu[j]; //将删除的学生后续的学生信息向前移动
}
memset(&stu[i - 1], 0, 95); //最后一个学生不可以和倒数第二个内容一致?做清除操作
}
printf("\n-------------------------------------------------------------------------------\n");
printf("删除成功!");
}
fopen_s(&fp,"C:\\Users\\80958\\Desktop\\test00.txt", "w+");
for (j = 0; j < i - 1; j++)
{
fprintf(fp, "%10s %10s %s %10s %10s %s ", stu[j].name, stu[j].num, stu[j].sex, stu[j].dep, stu[j].province, stu[j].mode);
if (!atoi(stu[j].mode)) {
for (int t = 0; t < 4; t++)fprintf(fp, "%s ", stu[j].score[t]);
}
else for (int t = 0; t < 9; t++)fprintf(fp, "%s ", stu[j].score[t]);
fprintf(fp, "%10s %10s %d %d\n", stu[j].ethnic, stu[j].pro, stu[j].familyNum, stu[j].fincome);
}
fflush(stdin);
printf("\n-------------------------------------------------------------------------------\n");
printf("\n 是否继续输入?([Y].是/[任意键].否)");
t = _getch();
} while (t == 'Y' || t == 'y');
fclose(fp);
}