#include <iostream>
#include <fstream>
using namespace std;
class student {
private:
int ScoreNum;
int *Score;
int totalScore;
float ScoreAvg;
public:
student()
{
Score = 0;
totalScore = 0;
ScoreAvg = 0.0;
}
~student()
{
delete []Score;
}
void setNum(int ScoreNum)
{
this->ScoreNum = ScoreNum;
Score = new int[ScoreNum];
}
int* getScore()
{
return Score;
}
void compute()
{
for(int i = 0; i < ScoreNum; i++) {
totalScore += Score[i];
}
ScoreAvg = (float)totalScore / ScoreNum;
}
int getTotal()
{
return totalScore;
}
float getAvg()
{
return ScoreAvg;
}
};
void readScore(student *&allStudent,int &studentNum,int &ScoreNum)
{
ifstream iFile("score.txt");
iFile >> studentNum >> ScoreNum;
allStudent = new student[studentNum];
for(int i = 0; i < studentNum; i++) {
allStudent[i].setNum(ScoreNum);
for(int j = 0; j < ScoreNum; j++){
iFile >> allStudent[i].getScore()[j];
}
allStudent[i].compute();
}
iFile.close();
}
void display(student *allStudent,int studentNum,int ScoreNum)
{
int sum = 0;
for(int i = 0; i < studentNum; i++){
cout << "student" << i+1
<< "-> total=" << allStudent[i].getTotal()
<< ",avg=" << allStudent[i].getAvg() << endl;
sum+= allStudent[i].getTotal();
}
cout << "total = " << sum << ", avg = " << (float)sum / (studentNum*ScoreNum);
}
int main()
{
student *allStudent;
int studentNum,ScoreNum;
readScore(allStudent,studentNum,ScoreNum);
display(allStudent,studentNum,ScoreNum);
delete []allStudent;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8ZnN0cmVhbT4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgpjbGFzcyBzdHVkZW50IHsKcHJpdmF0ZToKICAgIGludCBTY29yZU51bTsKCWludCAqU2NvcmU7CglpbnQgdG90YWxTY29yZTsKCWZsb2F0IFNjb3JlQXZnOwpwdWJsaWM6CglzdHVkZW50KCkKCXsKCQlTY29yZSA9IDA7CgkJdG90YWxTY29yZSA9IDA7CgkJU2NvcmVBdmcgPSAwLjA7Cgl9Cgl+c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