c++中定义一个学生类,成员变量包括学号和四门课的成绩,要求计算:(1)每个学生的

2024-11-17 06:59:57
推荐回答(4个)
回答1:

#include
#include
using namespace std;

class Student {
private:
enum { SIZE = 5 };
string SName;
string SNo;
double Score[SIZE];
public:
Student(){}
Student(string name, string no, double sc[]);
void Show() const;
double GetAverage() const { return Score[SIZE - 1]; }
double GetOptimumScore() const;
};

Student::Student(string name, string no, double sc[]) {
SName = name;
SNo = no;
Score[SIZE - 1] = 0.0;
for(int i = 0; i < SIZE - 1; ++i) {
Score[i] = sc[i];
Score[SIZE - 1] += sc[i];
}
Score[SIZE - 1] = Score[SIZE - 1]/(SIZE - 1);
}

void Student::Show() const {
cout << SName << "\t";
cout << SNo << "\t";
cout.precision();
cout.showpoint;
for(int i = 0; i < SIZE; ++i) cout << Score[i] << "\t";
cout << endl;
}

double Student::GetOptimumScore() const {
double max = 0.0;
for(int i = 0; i < SIZE - 1; ++i)
if(Score[i] > max) max = Score[i];
return max;
}

int main() {
Student s[5];
string name,no;
double score[5];
for(int i = 0; i < 5; ++i) {
cout << "学生" << i + 1 << "信息\n";
cout << "姓名 : ";
cin >> name;
cout << "学号 : " ;
cin >> no;
cout << "高等数学 : ";
cin >> score[0];
cout << "英语 : ";
cin >> score[1];
cout << "C编程 : ";
cin >> score[2];
cout << "电脑硬件 : ";
cin >> score[3];
cout << endl;
s[i] = Student(name,no,score);
}
cout << "姓名\t学号\t高数\t英语\t编程\t硬件\t\n";
for(int i = 0; i < 48; ++i) cout << "*";
cout << endl;
for(int i = 0; i < 5; ++i) s[i].Show();
cout << endl;
return 0;
}

回答2:

最高分
void cGPA:: Highest(cGPA *obj, int sem,int stud)
{
float temp;
temp=obj[0].gpa[0];

for (int j=0;j{
for (int i=0;i{
if (obj[j].gpa[i]>=temp)
{
obj[j].highest=obj[j].gpa[i];
}
}
}

}

回答3:

在main函数中定义多个Student对象或Student数组;
例如:Student students[3];
for(int i = 0; i < 3; i++)
{
students[i].init();//input the students infomation
//students[i].printf();
}
maxSum = temp;
cout << " the higthest score is "<< maxSum<在定义一个数据成员sum[.求总分
再输入数据的成员函数init()中加上
for(int i = 0;i< 10;i++)
{
sum= sscore[i]
}
再加上一个函数找最值的
int find ()
{
int temp
for (int i = 0; i < 10 ; i ++)
{
if (students[i].sum{ temp = students[i+1].sum;
}
}
return temp;
}
具体函数加在哪,你应该知道了吧

回答4:

在主函数中定义数组s[5],然后在主函数中在for函数内调用输入输出函数,你试试应该可以的。