Skip to content

Instantly share code, notes, and snippets.

Created December 5, 2014 02:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/41cafb1a0b3cb2e86db2 to your computer and use it in GitHub Desktop.
Save anonymous/41cafb1a0b3cb2e86db2 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
void printVector (const vector<string>& theVect, vector<bool>& bArray, int nItems){
for (int i = 0; i < nItems; ++i)
if (bArray[i] == true)
cout << theVect[i] << " ";
outFile << theVect[i];
cout << "\n";
outFile << "\n";
}
void nCombination(const vector<string> &Vect,int n, int r){
vector<bool> myBits(n, false); // everything is false now
int count = 1;
for (size_t i = n-1; i >= 0 && count <= r; --i, ++count){
myBits[i] = true;
}
do // start combination generator
{
printVector(Vect, myBits, n );
} while (next_permutation(myBits.begin(), myBits.end()));; // change the bit pattern
}
void nPermutation(vector<string> o, int r){
do {
for(int count = 0; count < r; count++){
cout << o[count] << " " ;
outFile << o[count] << " ";
}
cout<< endl;
outFile << endl;
}
while (next_permutation(o.begin(), o.end()));
}
int main(int argc, char** argv) {
int numofOps;
char Op;
int n;
int r;
string line;
ifstream myFile("infile.dat");
myFile >> numofOps;
myFile.ignore(1,'\n');
ofstream outFile;
outFile.open ("example.txt");
for(int q = 0; q < numofOps; ++q){
myFile.get(Op);
myFile >> n;
myFile>> r;
vector<string> original(n);
for(int i = 0;i <= n - 1; i++){
myFile.ignore(1,'\n');
myFile >> original[i];}
myFile.ignore(1,'\n');
sort(original.begin(), original.end());
cout<< '\n'<< endl;
if(Op == 'P'){
nPermutation(original, r);
}
else{
if(Op == 'C')
nCombination(original,n,r);
else
cout << "Incorrect Input" << endl;
}
original.clear();
}
outFile.close();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment