So I have this problem:
Input file: The first line of input will contain the list of books in queue 1. The second line will contain the list of books in queue 2. Each book will be separated by a comma. Book name can have spaces in between the string. There will be no duplicate book name. No empty input will be given.
Output file: The first line of output should display the total time. The second line should display the order of book after organizing, with each book separated by a comma.
If the input is:
chemistry,compsci A,bio A,english B
compsci B,compsci D,compsci C
The output should be:
170
compsci B,compsci A,compsci D,compsci C,chemistry,bio A,english B
But when I run my code, the output I get is this:
170
compsci A,compsci B,compsci D,compsci C,chemistry,bio A,english B
I think I am supposed to check queue 1 and 2 in sequence, queue1.front then queue2.front then back to queue1.front then queue2.front, but the way my code is its running queue1 first then moving on to queue2 which is why its putting it in the wrong order, but I am not sure how to rewrite the loops so it checks them in sequence. Please help as I am completely lost on how to do this.
Code:
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
|
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <vector>
#include <queue>
#include "ArgumentManager.h"
using namespace std;
queue<string> splitStringUsingComma(string text)
{
queue<string> sol;
stringstream ss(text);
while(ss.good())
{
string x;
getline(ss,x,',');
sol.push(x);
}
return sol;
}
int main(int argc, char* argv[])
{
ArgumentManager am(argc, argv);
ifstream input;
string infileName = am.get("input");
string outfileName = am.get("output");
input.open(infileName);
string text;
queue<string> queue1,queue2;
getline(input,text);
queue1 = splitStringUsingComma(text);
text.clear();
getline(input,text);
queue2 = splitStringUsingComma(text);
queue<string> queue3;
int cost{0};
vector<string> sol;
while(!queue1.empty())
{
if(queue1.front().find("compsci")!=string::npos)
{
sol.push_back(queue1.front());
cost+=20;
}
else
{
queue3.push(queue1.front());
cost+=10;
}
queue1.pop();
}
while(!queue2.empty())
{
if(queue2.front().find("compsci")!=string::npos)
{
sol.push_back(queue2.front());
cost+=20;
}
else
{
queue3.push(queue2.front());
cost+=10;
}
queue2.pop();
}
while(!queue3.empty())
{
sol.push_back(queue3.front());
queue3.pop();
cost+=20;
}
ofstream output;
output.open(outfileName);
output<<cost<<'\n';
for(int i = 0; i < sol.size() -1; ++i)
{
output << sol.at(i)<<",";
}
output << sol.back();
output<<endl;
input.close();
output.close();
return 0;
}
|