Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <list>
- #include <fstream>
- #include <vector>
- bool alreadyExists(std::list<std::string> &x, const std::string &n);
- int main(int argv, char* argc[]) {
- std::ifstream names, numbers;
- std::ofstream results;
- if (argv != 4) {
- std::cout << "Usage: names numbers results.\n";
- return 1;
- }
- names.open(argc[1]);
- if (!names) {
- std::cout << "Could not open names file. Try again\n";
- return 1;
- }
- numbers.open(argc[2]);
- if (!numbers) {
- std::cout << "Could not open numbers file. Try again.\n";
- return 1;
- }
- results.open(argc[3]);
- if (!results) {
- std::cout << "Could not open results file. Try again\n";
- return 1;
- }
- std::string tempName;
- int tempNumber;
- std::list<std::string> masterList, nameList;
- std::vector<int> numberList;
- std::vector<std::string> dead;
- while (!names.eof()) {
- names >> tempName;
- if (!alreadyExists(masterList,tempName))
- masterList.push_back(tempName);
- else
- results << "Duplicate name: " << tempName << std::endl;
- }
- while (!numbers.eof()) {
- numbers >> tempNumber;
- numberList.push_back(tempNumber);
- }
- masterList.pop_back();
- numberList.pop_back();
- for (unsigned int i = 0; i < numberList.size(); i++) {
- tempNumber = 1;
- results << "==========\n";
- results << "m = " << numberList[i] << std::endl;
- results << "...\n";
- results << "std::list\n";
- nameList = masterList;
- std::list<std::string>::iterator p = nameList.begin();
- while ( nameList.size() > 1) {
- if (tempNumber % numberList[i] == 0) {
- dead.push_back(*p);
- p = nameList.erase(p);
- }
- ++p;
- tempNumber++;
- if (p == nameList.end())
- p = nameList.begin();
- }
- results << "Survivor: " << nameList.front() << std::endl;
- results << "Removed:\n";
- for (unsigned int j = 0; j < dead.size(); j++)
- results << " " << j+1 << ": " << dead[j] << std::endl;
- }
- return 0;
- }
- bool alreadyExists(std::list<std::string> &x,const std::string &n) {
- for (std::list<std::string>::iterator i = x.begin(); i != x.end(); ++i)
- if (*i == n)
- return true;
- return false;
- }
Add Comment
Please, Sign In to add comment