从键盘输入某班级10个学生的学号、姓名、三门课程(语文、数学、英语)的成绩,分别实现如下功能:(1)统计每个学生的总分、平均分;(2)按总分从大到小排出名次;(3)打印出成绩
来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/21 21:32:50
从键盘输入某班级10个学生的学号、姓名、三门课程(语文、数学、英语)的成绩,分别实现如下功能:(1)统计每个学生的总分、平均分;(2)按总分从大到小排出名次;(3)打印出成绩
从键盘输入某班级10个学生的学号、姓名、三门课程(语文、数学、英语)的成绩,分别实现如下功能:
(1)统计每个学生的总分、平均分;
(2)按总分从大到小排出名次;
(3)打印出成绩在全班平均分以上的学生名单和数据信息.
(4)任意输入一个学号,能够查找出该学生在班级中的排名及其考试成绩.
要求:个功能要求分别用独立的函数来实现,在主函数中通过菜单选择来实现各个功能.且要求使用结构体数组来储存和处理学生信息
从键盘输入某班级10个学生的学号、姓名、三门课程(语文、数学、英语)的成绩,分别实现如下功能:(1)统计每个学生的总分、平均分;(2)按总分从大到小排出名次;(3)打印出成绩
用类对象与容器实现的,看看可以不!
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <iterator>
#include <iomanip>
using namespace std;
class student{
private:
double language;
double math;
double english;
double number;
double sumsocos;
double avesocos;
int place;
string name;
public:
student(double lenguagevalue=0,double mathvalue=0,double englishvalue=0,double numbervalue=0,double sumsocosvalue=0,double avesocos=0,int placevalue=0,string namevalue="lilei");
double getlanguage();
double getmath();
double getenglish();
double getnumber();
double getsumsocos();
double getplace();
double getavesocos();
string getname();
void setsumsocos(double digit);
void setave(double digit);
void setplace(int placevalue);
double sum();
double average();
};
student::student(double lenguagevalue,double mathvalue,double englishvalue,double numbervalue,double sumsocosvalue,double avesocosvalue,int placevalue,string namevalue):language(lenguagevalue),math(mathvalue),english(englishvalue),number(numbervalue),sumsocos(sumsocosvalue),avesocos(avesocosvalue),place(placevalue){
name=namevalue;
};
void searchstudent(vector< student >::iterator beg,vector< student >::iterator end,double number)
{
bool isfind=false;
for(size_t i=0;i<=number;)
{
if((*beg).getnumber()!=number)
{
beg++;
if(beg==end)
{
break;
}
}
else
{
cout<<"您查询的学生信息为 :"<<endl;
cout<<"姓名:"<<(*beg).getname()<<" 学号:"<<(*beg).getnumber()<<" 语文:"<<(*beg).getlanguage()<<" 数学:"<<(*beg).getmath()<<" 英语:"<<(*beg).getenglish()<<endl;
isfind=true;
break;
}
}
if(isfind==false)
{
cout<<"对不起,没有找到你需要的学生信息!"<<endl;
return;
}
};
double sumavesoco(vector<student>::iterator beg,vector<student>::iterator end){
vector<student>::iterator begin=beg;
vector<student>::iterator ended=end;
while(begin!=ended)
{
int sum=0;
int ave=0;
sum+=(*begin).getenglish();
sum+=(*begin).getlanguage();
sum+=(*begin).getmath();
ave=sum/3;
(*begin).setsumsocos(sum);
(*begin).setave(ave);
begin++;
}
vector<student>::iterator begin1=beg;
vector<student>::iterator ended1=end;
int sumave=0;
int sumsoco=0;
int count=0;
while(begin1!=ended1)
{
sumsoco+=(*begin1).getsumsocos();
count++;
begin1++;
}
sumave=sumsoco/count;
cout<<"学生总分与平均分为:"<<endl;
while(beg!=end)
{
cout<<"姓名:"<<setw(5)<<(*beg).getname()<<"总分:"<<setw(5)<<(*beg).getsumsocos()<<"平均分:"<<setw(5)<<(*beg).getavesocos()<<endl;
beg++;
}
return sumave;
};
void place(vector<student>::iterator beg,vector<student>::iterator end){
vector<student> a(beg,end);
vector<student>::iterator begin=a.begin();
for(int i=0;i<a.size();i++)
{
for(int j=i+1;j<a.size();j++)
{
if(a[i].getsumsocos()<a[j].getsumsocos())
{
student astudent;
astudent=a[i];
a[i]=a[j];
a[j]=astudent;
}
}
}
cout<<"完成排名,按总分大小排名后的次序为:"<<endl;
vector<student>::iterator begin1=a.begin();
while(begin1!=a.end())
{
cout<<setw(5)<<"姓名:"<<setw(5)<<(*begin1).getname()<<setw(5)<<"学号:"<<setw(5)<<(*begin1).getnumber()<<setw(5)<<"语文:"<<setw(5)<<(*begin1).getlanguage()<<"数学:"<<setw(5)<<(*begin1).getmath()<<"英语:"<<setw(5)<<(*begin1).getenglish()<<endl;
begin1++;
}
};
void searchupstudent(vector<student>::iterator beg,vector<student>::iterator end,double avesocos){
cout<<"总成绩在全班平均成绩以上的学生信息为:"<<endl;
while(beg!=end)
{
if((*beg).getsumsocos()>=avesocos)
{
cout<<"姓名:"<<setw(5)<<(*beg).getname()<<"学号:"<<setw(5)<<(*beg).getnumber()<<"总分:"<<setw(5)<<(*beg).getsumsocos()<<"平均分:"<<setw(5)<<(*beg).getavesocos()<<"语文:"<<setw(5)<<(*beg).getlanguage()<<"数学:"<<setw(5)<<(*beg).getmath()<<"英语:"<<setw(5)<<(*beg).getenglish()<<endl;
}
beg++;
}
};
struct
int main()
{
vector< student > stuarray;
loop:cout<<""<<endl;
int chose=0;
cout<<"请输入您的选择!"<<endl;
cin.clear();
cin.sync();
cin>>chose;
if(chose<=0||chose>5)
{
cout<<"输入错误,请重新输入!"<<endl;
goto loop;
}
char ch=0;
int count=0;
double sumave;
switch(chose)
{
case 1:
{
do{
cout<<"请输入学生信息!"<<endl;
double lan,math,english,number,socos,avesocos,place;
string name,sex;
cout<<"语文:";
cin>>lan;
cout<<endl<<"数学:";
cin>>math;
cout<<endl<<"英语:";
cin>>english;
cout<<endl<<"学号:";
cin>>number;
cin.sync();
cout<<endl<<"姓名:";
getline(cin,name);
cout<<endl;
student* astudent=new student(lan,math,english,number,socos=0,avesocos=0,place=0,name);
stuarray.push_back(*astudent);
cout<<"是否继续输入?"<<endl;
cin.clear();
cin.sync();
cin>>ch;
}while(tolower(ch)=='y');
cout<<endl;
goto loop;
}
case 2:
{
vector<student>::iterator beg=stuarray.begin();
vector<student>::iterator end=stuarray.end();
sumave=sumavesoco(beg,end);
cout<<"全班平均分为:"<<sumave<<endl;
goto loop;
}
case 3:
{ cout<<"请输入需要查询的学生学号!"<<endl;
int number2=0;
cin>>number2;
searchstudent(stuarray.begin(),stuarray.end(),number2);
goto loop;
}
case 4:
{
vector<student>::iterator beg=stuarray.begin();
vector<student>::iterator end=stuarray.end();
place(beg,end);
goto loop;
}
case 5:
{
vector<student>::iterator beg=stuarray.begin();
vector<student>::iterator end=stuarray.end();
searchupstudent(beg,end,sumave);
goto loop;
}
}
return 0;
}