For lab I have to do this: You will create a C++ program to find all unique permutation of a given string. Your program will read an input file contain a single string and output the all the possible unique permutation of that string using recursion method. The purpose of this lab is for you to practice recursion and string manipulation.
a. Input file The input file will contain a single string. An input can be empty, in this case your output should be “no permutation”. There will be no spaces, numbers, or special characters in the string. b. Output file Output should contain all the unique permutation of the given string Output needs to be in alphabetical order Examples:
input1.txt
mn
output1.txt
mn
nm
input2.txt
abc
output2.txt
abc
acb
bac
bca
cab
cba
input3.txt (empty)
output3.txt
no permutation
It includes an argumentmanager.h that has special commands that I need for the code to run on the server which I already have included in my code, but when I run it, it will read input1.txt and it will output the correct answer to output1.txt, but when I try running input2.txt the output file comes out blank and I have no idea why.
Code:
#include<iostream>
#include<string>
#include<fstream>
#include "ArgumentManager.h"
using namespace std;
void permute(string a, int l, int r);
ofstream output("output");
int main(int argc, char* argv[])
{
ArgumentManager am(argc, argv);
ifstream input;
ofstream output;
string infileName = am.get("input");
string outfileName = am.get("output");
input.open(infileName);
output.open(outfileName);
ifstream infile("input");
string str;
infile >> str;
int n = str.size();
permute(str, 0, n - 1);
system("pause");
return 1;
}
void permute(string a, int l, int r)
{
if (l == r)
output << a << endl;
else
{
for (int i = l; i <= r; i++)
{
char c = a[l];
a[l] = a[i];
a[i] = c;
permute(a, l + 1, r);
c = a[l];
a[l] = a[i];
a[i] = c;
}
}
}
Aucun commentaire:
Enregistrer un commentaire