字符串应用编码解码.doc

上传人:苏美尔 文档编号:8978146 上传时间:2021-01-28 格式:DOC 页数:2 大小:16.51KB
返回 下载 相关 举报
字符串应用编码解码.doc_第1页
第1页 / 共2页
字符串应用编码解码.doc_第2页
第2页 / 共2页
亲,该文档总共2页,全部预览完了,如果喜欢就下载吧!
资源描述

《字符串应用编码解码.doc》由会员分享,可在线阅读,更多相关《字符串应用编码解码.doc(2页珍藏版)》请在三一文库上搜索。

1、字符串应用:编码解码【题目】从键盘输入一个英文句子,设计一个编码、解码程序。编码过程:先键入一个正整数()。这个决定了转换关系。 例如当,输入的句子为ABCXYZ时,则其转换码为ABCXYZ不变。当时,其转换码为BCDYZA,其它的非字母字符不变。为使编码较于破译,将转换码的信息自左而右两两交换,若最后仅剩单个字符则不换。然后,将一开始表示转换关系的根据ascii表序号化成大写字母放在最前面。 如:abcABCxyzXYZ-/,1. n=3 cdeCDEzabZAB-/,1. 根据N的值转换 dcCeEDazZbBA/-1,. 两两交换 CdcCeEDazZbBA/-1,. 最后编码 解码过程

2、为编码的逆过程。【参考程序】var one:string; i,what,n,temp,s:integer;temp1:char;begin writeln(input your choice:); 编码,解码过程通过菜单选择 writeln(1.bian ma); 选1,编码 writeln(2.jie ma); 选2,解码 readln(what); 输入选择 writeln(input a string:); 无论是编码或是解码,均要输入一字符串 readln(one); 读入字符串到one中 if what=1 then begin 如果是编码过程 readln(n); 读入N n:=

3、n-1; 根据题意,要换成N-1,为什么?例如N=1,则字符不变 for i:=1 to length(one) do begin 字串one从头到尾编码 if ord(onei) in 65.90 then begin 如为大写字母 temp:=ord(onei)+n; 序号先加N if temp90 then temp:=temp-90+64; 超过Z的处理,保证处理后仍为 onei:=chr(temp); 大写字母 end; if ord(onei) in 97.122 then begin 如为小写字母 temp:=ord(onei)+n; if temp122 then temp:=

4、temp-122+96; 超过z的处理,保证为小写字母 onei:=chr(temp); 处理后放回原位置 end; end; s:=ord(one0); S放one字符串长度,用以控制两两交换次数 if odd(s) then dec(s); 假如是奇数,则减1,保证是偶数次 i:=1; repeat temp1:=onei; 以下三句实现前后两字符的两两交换 onei:=onei+1; onei+1:=temp1; i:=i+2; 一次便交换两个字符 until i=s; 直到字串结束 write(chr(ord(n+1+64); 输出N的对应大写字母 write(one); 输出编码后的

5、one字符串 end; if what=2 then begin 解码过程 n:=ord(upcase(one1)-64-1; 由one字串的头一个字符获取密钥N one:=copy(one,2,length(one)-1); 取出N后,one字符串减去第一个字符 s:=ord(one0); 以下实现字符串的两两交换 if odd(s) then dec(s); S控制两两交换的次数,保证是偶数次 i:=1; repeat temp1:=onei; 前后两个字符两两交换 onei:=onei+1; onei+1:=temp1; i:=i+2; until i=s; 直到字串结束 for i:=

6、1 to length(one) do begin 根据N的值还原字符串 if ord(onei) in65.90 then begin 大写字母 temp:=ord(onei)-n; 序号 -N if temp65 then temp:=temp+26; 保证在A.Z范围内转换 onei:=chr(temp); 转换后放回原处 end; if ord(onei) in97.122 then begin 小写字母 temp:=ord(onei)-n; if temp97 then temp:=temp+26; onei:=chr(temp); end; end; writeln(one); 输出解码后的字符串 end;end.

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 科普知识


经营许可证编号:宁ICP备18001539号-1