我正在尝试填充与另一个数组的结构匹配的多维数组。
我通过以下方式启动了一个阵列:
static char frequencyEn[][7] =
{
['A'] = ".082",
['B'] = ".015",
['C'] = ".028",
['D'] = ".043",
['E'] = ".127",
['F'] = ".022",
['G'] = ".020",
['H'] = ".061",
['I'] = ".070",
['J'] = ".002",
['K'] = ".008",
['L'] = ".040",
['M'] = ".024",
['N'] = ".067",
['O'] = ".075",
['P'] = ".019",
['Q'] = ".001",
['R'] = ".060",
['S'] = ".063",
['T'] = ".091",
['U'] = ".028",
['V'] = ".010",
['W'] = ".024",
['X'] = ".002",
['Y'] = ".020",
['Z'] = ".0O1"
};
此数组包含英语平均字母频率的值。我试图初始化和填充的第二个数组将保存所提供文本的实际字母频率。
最终目标是比较两个不同数组的值。我还没有处理将比较两个数组的代码 - 只有我用计数值填充第二个数组的部分。
以这种方式计算字符串的字母频率。如何初始化新阵列并使用这些字母频率值填充它?我期待创建一个与前面提到的数组结构相同的数组:
char *count(char *eText)
{
char * cryptText = "Some encrypted text.";
char string[100];
int c = 0, count[26] = {0};
int accum = 0;
while ( cryptText[c] != '\0' )
{
if ( cryptText[c] >= 'a' && cryptText[c] <= 'z' ){
count[cryptText[c]-'a']++;
accum++;
}
else if (cryptText[c] >= 'A' && cryptText[c] <= 'Z'){
count[cryptText[c]-'A']++;
accum++;
}
c++;
}
for ( c = 0 ; c < 26 ; c++ )
{
if( count[c] != 0 )
printf( "%c %f\n", c +'a', ((double)count[c])/accum);
}
}
我正在试图弄清楚如何使用与第一个数组相同的结构填充新数组。目前,代码给了我这种类型的打印输出:
b 0.103448
c 0.034483
d 0.034483
e 0.034483
g 0.034483
i 0.068966
j 0.068966
k 0.068966
l 0.034483
n 0.034483
p 0.034483
q 0.137931
r 0.103448
s 0.034483
u 0.034483
v 0.034483
x 0.034483
z 0.068966
如何以这种方式将这些值添加到数组中:
char frequencyCurrent[][]=
{
['A'] = ".000",
['B'] = ".103"
...
}
如果在上下文中查看内容很有帮助,可以使用更多代码来说明如何使用初始化数组:
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char * cryptText = 0;
static char clef[][7] =
{
['A'] = "X",
['B'] = "Y",
['C'] = "Z",
['D'] = "A",
['E'] = "B",
['F'] = "C",
['G'] = "D",
['H'] = "E",
['I'] = "F",
['J'] = "G",
['K'] = "H",
['L'] = "I",
['M'] = "J",
['N'] = "K",
['O'] = "L",
['P'] = "M",
['Q'] = "N",
['R'] = "O",
['S'] = "P",
['T'] = "Q",
['U'] = "R",
['V'] = "S",
['W'] = "T",
['X'] = "U",
['Y'] = "V",
['Z'] = "W"
};
static char frequencyEn[][7] =
{
['A'] = ".082",
['B'] = ".015",
['C'] = ".028",
['D'] = ".043",
['E'] = ".127",
['F'] = ".022",
['G'] = ".020",
['H'] = ".061",
['I'] = ".070",
['J'] = ".002",
['K'] = ".008",
['L'] = ".040",
['M'] = ".024",
['N'] = ".067",
['O'] = ".075",
['P'] = ".019",
['Q'] = ".001",
['R'] = ".060",
['S'] = ".063",
['T'] = ".091",
['U'] = ".028",
['V'] = ".010",
['W'] = ".024",
['X'] = ".002",
['Y'] = ".020",
['Z'] = ".0O1"
};
enum { MAX_CLEF = sizeof(clef) / sizeof(clef[0]) };
static char *prompt(FILE *fp, const char *prompt, char *buffer, size_t buflen)
{
printf("%s", prompt);
fflush(0);
return fgets(buffer, buflen, fp);
}
static void substitute(FILE *fp, const char *buffer, const char *pad1, const char *pad2)
{
int c;
const char *pad = pad1;
int col = 0;
for (int i = 0; (c = buffer[i]) != '\0'; i++)
{
if (col == 0)
{
fputs(pad, fp);
col += strlen(pad);
pad = pad2;
}
col++;
c = toupper(c);
if (c < MAX_CLEF && clef[c][0] != '\0')
{
fputs(clef[c], fp);
col += strlen(clef[c]);
}
else
{
putc(c, fp);
col++;
}
if (col > 72)
{
putc('\n', fp);
col = 0;
}
}
}
char *count(char *eText)
{
char string[100];
int c = 0, count[26] = {0};
int accum = 0;
char *frequencies[2]
while ( cryptText[c] != '\0' )
{
if ( cryptText[c] >= 'a' && cryptText[c] <= 'z' ){
count[cryptText[c]-'a']++;
accum++;
}
else if (cryptText[c] >= 'A' && cryptText[c] <= 'Z'){
count[cryptText[c]-'A']++;
accum++;
}
c++;
}
for ( c = 0 ; c < 26 ; c++ )
{
if( count[c] != 0 )
printf( "%c %f\n", c +'a', ((double)count[c])/accum);
}
}
int main(void)
{
char * buffer = 0;
long length;
FILE * plainTextFile = fopen ("plaintext.txt", "rb");
FILE * cipherTextFile = fopen("ciphertext.txt", "w+");
if (plainTextFile)
{
fseek (plainTextFile, 0, SEEK_END);
length = ftell (plainTextFile);
fseek (plainTextFile, 0, SEEK_SET);
buffer = malloc (length);
if (buffer)
{
fread (buffer, 1, length, plainTextFile);
}
fclose (plainTextFile);
}
if (buffer)
{
printf("%s", buffer);
}
else {
printf("failure");
}
substitute(cipherTextFile, buffer, "", " ");
if (cipherTextFile)
{
fseek (cipherTextFile, 0, SEEK_END);
length = ftell (cipherTextFile);
fseek (cipherTextFile, 0, SEEK_SET);
cryptText = malloc (length);
if (cryptText)
{
fread (cryptText, 1, length, cipherTextFile);
}
fclose (cipherTextFile);
}
if (cryptText)
{
printf("%s", cryptText);
}
else {
printf("failure");
}
}
答案 0 :(得分:0)
您拥有的预定义数组不是很有用。将浮点值存储为字符串,并且数组的索引从0到ASCII值“Z”而不是0到25。
相反,请像这样定义frequencyEn
:
double frequencyEn[] = {
.082, .015, .028, .043, .127, .022,
.020, .061, .070, .002, .008, .040,
.024, .067, .075, .019, .001, .060,
.063, .091, .028, .010, .024, .002,
.020, .001 };
然后您可以按如下方式进行比较:
if (frequencyEn[c] == ((double)count[c] / accum))
但是,浮点数学并不准确,所以你需要检查它是否“关闭”
if (fabs(frequencyEn[c] - ((double)count[c] / accum)) < 0.00001)