?? virginia.c
字號:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define max 2048
int key[1024];
char * encode (int * string, const int count,const int n)
{
int i,j;
char ch, * encode_string = (char *)malloc(count);
printf("The ciphertexts are :\n");
for (i = 0,j = 0;i < count;i++)
{
if ((string[i] + 'a') == ' ')
{
putchar(' ');
encode_string[i] = ' ';
}
else
{
if (j >= n)
j %= n;
string[i] = (string[i] + key[j++]) % 26;
ch = string[i] + 'a';
encode_string[i] = ch;
putchar(ch);
}
}
putchar('\n');
return encode_string;
}
char * decode(int * string, const int count,const int n)
{
int i,j;
char ch,*decode_string = (char *)malloc(count);
printf("\nThe plaintexts are :\n");
for (i = 0,j = 0;i < count;i++)
{
if ((string[i] + 'a') == ' ')
{
decode_string[i] = ' ';
putchar(' ');
}
else
{
if (j >= n)
j %= n;
string[i] = (string[i] - key[j++] + 26) % 26;
ch = string[i] + 'a';
decode_string[i] = ch;
putchar(ch);
}
}
putchar('\n');
return decode_string;
}
void count_frequency(const int count,const char * string)
{
int i, count_char = count;
float fre[26],sum = 0, temp;
for (i = 0;i < 26;i++)
fre[i] = 0;
for (i = 0;i < count;i++)
{
if (string[i] == ' ')
count_char--;
else
fre[string[i] - 'a']++;
}
for (i = 0;i < 26;i++)
if (fre[i] != 0)
{
printf("%c = %.3f\n",i + 'a',temp = fre[i] / count_char);
sum += temp*temp;
}
printf("The index of coinindence is %.3f.\n",sum);
}
int main()
{
int n;
char ch, * encode_string, *decode_string,quit;
int string_int[max],i,count;
FILE * fp=fopen("in.txt","r");
if (fp == NULL)
{
fprintf(stdout,"Can't open the file!");
exit(1);
}
do
{
printf("Plesse input the number n:");
scanf("%d",&n);
for (i = 0;i < n;i++)
key[i] = rand() % 26;
i = 0;
rewind(fp);
while ((ch = fgetc(fp)) != EOF)//會有空格
{
if (ch == ' ' ||ch == '\n'||ch == '\r')
ch = ' ';
ch = tolower(ch);
string_int[i++] = ch - 'a';
}
count = i;
encode_string = (char *)malloc(count);
decode_string = (char *)malloc(count);
encode_string = encode(string_int,count,n);
count_frequency(count,encode_string);
decode_string = decode(string_int,count,n);
count_frequency(count,decode_string);
free(encode_string);
free(decode_string);
printf("Do you want to quit?(Enter the \'q\' to quit.\'n\' to continue.)\n");
for (;(quit = getchar()) == '\r' || quit == '\n' || quit == ' ';);
}
while ( quit != 'q');
fclose(fp);
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -