#include <iostream>
using namespace std;
#include <string>
#include <ctime>
#include <cstdlib>
#include <windows.h>
/*
随机产生任意10个学生的3门课成绩,姓名和成绩输出到屏幕
求出10个同学的平均成绩和各门课的平均成绩,姓名和成绩输出到屏幕,
并重新在屏幕上输出按总分高低排序的姓名表和成绩表。
*/
/*学生的成绩类*/
class Score
{
public:
Score()
{
srand(time(0));
math = rand()%70+31;
frlg = rand()%70+31;
major = rand()%70+31;
Sleep(600);
}
Score(double tmp)
{
math = 0;
frlg = 0;
major = 0;
}
Score(const Score& other)
{
math = other.math;
frlg = other.frlg;
major = other.major;
}
Score& operator=(const Score& other)
{
if(this == &other)
return *this;
this->math = other.math;
this->frlg = other.frlg;
this->major = other.major;
return *this;
}
~Score(){}
double getMath()
{
return math;
}
void setMath(double t)
{
math = t;
}
double getFrlg()
{
return frlg;
}
void setFrlg(double t)
{
frlg = t;
}
double getMajor()
{
return major;
}
void setMajor(double t)
{
major = t;
}
double getAvgScore() const
{
return (math+frlg+major)/3;
}
double getTotalScore() const
{
return (math+frlg+major);
}
private:
double math;//数学
double frlg;//外语
double major;//专业
};
/*学生类*/
class Student
{
public:
Student():score()
{
srand(time(0));
name = rand()%26 + 'A';//姓名
Sleep(300);
}
Student(string tmpname):score(0)
{
name = "~";
}
Student(const Student& other)
{
name = other.name;
score = other.score;
}
Student& operator =(const Student& other)
{
if(this == &other)
return *this;
this->name = other.name;
this->score = other.score;
return *this;
}
~Student()
{
}
void print()//输出姓名、成绩的信息
{
cout << name << "\t"
<< score.getMath() << "\t"
<< score.getFrlg() << "\t"
<< score.getMajor() << endl;
}
void print2()//输出加上平均成绩,总分
{
cout << name << "\t"
<< score.getMath() << "\t"
<< score.getFrlg() << "\t"
<< score.getMajor() << "\t"
<< score.getAvgScore() << "\t\t"
<< score.getTotalScore() << endl;
}
void print3()//输出姓名、总分(排名)
{
cout << name << "\t"
<< score.getTotalScore();
}
friend double calcAvgSubject(Student stu[],int len,int select);
friend void sortByTotalScore(Student stu[],int len);
private:
string name;//姓名
Score score;//成绩
};
//友元函数,计算各门课的平均成绩
double calcAvgSubject(Student stu[],int len,int select)
{
double sum = 0;
for(int i=0; i<len; ++i)
{
if(1 == select)
sum += stu[i].score.getMath();
else if(2 == select)
sum += stu[i].score.getFrlg();
else if(3 == select)
sum += stu[i].score.getMajor();
}
return (sum/len);
}
//友元函数,按总分从高到低排列
void sortByTotalScore(Student stu[],int len)
{
for(int i=0; i<len-1; ++i)
{
for(int k=0; k<len-1-i; ++k)
{
if(stu[k].score.getTotalScore() < stu[k+1].score.getTotalScore())
{
Student obj = stu[k];
stu[k] = stu[k+1];
stu[k+1] = obj;
}
}
}
}
//主函数
int main()
{
Student stu[10];//10个学生
cout << "随机生成" << endl;
cout << "name\t" << "math\t" << "langu\t" << "major" << endl;
for(int i=0; i<10; ++i)
{
stu[i].print();
}
cout << endl;
cout << "计算平均成绩和总分以及各门课的平均成绩" << endl;
cout << "name\t" << "math\t" << "langu\t" << "major\t" << "avg\t\t" << "tatol\t" <<endl;
for(int j=0; j<10; ++j)
{
stu[j].print2();
}
cout << "avg:\t" << calcAvgSubject(stu,10,1) << "\t"
<< calcAvgSubject(stu,10,2) << "\t"
<< calcAvgSubject(stu,10,3) << "\t" << endl;
cout << endl;
cout << "按总分由高到低排序" << endl;
cout << "name\t" << "tatol\t" << "rank" <<endl;
sortByTotalScore(stu,10);
for(int a=0; a<10; ++a)
{
stu[a].print3();
cout << "\t" << a+1 << endl;
}
return 0;
}
//写的比较简单,要求基本达到。