『代码』··By/蜜汁炒酸奶

凯撒密码

/*
功能:凯撒密码
日期:2013-06-18
*/
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
void countFrequency(char *ch,int *frequency);
int main(void)
{
	char Ch[10000];
	int Frequency[26]={0};

	printf("请输入密文:n");
	gets(Ch);

	countFrequency(&Ch[0],&Frequency[0]);

	printf("n");
    system("pause");
	return 0;
}

/*************************************************************************
函数名:countFrequency
功能:统计英文字符出现的频率
参数:char *ch 要统计字符
      int *frequency 存放每个字符的出现频率的数组
返回值:无
*************************************************************************/

void countFrequency(char *ch,int *frequency)
{
	int i,j,len,num,max;
	len = strlen(ch);

 //计算并存放26个字母出现的频率
	for (i = 0 ; i < 26 ;i++)
	{
		num = 0;
		for (j=0;j<len;j++)
		{
			if (*(ch+j) == 65+i || *(ch+j)== 97+i)
			{
				num++;
			}
		}
		*(frequency+i) = num;
	}
//寻找出现频率最大字母
	max = frequency[0];

	for (i = 0 ; i < 26 ; i++)
	{
		if (max < frequency[i])
		{
			j=i;
			max = frequency[i];
		}
	}
//得出位移位数
	num=97+j-101;
//字母移动
	for (i = 0 ;i < len ; i++ )
	{
		if(ch[i] >= 97 && ch[i] <= 99 || ch[i] >= 65 && ch[i] <= 67)
		{
			ch[i] = ch[i] + 26 - num ;
		}
		else if((ch[i] >=68 && ch[i]<=90)||(ch[i]>=100 && ch[i] <= 122))
		{

			ch[i] = ch[i] - num;

		}
	}
	printf("解密后为:n");
	puts(ch);

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

预览
Loading comments...
0 条评论

暂无数据

example
预览