>我正在尝试创建一个包含所有字母(大写和小写(和一些符号组合的单词列表。基本上大部分的ASCII表。
我只是对文本文件将占用多少内存感到好奇.我想我会使用其他一些功能来删除不必要的单词。但现在我祈祷我的 1 TB 硬盘不会爆炸。 我不知道这种方法是否正确,使用带有下标数组的嵌套 for 循环,也许我应该使用链表以获得更好的性能。同时,我可以创建函数来删除字符数超过某些 tot 的行。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PATH "wordlist.txt"
#define SIZE 68
void Write(char * word[], FILE * f, int N)
{
for(int i = 0; i <= N; i++)
{
fprintf(f,"%s", word[i]);
}
fprintf(f,"n");
}
void thirteen_Digits(char * word[],char * letters[] , FILE * f)
{
for(int i = 0; i < SIZE; i++)
{
word[0] = letters[i];
for(int j = 0; j < SIZE ;j++)
{
word[1] = letters[j];
for(int k = 0; k < SIZE; k++)
{
word[2] = letters[k];
for(int l = 0; l < SIZE;l++)
{
word[3] = letters[l];
for(int m = 0; m< SIZE; m++)
{
word[4] = letters[m];
for(int n = 0; n < SIZE; n++)
{
word[5] = letters[n];
for(int o = 0; o < SIZE; o++)
{
word[6] = letters[o];
for(int p = 0; p < SIZE; p++)
{
word[7] = letters[p];
for(int q = 0; q < SIZE; q++)
{
word[8] = letters[q];
for(int r = 0; r < SIZE; r++)
{
word[9] = letters[r];
for(int s = 0; s < SIZE;s++)
{
word[10] = letters[s];
for(int t = 0; t < SIZE; t++)
{
word[11] = letters[t];
for(int u = 0; u < SIZE; u++)
{
word[12] = letters[u];
Write(word, f, 12);
}
}
}
}
}
}
}
}
}
}
}
}
}
fclose(f);
}
void twelve_Digits(char * word[],char * letters[] , FILE * f)
{
for(int i = 0; i < SIZE; i++)
{
word[0] = letters[i];
for(int j = 0; j < SIZE ;j++)
{
word[1] = letters[j];
for(int k = 0; k < SIZE; k++)
{
word[2] = letters[k];
for(int l = 0; l < SIZE;l++)
{
word[3] = letters[l];
for(int m = 0; m< SIZE; m++)
{
word[4] = letters[m];
for(int n = 0; n < SIZE; n++)
{
word[5] = letters[n];
for(int o = 0; o < SIZE; o++)
{
word[6] = letters[o];
for(int p = 0; p < SIZE; p++)
{
word[7] = letters[p];
for(int q = 0; q < SIZE; q++)
{
word[8] = letters[q];
for(int r = 0; r < SIZE; r++)
{
word[9] = letters[r];
for(int s = 0; s < SIZE;s++)
{
word[10] = letters[s];
for(int t = 0; t < SIZE; t++)
{
word[11] = letters[t];
Write(word, f,11);
}
}
}
}
}
}
}
}
}
}
}
}
}
void eleven_Digits(char * word[],char * letters[] , FILE * f)
{
for(int i = 0; i < SIZE; i++)
{
word[0] = letters[i];
for(int j = 0; j < SIZE ;j++)
{
word[1] = letters[j];
for(int k = 0; k < SIZE; k++)
{
word[2] = letters[k];
for(int l = 0; l < SIZE;l++)
{
word[3] = letters[l];
for(int m = 0; m< SIZE; m++)
{
word[4] = letters[m];
for(int n = 0; n < SIZE; n++)
{
word[5] = letters[n];
for(int o = 0; o < SIZE; o++)
{
word[6] = letters[o];
for(int p = 0; p < SIZE; p++)
{
word[7] = letters[p];
for(int q = 0; q < SIZE; q++)
{
word[8] = letters[q];
for(int r = 0; r < SIZE; r++)
{
word[9] = letters[r];
for(int s = 0; s < SIZE;s++)
{
word[10] = letters[s];
Write(word, f,10);
}
}
}
}
}
}
}
}
}
}
}
}
void ten_Digits(char * word[],char * letters[] , FILE * f)
{
for(int i = 0; i < SIZE; i++)
{
word[0] = letters[i];
for(int j = 0; j < SIZE ;j++)
{
word[1] = letters[j];
for(int k = 0; k < SIZE; k++)
{
word[2] = letters[k];
for(int l = 0; l < SIZE;l++)
{
word[3] = letters[l];
for(int m = 0; m< SIZE; m++)
{
word[4] = letters[m];
for(int n = 0; n < SIZE; n++)
{
word[5] = letters[n];
for(int o = 0; o < SIZE; o++)
{
word[6] = letters[o];
for(int p = 0; p < SIZE; p++)
{
word[7] = letters[p];
for(int q = 0; q < SIZE; q++)
{
word[8] = letters[q];
for(int r = 0; r < SIZE; r++)
{
word[9] = letters[r];
Write(word, f,9);
}
}
}
}
}
}
}
}
}
}
}
void nine_Digits(char * word[], char * letters[] ,FILE * f)
{
for(int i = 0; i < SIZE; i++)
{
word[0] = letters[i];
for(int j = 0; j < SIZE ;j++)
{
word[1] = letters[j];
for(int k = 0; k < SIZE; k++)
{
word[2] = letters[k];
for(int l = 0; l < SIZE;l++)
{
word[3] = letters[l];
for(int m = 0; m< SIZE; m++)
{
word[4] = letters[m];
for(int n = 0; n < SIZE; n++)
{
word[5] = letters[n];
for(int o = 0; o < SIZE; o++)
{
word[6] = letters[o];
for(int p = 0; p < SIZE; p++)
{
word[7] = letters[p];
for(int q = 0; q < SIZE; q++)
{
word[8] = letters[q];
Write(word, f, 8);
}
}
}
}
}
}
}
}
}
}
void eight_Digits(char * word[],char * letters[] , FILE * f)
{
for(int i = 0; i < SIZE; i++)
{
word[0] = letters[i];
for(int j = 0; j < SIZE ;j++)
{
word[1] = letters[j];
for(int k = 0; k < SIZE; k++)
{
word[2] = letters[k];
for(int l = 0; l < SIZE;l++)
{
word[3] = letters[l];
for(int m = 0; m< SIZE; m++)
{
word[4] = letters[m];
for(int n = 0; n < SIZE; n++)
{
word[5] = letters[n];
for(int o = 0; o < SIZE; o++)
{
word[6] = letters[o];
for(int p = 0; p < SIZE; p++)
{
word[7] = letters[p];
Write(word, f, 7);
}
}
}
}
}
}
}
}
}
void seven_Digits(char * word[], char * letters[] ,FILE * f)
{
for(int i = 0; i < SIZE; i++)
{
word[0] = letters[i];
for(int j = 0; j < SIZE ;j++)
{
word[1] = letters[j];
for(int k = 0; k < SIZE; k++)
{
word[2] = letters[k];
for(int l = 0; l < SIZE;l++)
{
word[3] = letters[l];
for(int m = 0; m< SIZE; m++)
{
word[4] = letters[m];
for(int n = 0; n < SIZE; n++)
{
word[5] = letters[n];
for(int o = 0; o < SIZE; o++)
{
word[6] = letters[o];
Write(word, f, 6);
}
}
}
}
}
}
}
}
void six_Digits(char * word[],char * letters[] , FILE * f)
{
for(int i = 0; i < SIZE; i++)
{
word[0] = letters[i];
for(int j = 0; j < SIZE ;j++)
{
word[1] = letters[j];
for(int k = 0; k < SIZE; k++)
{
word[2] = letters[k];
for(int l = 0; l < SIZE;l++)
{
word[3] = letters[l];
for(int m = 0; m< SIZE; m++)
{
word[4] = letters[m];
for(int n = 0; n < SIZE; n++)
{
word[5] = letters[n];
Write(word, f, 5);
}
}
}
}
}
}
}
void five_Digits(char * word[],char * letters[] , FILE * f)
{
for(int i = 0; i < SIZE; i++)
{
word[0] = letters[i];
for(int j = 0; j < SIZE ;j++)
{
word[1] = letters[j];
for(int k = 0; k < SIZE; k++)
{
word[2] = letters[k];
for(int l = 0; l < SIZE;l++)
{
word[3] = letters[l];
for(int m = 0; m< SIZE; m++)
{
word[4] = letters[m];
Write(word, f, 4);
}
}
}
}
}
}
void four_Digits(char * word[], char * letters[] ,FILE * f)
{
for(int i = 0; i < SIZE; i++)
{
word[0] = letters[i];
for(int j = 0; j < SIZE ;j++)
{
word[1] = letters[j];
for(int k = 0; k < SIZE; k++)
{
word[2] = letters[k];
for(int l = 0; l < SIZE;l++)
{
word[3] = letters[l];
Write(word, f, 3);
}
}
}
}
}
int main()
{
FILE *f;
if(!(f=fopen(PATH, "a")))
{
perror("Errore");
exit(-1);
}
char * letters[SIZE] = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
".","_","1","2","3","4","5","6","7","8","9","0","!","@","$","§"};
char * word13[] = {"A","A","A","A","A","A","A","A","A","A","A","A","A"};
char * word12[] = {"A","A","A","A","A","A","A","A","A","A","A","A"};
char * word11[] = {"A","A","A","A","A","A","A","A","A","A","A"};
char * word10[] = {"A","A","A","A","A","A","A","A","A","A"};
char * word9[] = {"A","A","A","A","A","A","A","A","A"};
char * word8[] = {"A","A","A","A","A","A","A","A"};
char * word7[] = {"A","A","A","A","A","A","A"};
char * word6[] = {"A","A","A","A","A","A"};
char * word5[] = {"A","A","A","A","A"};
char * word4[] = {"A","A","A","A"};
four_Digits(word4,letters, f);
five_Digits(word5,letters, f);
six_Digits(word6,letters, f);
seven_Digits(word7,letters, f);
eight_Digits(word8,letters, f);
nine_Digits(word9,letters, f);
ten_Digits(word10, letters,f);
eleven_Digits(word11,letters, f);
twelve_Digits(word12, letters,f);
thirteen_Digits(word13,letters, f);
exit(EXIT_SUCCESS);
}
我将不胜感激任何帮助,使alghorithm更快。
这个程序不会在你的有生之年完成 - 它会在此之前很久就填满你的硬盘。
有 6813= 6 × 10 23 从 68 个字符集中选择的13个字符的可能组合。每个字节为 14 个字节(13 个字符 + 一个换行符(,将它们全部存储在一个文件中大约需要 9.3 × 10个 24字节,即9.3 万亿 TB。这比地球上现有的数据存储量大几个数量级。
你需要重新考虑你在这里要做什么。这显然是行不通的。
但是,从实现的角度来看,有一种比使用嵌套循环更简单的方法:
void permute(int length, int maximum_digit)
{
int *digits = calloc(length, sizeof(int));
do {
print_digits(digits, length);
} while (increment(digits, length, maximum_digit));
free(digits);
}
此函数创建一个可变位数的数组(由length
设置(,并在每一步"递增"该数组,直到达到最大值 (maximum_digit
(。
我们可以通过单步执行数组来实现该增量;对于每个数字,我们递增它并在它低于其最大值时停止,或者将其归零并继续,否则继续:
int increment(int *digits, int length, int maximum_digit)
{
for (int i = 0; i < length; i++) {
if (digits[i] + 1 == maximum_digit) {
digits[i] = 0;
} else {
digits[i]++;
return 1;
}
}
return 0;
}
print_digits()
的实现留给读者。