回复 5楼 苏洛离
#include<iostream>
#include<string>
using namespace std;
class Student
{
private:
string name;
long unsigned int num;
float score;
static int k;
static float averscore;
public:
void input();
void display();
void averScore();
static float printaverScore(){ return averscore;}
};
float Student::averscore = 0.0;
int Student::k = 0;
void Student::averScore()
{
k++;
averscore = (averscore + score) / (float) k ;
}
void Student::input()
{
cout<<"Enter the name:\n";
cin>>name;
cout<<"Enter the number:\n";
cin>>num;
cout<<"Enter the score:\n";
cin>>score;
}
void Student::display()
{
cout<<name<<"\t\t"<<num<<"\t\t"<<score<<endl;
}
int main()
{
Student stu[100];
int i = -1;
char ch = 'y';
cout<<"Pls input the infomation of students"<<endl;
do
{
i++;
stu[i].input();
stu[i].averScore();
cout<<"[y/n?]"<<endl;
cin>>ch;
}while(ch == 'Y' || ch == 'y');
cout<<"Name"<<"\t\t"<<"Number"<<"\t\t"<<"Score"<<endl;
for(int j = 0;j<=i;j++)
stu[j].display();
cout<<"The average score of these students is: "<<Student::printaverScore()<<endl;
return 0;
}