fork download
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. class student {
  7. private:
  8. int ScoreNum;
  9. int *Score;
  10. int totalScore;
  11. float ScoreAvg;
  12. public:
  13. student()
  14. {
  15. Score = 0;
  16. totalScore = 0;
  17. ScoreAvg = 0.0;
  18. }
  19. ~student()
  20. {
  21. delete []Score;
  22. }
  23. void setNum(int ScoreNum)
  24. {
  25. this->ScoreNum = ScoreNum;
  26. Score = new int[ScoreNum];
  27. }
  28. int* getScore()
  29. {
  30. return Score;
  31. }
  32. void compute()
  33. {
  34. for(int i = 0; i < ScoreNum; i++) {
  35. totalScore += Score[i];
  36. }
  37. ScoreAvg = (float)totalScore / ScoreNum;
  38. }
  39. int getTotal()
  40. {
  41. return totalScore;
  42. }
  43. float getAvg()
  44. {
  45. return ScoreAvg;
  46. }
  47. };
  48.  
  49. void readScore(student *&allStudent,int &studentNum,int &ScoreNum)
  50. {
  51. ifstream iFile("score.txt");
  52. iFile >> studentNum >> ScoreNum;
  53. allStudent = new student[studentNum];
  54. for(int i = 0; i < studentNum; i++) {
  55. allStudent[i].setNum(ScoreNum);
  56. for(int j = 0; j < ScoreNum; j++){
  57. iFile >> allStudent[i].getScore()[j];
  58. }
  59. allStudent[i].compute();
  60. }
  61. iFile.close();
  62. }
  63.  
  64. void display(student *allStudent,int studentNum,int ScoreNum)
  65. {
  66. int sum = 0;
  67. for(int i = 0; i < studentNum; i++){
  68. cout << "student" << i+1
  69. << "-> total=" << allStudent[i].getTotal()
  70. << ",avg=" << allStudent[i].getAvg() << endl;
  71. sum+= allStudent[i].getTotal();
  72. }
  73. cout << "total = " << sum << ", avg = " << (float)sum / (studentNum*ScoreNum);
  74. }
  75.  
  76. int main()
  77. {
  78. student *allStudent;
  79. int studentNum,ScoreNum;
  80. readScore(allStudent,studentNum,ScoreNum);
  81. display(allStudent,studentNum,ScoreNum);
  82. delete []allStudent;
  83. return 0;
  84. }
Runtime error #stdin #stdout #stderr 0s 4052KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc