学生信息包括:姓名、学号、性别和英语、数学、程序设计、体育成绩。
从键盘输入数据,建立数据文件student.dat。
实现对学生或学号查询,显示信息。
对所有学生,按照班级计算平均成绩。
分别按照英语、数学、程序设计和体育成绩排序生成结果文件。
话不多说,上测试结果图
代码如下:
#include <iostream>
#include <fstream>
#include <Windows.h>
using namespace std;
#define max 30
class student //定义学生数据类
{
private: //数据保护
string name;
string id;
string gender;
float English = 0;
float Math = 0;
float Program = 0;
float PE = 0;
public: //写入数据
void setstuinfo(string name, string id, string gender, float English, float Math, float Program, float PE) {
this->name = name;
this->id = id;
this->gender = gender;
this->English = English;
this->Math = Math;
this->Program = Program;
this->PE = PE;
}
void setName(string name) {
this->name = name;
}
void setId(string id) {
this->id = id;
}
void setGender(string gender) {
this->gender = gender;
}
void setEnglish(float English) {
this->English = English;
}
void setMath(float Math) {
this->Math = Math;
}
void setProgram(float Program) {
this->Program = Program;
}
void setPE(float PE) {
this->PE = PE;
}
string showname() {
return this->name;
}
string showid() {
return this->id;
}
string showGender() {
return this->gender;
}
float showEnglish() {
return this->English;
}
float showMath() {
return this->Math;
}
float showProgram() {
return this->Program;
}
float showPE() {
return this->PE;
}
};
class addressbooks { //定义接收数据量:30个
public:
class student studentarray[max];
int showsize() {
return this->size;
}
void sizeadd() {
this->size++;
}
void inisize() {
this->size = 0;
}
private:
int size = 0; //数据保护
};
void addstudent(addressbooks* abs){ //录入学生成绩信息
if (abs->showsize() == max)
{
cout << "存储容量已满。" << endl;
}
else
{
string name;
cout << "请输入学生姓名:" << endl;
cin >> name;
abs->studentarray[abs->showsize()].setName(name);
string id;
cout << "请输入学生学号:" << endl;
cin >> id;
abs->studentarray[abs->showsize()].setId(id);
string Gender;
cout << "请输入学生的性别:" << endl;
cin >> Gender;
abs->studentarray[abs->showsize()].setGender(Gender);
float English;
cout << "请输入学生的英语成绩:" << endl;
cin >> English;
abs->studentarray[abs->showsize()].setEnglish(English);
float Math;
cout << "请输入学生的数学成绩:" << endl;
cin >> Math;
abs->studentarray[abs->showsize()].setMath(Math);
float Program;
cout << "请输入学生的程序设计成绩" << endl;
cin >> Program;
abs->studentarray[abs->showsize()].setProgram(Program);
float PE;
cout << "请输入学生的体育成绩" << endl;
cin >> PE;
abs->studentarray[abs->showsize()].setPE(PE);
cout << endl;
ofstream fout; //创建对象管理输出流
fout.open("student.dat", ios_base::out | ios_base::app);
fout << abs->studentarray[abs->showsize()].showname() << " " << abs->studentarray[abs->showsize()].showGender() << " " << abs->studentarray[abs->showsize()].showid() << " " << abs->studentarray[abs->showsize()].showEnglish() << " " << abs->studentarray[abs->showsize()].showMath() << " " << abs->studentarray[abs->showsize()].showProgram() << " " << abs->studentarray[abs->showsize()].showPE()<<" ";
cout << "单次录入完毕,一秒后清屏。" << endl; //至此,单个学生信息输入完毕
fout.close();
Sleep(1000);
system("cls");
}
}
void clear() { //用打开文件的默认方式清除数据
ofstream cls1;
ofstream cls2;
ofstream cls3;
ofstream cls4;
ofstream cls5;
ofstream cls6;
cls1.open("student.dat");
cls2.open("average.dat");
cls3.open("englishsort.dat");
cls4.open("mathsort.dat");
cls5.open("programsort.dat");
cls6.open("pesort.dat");
cout << "数据已清除。。。" << endl;
cout << "一秒后清屏。。。" << endl;
cout << endl;
Sleep(1000);
system("cls");
}
int ifexit(string tmps) {
if (tmps == "end") {
return 1;
}
return 0;
}
void getdata(addressbooks* abs2) {
int exit = 0;
string tmps;
float tmpf;
abs2->inisize();
int i = 0;
ifstream fin("student.dat", ios_base::in);
while (i<max) { //判断是否接收到数据末尾
fin >> tmps;
exit = ifexit(tmps);
if (exit == 1) {
break; //避免收集空数据
}
abs2->studentarray[i].setName(tmps);
fin >> tmps;
abs2->studentarray[i].setGender(tmps);
fin >> tmps;
abs2->studentarray[i].setId(tmps);
fin >> tmpf;
abs2->studentarray[i].setEnglish(tmpf);
fin >> tmpf;
abs2->studentarray[i].setMath(tmpf);
fin >> tmpf;
abs2->studentarray[i].setProgram(tmpf);
fin >> tmpf;
abs2->studentarray[i].setPE(tmpf);
abs2->sizeadd();
i++;
}
}
void namesearch(addressbooks* abs2){
getdata(abs2);
string name;
int i = 0;
cout << "请输入要查找学生的姓名:" << endl;
cin >> name;
for (i = 0; i < abs2->showsize(); i++) //对学生姓名进行遍历查找
{
if (name == abs2->studentarray[i].showname())
{
cout << "学生: " << abs2->studentarray[i].showname() << " 性别:" << abs2->studentarray[i].showGender() << " 学号:" << abs2->studentarray[i].showid() << " 的英语成绩为:" << abs2->studentarray[i].showEnglish() << " 数学成绩为:" << abs2->studentarray[i].showMath() << " 程序设计成绩为:" << abs2->studentarray[i].showProgram() << " 体育成绩为:" << abs2->studentarray[i].showPE() << endl;
break;
}
}
if (i >= abs2->showsize()) //查无此人
{
cout << "查无此人" << endl;
}
}
void idsearch(addressbooks* abs2) {
getdata(abs2);
string id;
int i = 0;
cout << "请输入要查找学生的学号:" << endl;
cin >> id;
for (i = 0; i < abs2->showsize(); i++) //对学生姓名进行遍历查找
{
if (id == abs2->studentarray[i].showid())
{
cout << "学生: " << abs2->studentarray[i].showname() << " 性别:" << abs2->studentarray[i].showGender() << " 学号:" << abs2->studentarray[i].showid() << " 的英语成绩为:" << abs2->studentarray[i].showEnglish() << " 数学成绩为:" << abs2->studentarray[i].showMath() << " 程序设计成绩为:" << abs2->studentarray[i].showProgram() << " 体育成绩为:" << abs2->studentarray[i].showPE() << endl;
break;
}
}
if (i >= abs2->showsize()) //查无此人
{
cout << "查无此人" << endl;
}
}
void average(addressbooks* abs2) {
getdata(abs2);
float averagesc[max];
for (int i = 0; i < abs2->showsize(); i++) {
averagesc[i] = (abs2->studentarray[i].showEnglish()+abs2->studentarray[i].showMath()+abs2->studentarray[i].showProgram()+abs2->studentarray[i].showPE()) / 4;
}
ofstream fout;
fout.open("average.dat", ios_base::out | ios_base::app);
int t = 0;
for (t = 0; t < abs2->showsize(); t++) {
fout << "学生:" << abs2->studentarray[t].showname() << " 学号:" << abs2->studentarray[t].showid() << " 性别:" << abs2->studentarray[t].showGender() << " 四门课程平均成绩:" << averagesc[t] << endl;
}
cout << "结果已输出至文件:average.dat" << endl;
}
void stusortE(addressbooks* abs2) { //采用冒泡排序算法,记录下数据条的变化,输出时改变顺序即可
getdata(abs2);
int sortn[max];
float score[max];
int tmpn[max+1];
int j = 0;
for (j = 0; j < abs2->showsize(); j++) {
tmpn[j] = j;
}
tmpn[j] = '\0';
for (int i = 0; i < abs2->showsize(); i++) {
score[i] = abs2->studentarray[i].showEnglish();
}
for (int i = 0; i < abs2->showsize(); i++) {
for (int j = 0; j < abs2->showsize() - i - 1; j++) {
if (score[j] < score[j+1]) {
int temp;
int tempn;
temp = score[j + 1];
tempn = tmpn[j];
tmpn[j] = tmpn[j+1];
score[j + 1] = score[j];
tmpn[j + 1] = tempn;
score[j] = temp;
}
}
}
ofstream fout;
fout.open("englishsort.dat", ios_base::out | ios_base::app);
cout << "按照英语成绩排序后的结果已输出至englishsort.dat" << endl;
fout << "按照英语成绩排序后的结果" << endl;
for (int i = 0;i<j; i++) {
fout << "学生: " << abs2->studentarray[tmpn[i]].showname() << " 性别:" << abs2->studentarray[tmpn[i]].showGender() << " 学号:" << abs2->studentarray[tmpn[i]].showid() << " 的英语成绩为:" << abs2->studentarray[tmpn[i]].showEnglish() << " 数学成绩为:" << abs2->studentarray[tmpn[i]].showMath() << " 程序设计成绩为:" << abs2->studentarray[tmpn[i]].showProgram() << " 体育成绩为:" << abs2->studentarray[tmpn[i]].showPE() << endl;
}
}
void stusortM(addressbooks* abs2) { //采用冒泡排序算法,记录下数据条的变化,输出时改变顺序即可
getdata(abs2);
int sortn[max];
float score[max];
int tmpn[max + 1];
int j = 0;
for (j = 0; j < abs2->showsize(); j++) {
tmpn[j] = j;
}
tmpn[j] = '\0';
for (int i = 0; i < abs2->showsize(); i++) {
score[i] = abs2->studentarray[i].showMath();
}
for (int i = 0; i < abs2->showsize(); i++) {
for (int j = 0; j < abs2->showsize() - i - 1; j++) {
if (score[j] < score[j + 1]) {
int temp;
int tempn;
temp = score[j + 1];
tempn = tmpn[j];
tmpn[j] = tmpn[j+1];
score[j + 1] = score[j];
tmpn[j + 1] = tempn;
score[j] = temp;
}
}
}
ofstream fout;
fout.open("mathsort.dat", ios_base::out | ios_base::app);
cout << "按照数学成绩排序后的结果已输出至mathsort.dat" << endl;
fout << "按照数学成绩排序后的结果" << endl;
for (int i = 0; i < j; i++) {
cout << tmpn[i] << " ";
}
for (int i = 0; i<j; i++) {
fout << "学生: " << abs2->studentarray[tmpn[i]].showname() << " 性别:" << abs2->studentarray[tmpn[i]].showGender() << " 学号:" << abs2->studentarray[tmpn[i]].showid() << " 的英语成绩为:" << abs2->studentarray[tmpn[i]].showEnglish() << " 数学成绩为:" << abs2->studentarray[tmpn[i]].showMath() << " 程序设计成绩为:" << abs2->studentarray[tmpn[i]].showProgram() << " 体育成绩为:" << abs2->studentarray[tmpn[i]].showPE() << endl;
}
}
void stusortPr(addressbooks* abs2) { //采用冒泡排序算法,记录下数据条的变化,输出时改变顺序即可
getdata(abs2);
int sortn[max];
float score[max];
int tmpn[max + 1];
int j = 0;
for (j = 0; j < abs2->showsize(); j++) {
tmpn[j] = j;
}
tmpn[j] = '\0';
for (int i = 0; i < abs2->showsize(); i++) {
score[i] = abs2->studentarray[i].showProgram();
}
for (int i = 0; i < abs2->showsize(); i++) {
for (int j = 0; j < abs2->showsize() - i - 1; j++) {
if (score[j] < score[j + 1]) {
int temp;
int tempn;
temp = score[j + 1];
tempn = tmpn[j];
tmpn[j] = tmpn[j+1];
score[j + 1] = score[j];
tmpn[j + 1] = tempn;
score[j] = temp;
}
}
}
ofstream fout;
fout.open("programsort.dat", ios_base::out | ios_base::app);
cout << "按照程序设计成绩排序后的结果已输出至programsort.dat" << endl;
fout << "按照程序设计成绩排序后的结果" << endl;
for (int i = 0; i<j; i++) {
fout << "学生: " << abs2->studentarray[tmpn[i]].showname() << " 性别:" << abs2->studentarray[tmpn[i]].showGender() << " 学号:" << abs2->studentarray[tmpn[i]].showid() << " 的英语成绩为:" << abs2->studentarray[tmpn[i]].showEnglish() << " 数学成绩为:" << abs2->studentarray[tmpn[i]].showMath() << " 程序设计成绩为:" << abs2->studentarray[tmpn[i]].showProgram() << " 体育成绩为:" << abs2->studentarray[tmpn[i]].showPE() << endl;
}
}
void stusortPE(addressbooks* abs2) { //采用冒泡排序算法,记录下数据条的变化,输出时改变顺序即可
getdata(abs2);
int sortn[max];
float score[max];
int tmpn[max + 1];
int j = 0;
for (j = 0; j < abs2->showsize(); j++) {
tmpn[j] = j;
}
tmpn[j] = '\0';
for (int i = 0; i < abs2->showsize(); i++) {
score[i] = abs2->studentarray[i].showPE();
}
for (int i = 0; i < abs2->showsize(); i++) {
for (int j = 0; j < abs2->showsize() - i - 1; j++) {
if (score[j] < score[j + 1]) {
int temp;
int tempn;
temp = score[j + 1];
tempn = tmpn[j];
tmpn[j] = tmpn[j+1];
score[j + 1] = score[j];
tmpn[j + 1] = tempn;
score[j] = temp;
}
}
}
ofstream fout;
fout.open("pesort.dat", ios_base::out | ios_base::app);
cout << "按照体育成绩排序后的结果已输出至pesort.dat" << endl;
fout << "按照体育成绩排序后的结果" << endl;
for (int i = 0; i<j; i++) {
fout << "学生: " << abs2->studentarray[tmpn[i]].showname() << " 性别:" << abs2->studentarray[tmpn[i]].showGender() << " 学号:" << abs2->studentarray[tmpn[i]].showid() << " 的英语成绩为:" << abs2->studentarray[tmpn[i]].showEnglish() << " 数学成绩为:" << abs2->studentarray[tmpn[i]].showMath() << " 程序设计成绩为:" << abs2->studentarray[tmpn[i]].showProgram() << " 体育成绩为:" << abs2->studentarray[tmpn[i]].showPE() << endl;
}
}
void exit() {
ifstream fin;
fin.open("student.dat", ios_base::in);
string temps;
fin >> temps;
if (temps != "\0") {
ofstream fout; //创建对象管理输出流
fout.open("student.dat", ios_base::out | ios_base::app);
fout << " end"; //记录结束信号
}
}
void showmenu1() //显示一级菜单
{
cout << endl;
cout << " ===============学生学籍管理系统===============" << endl;
cout << " ----------------------------------------------" << endl;
cout << "| 0、退出系统 |" << endl;
cout << "| 1、写入数据 |" << endl;
cout << "| 2、查找学生 |" << endl;
cout << "| 3、计算平均 |" << endl;
cout << "| 4、进行排序 |" << endl;
cout << "| 5、清除数据 |" << endl;
cout << " ----------------------------------------------" << endl;
cout << " ===============学生学籍管理系统===============" << endl;
cout << "注意:本系统目前支持查找前30个学生的数据。如要扩增,修改max的定义值即可,但可能会超出堆栈保留大小" << endl;
cout << "提示:如在执行写入操作之后需进行除写入以及清除以外的操作,请先退出系统重新进入" << endl;
}
void showmenu2(addressbooks* abs2) //显示查找学生数据的二级菜单
{
int select;
cout << endl;
cout << " ===============学生学籍管理系统===============" << endl;
cout << " ----------------------------------------------" << endl;
cout << "| 0、返回上页 |" << endl;
cout << "| 1、姓名查找 |" << endl;
cout << "| 2、学号查找 |" << endl;
cout << " ----------------------------------------------" << endl;
cout << " ===================查找模块===================" << endl;
cout << " ===============学生学籍管理系统===============" << endl;
cout << endl;
cout << "请选择模式:" << endl;
cin >> select;
switch (select) //模式选择
{
case 0:system("cls"); break;
case 1:namesearch(abs2); break;
case 2:idsearch(abs2); break;
case -1:; break;
default:break;
}
}
void showmenu3(addressbooks* abs2) //显示查找学生数据的二级菜单
{
abs2->inisize();
int sortn[max];
int select;
cout << endl;
cout << " ===============学生学籍管理系统===============" << endl;
cout << " ----------------------------------------------" << endl;
cout << "| 0、返回上页 |" << endl;
cout << "| 1、英语排序 |" << endl;
cout << "| 2、数学排序 |" << endl;
cout << "| 3、程序设计排序 |" << endl;
cout << "| 4、体育排序 |" << endl;
cout << " ----------------------------------------------" << endl;
cout << " ===================查找模块===================" << endl;
cout << " ===============学生学籍管理系统===============" << endl;
cout << endl;
cout << "请选择模式:" << endl;
cin >> select;
switch (select) //模式选择
{
case 0:system("cls"); break;
case 1:stusortE(abs2); break;
case 2:stusortM(abs2); break;
case 3:stusortPr(abs2); break;
case 4:stusortPE(abs2); break;
default:break;
}
}
int main() {
addressbooks abs1; //用于写入
addressbooks abs2; //用于获取
abs1.inisize(); //初始化本次数据总量
abs2.inisize();
int select;
while (true) //显示菜单界面
{
showmenu1();
cout << "请选择模式:" << endl;
cin >> select;
switch (select) //模式选择
{
case 0:exit(); cout << "感谢使用。" << endl; return 0; break; //退出系统
case 1:addstudent(&abs1); break; //添加数据
case 2:showmenu2(&abs2); break; //进入查询二级菜单
case 3:average(&abs2); break; //计算平均分
case 4:showmenu3(&abs2); break; //进入排序二级菜单
case 5:clear(); break; //清除文件中的所有数据
default:system("cls"); cout << "输入不合规范,请重新输入。" << endl; break;
}
}
}