用C语言填充多维数组



我正在尝试填充一个多维数组,以匹配另一个数组的结构。

我有一个数组按如下方式初始化:

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] != '' )
   {
      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 %fn", 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]) != ''; i++)
  {
    if (col == 0)
    {
      fputs(pad, fp);
      col += strlen(pad);
      pad = pad2;
    }
    col++;
    c = toupper(c);
    if (c < MAX_CLEF && clef[c][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] != '' )
   {
      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 %fn", 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到'Z'的ASCII值,而不是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))

但是,浮点运算不是精确的,所以你需要检查它是否"close"

if (fabs(frequencyEn[c] - ((double)count[c] / accum)) < 0.00001)

相关内容

  • 没有找到相关文章

最新更新