Advertisement
Tranvick

Untitled

Jan 16th, 2013
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3.  
  4. using namespace std;
  5.  
  6. enum Gender{male, female};
  7.  
  8. class Man {
  9. protected:
  10.     char * name;
  11.     Gender gender;
  12.     int age;
  13. public:
  14.     Man(char *, Gender, int);
  15.     ~Man();
  16.     const char * getname() const;
  17. };
  18.  
  19. Man :: Man(char * s, Gender f, int x) : gender(f), age(x) {
  20.     name = new char[strlen(s) + 1];
  21.     for (int i = 0; i <= strlen(s); ++i) name[i] = s[i];
  22. }
  23.  
  24. Man :: ~Man() {
  25.     delete [] name;
  26. }
  27.  
  28. const char * Man :: getname() const {
  29.     return name;
  30. }
  31.  
  32. class Student : public Man {
  33. private:
  34.     int group, mark;
  35. public:
  36.     Student(char *, Gender, int, int, int);
  37.     int getgroup() const;
  38. };
  39.  
  40. Student :: Student(char * s, Gender f, int x, int y, int z) : Man(s, f, x), group(y), mark(z) {}
  41.  
  42. int Student :: getgroup() const {
  43.     return group;
  44. }
  45.  
  46. bool cmp1(Student * a, Student * b) {
  47.     return strcmp(a -> getname(), b -> getname()) < 0;
  48. }
  49.  
  50. bool cmp2(Student * a, Student * b) {
  51.     return a -> getgroup() < b -> getgroup();
  52. }
  53.  
  54. int main(){
  55.     int n, b, c, d;
  56.     char s[100], g;
  57.     setlocale(LC_ALL, ".1251");
  58.     cout << "Введите количество студентов: ";
  59.     cin >> n;
  60.     Student **a = new Student * [n];
  61.     for (int i = 0; i < n; ++i) {
  62.         cout << "Введите фамилию, пол(M/F), возраст, номер группы и оценку " << i + 1 << "-ого студента:\n";
  63.         cin >> s >> g >> b >> c >> d;
  64.         a[i] = new Student(s, g == 'M' ? male : female, b, c, d);
  65.     }
  66.     sort(a, a + n, cmp1);
  67.     cout << "\nСписок студентов после сортировки:\n";
  68.     for (int i = 0; i < n; ++i)
  69.         cout << a[i] -> getname() << endl;
  70.  
  71.     sort(a, a + n, cmp2);
  72.     int i = 0;
  73.     while (i < n) {
  74.         int j = i + 1;
  75.         while (j < n && a[j] -> getgroup() == a[i] -> getgroup()) ++j;
  76.         cout << a[i] -> getgroup() << "-ая группа:\n";
  77.         for (; i < j; ++i)
  78.             cout << a[i] -> getname() << endl;
  79.     }
  80.  
  81.     for (int i = 0; i < n; ++i) delete a[i];
  82.     delete [] a;
  83.     return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement