我试图创建一个包含所有字母组合(大写和小写)和一些符号的词汇表。基本上大部分是ASCII表。
我只是想知道文本文件将占用多少内存。我想我会用一些其他功能来删除不必要的单词。但是现在我祈祷我的1 TB硬盘不会爆炸。 我不知道这个方法是否是正确的方法,使用带有下标数组的嵌套for循环,也许我应该使用链表来获得更好的性能。与此同时,我可以创建删除字符多于某些字符的行的函数。
#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更快的任何帮助。
答案 0 :(得分:3)
这个程序不会在你的生命周期内完成 - 它会在很久之前填满你的硬盘。
有68个 13 = 6×10 23 可能的组合,其中包括从68个字符集中选择的13个字符。每个14字节(13个字符+一个换行符),将它们全部存储在一个文件中将需要大约9.3×10 24 字节,或 9.3万亿太字节。这比地球上存在的数据存储量大几个数量级。
你需要重新考虑你在这里要做的事情。这显然不起作用。
从实现的角度来看,有一种比使用嵌套循环更简单的方法:
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()
的实现留给读者。