別の配列の構造に一致する多次元配列を作成しようとしています。多次元配列をC言語で実装する
私は次のように1列にinitilizedています
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"
};
この配列は英語で平均手紙周波数の値を保持します。 initilizeとpopulateをしようとしている2番目の配列は、指定されたテキストの実際の文字周波数を保持します。
最終的な目標は、2つの異なる配列の値を比較することです。私はまだ2つの配列を比較するコードに取り組んでいません - カウントされた値で2番目の配列を配置する部分だけです。
このように文字列の文字数がカウントされます。どのように新しい配列を初期化し、これらの文字の頻度値を入力できますか?私が以前に提示配列と同じ構造を持つ配列を作成するために探しています:
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");
}
}
どのような配列を作成しますか?この質問は不明です。何をしたいですか? –
'frequencyEn'配列は本当に奇妙に見えます。あなたはそれで解決しようとしている問題は何ですか?表示された次のコードでは使用されません。 – 5gon12eder
@ 5gon12eder私はそれが奇妙で、それを改善するために何ができるかを知ることに興味があります。私はC言語を初めて使っています。編集を参照してください。 – user25976