#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
// максимальное количество оценок
#define MAX_RATING 20
// максимальное количество студентов
#define MAX_STUDENT 10
class Student {
string surname;
int *rating; // тут просится vector, но вы его не изучали :)
int count;
public:
Student();
~Student() { delete [] rating; }
friend ostream &operator<<(ostream &os, const Student &st);
friend istream &operator>>(istream &is, Student &st);
public:
double avg() const; // const т.к. средняя оценка НЕ МЕНЯЕТ объект
};
Student::Student() :surname(), count(0) { rating = new int[MAX_RATING]; }
double Student::avg() const {
double d = 0.0;
if (count > 0) {
for (int i = 0; i < count; i++)
d += rating[i];
d /= count;
}
return d;
}
ostream &operator<<(ostream &os, const Student &st) {
os << "Surname: " << st.surname << endl;
os << "Ratings: ";
if (st.count > 0)
for (int i = 0; i < st.count; i++)
os << st.rating[i] << ' ';
else
os << "none";
os << endl;
os << "Avg: ";
if (st.count > 0)
os << setprecision(3) << setw(4) << st.avg(); // 4 = lenght("none"), 3 = 4-1
else
os << "none";
os << endl;
return os;
}
istream &operator>>(istream &is, Student &st) {
is >> st.surname; // сначала фамилия до пробела
is >> st.count; // потом количество оценок
if (st.count > MAX_RATING) st.count = MAX_RATING;
// и наконец сами оценки
for (int i = 0; i < st.count; i++) is >> st.rating[i];
// !! не проверяем корректность оценок
return is;
}
int main() {
Student *st = new Student[MAX_STUDENT];
// читаем студентов из потока ввода пока не кончатся
// !! это особенность ideone, в котором stdin - конечный файл
int stCount = 0;
for (; (stCount < MAX_STUDENT) && !cin.eof(); stCount++)
cin >> st[stCount];
// вывод прочитанного сиска студентов
for (int i = 0; i < stCount; i++)
cout << st[i] << endl;
delete [] st;
return 0;
}