fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. namespace
  9. {
  10. struct seiseki
  11. {
  12. std::string namae;
  13.  
  14. int kokugo;
  15. int sugaku;
  16. };
  17.  
  18. struct kamoku
  19. {
  20. std::string kamoku_mei;
  21. int seiseki::*score_ptr;
  22. };
  23.  
  24. std::vector<seiseki> seiseki_data
  25. {
  26. {"太郎", 74, 93},
  27. {"次郎", 78, 82},
  28. {"三郎", 94, 68}
  29. };
  30.  
  31. std::vector<kamoku> kamoku_list
  32. {
  33. {"KOKUGO", &seiseki::kokugo},
  34. {"SUGAKU", &seiseki::sugaku}
  35. };
  36.  
  37. int keisan_kojin_gokei(seiseki score)
  38. {
  39. return score.kokugo + score.sugaku;
  40. }
  41. } // namespace
  42.  
  43. int main()
  44. {
  45. cout << "namae ";
  46. for ( auto kamoku : kamoku_list )
  47. cout << setw(8) << kamoku.kamoku_mei;
  48. cout << " gokei" << endl;
  49.  
  50. cout << fixed << setprecision(1);
  51. for ( auto score : seiseki_data )
  52. {
  53. cout << setw(8) << score.namae
  54. << setw(8) << score.kokugo
  55. << setw(8) << score.sugaku
  56. << " "
  57. << setw(8) << keisan_kojin_gokei(score)
  58. << endl;
  59. }
  60.  
  61. cout << " ";
  62. for ( auto kamoku_idx = 0; kamoku_idx < kamoku_list.size(); kamoku_idx++ )
  63. {
  64. double total = 0.0;
  65. for ( auto score : seiseki_data )
  66. {
  67. total += score.*(kamoku_list[kamoku_idx].score_ptr);
  68. }
  69. cout << setw(8) << total / seiseki_data.size();
  70. }
  71. double total = 0.0;
  72. for ( auto score : seiseki_data )
  73. total += keisan_kojin_gokei(score);
  74. cout << setw(8) << total / seiseki_data.size();
  75. cout << endl;
  76.  
  77.  
  78.  
  79. cout << endl;
  80.  
  81. for ( auto kamoku : kamoku_list )
  82. {
  83. cout << "<" << kamoku.kamoku_mei << ">" << endl;
  84.  
  85. std::sort(seiseki_data.begin(), seiseki_data.end(),
  86. [kamoku](seiseki lhs, seiseki rhs)
  87. {
  88. return lhs.*(kamoku.score_ptr) > rhs.*(kamoku.score_ptr);
  89. }
  90. );
  91.  
  92. auto rank = 0;
  93. for ( auto score : seiseki_data )
  94. {
  95. // std::printf(" %d %s %d\n", ++rank, score.namae.c_str(), score.*(kamoku.score_ptr));
  96. cout << ++rank << " " << score.namae << " " << score.*(kamoku.score_ptr) << endl;
  97. }
  98.  
  99. cout << endl;
  100. }
  101. }
  102.  
  103.  
Success #stdin #stdout 0s 3044KB
stdin
Standard input is empty
stdout
namae     KOKUGO  SUGAKU gokei
  太郎      74      93      167
  次郎      78      82      160
  三郎      94      68      162
            82.0    81.0   163.0

<KOKUGO>
1 三郎 94
2 次郎 78
3 太郎 74

<SUGAKU>
1 太郎 93
2 次郎 82
3 三郎 68