#include<iostream>
using namespace std;
class Student
{
private:
int id;
string name;
int age;
double cpp;
double eng;
double math;
double average;
public:
double sum;
Student(int, string, int, double, double, double);
Student(const Student& s);
void SetInfo();
void ShowInfo();
double Sum();
double Average();
};
Student::Student(int id1=0, string name = "", int age1 = 0, double cpp1 = 0, double eng1 = 0, double math1 = 0)
{
id = id1;
this -> name = name;
age = age1;
cpp = cpp1;
eng = eng1;
math = math1;
}
Student::Student(const Student& s)
{
id = s.id;
name = s.name;
age = s.age;
cpp = s.cpp;
eng = s.eng;
math = s.math;
}
void Student::SetInfo()
{
cout << "请输入学生id:" ;
cin >> id;
cout << "请输入学生姓名:" ;
cin >> name;
cout << "请输入学生年龄:" ;
cin >> age;
cout << "请输入学生cpp成绩:" ;
cin >> cpp;
cout << "请输入学生eng成绩:";
cin >> eng;
cout << "请输入学生math成绩:";
cin >> math;
}
void Student::ShowInfo()
{
string t = "\t";
cout << t << id << t << name << t << age << t << cpp << t << eng << t << math << t << Sum() << t << Average() << endl;
}
double Student::Sum()
{
sum=cpp + eng + math;
return sum;
}
double Student::Average()
{
average=Sum() / 3;
return average;
}
int main()
{
Student p;
int n = 0;
cout << "学生个数为:" ;
cin >> n;
Student classes[6];
for (int i = 0;i < n;i++)
{
classes[i].SetInfo();
classes[i].Sum();
classes[i].Average();
classes[i].ShowInfo();
}
for (int i = 0;i < n;i++)
{
for (int j = 0; j < n - 1; j++)
{
if (classes[j].Sum() < classes[j + 1].Sum())
{
p=(classes[j]);
classes[j]= (classes[j + 1]);
classes[j + 1]= (p);
}
}
}
cout << "成绩信息总表如下:" << endl;
string t = "\t";
cout << t << "id" << t << "name" << t << "age" << t << "cpp" << t << "eng" << t << "math" << t << "Sum" << t << "Average" << endl;
for (int i = 0;i < n;i++)
{
classes[i].ShowInfo();
}
return 0;
}
随便练练