一.设计要求:
学生成绩管理系统,每个学生是一个记录,包括学号,姓名,性别,3门课程成绩。系统 要求实现以下功能:
1、信息录入:录入学生成绩信息(包括学生学号、姓名、各门课程的成绩等);
2、信息查询:输入学号,查询学生各门课程的成绩,并显示。
3、排序:按各门课程的成绩平均分进行排序,并显示。
4、信息删除与修改——输入学号,删除该学生的成绩信息。
方法步骤:
1、初步完成总体设计,搭好框架,确定人机对话的界面,确定函数个数。
2、建立一个文件,将每位学生的信息写入文件中并能显示于屏幕上。
3、完成上述信息查询(学生学号、姓名等)、排序、信息删除与修改功能。
实现的应用系统功能要求:
1、用C++语言实现程序设计;
2、利用结构体数组实现学生信息的数据结构设计;
3、系统的各个功能模块要求用函数的形式实现;
4、界面友好(良好的人机交互),程序应该有详细的注释。
二.代码实现
#include<iostream>
#include<string>
using namespace std;uct Student
{
int studentNo;
char name[20];
char sex[5];
int math;
int Chinese;
int English;
int sum;
}stu[10];
int chioce();
int InformationInput();
void Display();
int InformationSearch();
int GradeSort();
int InformationDelete();
int InformationRevise();
void main()
{
while (1)
{
chioce();
getchar();
}
}
int chioce()
{
int number;
cout << "----------------------------- " << endl
<< "请选择您要实现的功能的序号:" << endl
<< "1.信息录入" << endl
<< "2.显示学生信息" << endl
<< "3.信息查询" << endl
<< "4.按平均分进行排序" << endl
<< "5.信息删除" << endl
<< "6.信息修改" << endl
<< "7.退出" << endl
<< "-----------------------------" << endl;
cin >> number;
switch (number)
{
case 1:InformationInput(); break;
case 2:Display(); break;
case 3: InformationSearch(); break;
case 4: GradeSort(); break;
case 5: InformationDelete(); break;
case 6: InformationRevise(); break;
case 7: exit(0);
default:cout << "输入错误,请重新输入:" << endl;
}
return 0;
}
int InformationInput()//信息录入函数
{
struct Student stu1;
FILE *fp;
fp = fopen("grade.txt", "a");
if ((fp = fopen("grade.txt", "a")) == NULL)
{
cout << "打开文件失败!" << endl;
exit(0);
}
cout << "学号 ," << "姓名 ," << "性别 ,"
<< "语文 ," << "数学 ," <<"英语"<< endl;
cin >> stu1.studentNo;
cin >> stu1.name;
cin >> stu1.sex;
cin >> stu1.Chinese;
cin >> stu1.math;
cin >> stu1.English;
stu1.sum = stu1.Chinese + stu1.English + stu1.math;
fwrite(&stu1, sizeof(struct Student), 1, fp);
fclose(fp);
getchar();
return 0;
}
void Display()//显示函数
{
FILE *fp;
if ((fp = fopen("grade.txt", "r")) == NULL)
{
cout << "打开文件失败!" << endl;
exit(0);
}
cout << "学号, 姓名, 性别,语文,数学,英语,总分" << endl;
while (!feof(fp))
{
int i = 0;
fread(&stu[i], sizeof(struct Student), 1, fp);
if (!feof(fp))
{
cout << stu[i].studentNo << ","<< stu[i].name
<< ", "<< stu[i].Chinese << ", "<< stu[i].math
<< ", " << stu[i].English << ", "<<stu[i].sum
<< endl;
i++;
}
}
fclose(fp);
getchar();
}
int InformationSearch()//信息查询函数
{
int n=5,i;
int number;
FILE *fp;
if ((fp = fopen("grade.txt", "r")) == NULL)
{
cout << "打开文件失败!" << endl;
exit(0);
}
for (i = 0; i<n; i++)
fread(&stu[i], sizeof(struct Student), 1, fp);
cout << "输入要查询学生的学号:";
cin >> number;
cout << "学号, 姓名, 性别,语文,数学,英语,总分" << endl;
for (i = 0; i < n; i++)
{
if (number == stu[i].studentNo)
cout << stu[i].studentNo << "," << stu[i].name
<< ", " << stu[i].sex << ",<<stu[i].Chinese
<< ", " << stu[i].math <<","stu[i].English
<< ", " << stu[i].sum << endl;
}
fclose(fp);
getchar();
}
int GradeSort()//根据平均分排序函数
{
int n=5;
struct Student stud1;
FILE *fp;
if ((fp = fopen("grade.txt", "r")) == NULL)
{
cout << "打开文件失败!" << endl;
exit(0);
}
int num = 0;
do
{
fread(&stu[num], sizeof(struct Student), 1, fp);
num++;
} while (!feof(fp));
num=num-1;
for (int i = 0; i < num - 1;i++)
{
for (int j = 0; j < num - 1 - i;j++)
{
if (stu[j].sum<stu[j+1].sum)
{
struct Student temp;
temp = stu[j];
stu[j] = stu[j+1];
stu[j+1] = temp;
}
}
}
cout << "学号 ," << "姓名 ," << "性别 ," << "语文 ,"
<< "数学 ," << "英语 ," <<"总分 "<< endl;
for (int i = 0; i<num; i++)
cout << stu[i].studentNo << "," << stu[i].name << ", "
<< stu[i].sex << ", " << stu[i].Chinese << ", "
<< stu[i].math << ", " << stu[i].English << ", "
<< stu[i].sum << endl;
ifclose(fp);
getchar();
}
int InformationDelete()//信息删除函数
{
int n = 5,t;
int number;
FILE *fp;
if ((fp = fopen("grade.txt", "r")) == NULL)
{
cout << "打开文件失败!" << endl;
exit(0);
}
int num=0;
do
{
fread(&stu[num], sizeof(struct Student), 1, fp);
num++;
} while (!feof(fp));
num = num - 1;
if ((fp = fopen("grade.txt", "w")) == NULL)
{
cout << "打开文件失败! " << endl;
exit(0);
}
cout << "请输入删除学生的学号" << endl;
cin >> number;
for (int i = 0; i<num; i++)
if (number == stu[i].studentNo)
t = i;
for (int i = 0; i<t; i++)
{
fwrite(&stu[i], sizeof(struct Student), 1, fp);
}
for (int i = t+1 ; i<num; i++)
{
fwrite(&stu[i], sizeof(struct Student), 1, fp);
}
cout << "该学生信息已经删除成功!" << endl;
fclose(fp);
getchar();
return 0;
}
int InformationRevise()//信息修改函数
{
int n = 5;
int number;
int t;
FILE *fp, *fp1;
if ((fp = fopen("grade.txt", "r")) == NULL)
{
cout << "打开文件失败!" << endl;
exit(0);
}
int num = 0;
do
{
fread(&stu[num], sizeof(struct Student), 1, fp);
num++;
} while (!feof(fp));
num = num - 1;
if ((fp1 = fopen("grade.txt", "w")) == NULL)
{
cout << "打开文件失败!" << endl;
exit(0);
}
cout << "请输入要修改学生的学号:";
cin >> number;
for (int i = 0; i<num; i++)
if (number == stu[i].studentNo)
{
t = i;
cout << "输入要修改学生的信息:" << endl;
cout << "学号:" ;
cin >> stu[t].studentNo;
cout << "姓名:" ;
cin >> stu[t].name;
cout << "性别:";
cin >> stu[t].sex;
cout << "语文:" ;
cin >> stu[t].Chinese;
cout << "数学:" ;
cin >> stu[t].math;
cout << "英语:" ;
cin >> stu[t].English;
stu[t].sum = stu[t].Chinese + stu[t].English + stu[t].math;
for (int i = 0; i<num; i++)
fwrite(&stu[i], sizeof(struct Student), 1, fp1);
cout << "该学生信息已修改成功!";
}
fclose(fp);
fclose(fp1);
getchar();
return 0;
}