Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- #include <queue>
- using namespace std;
- class Student
- {
- char *name;
- char gender;
- public:
- Student()
- {
- name = new char[1];
- strcpy(name,"");
- gender = '0';
- }
- Student(char *name, double gender) : gender(gender)
- {
- this->name = new char[strlen(name) + 1];
- strcpy(this->name, name);
- }
- Student(Student const& other)
- {
- this->name = new char[strlen(other.name) + 1];
- strcpy(this->name, other.name);
- this->gender = other.gender;
- }
- Student& operator=(Student const& other)
- {
- if(this != &other)
- {
- delete[] this->name;
- this->name = new char[strlen(other.name) + 1];
- strcpy(this->name, other.name);
- this->gender = other.gender;
- }
- return *this;
- }
- ~Student()
- {
- delete[] name;
- }
- friend ostream& operator<<(ostream& os, const Student& student)
- {
- os << student.name << " ";
- return os;
- }
- void print()
- {
- cout << getName() << " " << getGender() << endl;
- }
- void input()
- {
- cin >> name;
- cin >> gender;
- if(gender != 'F' && gender != 'M')
- input();
- }
- /*friend istream& operator>>(istream& is, Student& student)
- {
- is.getline(student.name,20) ;
- is >> student.gender;
- return is;
- }*/
- char getGender()
- {
- return gender;
- }
- char* getName()
- {
- return name;
- }
- void setGender(char g)
- {
- gender = g;
- }
- void setName(char* _name)
- {
- this->name = new char[strlen(_name) + 1];
- strcpy(this->name, _name);
- }
- bool operator<(Student const& student)
- {
- return name[0] < student.name[0];
- }
- void swapStudents(Student& student)
- {
- char* helper;
- helper = new char[1];
- strcpy(helper,"");
- strcpy(helper, name);
- strcpy(name, student.name);
- strcpy(student.name, helper);
- }
- };
- void sortName(queue<Student>& student, int n)
- {
- queue<Student> helper;
- Student a[n];
- int i = 0;
- while(!student.empty())
- {
- a[i] = student.front();
- student.pop();
- i++;
- }
- for(int i = 0; i < n - 1; i++) {
- int minIndex = i;
- for(int j = i + 1; j < n; j++)
- if (a[j] < a[minIndex])
- minIndex = j;
- a[i].swapStudents(a[minIndex]);
- }
- for(int j = 0; j < i; j++)
- {
- student.push(a[j]);
- }
- /*for(unsigned i = 0; i < student.size() - 1; i++)
- {
- helper.push(student.front());
- student.pop();
- for(unsigned j = i + 1; j < student.size(); j++)
- if (student.back() < helper.back())
- student.back().swapStudents(helper.back());
- }
- student.queue::swap(helper);*/
- }
- void processQueue()
- {
- int n;
- cin >> n;
- queue<Student> peopleInLine;
- for(int i = 0; i < n; i++)
- {
- Student s;
- s.input();
- peopleInLine.push(s);
- }
- int females = 0, males = 0;
- queue<Student> femalesInClub;
- queue<Student> malesInClub;
- while(!peopleInLine.empty())
- {
- if(peopleInLine.front().getGender() == 'F')
- {
- femalesInClub.push(peopleInLine.front());
- females++;
- }
- else
- {
- malesInClub.push(peopleInLine.front());
- males++;
- }
- peopleInLine.pop();
- }
- sortName(femalesInClub, females);
- queue<Student> peopleInClub;
- while(!femalesInClub.empty())
- {
- peopleInClub.push(femalesInClub.front());
- femalesInClub.pop();
- }
- sortName(malesInClub, males);
- while(!malesInClub.empty())
- {
- peopleInClub.push(malesInClub.front());
- malesInClub.pop();
- }
- while(!peopleInClub.empty())
- {
- cout << peopleInClub.front();
- peopleInClub.pop();
- }
- }
- int main()
- {
- processQueue();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement