学生信息管理系统

时间:2021-09-09 20:19:31

使用时需要用户自己在d盘根目录创建文件“学生信息.txt”

学生信息管理系统开发
学生信息至少包括学号,姓名,性别,计算机分数,数学分数,英语分数等(学号不得相等)。该系统需要能够提供下列功能:
(1)系统以菜单方式工作(必做)
(2)学生信息录入功能(学生信息用文件保存)(必做)
(3)学生信息浏览功能 (能查看所有同学的记录)(必做)
(4)学生信息查询功能,查询方式:(至少完成一项)
1)按学号查询
2)按姓名查询
(5)成绩排序统计功能:(至少完成一项)
1)按照指定的要求对学生记录进行排序
2)按照指定的学科对成绩进行统计,需要提供该门成绩的:最高分、最低分、平均分、及格率及在五个分数段的学生人数比率([0,59],[60,69],[70,79],[80,89],[90,100])
(6)学生信息删除,修改功能(可选项)。
其中:学号系统随机生成,范围在2016000—2016999内。

源代码:

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

#define MAX_SIZE 1000

int Total = 0; // 记录当前学生人数


struct Students
{
int studentID;
char name[20];
char sex;
int computerScore, mathScore, englishScore;
} info[MAX_SIZE];
struct Students t; // 做临时变量

// 退出
void quit()
{
system("cls");
printf("欢迎使用~再见~!\n");
}

// 导入文件
void loadInformation()
{
FILE *fp;
int i = 0;
fp = fopen("d:\\学生信息.txt", "r"); // r: 只读
if (fp == NULL)
{
printf("Can not open the file !\n");
exit(0);
}
while (!feof(fp))
{
fscanf(fp, "%d\t%s\t%c\t%d\t%d\t%d", &info[i].studentID, &info[i].name, &info[i].sex, &info[i].computerScore, &info[i].mathScore, &info[i].englishScore);
if (info[i].studentID >= 2016000)
i++;
}
fclose(fp);
Total = i;
}

// 保存到文件
void saveInformation()
{
FILE *fp;
fp = fopen("d:\\学生信息.txt", "wt"); // wt : 清空原有文本并写入
if (fp == NULL)
{
printf("Connot open file!\n");
exit(0);
}
for (int i = 0; i < Total; i++)
{
fprintf(fp, "%d\t%s\t%c\t%d\t%d\t%d\n", info[i].studentID, info[i].name, info[i].sex, info[i].computerScore, info[i].mathScore, info[i].englishScore);
}
fclose(fp);
}

// 浏览所有学生信息
void displayAll()
{
printf("\n学生信息:\n学号\t姓名\t性别\t计算机\t数学\t英语\n\n");
for (int i = 0; i < Total; i++)
{
printf("%d\t%s\t%c\t%d\t%d\t%d\n", info[i].studentID, info[i].name, info[i].sex, info[i].computerScore, info[i].mathScore, info[i].englishScore);
}
}

// 判断是否为空!
bool isEmpty()
{
if (Total != 0) return false;
return true;
}

// 判断学生信息是否存在
int isExist(int studentID)
{
for (int i = 0; i < Total; i++)
{
if (studentID == info[i].studentID) return i;
}
return -1;
}

// 判断分数是否输入正确
bool isRightScore(int score)
{
if ((score < 0) || (score > 100)) return false;
else return true;
}

// 产生随机数
int random()
{
srand((unsigned)time(NULL));
return rand() % 1000 + 2016000;
}

// 产生随机学号
void getStudentID(int i)
{
int studentID = random();
while (isExist(studentID) != -1)
studentID = random();
info[i].studentID = studentID;
}

// 输入学生信息
void inputInfomation(int i)
{
printf("请输入第 %d 位学生信息: \n", i+1);
printf("学 号: %d\n", info[i].studentID);
printf("姓 名: ");
scanf("%s", info[i].name);
printf("性 别: ");
getchar();
scanf("%c", &info[i].sex);
while (true)
{
printf("计算机分数: ");
scanf("%d", &info[i].computerScore);
if (!isRightScore(info[i].computerScore))
printf("请输入正确的分数,正确的分数范围:[0,100]\n");
else break;
}
while (true)
{
printf("数 学 分数: ");
scanf("%d", &info[i].mathScore);
if (!isRightScore(info[i].mathScore))
printf("请输入正确的分数,正确的分数范围:[0,100]\n");
else break;
}
while (true)
{
printf("英 语 分数: ");
scanf("%d", &info[i].englishScore);
if (!isRightScore(info[i].englishScore))
printf("请输入正确的分数,正确的分数范围:[0,100]\n");
else break;
}
}

// 做排序交换
void doExchange(int i, int j)
{
t = info[i]; info[i] = info[j]; info[j] = t;
}

// 按姓名的首字母排序
void sortInformationByName()
{
int i, j;
for (i = 0; i < Total; i++)
for (j = 1; j < Total - i; j++)
{
if (strcmp(info[j].name, info[j - 1].name) < 0)
doExchange(j, j - 1);
}
displayAll();
}

// 删除学生信息
void deleteInformationByID()
{
int studentID, index;
printf("请输入您要删除学生的学号: ");
scanf("%d", &studentID);
if ((index = isExist(studentID)) != -1)
{
for (index; index < Total - 1; index++)
{
info[index] = info[index+1];
}
saveInformation();
printf("删除成功!");
--Total;
}
else
printf("该学生信息不存在!请检查是否输入正确!\n");
}

// 添加学生信息
void addInformation()
{
int add_num;
printf("请输入要添加信息的条数: ");
scanf("%d", &add_num);
for (int i = Total; i < Total+add_num; i++)
{
getStudentID(i);
inputInfomation(i);
}
Total += add_num;
saveInformation();
printf("添加成功!\n");
}

// 修改学生信息
void modifyInfomationByID()
{
int studentID, index;
printf("请输入被修改学生的学号或姓名: ");
scanf("%d", &studentID);
if ((index = isExist(studentID)) != -1)
{
printf("您将修改此学生的信息: %d(学号)\t%s(姓名)\n", info[index].studentID, info[index].name);
inputInfomation(index);
saveInformation();
printf("修改完成!");
}
else
printf("此学生信息不存在!请检查输入是否正确!");
}

// 每个分数段的人数比率
void computerScore_EachRate()
{
int number_60 = 0, number_70 = 0, number_80 = 0, number_90 = 0, number_100 = 0; // 低于各分值的人数
double rate;

for (int i = 0; i < Total; i++)
{
if ((info[i].computerScore >= 0) && info[i].computerScore < 60)
++number_60;
else if (info[i].computerScore < 70)
++number_70;
else if (info[i].computerScore < 80)
++number_80;
else if (info[i].computerScore < 90)
++number_90;
else if (info[i].computerScore <= 100)
++number_100;
}
rate = (number_60*1.0) / Total;
printf("[0,60): %4lf\%\n", rate * 100);
rate = (number_70*1.0) / Total;
printf("[60,70): %4lf\%\n", rate * 100);
rate = (number_80*1.0) / Total;
printf("[70,80): %4lf\%\n", rate * 100);
rate = (number_90*1.0) / Total;
printf("[80,90): %4lf\%\n", rate * 100);
rate = (number_100*1.0) / Total;
printf("[90,100): %4lf\%\n", rate * 100);
}

// 计算机的及格率
double computerScore_Rate()
{
int number = 0; // 及格人数
double rate;
for (int i = 0; i < Total; i++)
{
if ( info[i].computerScore >= 60)
{
++number;
}
}
rate = (number*1.0) / Total;
return rate; // 用百分号(%)可以乘以一百再返回
}

// 计算机的平均分
double computerScore_Average()
{
int sum = 0;
double average;
for (int i = 0; i < Total; i++)
sum += info[i].computerScore;
average = (sum*1.0) / Total;
return average;
}

// 计算机的最低分
int computerScore_MIN()
{
int minComputerScore = 100;
for (int i = 0; i < Total; i++)
{
if (info[i].computerScore <= minComputerScore)
minComputerScore = info[i].computerScore;
}
return minComputerScore;
}

// 计算机的最高分
int computerScore_MAX()
{
int maxComputerScore = 0;
for (int i = 0; i < Total; i++)
{
if (info[i].computerScore >= maxComputerScore )
maxComputerScore = info[i].computerScore;
}
return maxComputerScore;
}

// 显示某学生的信息
void displayOne(int i)
{
printf("\n学生信息:\n学号\t姓名\t性别\t计算机\t数学\t英语\n\n");
printf("%d\t%s\t%c\t%d\t%d\t%d\n", info[i].studentID, info[i].name, info[i].sex, info[i].computerScore, info[i].mathScore, info[i].englishScore);
}

// 查找学生信息(按学号查找)
void searchInformationByID()
{
int studentID;
int index;
printf("请输入学号: ");
scanf("%d", &studentID);
index = isExist(studentID);
if (index != -1)
displayOne(index);
else
printf("此学生信息不存在!请检查输入是否有误!\n");
}

// 录入学生信息
void getInformation()
{
int num;
char choose;
if ( !isEmpty() )
{
printf("是否清空所有信息重新录入:y 是\tn 否\n");
getchar();
choose = getchar();
if (choose == 'Y' || choose == 'y')
{
Total = 0;
printf("请输入您要录入的人数: ");
scanf("%d", &num);
for (int i = 0; i < num; i++)
{
getStudentID(i);
inputInfomation(i);
}
saveInformation();
Total += num;
printf("重新录入成功!\n");
}
else
printf("已取消重新录入!\n");
}
else
{
printf("请输入您要录入的人数: ");
scanf("%d", &num);
for (int i = 0; i < num; i++)
{
getStudentID(i);
inputInfomation(i);
}
saveInformation();
Total += num;
printf("录入成功!\n");
}

}

// 菜单
int menu()
{
int select = 0;
printf("_______________________________________________________________________________\n");
printf("*************************欢迎使用学生信息管理系统******************************\n");
printf(" \t╔════════════════════╗\n");
printf(" \t║1.学生信息录入 ");
printf(" 2.学生信息添加 ║\n");
printf(" \t║3.学生信息浏览 ");
printf(" 4.学生信息查询 ║\n");
printf(" \t║5.成绩排序统计 ");
printf(" 6.删除学生信息 ║\n");
printf(" \t║7.修改信息 ");
printf(" 0.退出系统 ║\n");
printf(" \t╚════════════════════╝\n");
printf("*******************************************************************************\n");
printf("_______________________________________________________________________________\n");
printf("请选择:");
scanf("%d", &select);
return select;
}

int main()
{
loadInformation();
for (;;)
{
system("color a");
system("pause");
system("cls");
printf("_______________________________________________________________________________\n");
//printf("现有学生信息条数:%d\n",w);
switch (menu())
{
case 1:getInformation(); break;
case 2:addInformation(); break;
case 3:displayAll(); break;
case 4:searchInformationByID(); break;
case 5:sortInformationByName(); break;
case 6:deleteInformationByID(); break;
case 7:modifyInfomationByID(); break;
//case 8:rekey(); break;
case 0:quit(); exit(0);
default:
printf("您输入有误!\n");
}
}
return 0;
}