整个系统包括三个文件:学生信息文件、体育信息文件、密码文件、
main.cpp
#include <iostream>
#include <algorithm>
#include <windows.h>
#include <conio.h>
#include "paixu.h"
#include "data.h"
#include "basic.h"
#include "file.h"
#include "password.h"
#include "menu.h"
#include "sport.h"
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <windows.h>
using namespace std;
int main(){
system("color b2");
system("cls");
welcome();
int j = 0;
for(int i = 3;i >= 1;i--){
password();
if(flag == 1)
break;
else{
if(i - 1 == 0)
{
Sleep(500);
break;
}
printf("密码错误,您还有%d次机会!\n",i-1);
Sleep(500);
}
}
if(flag == 1){
printf("密码正确\n");
Sleep(500);
system("cls");
while(1)
{
j++;
Sleep(500);
Student stu[500];
sport spo[500];
readstu();
if(j >= 2){
printf("按任意键返回");
getch();
system ("cls");
}
printf("请输入您要选择的功能键:\n");
menu();//功能菜单
int n = getch();
switch(n)
{
case '0'://退出
printf("谢谢使用!");
exit(0);
case '1'://增加学生信息
stu_add();//增加新的学生
savestu();
break;
case '2'://删除学生信息
delete1();//删除学生
savestu();
break;
case '3'://改变学生的信息
change();//改变学生信息
savestu();
break;
case '4'://查找某个学生的信息
search1();//按学号查找学生并输出该生信息
break;
case '5':// 对bmi进行排序
mysort();
break;
case '6'://输出所有学生的信息
glanceall();
break;
case '7':
menu1();
int m = getch();
switch(m){
case '0':
break;
case '1':
addsport();
if(flag2 == 0)
stu_addscore();
savestu();
break;
case '2':
delsport();
savestu();
break;
case '3':
changesport();
savestu();
break;
case '4':
glanceallsport();
break;
default:
printf("请输入正确的选择\n");
break;
}
}
}
}
}
data.h
#ifndef _data_h_
#define _data_h_
#include <iostream>
#include <algorithm>
#include <windows.h>
#include <conio.h>
using namespace std;
struct Student{
char name[100];
char number[20];
char sex[2];
int age;
int weight;
int height;
float BMI()
{
return (double)weight / pow((double)height / 100, 2);
}
int sport_score[100];
};
struct sport{
int sport_num;
char sport_name[20];
};
Student stu[500];
char nam[100];
char num[20];
char sex1[2];
int age1;
int wei;
int hei;
int count1 = 0;
char code[20];
int flag = 0;
sport spo[500];
int sportnum;
char sportname[20];
int sum = 0;
int flag2 = 0;
int number5;
#endif
file.h
#ifndef _file_h_
#define _file_h_
#include "data.h"
#include <iostream>
#include <algorithm>
#include <windows.h>
#include <conio.h>
using namespace std;
void readstu() //运行前把文件内容读取到电脑内存
{
FILE *p;
p = fopen("sport.txt" , "r");
if(p == NULL)
{
printf("无法打开文件\n");
exit(1);
}
int j = 0;
while(!feof(p))
{
fscanf(p , "%d %s ", &spo[j].sport_num, &spo[j].sport_name);
j++;
}
sum = j;
FILE *FP;
FP = fopen("student.txt" , "r"); //以只读方式打开当前目录下的.txt
if(FP == NULL)
{
printf("无法打开文件\n");
exit(1);
}
int i = 0;
while(!feof(FP))
{
fscanf(FP , "%s %s %s %d %d %d ", &stu[i].number, &stu[i].name, &stu[i].sex, &stu[i].age , &stu[i].height, &stu[i].weight);
for(int j = 0;j < sum;j++){
int num;
fscanf(FP, "%d", &num);
fscanf(FP, " %d", &stu[i].sport_score[num]);
}
i++;
}
count1 = i;
fclose(FP);
fclose(p);
}
void savestu()//保存学生信息到文件
{
FILE *fp, *p;
if((fp = fopen("student.txt" , "w")) == NULL ||(p = fopen("sport.txt" , "w")) == NULL)// 以可写的方式打开当前目录下的.txt
{
printf("不能打开此文件,请按任意键退出\n");
exit(1);
}
int i = 0;
while(i < count1)
{
fprintf(fp , "%s %s %s %d %d %d", stu[i].number , stu[i].name, stu[i].sex, stu[i].age, stu[i].height, stu[i].weight);
for(int j = 0;j < sum;j++){
fprintf(fp, " %d %d", spo[j].sport_num, stu[i].sport_score[spo[j].sport_num]);
}
i++;
if(i < count1)
fprintf(fp, "\n");
}
int j = 0;
while(j < sum){
fprintf(p, "%d %s",spo[j].sport_num, spo[j].sport_name);
j++;
if(j < sum)
fprintf(p, "\n");
}
fclose(fp);
fclose(p);
}
#endif
basic.h
#ifndef _basic_h_
#define _basic_h_
#include "data.h"
#include <iostream>
#include <algorithm>
#include <windows.h>
#include <conio.h>
using namespace std;
void title_head(char *ch){
printf(" \n ======>>>学生健康管理系统>>>======\n\n %s \n\n",ch);
}
void stu_add(){
char flag;//用于判断继续还是结束,ESC的ASC||码为27
title_head("增加学生基本信息");
while(flag != 27){
printf("姓名:");
scanf("%s", stu[count1].name);
printf("学号:");
scanf("%s", stu[count1].number);
printf("性别:");
scanf("%s", stu[count1].sex);
printf("年龄:");
scanf("%d", &stu[count1].age);
printf("身高:");
scanf("%d", &stu[count1].height);
printf("体重:");
scanf("%d", &stu[count1].weight);
for(int j = 1;j <= sum;j++){
printf("%s成绩:",spo[j].sport_name);
scanf("%d",&stu[count1].sport_score[spo[j].sport_num]);
}
count1++;
printf("按任意键继续,结束输入请按Esc\n");
flag = getch();//暂停程序当i接收后继续下一条指令
}
}
void delete1(){//删除学生信息
int i;
printf("请输入要删除的学生的学号:\n");
scanf("%s", &num);
for(i = 0;i < count1;i++)
if(strcmp(num , stu[i].number) == 0)
break;
if(i == count1){
printf("该学生不存在\n");
return;
}
for(;i < count1;i++){
strcpy(stu[i].name, stu[i+1].name);
strcpy(stu[i].number, stu[i+1].number);
strcpy(stu[i].sex, stu[i+1].sex);
stu[i].age = stu[i+1].age;
stu[i].height = stu[i+1].height;
stu[i].weight = stu[i+1].weight;
}
count1--;
printf("删除成功\n");
}
void search1(){
int i;
printf("请输入要查找的学号:\n");
scanf("%s", &num);
for(i = 0;i < count1;i++)
if(strcmp(num , stu[i].number) == 0)
break;
if(i == count1){
printf("未查到该学生学号");
return;
}
printf("查询成功!该学生信息为:\n");
printf("姓名:%s\n学号:%s\n性别:%s\n年龄:%d\n身高:%d\n体重:%d\nBMI:%f\n",stu[i].name, stu[i].number,stu[i].sex,stu[i].age,stu[i].height,stu[i].weight,stu[i].BMI());
for(int k = 0;k < sum;k++)
printf("%s成绩:%d\n", spo[k].sport_name,stu[i].sport_score[spo[k].sport_num]);
}
//浏览所有信息
void glanceall(){
int i = 0;
if(count1 == 0){
printf("还没有学生信息,请增加学生信息\n");
return;
}
while(i < count1){
printf("姓名:%s\n",stu[i].name);
printf("学号:%s\n",stu[i].number);
printf("性别:%s\n",stu[i].sex);
printf("年龄:%d\n",stu[i].age);
printf("体重:%d\n",stu[i].weight);
printf("身高:%d\n",stu[i].height);
printf("BMI指数:%f\n",stu[i].BMI());
for(int j = 0;j < sum;j++){
printf("%s成绩:%d\n",spo[j].sport_name, stu[i].sport_score[spo[j].sport_num]);
}
printf("\n");
i++;
}
}
void change(){
int i;
printf("请输入要修改的学号:\n");
scanf("%s", &num);
for(i = 0;i < count1;i++)
if(strcmp(num , stu[i].number) == 0){
printf("已查到该学生信息,请输入修改后的学生信息:\n");
break;
}
if(i == count1){
printf("未查到该学生信息!");
return;
}
printf("姓名:\n");
scanf("%s", nam);
strcpy(stu[i].name , nam);
printf("学号:\n");
scanf("%s", &num);
strcpy(stu[i].number , num);
printf("性别:\n");
scanf("%s", sex1);
strcpy(stu[i].sex , sex1);
printf("年龄:\n");
scanf("%d", &age1);
stu[i].age = age1;
printf("身高:\n");
scanf("%d", &hei);
stu[i].height = hei;
printf("体重:\n");
scanf("%d", &wei);
stu[i].weight = wei;
printf("需要修改成绩请输入 1, 否则输入 0\n");
int a = getch();
if(a == '1'){
printf("请输入需要修改的成绩的编号:\n");
int k = 0;
scanf("%d" ,&sportnum);
printf("请输入修改后的成绩:\n");
scanf("%d", &k);
stu[i].sport_score[sportnum] = k;
}
printf("修改成功!");
}
#endif
menu.h
#ifndef _menu_h_
#define _menu_h_
#include "data.h"
#include <iostream>
#include <algorithm>
#include <windows.h>
#include <conio.h>
using namespace std;
void menu()
{
system("color b2");
printf(" ________________________________________________ \n");
printf(" | |\n");
printf(" | 学生体测信息管理系统 |\n");
printf(" | |\n");
printf(" | 系统中已存学生人数:%d |\n", count1);
printf(" | 0、退出系统 |\n");
printf(" | 1、增加学生信息 |\n");
printf(" | 2、删除学生信息 |\n");
printf(" | 3、修改学生信息 |\n");
printf(" | 4、查找学生的信息 |\n");
printf(" | 5、按指定方式排序 |\n");
printf(" | 6、浏览全部学生信息 |\n");
printf(" | 7、修改运动项目信息 |\n");
printf(" |________________________________________________|\n");
}
void welcome()
{
system("color b1");
printf(" ********************* 欢迎登录学生健康状况管理系统 *********************** \n");
}
void menu1(){
system("color b2");
printf(" ________________________________________________ \n");
printf(" | |\n");
printf(" | |\n");
printf(" | 系统中已存项目数目:%d |\n", sum);
printf(" | 0、返回上一层 |\n");
printf(" | 1、增加项目信息 |\n");
printf(" | 2、删除项目信息 |\n");
printf(" | 3、修改项目信息 |\n");
printf(" | 4、浏览全部项目信息 |\n");
printf(" |________________________________________________|\n");
}
#endif
paixu.h
#ifndef _paixu_h_
#define _paixu_h_
#include "data.h"
#include <iostream>
#include <algorithm>
#include <windows.h>
#include <conio.h>
using namespace std;
bool cmp1(Student x , Student y)
{
if(x.age != y.age)
return x.age < y.age;
if(x.height != y.height)
return x.height < y.height;
return x.weight < y.weight;
}
bool cmp2(Student x , Student y)
{
if(x.height != y.height)
return x.height < y.height;
if(x.weight != y.weight)
return x.weight < y.weight ;
return x.BMI() < y.BMI();
}
bool cmp3(Student x , Student y)
{
if(x.weight != y.weight)
return x.weight < y.weight;
if(x.height != y.height)
return x.height < y.height;
return x.BMI() < y.BMI();
}
bool cmp4(Student x , Student y)
{
if(x.BMI() != y.BMI())
return x.BMI() < y.BMI();
if(x.height != y.height)
return x.height < y.height;
return x.weight < y.weight;
}
void print(){
for(int i = 0;i < count1 ;i++)
{
printf("姓名:%s\n",stu[i].name);
printf("学号:%s\n",stu[i].number);
printf("性别:%s\n",stu[i].sex);
printf("年龄:%d\n",stu[i].age);
printf("体重:%d\n",stu[i].weight);
printf("身高:%d\n",stu[i].height);
printf("BMI指数:%f\n",stu[i].BMI());
for(int q = 0;q < sum;q++){
printf("%s成绩:%d\n",spo[q].sport_name,stu[i].sport_score[spo[q].sport_num]);
}
printf("\n");
}
}
bool cmpsportup(Student &a, Student &b){
return a.sport_score[number5] < b.sport_score[number5];
}
bool cmpsportdown(Student &a, Student &b){
return a.sport_score[number5] > b.sport_score[number5];
}
void mysortsport(){
printf("请输入要排序的项目编号:\n");
scanf("%d", &sportnum);
number5 = sportnum;
printf("请输入项目成绩排序任务方式:\n");
printf(" | 0、退出 |\n");
printf(" | 1、升序 |\n");
printf(" | 2、降序 |\n");
printf(" | |\n");
int n = getch();
switch(n)
{
case '0':
return ;
case '1':
sort(stu, stu + count1, cmpsportup);
print();
break;
case '2':
sort(stu, stu + count1, cmpsportdown);
print();
break;
default:
printf("请输入正确的选择\n");
break;
}
}
void mysort(){
printf("请输入排序任务方式:\n");
printf(" | 0、退出 |\n");
printf(" | 1、年龄 |\n");
printf(" | 2、身高 |\n");
printf(" | 3、体重 |\n");
printf(" | 4、BMI |\n");
printf(" | 5、指定编号成绩 |\n");
printf(" | |\n");
int n = getch();
switch(n)
{
case '0':
return ;
case '1':
sort(stu, stu + count1, cmp1);
print();
break;
case '2':
sort(stu, stu + count1, cmp2);
print();
break;
case '3':
sort(stu, stu + count1, cmp3);
print();
break;
case '4':
sort(stu, stu + count1, cmp4);
print();
break;
case '5':
mysortsport();
break;
default:
printf("请输入正确的选择\n");
break;
}
}
#endif
password.h
#ifndef _password_h_
#define _password_h_
#include "data.h"
#include <iostream>
#include <algorithm>
#include <windows.h>
#include <conio.h>
using namespace std;
void password() //运行前把密码文件内容读取到电脑内存
{
printf("请输入密码:");
scanf("%s", &code);
FILE *fp;
fp = fopen("password.txt" , "rb"); //以只读方式打开当前目录下的.txt
if(fp == NULL){
printf("不存在密码文件\n");
exit(0); //终止程序
}
char pass[20];
fscanf(fp , "%s", &pass);
if(strcmp(pass, code) == 0)
flag = 1;
fclose(fp);
}
#endif
sport.h
#ifndef _sport_h_
#define _sport_h_
#include "data.h"
#include <iostream>
#include <algorithm>
#include <windows.h>
#include <conio.h>
using namespace std;
int mark;
void stu_addscore(){
for(int i = 0;i < count1;i++){
printf("请增加学号为%s学生的%s成绩:",stu[i].number,spo[sum].sport_name);
scanf("%d", &mark);
stu[i].sport_score[sum] = mark;
}
}
void addsport()
{
char flag;
title_head("增加体育项目\n");
while(flag != 27){
printf("项目编号: \n");
scanf("%d", &sportnum);
int i;
for(i = 0;i < sum;i++)
if(spo[i].sport_num == sportnum){
printf("项目已存在\n");
flag2 = 1;
return;
}
spo[sum].sport_num = sportnum;
printf("项目名称:\n");
scanf("%s", &sportname);
strcpy(spo[sum].sport_name, sportname);
sum++;
printf("按任意键继续,结束输入请按Esc\n");
flag = getch();//暂停程序当i接收后继续下一条指令
}
}
void delsport(){
char flag;
title_head("删除运动项目\n");
while(flag != 27){
printf("请输入要删除的项目编号\n");
scanf("%d", &sportnum);
int i;
for(i = 0;i < sum;i++)
if(spo[i].sport_num == sportnum)
break;
if(i == sum){
printf("该项目不存在\n");
return;
}
for(;i < sum;i++){
spo[i].sport_num = spo[i+1].sport_num;
strcpy(spo[i].sport_name, spo[i+1].sport_name);
}
sum--;
printf("删除成功\n");
printf("按任意键继续,结束输入请按Esc\n");
flag = getch();//暂停程序当i接收后继续下一条指令
}
}
void glanceallsport(){
if(sum == 0){
printf("还没有项目信息,请增加项目信息\n");
return;
}
int i = 0;
while(i < sum){
printf("编号:%d",spo[i].sport_num);
printf(" 项目名称:%s\n",spo[i].sport_name);
i++;
}
}
void changesport(){
int i, n;
printf("请输入要修改的项目编号:\n");
scanf("%d", &sportnum);
for(i = 0;i < sum;i++)
if(spo[i].sport_num == sportnum){
printf("已查到该项目信息,修改项目编号请输入1:\n");
printf(" 修改项目名称请输入2:\n");
break;
}
if(i == count1){
printf("未查到该学生信息!");
return;
}
scanf("%d", &n);
if(n == 1){
printf("请输入修改后的编号:\n");
scanf("%d", &sportnum);
spo[i].sport_num = sportnum;
printf("修改成功!");
}
else if(n == 2){
printf("请输入修改后的名称:\n");
scanf("%s", &sportname);
strcpy(spo[i].sport_name , sportname);
printf("修改成功!\n");
}
else
printf("不存在此选项\n");
}
#endif
student.txt
001 老大 男 18 184 88 5 100 3 56
002 老二 女 19 166 55 5 86 3 95
003 老三 男 17 177 75 5 84 3 94
004 老四 男 18 188 99 5 85 3 84
005 老五 男 19 170 60 5 88 3 88
sport.txt
5 长跑
3 50米
password.txt
10086