Guest User

Untitled

a guest
Jul 16th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <list>
  4. #include <fstream>
  5. #include <vector>
  6.  
  7. bool alreadyExists(std::list<std::string> &x, const std::string &n);
  8.  
  9.  
  10. int main(int argv, char* argc[]) {
  11.    
  12.  
  13.     std::ifstream names, numbers;
  14.     std::ofstream results;
  15.  
  16.  
  17.     if (argv != 4) {
  18.         std::cout << "Usage: names numbers results.\n";
  19.         return 1;
  20.     }
  21.  
  22.     names.open(argc[1]);
  23.     if (!names) {
  24.         std::cout << "Could not open names file. Try again\n";
  25.         return 1;
  26.     }
  27.  
  28.     numbers.open(argc[2]);
  29.     if (!numbers) {
  30.         std::cout << "Could not open numbers file. Try again.\n";
  31.         return 1;
  32.     }
  33.  
  34.     results.open(argc[3]);
  35.     if (!results) {
  36.         std::cout << "Could not open results file. Try again\n";
  37.         return 1;
  38.     }
  39.  
  40.     std::string tempName;
  41.     int tempNumber;
  42.     std::list<std::string> masterList, nameList;
  43.     std::vector<int> numberList;
  44.     std::vector<std::string> dead;
  45.    
  46.    
  47.     while (!names.eof()) {
  48.         names >> tempName;
  49.         if (!alreadyExists(masterList,tempName))
  50.             masterList.push_back(tempName);
  51.         else
  52.             results << "Duplicate name: " << tempName << std::endl;
  53.     }
  54.     while (!numbers.eof()) {
  55.         numbers >> tempNumber;
  56.         numberList.push_back(tempNumber);
  57.     }
  58.     masterList.pop_back();
  59.     numberList.pop_back();
  60.     for (unsigned int i = 0; i < numberList.size(); i++) {
  61.         tempNumber = 1;
  62.         results << "==========\n";
  63.         results << "m = " << numberList[i] << std::endl;
  64.         results << "...\n";
  65.         results << "std::list\n";
  66.        
  67.         nameList = masterList;
  68.         std::list<std::string>::iterator p = nameList.begin();
  69.  
  70.         while ( nameList.size() > 1) {
  71.             if (tempNumber % numberList[i] == 0) {
  72.                 dead.push_back(*p);
  73.                 p = nameList.erase(p);
  74.             }
  75.             ++p;
  76.             tempNumber++;
  77.             if (p == nameList.end())
  78.                 p = nameList.begin();
  79.         }
  80.         results << "Survivor:    " << nameList.front() << std::endl;
  81.         results << "Removed:\n";
  82.         for (unsigned int j = 0; j < dead.size(); j++)
  83.             results << "  " << j+1 << ": " << dead[j] << std::endl;
  84.     }
  85.  
  86.  
  87.     return 0;
  88. }
  89.  
  90. bool alreadyExists(std::list<std::string> &x,const std::string &n) {
  91.     for (std::list<std::string>::iterator i = x.begin(); i != x.end(); ++i)
  92.         if (*i == n)
  93.             return true;
  94.     return false;
  95. }
Add Comment
Please, Sign In to add comment