学生成绩管理程序:用面向对象的类设计一个菜单驱动的学生成绩管理程序。学生的基本信息包括学生的学号、姓名、数学、语文、英语等信息。实现如下管理功能:(1)能输入并显示n个学生的基本信息。(2)计算学生的平均分和总分。(3)按总分降序进行排序。(4)任意输入一个学号,能显示该学生的基本信息。(5)可添加学生信息到指定位置,亦可在最后追加一批学生信息。(6)文件的导入和导出(从文件中读取若干条学生信息,或者将学生信息输出到文件中)。
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <string.h>
using namespace std;
int num=0;
class Student
{
public:
int id;
string name;
int math;
int Chinese;
int English;
int sum;
int aver;
};
class Function
{
public:
void ShowMenu();//显示菜单
void CreatList(Student str[]);//创建
void AddStu(Student str[]);//添加
void Gather(Student str[]);//查询
void Sort(Student str[]);//排序
bool Compare(Student p,Student q);
void Search(Student str[]);//查询
void display(Student str[]);//显示
void Fileinto(Student str[]);//Wenjian
void Fileout(Student str[]);//wenjian
};
int main()
{
Student str[1000];
Function use;
use.ShowMenu();
while(1)
{
int n;
cin >> n;
switch (n)
{
case 1: //创建
{
use.CreatList(str);
break;
}
case 2: //添加
{
use.AddStu(str);
break;
}
case 3: //汇总
{
use.Gather( str);
break;
}
case 4: //修改
{
use.Sort(str);
break;
}
case 5: //删除
use.Search(str);
break;
case 6: //显示
use.display(str);
break;
case 7: //文件导入
use.Fileinto(str);
break;
case 8: //文件导出
use.Fileout(str);
break;
case 9:
exit(0);
default: cout << "输入错误,请重新输入 : "; break;
}
}
return 0;
}
void Function:: ShowMenu()
{
cout << " 请选择下面的菜单 :" << endl;
cout << " 1 创建" << endl;//创建
cout << " 2 添加" << endl;
cout << " 3 汇总" << endl;
cout << " 4 排序" << endl;
cout << " 5 查询" << endl;
cout << " 6 显示" << endl;
cout << " 7 文件导入" << endl;
cout << " 8 文件导出" << endl;
cout << " 9 退出" << endl;
cout << " 请选择 :" ;//显示菜单
}
void Function:: CreatList(Student str[])//创建
{
int i,n;
cout << "输入创建的学生的个数" << endl;
cin >> n;
cout << "请输入信息" << endl;
for (i = 0; i < n; i++)
{
cin >> str[i].id >> str[i].name >> str[i].math >> str[i].Chinese >> str[i].English ;
str[i].sum=str[i].math+str[i].English+str[i].Chinese;
str[i].aver=str[i].sum/3;
num++;
}
cout << "学生信息: " << endl;
for (i = 0; i < num; i++)
{
cout << str[i].id << " " << str[i].name << " " << str[i].math << " " << str[i].Chinese << " " << str[i].English << endl;
}
}
void Function:: AddStu(Student str[])//添加
{
cout<<"最后追加or指定位置添加?(1/2)";
int m;
cin>>m;
if(m==1)
{
cout << "添加的通讯者的数目 : ";
int x;
cin >> x;
cout << "请输入" << endl;
for (int i = num; i < num + x; i++)
{
cin >> str[i].id >> str[i].name >> str[i].math >> str[i].Chinese >> str[i].English ;
str[i].sum=str[i].math+str[i].English+str[i].Chinese;
str[i].aver=str[i].sum/3;
}
cout << "以下是你输入的信息: " << endl;
for (int i = num; i < num + x; i++)
{
cout << str[i].id << " " << str[i].name << " " << str[i].math << " " << str[i].Chinese << " " << str[i].English<< endl;
}
num += x;
}
if(m==2)
{
cout << "添加的通讯者的数目 : ";
int n,place;
cin >> n;
cout<<"请输入指定位置";
cin>>place;
for(int i=place;i<place+n;i++)
{
int id;
string name;
int math;
int Chinese;
int English;
str[i+n].id=str[i].id;
str[i+n].name=str[i].name;
str[i+n].math=str[i].math;
str[i+n].English=str[i].English;
cout<<"请输入信息"<<endl;
cin >> num >> name >> math >> Chinese >> English ;
str[i].id=id;
str[i].name=name;
str[i].math=math;
str[i].English=English;
}
cout << "以下是你输入的信息: " << endl;
for (int i = place; i < place+n; i++)
{
cout << str[i].id << " " << str[i].name << " " << str[i].math << " " << str[i].Chinese << " " << str[i].English<< endl;
}
num+=n;
}
}
void Function:: Gather(Student str[])//汇总
{
for (int i = 0; i < num; i++)
{
str[i].sum=str[i].math+str[i].English+str[i].Chinese;
str[i].aver=str[i].sum/3;
cout << str[i].id << " " << str[i].name << " " << str[i].math << " " << str[i].Chinese << " " << str[i].English<<" "<<str[i].sum<<" "<<str[i].aver<< endl;
}
}
void Function:: Sort(Student str[])//排序
{
for(int i=0;i<num;i++)
{
sort(str,str+num,Compare);
}
for(int j=0;j<num;j++)
{
cout << str[j].id << " " << str[j].name << " " << str[j].math << " " << str[j].Chinese << " " << str[j].English<<" "<<str[j].sum<<" "<<str[j].aver<< endl;
}
}
bool Function:: Compare(Student p, Student q)
{
return p.sum>q.sum;
}
void Search(Student str[])//查询
{
int m(0), flag(0);
int ID;
cout << "请输入所查询的学号 :";
cin >> ID ;
for (int i = 0; i < num; i++)
{
if (str[i].id == ID )
{
cout << str[i].id << " " << str[i].name << " " << str[i].math << " " << str[i].Chinese << " " << str[i].English<<" "<<str[i].sum<<" "<<str[i].aver<< endl;
flag = 1;
break;
}
}
if (flag == 0)
cout << "未找到所查学号,请重新查询 " << endl;
}
void Function:: display(Student str[])//显示
{
for (int i = 0; i < num; i++)
{
cout << str[i].id << " " << str[i].name << " " << str[i].math << " " << str[i].Chinese << " " << str[i].English<<" "<<str[i].sum<<" "<<str[i].aver<< endl;
}
}
void Function:: Fileinto(Student str[])
{
ifstream daoru("学生成绩.txt", ios::in);
if (!daoru)
{
cout << "文件导入失败" << endl;
exit(0);
}
cout << "请输入导入学生个数 : ";
int n, i, j;
cin >> n;
for (j = 0; j < n; j++)
{
daoru >> str[i].id >> str[i].name >> str[i].math >> str[i].Chinese >> str[i].English ;
}
daoru.close();
cout << "导入结束" << endl;
system("pause");
system("cls");
}
void Function:: Fileout(Student str[])
{
ofstream daochu("f_1.txt", ios::out);
if (!daochu)
{
cout << "文件导出失败" << endl;
exit(0);
}
for (int i = 0; i < num; i++)
{
cout << str[i].id << " " << str[i].name << " " << str[i].math << " " << str[i].Chinese << " " << str[i].English<<" "<<str[i].sum<<" "<<str[i].aver<< endl;
}
daochu.close();
cout<<"成功导出"<<endl;
system("pause");
system("cls");
}