原题源于北京大学在coursera上开的《C++程序设计》课后一道编程作业题。见下图:
代码如下:
#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
void input()
{
getline(cin, name, ',');
cin>>age>>a>>num>>a>>g1>>a>>g2>>a>>g3>>a>>g4;
}
void ave()
{
average = (double)((g1+g2+g3+g4)/4.0);
}
void output()
{
cout<<name<<a<<age<<a<<num<<a<<average;
}
private:
int age, num, g1, g2, g3, g4;
double average;
string name;
char a;
};
int main()
{
Student student;
student.input();
student.ave();
student.output();
return 0;
}