94 to the power of n combinations of n long string of char
Dec 25, 2016 at 12:33pm UTC
I'm sorry for this title, but i have no clue how should i name it.
So as input, i set value of variable n, and this program should write to text file different combinations of string of char of size n ( char value 33-126 ASCII table).
For example for n=3:
aaa
aab
aac
...
~~|
~~}
~~~
I wrote two different programs but non of them works correctly. Could u give me some hints/advice?
Sorry for my bad english.
1.Program:
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
#include <iostream>
#include <fstream>
#include <cmath>
#include <stdio.h>
using namespace std;
char znak(int a)
{
char c;
return c=33+(a%94);
}
void generator(string *tab,long long int rozmiar, int n)
{
int a=0;
if (n>1)
{
for (int i=0;i<rozmiar;i++)
{
a=i/94;
tab[i]+=tab[(i+a)%rozmiar];
}
generator(tab,rozmiar,n-1);
}
}
int main()
{
ofstream ciag;
string *tab;
long long int rozmiar=1;
int n=3;
for (int z=0;z<n;z++)
rozmiar=rozmiar*94;
tab= new string [rozmiar];
for (int i=0;i<rozmiar;i++)
tab[i]+=znak(i);
generator(tab,rozmiar,n);
ciag.open("ciag.txt" , ios::in | ios::out );
for (int i=0;i<rozmiar;i++)
ciag << tab[i] << endl;
ciag.close();
delete [] tab;
return 0;
}
2. Program
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
#include <iostream>
#include <fstream>
#include <cmath>
#include <stdio.h>
using namespace std;
int main()
{
ofstream ciag;
int n,i,j,m,y;
long long int rozmiar=1;
int k=0;
string * tab;
char tempc=33;
cout<<"Type n" <<endl;
cin>>n;
for (int z=0;z<n;z++)
rozmiar=rozmiar*94;
tab = new string [rozmiar];
//cout<<rozmiar<<endl;
rozmiar=1;
for (j=1;j<n+1;j++)
{
y=0;
rozmiar=rozmiar*94;
for (int z=0;z<j;z++)
{
for (i=k;i<rozmiar;i++)
{
if (tempc>=127)
{
tempc=33+y;
y++;
}
tab[i]+=tempc;
tempc++;
//cout<<tab[i]<<endl;
}
//cout<<tempc<<endl;
}
k=k+i;
}
ciag.open("hasla.txt" , ios::in | ios::out );
for (i=0;i<rozmiar;i++)
ciag << tab[i] << endl;
ciag.close();
delete [] tab;
return 0;
}
Dec 25, 2016 at 1:59pm UTC
1 2 3 4 5 6
void write_all_strings( std::ostream& stm, std::size_t n,
int minv = 33, int maxv = 126, std::string prefix = {} )
{
if ( n == 0 ) stm << prefix << '\n' ;
else for ( int c = minv ; c <= maxv ; ++c ) write_all_strings( stm, n-1, minv, maxv, prefix + char (c) ) ;
}
http://rextester.com/DFZ32644
Topic archived. No new replies allowed.