qfedu-cpp-level/day3/homework/h4.cpp

53 lines
1.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 设计一个学生类包括学号、姓名、成绩并设计接口函数用来输出这些学生数据并计算平均分。并编写main函数进行测试
// 输出如:
// 学号 姓名 成绩 1 张XX 98 2 王XX 90 3 XXX 89 平均成绩: XX
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
using namespace std;
class Student
{
private:
int sid;
string name;
float score;
public:
Student(int sid, const string &name, float score) : sid(sid), name(name), score(score)
{
}
~Student() {}
public:
static void avgAndShow(Student *stus, int n);
};
void Student::avgAndShow(Student *stus, int n)
{
float avg = 0.0f;
cout << "学号\t"
<< "姓名\t"
<< "成绩" << endl;
for (int i = 0; i < n; i++)
{
cout << stus[i].sid << "\t" << stus[i].name << "\t" << stus[i].score << endl;
avg += stus[i].score / n;
}
cout << "平均成绩: " << setprecision(3) << avg << endl;
}
int main()
{
Student *stus = new Student[3]{
Student(1, "张XX", 98),
Student(2, "王XX", 90),
Student(3, "XXX", 89)};
Student::avgAndShow(stus, 3);
return 0;
}