Advertisement
vkichukova

Untitled

Nov 22nd, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <queue>
  4. using namespace std;
  5.  
  6. class Student
  7. {
  8. char *name;
  9. char gender;
  10.  
  11. public:
  12.  
  13. Student()
  14. {
  15. name = new char[1];
  16. strcpy(name,"");
  17. gender = '0';
  18. }
  19. Student(char *name, double gender) : gender(gender)
  20. {
  21. this->name = new char[strlen(name) + 1];
  22. strcpy(this->name, name);
  23. }
  24. Student(Student const& other)
  25. {
  26. this->name = new char[strlen(other.name) + 1];
  27. strcpy(this->name, other.name);
  28. this->gender = other.gender;
  29. }
  30.  
  31. Student& operator=(Student const& other)
  32. {
  33. if(this != &other)
  34. {
  35.  
  36. delete[] this->name;
  37. this->name = new char[strlen(other.name) + 1];
  38. strcpy(this->name, other.name);
  39. this->gender = other.gender;
  40. }
  41. return *this;
  42. }
  43.  
  44. ~Student()
  45. {
  46. delete[] name;
  47. }
  48.  
  49. friend ostream& operator<<(ostream& os, const Student& student)
  50. {
  51. os << student.name << " ";
  52. return os;
  53. }
  54. void print()
  55. {
  56. cout << getName() << " " << getGender() << endl;
  57. }
  58. void input()
  59. {
  60. cin >> name;
  61. cin >> gender;
  62. if(gender != 'F' && gender != 'M')
  63. input();
  64. }
  65. /*friend istream& operator>>(istream& is, Student& student)
  66. {
  67.  
  68. is.getline(student.name,20) ;
  69. is >> student.gender;
  70.  
  71. return is;
  72. }*/
  73. char getGender()
  74. {
  75. return gender;
  76. }
  77. char* getName()
  78. {
  79. return name;
  80. }
  81. void setGender(char g)
  82. {
  83. gender = g;
  84. }
  85. void setName(char* _name)
  86. {
  87. this->name = new char[strlen(_name) + 1];
  88. strcpy(this->name, _name);
  89. }
  90. bool operator<(Student const& student)
  91. {
  92. return name[0] < student.name[0];
  93. }
  94. void swapStudents(Student& student)
  95. {
  96. char* helper;
  97. helper = new char[1];
  98. strcpy(helper,"");
  99. strcpy(helper, name);
  100. strcpy(name, student.name);
  101. strcpy(student.name, helper);
  102. }
  103. };
  104.  
  105. void sortName(queue<Student>& student, int n)
  106. {
  107. queue<Student> helper;
  108. Student a[n];
  109. int i = 0;
  110. while(!student.empty())
  111. {
  112. a[i] = student.front();
  113. student.pop();
  114. i++;
  115. }
  116. for(int i = 0; i < n - 1; i++) {
  117. int minIndex = i;
  118. for(int j = i + 1; j < n; j++)
  119. if (a[j] < a[minIndex])
  120. minIndex = j;
  121.  
  122. a[i].swapStudents(a[minIndex]);
  123. }
  124. for(int j = 0; j < i; j++)
  125. {
  126. student.push(a[j]);
  127. }
  128. /*for(unsigned i = 0; i < student.size() - 1; i++)
  129. {
  130. helper.push(student.front());
  131. student.pop();
  132. for(unsigned j = i + 1; j < student.size(); j++)
  133. if (student.back() < helper.back())
  134. student.back().swapStudents(helper.back());
  135.  
  136. }
  137.  
  138. student.queue::swap(helper);*/
  139. }
  140.  
  141. void processQueue()
  142. {
  143. int n;
  144. cin >> n;
  145. queue<Student> peopleInLine;
  146.  
  147. for(int i = 0; i < n; i++)
  148. {
  149. Student s;
  150. s.input();
  151. peopleInLine.push(s);
  152. }
  153.  
  154. int females = 0, males = 0;
  155. queue<Student> femalesInClub;
  156. queue<Student> malesInClub;
  157. while(!peopleInLine.empty())
  158. {
  159. if(peopleInLine.front().getGender() == 'F')
  160. {
  161. femalesInClub.push(peopleInLine.front());
  162. females++;
  163. }
  164. else
  165. {
  166. malesInClub.push(peopleInLine.front());
  167. males++;
  168. }
  169. peopleInLine.pop();
  170. }
  171.  
  172. sortName(femalesInClub, females);
  173.  
  174. queue<Student> peopleInClub;
  175. while(!femalesInClub.empty())
  176. {
  177. peopleInClub.push(femalesInClub.front());
  178. femalesInClub.pop();
  179. }
  180.  
  181. sortName(malesInClub, males);
  182.  
  183. while(!malesInClub.empty())
  184. {
  185. peopleInClub.push(malesInClub.front());
  186. malesInClub.pop();
  187. }
  188.  
  189. while(!peopleInClub.empty())
  190. {
  191. cout << peopleInClub.front();
  192. peopleInClub.pop();
  193. }
  194. }
  195.  
  196. int main()
  197. {
  198. processQueue();
  199.  
  200. return 0;
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement