fork(1) download
  1. #include<iostream>
  2. #include<iomanip>
  3. #include<string>
  4.  
  5. using namespace std;
  6.  
  7. // максимальное количество оценок
  8. #define MAX_RATING 20
  9. // максимальное количество студентов
  10. #define MAX_STUDENT 10
  11.  
  12. class Student {
  13. string surname;
  14. int *rating; // тут просится vector, но вы его не изучали :)
  15. int count;
  16. public:
  17. Student();
  18. ~Student() { delete [] rating; }
  19. friend ostream &operator<<(ostream &os, const Student &st);
  20. friend istream &operator>>(istream &is, Student &st);
  21. public:
  22. double avg() const; // const т.к. средняя оценка НЕ МЕНЯЕТ объект
  23. };
  24.  
  25. Student::Student() :surname(), count(0) { rating = new int[MAX_RATING]; }
  26.  
  27. double Student::avg() const {
  28. double d = 0.0;
  29. if (count > 0) {
  30. for (int i = 0; i < count; i++)
  31. d += rating[i];
  32. d /= count;
  33. }
  34.  
  35. return d;
  36. }
  37.  
  38. ostream &operator<<(ostream &os, const Student &st) {
  39. os << "Surname: " << st.surname << endl;
  40. os << "Ratings: ";
  41. if (st.count > 0)
  42. for (int i = 0; i < st.count; i++)
  43. os << st.rating[i] << ' ';
  44. else
  45. os << "none";
  46. os << endl;
  47. os << "Avg: ";
  48. if (st.count > 0)
  49. os << setprecision(3) << setw(4) << st.avg(); // 4 = lenght("none"), 3 = 4-1
  50. else
  51. os << "none";
  52. os << endl;
  53.  
  54. return os;
  55. }
  56.  
  57. istream &operator>>(istream &is, Student &st) {
  58. is >> st.surname; // сначала фамилия до пробела
  59. is >> st.count; // потом количество оценок
  60. if (st.count > MAX_RATING) st.count = MAX_RATING;
  61. // и наконец сами оценки
  62. for (int i = 0; i < st.count; i++) is >> st.rating[i];
  63. // !! не проверяем корректность оценок
  64.  
  65. return is;
  66. }
  67.  
  68. int main() {
  69. Student *st = new Student[MAX_STUDENT];
  70.  
  71. // читаем студентов из потока ввода пока не кончатся
  72. // !! это особенность ideone, в котором stdin - конечный файл
  73. int stCount = 0;
  74. for (; (stCount < MAX_STUDENT) && !cin.eof(); stCount++)
  75. cin >> st[stCount];
  76.  
  77. // вывод прочитанного сиска студентов
  78. for (int i = 0; i < stCount; i++)
  79. cout << st[i] << endl;
  80.  
  81. delete [] st;
  82. return 0;
  83. }
Success #stdin #stdout 0s 3468KB
stdin
Иванов 4 3 4 5 3
Петров 3 4 3 3
Сидоров 6 4 5 3 4 4 5 
Гулёна 0
stdout
Surname: Иванов
Ratings: 3 4 5 3 
Avg: 3.75

Surname: Петров
Ratings: 4 3 3 
Avg: 3.33

Surname: Сидоров
Ratings: 4 5 3 4 4 5 
Avg: 4.17

Surname: Гулёна
Ratings: none
Avg: none