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
|
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
using namespace std;
//for left and right column
struct dstring
{
string id1;
string id2;
} ;
vector<dstring> v(string filename)
//extracts files from file to a vector with 2 elements per cell
{
vector<dstring> v;
ifstream ifile;
ifile.open(filename.c_str());
string std;
string buffer1 = "", buffer2 = "";
int c=0;
while(ifile)
{
getline (ifile, std);
unsigned int n=0, k=0;
while(std[n] != ';' && n < std.size())
{
buffer1 += std[n];
n++;
}
n++;
while(getline (ifile,std) && n < std.size())
{
buffer2 += std[n];
n++;
}
c++;
dstring d;
d.id1 = buffer1;
d.id2 = buffer2;
v.push_back(d);
}
ifile.close();
return v;
}
string idfinderleft(string str, vector<dstring> v1)
//searches string in the left column, returns the right column value
{
unsigned int n=0;
do
{
if(v1[n].id1.c_str() == str.c_str())
{
return v1[n].id2;
}
n++;
}while(v1[n].id1.c_str() != str.c_str() && n < v1.size());
}
string idfinderright(string str, vector<dstring> v1)
//reversed variant of idfinder
{
unsigned int n=0;
do
{
if(v1[n].id2.c_str() == str.c_str())
{
return v1[n].id1;
}
n++;
}while(v1[n].id2.c_str() != str.c_str() && n < v1.size());
}
//given course id, returns a vector that has student ids who participate in course
vector<string> enrolled(string str, vector<dstring> v1)
{
unsigned int n=0;
vector<string> v;
while(n < v1.size())
{
if(v1[n].id2 == str)
{
v.push_back(v1[n].id1);
}
n++;
}
return v;
}
int main()
{
unsigned int counter = 0;
string filename1, filename2, filename3, courseid, coursename;
cin >> filename1;
vector<dstring> v1 = v(filename1);
cin >> filename2;
vector<dstring> v2 = v(filename2);
cin >> filename3;
vector<dstring> v3 = v(filename3);
cin >> coursename;
courseid = idfinderright(coursename, v1);
vector<string> ve = enrolled(courseid, v2);
while(counter < ve.size())
{
cout << idfinderleft(ve[counter], v3);
counter++;
}
return 0;
}
|