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
|
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
enum class gender { Male, Female };
class Student {
private:
string id{"None"}, fname {"None"}, lname{"None"}, cname{"None"};
int Icount {};
gender g {gender::Female};
public:
Student() { }
Student(const string& _id, gender _g, const string& _fname, const string& _lname, const string& _cname, int _Icount) :
fname(_fname), lname(_lname), id(_id), cname(_cname), Icount(_Icount), g(_g) {}
void setfname(const string& _fname) { fname = _fname; }
string getfname() const {return fname; }
void setlname(const string& _lname) { lname = _lname; }
string getlname() const { return lname; }
void setid(const string& _id) { id = _id; }
string getid() const { return id; }
void setcname(const string& _cname) { cname = _cname; }
string getcname() const { return cname; }
void setIcount(int _Icount) { Icount = _Icount; }
int getIcount() const { return Icount; }
void setg(gender _g) { g = _g; }
gender getg() const { return g; }
void print() const {
cout << "ID [" << id << "] First Name [" << fname << "] Last Name [" << lname << "] Gender [";
if (g == gender::Female)
cout << "Female";
else
cout << "Male";
cout << "] Course [" << cname << "] Attendance [" << Icount << "]\n";
}
};
vector<Student> readStudents(const string& filename) {
ifstream f(filename);
if (!f) {
cout << "Failed to open file";
exit(1);
}
int size {};
string temp;
vector <Student> students;
f >> size;
getline(f, temp);
getline(f, temp);
for (int i = 0; i < size; i++) {
int c {};
string _fname, _lname, _id, _cname;
int _Icount {};
if (f >> _id >> c >> _fname >> _lname >> _cname >> _Icount) {
gender _g {c == 0 ? gender::Male : gender::Female};
students.emplace_back(_id, _g, _fname, _lname, _cname, _Icount);
}
}
return students;
}
void printStudents(const vector<Student>& stds) {
for (int i = 0; i < stds.size(); i++)
stds[i].print();
}
vector<Student> getFemales(const vector<Student>& stds) {
vector<Student> stf;
for (int i = 0; i < stds.size(); ++i)
if (stds[i].getg() == gender::Female)
stf.push_back(stds[i]);
return stf;
}
vector<Student> getLowAttendance(const vector<Student>& stds) {
vector<Student> stl;
for (int i = 0; i < stds.size(); ++i)
if (stds[i].getIcount() < 20)
stl.push_back(stds[i]);
return stl;
}
int main() {
const auto stds_vect {readStudents("text.txt")};
printStudents(stds_vect);
printStudents(getFemales(stds_vect));
printStudents(getLowAttendance(stds_vect));
}
|