Mapping Cipher

字母表映射加密算法实现
题目: 输入字母表中所映射的密文, 再将你输入的原文按照对应的密文所转换后输出。(加密)

        Mapping Cipher
You are given an alphabetic mapping table to create cipher text. The table is defined
by a string using 26 lower-case alphabets, such as
incpfvztsmbexowhlgkqjyrdau
In the string all characters are different. Using this table to generate cipher text, it
performs the following mapping scheme:
a -> i, A->I
b -> n, B->N
c -> c, C->C
d -> p, D->P
. . . . . . .
y -> a, Y->A
z -> u, Z->U
Any non-alphabetic character are kept unchanged.
Apply the scheme to plain text:
There are 2 dogs and 3 cats.
It rains dogs & cats!
Obtain the following cipher text:
Qtfgf igf 2 pwzk iop 3 ciqk.
Sq gisok pwzk & ciqk!

Input
There are several test cases. Each test case starts with a line of mapping string
described above. Following are plain text enclosed by two lines of string "***".
Your task is to output the cypher text encrypted using the given mapping string. There
is a blank line after each case. Finally, you will reach a line of string "###" which
signals the end of test. Each line in the test case is no more than 1000 characters. Each
line in test cases is no more than 1000 characters.

Output
The cipher text of each test case is prefixed with a cipher text number as shown in the
sample output. Place a blank line between cases.

Sample Input
incpfvztsmbexowhlgkqjyrdau
***
There are 2 dogs and 3 cats.
It rains dogs & cats!
***
xowhlgkqjyrdauincpfvztsmbe
***
There are 2 dogs and 3 cats.
It rains dogs & cats!
***
###

Sample Output
Cypher 1:
Qtfgf igf 2 pwzk iop 3 ciqk.
Sq gisok pwzk & ciqk!
Cypher 2:
Vqlpl xpl 2 hikf xuh 3 wxvf.
Jv pxjuf hikf & wxvf!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
	int flag=1, test=1;
	
	while(1)
	{
		char arr[26]={0}; //存取字母映射的密文
		char z[10]={0}; //存取密文后的***
		char str[1001]={0}; //存取输入的原文
		
		gets(arr);
		if(arr[0]=='#' && arr[1]=='#' && arr[2]=='#') break;
		
		gets(z);
		
		if(flag) flag=0;
		else printf("\n");
		
		printf("Cypher %d:\n",test++);
		
		while(gets(str))
		{
			if(str[0]=='*' && str[1]=='*' && str[2]=='*') break;
			
			for(int i=0; i<strlen(str); i++)
			{
				if(str[i]>='a' && str[i]<='z') printf("%c",arr[str[i]-'a']);
				else if(str[i]>='A' && str[i]<='Z') printf("%c",arr[str[i]-'A']+'A'-'a');
				else printf("%c",str[i]);
			}
			
			printf("\n");
		}
		
		scanf("\n");
			
	}
    
    return 0;
}



将一下python代码进行逐句分析,并详细说出为什么这么做# 标准英文频率 std_freq = [ 8.167, 1.492, 2.782, 4.253, 12.702, 2.228, 2.015, 6.966, 7.507, 0.153, 0.747, 4.025, 2.406, 6.749, 7.507, 1.929, 0.095, 5.987, 6.327, 9.056, 2.758, 0.978, 2.360, 0.150, 1.974, 0.074 ] # 将每个字母出现的频率进行,标准频率排名 std_rank = [4, 19, 0, 8, 14, 2, 18, 3, 5, 13, 11, 1, 15, 6, 17, 10, 20, 7, 9, 24, 22, 21, 25, 12, 16, 23] #密文中出现的字母及频率 def analyze_cipher(cipher): count = [0] * 26 total = 0 for char in cipher: if char.isalpha(): c = char.upper() count[ord(c) - ord('A')] += 1 total += 1 freq = [count[i] * 100.0 / total if total else 0 for i in range(26)] return count, freq #密文中出现字母频率的排名 def get_rank(freq): rank = [0] * 26 for i in range(26): for j in range(26): if freq[j] > freq[i]: rank[i] += 1 return rank #初始映射表 def init_mapping(cipher_rank): mapping = [None] * 26 used = [False] * 26 # 优先匹配高频字母(前6位:E,T,A,O,I,N) for i in range(6): for c in range(26): if cipher_rank[c] == i: for p in range(26): if std_rank[p] == i and not used[p]: mapping[c] = chr(ord('A') + p) used[p] = True break break # 填充剩余字母 for c in range(26): if mapping[c] is None: for p in range(26): if not used[p]: mapping[c] = chr(ord('A') + p) used[p] = True break return mapping #使用映射表进行解密 def decrypt(cipher, mapping): plain = [] for char in cipher: if char.isalpha(): c = char.upper() new_char = mapping[ord(c) - ord('A')] plain.append(new_char.lower() if char.islower() else new_char) else: plain.append(char) return ''.join(plain) #打印密文的频率和标准英文的频率 def print_analysis(count, freq, cipher_rank): print("\n===== 密文频率分析 =====") print("字母 | 出现次数 | 频率(%) | 排名") print("-------------------------") for i in range(26): print(f" {chr(ord('A') + i)} | {count[i]:5d} | {freq[i]:5.2f} | {cipher_rank[i]:2d}") print("\n===== 标准英文频率排名 =====") print("高频字母(前6位): E(12.7%) > T(9.1%) > A(8.2%) > O(7.5%) > I(7.0%) > N(6.7%)") #主函数 cipher = input("请输入密文: ") count, freq = analyze_cipher(cipher) cipher_rank = get_rank(freq) print_analysis(count, freq, cipher_rank) mapping = init_mapping(cipher_rank) plain = decrypt(cipher, mapping) while True: print(f"\n当前解密结果\n{plain}") cmd = input("\n映射表调整 (格式: 密文 明文,如 'X E';输入q退出): ") if cmd.lower() == 'q': break if len(cmd) >= 3 and cmd[0].isalpha() and cmd[2].isalpha(): c = ord(cmd[0].upper()) - ord('A') p = ord(cmd[2].upper()) - ord('A') # 处理冲突 for i in range(26): if mapping[i] == chr(ord('A') + p): for j in range(26): if chr(ord('A') + j) not in mapping: mapping[i] = chr(ord('A') + j) break break mapping[c] = chr(ord('A') + p) plain = decrypt(cipher, mapping) else: print("输入格式错误,请重试!") print(f"\n最终解密结果\n{plain}")
10-16
分析修改一下以下python代码并给出修改后的完整代码#include <stdio.h> #include <string.h> #include <ctype.h> #include <math.h> const float std_freq[26] = { # type: ignore 8.167, 1.492, 2.782, 4.253, 12.702, 2.228, 2.015, 6.966, 7.507, 0.153, 0.747, 4.025, 2.406, 6.749, 7.507, 1.929, 0.095, 5.987, 6.327, 9.056, 2.758, 0.978, 2.360, 0.150, 1.974, 0.074 }; void analyze_cipher(const char *cipher, int count[26], float freq[26]) { # type: ignore int total = 0; memset(count, 0, 26*sizeof(int)); //统计每个字母出现的次数// for (int i = 0; cipher[i]; i++) { if (isalpha(cipher[i])) { char c = toupper(cipher[i]); count[c-'A']++; total++; } } //计算每个字母出现的频率// for (int i = 0; i < 26; i++) { freq[i] = total ? (count[i] * 100.0f / total) : 0; } } void get_rank(const float freq[26], int rank[26]) { # type: ignore for (int i = 0; i < 26; i++) { rank[i] = 0; for (int j = 0; j < 26; j++) { if (freq[j] > freq[i]) rank[i]++; } } } void init_mapping(const int cipher_rank[26], char mapping[26]) { # type: ignore // 标准频率排名 int std_rank[26] = {4,19,0,8,14,2,18,3,5,13,11,1,15,6,17,10,20,7,9,24,22,21,25,12,16,23}; // 临时数组记录已使用的明文字母 int used[26] = {0}; // 优先匹配高频字母(前6位:E,T,A,O,I,N) for (int i = 0; i < 6; i++) { // 找到密文中排名第i的字母 for (int c = 0; c < 26; c++) { if (cipher_rank[c] == i) { // 匹配标准排名第i的字母 for (int p = 0; p < 26; p++) { if (std_rank[p] == i && !used[p]) { mapping[c] = 'A' + p; used[p] = 1; break; } } break; } } } //计算频率的排名// void get_rank(const float freq[26], int rank[26]) { for (int i = 0; i < 26; i++) { rank[i] = 0; for (int j = 0; j < 26; j++) { if (freq[j] > freq[i]) rank[i]++; } } } //填充剩余字母// for (int c = 0; c < 26; c++) { if (!mapping[c]) { // 未映射的密文字母 for (int p = 0; p < 26; p++) { if (!used[p]) { mapping[c] = 'A' + p; used[p] = 1; break; } } } } } //使用映射表解密// void decrypt(const char *cipher, const char mapping[26], char *plain) { for (int i = 0; cipher[i]; i++) { if (isalpha(cipher[i])) { char c = toupper(cipher[i]); plain[i] = islower(cipher[i]) ? tolower(mapping[c-'A']) : mapping[c-'A']; } else { plain[i] = cipher[i]; // 保留非字母字符 } } plain[strlen(cipher)] = '\0'; } //显示频率分析结果// void print_analysis(const int count[26], const float freq[26], const int rank[26]) { printf("\n===== 密文频率分析 =====\n"); printf("字母 | 出现次数 | 频率(%%) | 排名\n"); printf("-------------------------\n"); for (int i = 0; i < 26; i++) { printf(" %c | %5d | %5.2f | %2d\n", 'A'+i, count[i], freq[i], rank[i]); } printf("\n===== 标准英文频率排名 =====\n"); printf("高频字母(前6位): E(12.7%%) > T(9.1%%) > A(8.2%%) > O(7.5%%) > I(7.0%%) > N(6.7%%)\n"); } int main() { char cipher[10000], plain[10000]; int count[26], cipher_rank[26]; float freq[26]; char mapping[26] = {0}; // 密文→明文映射表(索引=密文'A'-'Z') // 输入密文// printf("请输入密文: "); fgets(cipher, sizeof(cipher), stdin); cipher[strcspn(cipher, "\n")] = '\0'; // 移除换行符 //频率分析// analyze_cipher(cipher, count, freq); get_rank(freq, cipher_rank); print_analysis(count, freq, cipher_rank); //生成初始映射并解密// init_mapping(cipher_rank, mapping); decrypt(cipher, mapping, plain); //交互式调整映射// char cmd[10]; while (1) { printf("\n===== 当前解密结果 =====\n%s\n", plain); printf("\n映射表调整 (格式: 密文 明文,如 'X E';输入q退出): "); fgets(cmd, sizeof(cmd), stdin); cmd[strcspn(cmd, "\n")] = '\0'; if (cmd[0] == 'q' || cmd[0] == 'Q') break; // 解析调整命令(如"X E"表示密文X→明文E)// if (strlen(cmd)>=3 && isalpha(cmd[0]) && isalpha(cmd[2])) { char c = toupper(cmd[0]) - 'A'; // 密文字母索引 char p = toupper(cmd[2]) - 'A'; // 明文字母索引 // 处理冲突:如果p已被其他字母映射,先释放 for (int i = 0; i < 26; i++) { if (mapping[i] == 'A' + p) { // 为被占用的字母分配新的未使用字母 for (int j = 0; j < 26; j++) { int used = 0; for (int k = 0; k < 26; k++) { if (mapping[k] == 'A' + j) { used = 1; break; } } if (!used) { mapping[i] = 'A' + j; break; } } break; } } //更新映射// mapping[c] = 'A' + p; decrypt(cipher, mapping, plain); // 重新解密 } else { printf("输入格式错误,请重试!\n"); } } //输出最终结果// printf("\n===== 最终解密结果 =====\n%s\n", plain); return 0; }
10-16
课程设计报告:总体方案设计说明 一、软件开发环境配置 本系统采用C++作为核心编程语言,结合Qt 5.12.7框架进行图形用户界面开发。数据库管理系统选用MySQL,用于存储用户数据与小精灵信息。集成开发环境为Qt Creator,操作系统平台为Windows 10。 二、窗口界面架构设计 系统界面由多个功能模块构成,各模块职责明确,具体如下: 1. 起始界面模块(Widget) 作为应用程序的入口界面,提供初始导航功能。 2. 身份验证模块(Login) 负责处理用户登录与账户注册流程,实现身份认证机制。 3. 游戏主大厅模块(Lobby) 作为用户登录后的核心交互区域,集成各项功能入口。 4. 资源管理模块(BagWidget) 展示用户持有的全部小精灵资产,提供可视化资源管理界面。 5. 精灵详情模块(SpiritInfo) 呈现选定小精灵的完整属性数据与状态信息。 6. 用户名录模块(UserList) 系统内所有注册用户的基本信息列表展示界面。 7. 个人资料模块(UserInfo) 显示当前用户的详细账户资料与历史数据统计。 8. 服务器精灵选择模块(Choose) 对战准备阶段,从服务器可用精灵池中选取参战单位的专用界面。 9. 玩家精灵选择模块(Choose2) 对战准备阶段,从玩家自有精灵库中筛选参战单位的操作界面。 10. 对战演算模块(FightWidget) 实时模拟精灵对战过程,动态呈现战斗动画与状态变化。 11. 对战结算模块(ResultWidget) 对战结束后,系统生成并展示战斗结果报告与数据统计。 各模块通过统一的事件驱动机制实现数据通信与状态同步,确保系统功能的连贯性与数据一致性。界面布局遵循模块化设计原则,采用响应式视觉方案适配不同显示环境。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值