Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstring>
- #include <iostream>
- #include <fstream>
- using namespace std;
- class MyException{
- public:
- MyException() : message("Mark must be between 0 and 10\n") {}
- void printMessage() { cout << message; }
- private:
- const char * message;
- };
- class Man {
- protected:
- char * name;
- public:
- Man(char *);
- virtual ~Man();
- const char * getname() const;
- };
- Man :: Man(char * s) {
- int n = strlen(s);
- name = new char [n + 1];
- for (int i = 0; i <= n; ++i) name[i] = s[i];
- }
- Man :: ~Man() {
- delete [] name;
- }
- const char * Man :: getname() const {
- return name;
- }
- class Staff : public Man {
- protected:
- char * disc;
- public:
- Staff(char *, char *);
- virtual ~Staff();
- const char * getdisc() const;
- };
- Staff :: Staff(char * s, char * f) : Man(s) {
- int n = strlen(f);
- disc = new char [n + 1];
- for (int i = 0; i <= n; ++i) disc[i] = f[i];
- }
- Staff :: ~Staff() {
- delete [] disc;
- }
- const char * Staff :: getdisc() const {
- return disc;
- }
- class Student : public Staff {
- private:
- struct MyStruct {
- char * disc;
- int mark;
- } my;
- public:
- Student(char *, char *, int);
- virtual ~Student() {}
- int getmark() const;
- };
- Student :: Student(char * s, char * f, int x) : Staff(s, f) {
- if (x < 0 || x > 10) throw MyException();
- my.disc = disc;
- my.mark = x;
- }
- int Student :: getmark() const {
- return my.mark;
- }
- int main() {
- setlocale(LC_ALL, ".1251");
- ifstream fin("input.txt");
- int n, m;
- fin >> n;
- if (n <= 0) {
- cout << "Количество студентов должно быть положительным числом\n";
- return 0;
- }
- Student ** students = new Student * [n];
- for (int i = 0; i < n; ++i) {
- char * name, * disc;
- int mark;
- name = new char[20];
- disc = new char[20];
- fin >> name >> disc >> mark;
- try {
- students[i] = new Student(name, disc, mark);
- } catch (MyException e) {
- e.printMessage();
- students[i] = NULL;
- }
- delete [] name;
- delete [] disc;
- }
- char s[100];
- int ans = 0;
- cout << "Введите название дисциплины:\n";
- cin >> s;
- for (int i = 0; i < n; ++i)
- if (students[i] != NULL && strcmp(students[i] -> getdisc(), s) == 0) ans += students[i] -> getmark() > 3;
- cout << "Количество студентов, сдавших указанную дисциплину положительно: " << ans << endl << endl;
- fin >> m;
- Staff ** staffs = new Staff *[m];
- for (int i = 0; i < m; ++i) {
- char * name, * disc;
- name = new char[100];
- disc = new char[100];
- fin >> name >> disc;
- staffs[i] = new Staff(name, disc);
- delete [] name;
- delete [] disc;
- }
- for (int i = 0; i < m; ++i) {
- int sum = 0, count = 0;
- for (int j = 0; j < n; ++j)
- if (students[j] != NULL && strcmp(students[j] -> getdisc(), staffs[i] -> getdisc()) == 0) {
- ++count;
- sum += students[j] -> getmark();
- }
- cout << "Преподаватель: " << staffs[i] -> getname() << "\nДисциплина: " << staffs[i] -> getdisc();
- if (count) cout << "\nСредний балл: " << (sum + .0) / count;
- else cout << "Ни один студент не сдавал экзамен этому преподавателю";
- cout << endl << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement