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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
std::string make_string(unsigned int value, std::size_t nchars, char a, char b)
{
std::string result;
for (; result.size() < nchars; value /= 2) result += value & 1U ? a : b;
if (result.at(0) == '0')
{
result.at(0) = 'p';
}
else
{
result.at(0) = 'p';
}
if (result.at(1) == '0')
{
result.at(1) = '5';
}
else
{
result.at(1) = 'M';
}
if (result.at(2) == '0')
{
result.at(2) = '8';
}
else
{
result.at(2) = 'x';
}
if (result.at(3) == '0')
{
result.at(3) = '3';
}
else
{
result.at(3) = 'J';
}
if (result.at(4) == '0')
{
result.at(4) = 'N';
}
else
{
result.at(4) = 'Q';
}
if (result.at(5) == '0')
{
result.at(5) = 'e';
}
else
{
result.at(5) = '9';
}
if (result.at(6) == '0')
{
result.at(6) = 'o';
}
else
{
result.at(6) = '7';
}
if (result.at(7) == '0')
{
result.at(7) = 'I';
}
else
{
result.at(7) = 'm';
}
return result; // note: the bits are in reverse
}
void print_strings(std::size_t nchars, char a, char b)
{
std::ofstream myfile("example.txt");
myfile.open("example.txt");
const unsigned int ub = 1U << nchars;
for (unsigned int n = 0; n < ub; ++n)
{
myfile << std::setw(3) << make_string(n, nchars, a, b) << '\n';
}
}
int print_strings_alt(std::size_t nchars, char a, char b, std::string str = {}, int n = 0)
{
if (nchars == 0)
{
if (str.at(0) == '0')
{
str.at(0) = 'p';
}
else
{
str.at(0) = 'p';
}
if (str.at(1) == '0')
{
str.at(1) = '5';
}
else
{
str.at(1) = 'M';
}
if (str.at(2) == '0')
{
str.at(2) = '8';
}
else
{
str.at(2) = 'x';
}
if (str.at(3) == '0')
{
str.at(3) = '3';
}
else
{
str.at(3) = 'J';
}
if (str.at(4) == '0')
{
str.at(4) = 'N';
}
else
{
str.at(4) = 'Q';
}
if (str.at(5) == '0')
{
str.at(5) = 'e';
}
else
{
str.at(5) = '9';
}
if (str.at(6) == '0')
{
str.at(6) = 'o';
}
else
{
str.at(6) = '7';
}
if (str.at(7) == '0')
{
str.at(7) = 'I';
}
else
{
str.at(7) = 'm';
}
std::ofstream myfile("example.txt");
myfile.open("example.txt");
myfile << std::setw(3) << str << '\n';
return n + 1;
}
else
{
const int nxt = print_strings_alt(nchars - 1, a, b, str + b, n);
return print_strings_alt(nchars - 1, a, b, str + a, nxt);
}
}
int main()
{
std::ofstream myfile("example.txt");
myfile.open("example.txt");
print_strings(8, '0', '1');
std::cout << "-------------------\n";
print_strings_alt(8, '0', '1');
myfile.close();
}
|