Anda di halaman 1dari 3

#include <iostream>

#include <vector>
#include <string>
#include <fstream>
#include <sstream>
// You can include additional headers if you want

using namespace std;

void queryFile(string infilename, string outfilename){


int to_num_stud;int to_num_cour;int cr1=0;int cr2=0;int cr3=0;
ifstream ifs;
ifs.open(infilename);
ifs>>to_num_stud>>to_num_cour;
string data1; //data1="A"
getline(ifs,data1);
string Roll,Name; //Roll="RollNo",Name="Name"
ifs>>Roll>>Name;
vector<vector<string>>v1;
vector<string>v2;
string roll,name;
ifs>>roll>>name;
while(roll!="B"){
v2.push_back(roll);v2.push_back(name);
v1.push_back(v2);
v2.clear();cr1++;
ifs>>roll>>name;
}
string trash;
ifs>>trash; //trash="CourseID"
string Course="CourseID";
vector<vector<string>>v3;
vector<string>v4;
string cour;
ifs>>roll>>cour;
while(roll!="Q"){
v4.push_back(roll);v4.push_back(cour);
v3.push_back(v4);
v4.clear();cr2++;
ifs>>roll>>cour;
}
vector<vector<string>>v5;
vector<string>v6;
v6.push_back(cour);
string q;
ifs>>q;
v6.push_back(q);
v5.push_back(v6);
v6.clear();cr3++;
string trash1;
while(getline(ifs,trash1)){
istringstream iss(trash1);
iss>>roll>>q;
v6.push_back(roll);v6.push_back(q);
v5.push_back(v6);
v6.clear();cr3++;
}
int count=0;
vector<int>v7; //vector for no. of courses taken by each student
for(int i=0;i<cr1;i++){
for(int j=0;j<cr2;j++){
if(v1[i][0]==v3[j][0]){
count++;
}
else{}
}
v7.push_back(count);
count=0;
}

vector<vector<string>>vec1;
vector<string>vec2;
for(int i=0;i<cr3;i++){
if(v5[i][0]=="X"){
for(int j=0;j<cr2;j++){
if(v5[i][1]==v3[j][1]){
vec2.push_back(v3[j][0]);
}
else{}
}
vec1.push_back(vec2);
vec2.clear();
}
else if(v5[i][0]=="Y"){
for(int j=0;j<cr2;j++){
string s1,s2;
s1=v5[i][1];s2=v3[j][1];
if(s1[0]==s2[0] && s1[1]==s2[1] && s1[2]==s2[2]){
vec2.push_back(v3[j][0]);
}
}
vec1.push_back(vec2);
vec2.clear();
}
else{
if(v5[i][1]=="none"){
for(int i=0;i<cr1;i++){
if(v7[i]==0){
vec2.push_back(v1[i][1]);
}
else{}
}
}
vec1.push_back(vec2);
vec2.clear();
if(v5[i][1]=="one"){
for(int i=0;i<cr1;i++){
if(v7[i]>0){
vec2.push_back(v1[i][1]);
}
else{}
}
}
vec1.push_back(vec2);
vec2.clear();
if(v5[i][1]=="all"){
for(int i=0;i<cr1;i++){
if(v7[i]==to_num_cour){
vec2.push_back(v1[i][1]);
}
else{}
}
}
vec1.push_back(vec2);
vec2.clear();
}
}
ifs.close();
ofstream ofs;
ofs.open(outfilename);
for(int i=0;i<vec1.size()-1;i++){
if(vec1[i].size()==0){
ofs<<"N"<<endl;
}
else{
for(int j=0;j<vec1[i].size();j++){
ofs<<vec1[i][j]<<endl;
}
}
}
if(vec1[vec1.size()-1].size()==0){
ofs<<"N";
}
else{
for(int j=0;j<vec1[vec1.size()-1].size()-1;j++){
ofs<<vec1[vec1.size()-1][j]<<endl;
}
ofs<<vec1[vec1.size()-1][vec1[vec1.size()-1].size()-1];
}
ofs.close();
}

Anda mungkin juga menyukai