題目:有三名同學,在一次考試中三科成績分別如下表,請輸出三名同學的平均成績:
語文 | 數學 | 英語 | |
張三 | 100 | 100 | 100 |
李四 | 90 | 50 | 100 |
王五 | 60 | 70 | 80 |
#include <iostream>
#include <string>
/*考試成績統計*/
using namespace std;class Score {private :string id;int chinese;int english;int math;public :Score(string name, int score1, int score2, int score3) : id(name), chinese(score1), english(score2), math(score3) {}double getAvgScore(){return (chinese + math + english) / 3.0;}string getName() {return id;}
}; int main() {Score s1("張三", 100, 100, 100);Score s2("李四", 90, 50, 100);Score s3("王五", 60, 70, 80);cout << s1.getName() << "的平均成績是" << s1.getAvgScore() << endl;cout << s2.getName() << "的平均成績是" << s2.getAvgScore() << endl;cout << s3.getName() << "的平均成績是" << s3.getAvgScore() << endl;return 0;
}
?