答案:这两天比较忙,思想是:单独建立一个应聘者类,读取文件的时候将对象压入vector<Applyer*>中,然后通过标准库的sort()函数,使用比较Applyer的中和分数的方式排序,然后筛选,筛选的时候从分数大小顺序,按志愿测试部门对象是否满人,满人,则使用第二志愿,同样先拍断是否满人。若部门人已满,则判断录取的人当中是否有相同分数的人,若有,比较内部录取人员和当前人员的报名号,录取最小报名号的人(具体实现是若当前人员的报名号小,则替换那个人所占用的位置,否则什么也不做。)因为录取的人都保存在各个部门的last_applyer向量中了,所以最后打印他的信息(采用STL函数for_each())文件的组织方式是分开的,每个文件的最开始列出了文件的名字。//filename applyer.cpp#include "applier.h"
bool operator<(Applyer& first, Applyer& second){ return first.complex() < second.complex() ? true : false;}
int Applyer::number() const{return _number;}
int& Applyer::complex(){return _complex_results;}
int Applyer::first_aspiration() const{return _wish1;}
int Applyer::second_aspiration() const{return _wish2;}
Applyer& Applyer::operator=(Applyer & that){ if(this == & that) return *this; this->_number = that._number; this->_complex_results = that._complex_results; this->_wish1 = that._wish1; this->_wish2 = that._wish2; return *this;}
//fiolename applyer.h#ifndef APPLYER_H#define APPLYER_H
#include <map>#include <string>
class Applyer{private: mutable int _number; int _complex_results; int _wish1; int _wish2;public: Applyer(int num, int complex_result, int wish1, int wish2) : _number(num), _complex_results(complex_result), _wish1(wish1), _wish2(wish2){} friend bool operator<(Applyer,Applyer);//基于综合成绩的比较 Applyer& operator=(Applyer & that); int number() const; int& complex(); int first_aspiration() const; int second_aspiration() const;};
#endif
//filename arrange.h
#ifndef ARRANGE_H#define ARRANGE_H#include "applyer.h"#include <vector>#include <cstdlib>#include <algorithm>#include <iostream>
static struct Department{//单件模式 int capacity; int had; std::vector<Applyer*> last_applyer; Department(int c) : capacity(c), had(0){} bool full(){return had >= capacity;} }department1(2), department2(4), department3(5), department4(7),department5(6), department6(4);//括号内的数是每个部门需要招聘的人数
void arrange(std::vector<Applyer*>&);void admit(Applyer *people,Department);void print_num(Applyer *applyer);void print();
#endif
#include "arrange.h"
void arrange(std::vector<Applyer*>& applyer){ for(std::vector<Applyer*>::iterator it = applyer.begin(); it != applyer.end(); ++it){ switch((*it)->first_aspiration()){ case 1: if(department1.full()) break; admit(*it, department1); break; case 2: if(department2.full())break; admit(*it, department2); break; case 3: if(department3.full()) break; admit(*it, department3); break; case 4: if(department4.full()) break; admit(*it, department4); break; case 5: if(department5.full())break; admit(*it, department5); break; case 6: if(department6.full()) break; admit(*it, department6); break; default: exit(-1); break; } switch((*it)->second_aspiration()){ case 1: if(department1.full()) break; admit(*it, department1); break; case 2: if(department2.full()) break; admit(*it, department2); break; case 3: if(department3.full()) break; admit(*it, department3); break; case 4: if(department4.full()) break; admit(*it, department4); break; case 5: if(department5.full()) break; admit(*it, department5); break; case 6: if(department6.full()) break; admit(*it, department6); break; default: exit(-1); break; } }}//录取void admit(Applyer *people,Department a_part){ for(std::vector<Applyer*>::iterator it = a_part.last_applyer.begin(); it != a_part.last_applyer.end(); ++it){ if((*it)->complex() == people->complex()){ if((*it)->number() > people->number()) (**it) = (*people); //如果同样的综合分书,选择报名号小的那人 } } a_part.last_applyer.push_back(people);}
inline void print(){ for_each(department1.last_applyer.begin(), department1.last_applyer.end(),print_num);}
inline void print_num(Applyer * applyer){ std::cout<<applyer->number();}
//filanem employee.cpp#include "applyer.h"#include "arrange.h"
#include <vector>#include <string>#include <map>#include <algorithm>#include
上一个:c++编程题
下一个:请问怎么学习C++?