1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
#include <iostream>
#include <string>
#include <set>
#include <algorithm>
#include <fstream>
#include <iterator>
using namespace std;
class CStudent
{
string m_strFN;
int m_iPoints;
public:
CStudent() {}
CStudent (int iPoints)
{
m_iPoints = iPoints;
}
CStudent(const CStudent& e)
{
m_strFN = e.m_strFN;
m_iPoints = e.m_iPoints;
}
~CStudent() {}
void Output (ostream& toStream) const
{
toStream << "Student Fac# = " << GetFN() << " " << GetPoints() << endl;
}
void Input( istream& fromStream)
{
fromStream >> m_strFN >> m_iPoints;
}
void SetFN (const string &strFN)
{
m_strFN = strFN;
}
const string& GetFN() const
{
return m_strFN;
}
void SetPoints (const int _iPoints)
{
m_iPoints = _iPoints;
}
const int GetPoints() const
{
return m_iPoints;
}
friend ostream& operator<< (ostream& toStream, const CStudent& stud)
{
stud.Output(toStream);
return toStream;
}
friend istream& operator>> (istream& fromStream,CStudent& stud)
{
stud.Input(fromStream);
return fromStream;
}
CStudent& operator=(CStudent& st)
{
m_strFN = st.m_strFN;
m_iPoints = st.m_iPoints;
return *this;
}
bool operator< (const CStudent& e) const
{
return m_iPoints < e.m_iPoints;
}
bool operator== (const CStudent& e) const
{
return m_iPoints == e.m_iPoints;
}
static bool GetPortionGlobal (const CStudent& value)
{
g_iCurrent++;
return (g_iCurrent < g_iRange);
}
static int g_iRange;
static int g_iCurrent;
};
int CStudent::g_iRange = 0;
int CStudent::g_iCurrent = 0;
class CStudentGroup
{
set<CStudent> m_setStudents;
public:
CStudentGroup(set<CStudent>& set)
{
m_setStudents = set;
}
CStudentGroup() {}
void readIn(ifstream ifile);
void printGroup(int iFrom,int iTo);
void makeRanges(int& iP1,int& iP2,int& iP3,int& iP4);
};
void CStudentGroup::readIn(ifstream ifile)
{
if (m_setStudents.size())
{
m_setStudents.clear();
}
copy (istream_iterator<CStudent> (ifile),istream_iterator<CStudent>(),inserter(m_setStudents,m_setStudents.begin()));
}
void CStudentGroup::printGroup(int iFrom,int iTo)
{
set<CStudent>::iterator Strudentiter1 = lower_bound(m_setStudents.begin(),m_setStudents.end(),CStudent(iFrom));
set<CStudent>::iterator Strudentiter2 = lower_bound(m_setStudents.begin(),m_setStudents.end(),CStudent(iTo));
if (Strudentiter2 != m_setStudents.end())
{
Strudentiter2--;
}
copy(Strudentiter1,Strudentiter2,ostream_iterator<CStudent>(cout,"'\n"));
}
class GetPortion
{
public:
GetPortion(int iInit) : m_iCurrent(0),m_iRange(iInit) {}
bool operator() (CStudent value)
{
m_iCurrent++;
return (m_iCurrent < m_iRange);
}
private:
int m_iCurrent,m_iRange;
};
void CStudentGroup::makeRanges(int& iP1,int& iP2,int& iP3,int& iP4)
{
CStudent::g_iRange = m_setStudents.size() / 4;
CStudent::g_iCurrent = 0;
if (!CStudent::g_iRange)
{
iP1 = iP2 = iP3 = iP4 = 0;
return;
}
set<CStudent>::iterator split = partition(m_setStudents.begin(),m_setStudents.end(),CStudent::GetPortionGlobal);
iP1 = (*(++split)).GetPoints();
CStudent::g_iCurrent = 0;
split = partition(split,m_setStudents.end(),CStudent::GetPortionGlobal);
iP2 = (*(++split)).GetPoints();
CStudent::g_iCurrent = 0;
split = partition(split,m_setStudents.end(),CStudent::GetPortionGlobal);
iP3 = (*(++split)).GetPoints();
CStudent::g_iCurrent = 0;
split = m_setStudents.end();
iP4 = (*(--split)).GetPoints();
}
int main()
{
CStudent oSt1,oSt2,oSt3,oSt4;
oSt1.SetFN("106031");
oSt1.SetPoints(10);
oSt2.SetFN("106032");
oSt2.SetPoints(20);
oSt3.SetFN("106033");
oSt3.SetPoints(30);
oSt4.SetFN("106034");
oSt4.SetPoints(60);
set<CStudent> student_set;
student_set.insert(oSt1);
student_set.insert(oSt2);
student_set.insert(oSt3);
student_set.insert(oSt4);
CStudentGroup stGroup (student_set);
CStudentGroup stGroup2;
stGroup2.readIn(ifstream("input.txt"));
cout << "*************\n";
int iP1 = 0, iP2 = 0, iP3 = 0, iP4 = 0;
stGroup2.makeRanges(iP1,iP2,iP3,iP4);
cout << "GR_1: " << iP1 << endl;
stGroup2.printGroup(0,iP1);
cout << "GR_2: " << iP2 << endl;
stGroup2.printGroup(iP1,iP2);
cout << "GR_3: " << iP3 << endl;
stGroup2.printGroup(iP2,iP3);
cout << "GR_4: " << iP4 << endl;
stGroup2.printGroup(iP3,iP4);
return 0;
}
|