第八周项目4-字符串加密
2015-10-30 09:03
162 查看
<pre class="cpp" name="code">/* Copyright (c)2015,烟台大学计算机与控制工程学院 All rights reserved. 文件名称:jiami.cbp 作 者:李涵睿 完成日期:2015年10月30日 版 本 号:v1.0 问题描述:一个文本串可用事先编制好的字符映射表进行加密。例如,设字符映射表为: abcdefghijklmnopqrstuvwxyz ngzqtcobmuhelkpdawxfyivrsj 则字符串“lao he jiao shu ju jie gou”被加密为“enp bt umnp xby uy umt opy”。 设计一个程序,实现加密、解密算法,将输入的文本进行加密后输出,然后进行解密并输出。 输入描述:无 程序输出:测试数据 */
头文件及功能函数详见【顺序串算法库】
main函数:
#include <iostream> #include <stdio.h> #include "sqString.h" using namespace std; SqString A,B; SqString EnCrypt(SqString p) { int i=0,j; SqString q; while (i<p.length) { for (j=0; p.data[i]!=A.data[j]; j++); if (j>=p.length) //在A串中未找到p.data[i]字母 q.data[i]=p.data[i]; else //在A串中找到p.data[i]字母 q.data[i]=B.data[j]; i++; } q.length=p.length; return q; } SqString UnEncrypt(SqString q) { int i=0,j; SqString p; while (i<q.length) { for (j=0; q.data[i]!=B.data[j]; j++); if (j>=q.length) //在B串中未找到q.data[i]字母 p.data[i]=q.data[i]; else //在B串中找到q.data[i]字母 p.data[i]=A.data[j]; i++; } p.length=q.length; return p; } int main() { SqString p,q; StrAssign(A,"abcdefghijklmnopqrstuvwxyz"); //建立A串 StrAssign(B,"ngzqtcobmuhelkpdawxfyivrsj"); //建立B串 char str[MaxSize]; printf("\n"); printf("输入原文串:"); gets(str); //获取用户输入的原文串 StrAssign(p,str); //建立p串 printf("加密解密如下:\n"); printf(" 原文串:"); DispStr(p); q=EnCrypt(p); //p串加密产生q串 printf(" 加密串:"); DispStr(q); p=UnEncrypt(q); //q串解密产生p串 printf(" 解密串:"); DispStr(p); printf("\n"); return 0; }
运行结果:
学习心得&知识点总结:
程序算法的应用。
相关文章推荐
- 第九周项目3——稀疏矩阵三元组表示的实现与应用(1)
- 无题
- 第8周项目2建立链串的算法库
- c++boost中的asio介绍
- 【mysql】重置密码的方法
- 4:登录知乎后爬取首页问题
- 第九周 项目3 稀疏矩阵的三元组表示的实现及应用(矩阵相加)
- 水上升下降抵消题(Problem ID:1049)
- 浅谈android事件分发机制
- 【mysql】报错解决方案The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
- spring mvc中的@PathVariable
- 第9周 项目1-猴子选大王(数组版)
- 第七周项目4队列数组
- 导航软件后台播放语音有杂音,卡顿
- python中的StringIO模块
- 用名称字符串导入模块(仅作了解即可)
- 解决Win10下Genymotion无法打开问题
- 从内存地址解析Java的static关键字的作用
- 不超过地下通道题(Problem ID:1037)
- 现代操作系统——分布式系统概述