串的基本运算实现-加密解密串

来源:互联网 发布:你最喜欢的首饰知乎 编辑:程序博客网 时间:2024/04/29 06:13

问题及代码:

/** Copyright (c) 2016, 烟台大学计算机与控制工程学院* All rights reserved.* 文件名称:encrypt.cpp* 作    者:单昕昕* 完成日期:2016年4月18日* 版 本 号:v1.0* 问题描述:一个文本串可用事先给定的字母映射表进行加密。            例如,设字母映射表为:            abcdefghijklmnopqrstuvwxyz            ngzqtcobmuhelkpdawxfyivrsj            则字符串“abc”被加密为“ngz”。            设计一个程序exp4-4.cpp将输入的文本串进行加密后输出,然后进行解密并输出。* 程序输入:一个字符串。* 程序输出:原文串、加密串和解密串。*/#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int MaxSize=100;//非紧缩格式的顺序串的类型定义typedef struct{    char data[MaxSize];    int length;} SqString;SqString str;//原文串SqString s1;//加密串SqString s2;//解密串void StrAssign(SqString &s,char cstr[])//将一个字符串常量赋给串s{    int i;    for(i=0; cstr[i]!='\0'; ++i)        s.data[i]=cstr[i];    s.length=i;}void Disstr(SqString s)//输出串s的所有元素值{    int i;    if(s.length>0)    {        for(i=0; i<s.length; ++i)            cout<<s.data[i];        cout<<endl;    }}void EncryptStr(SqString s)//加密串{    s1.length=0;//初始化串的长度    int i;    for(i=0; i<s.length; ++i)//枚举加密    {        if(s.data[i]=='a') s1.data[i]='n';        else if(s.data[i]=='b') s1.data[i]='g';        else if(s.data[i]=='c') s1.data[i]='z';        else if(s.data[i]=='d') s1.data[i]='q';        else if(s.data[i]=='e') s1.data[i]='t';        else if(s.data[i]=='f') s1.data[i]='c';        else if(s.data[i]=='g') s1.data[i]='o';        else if(s.data[i]=='h') s1.data[i]='b';        else if(s.data[i]=='i') s1.data[i]='m';        else if(s.data[i]=='j') s1.data[i]='u';        else if(s.data[i]=='k') s1.data[i]='h';        else if(s.data[i]=='l') s1.data[i]='e';        else if(s.data[i]=='m') s1.data[i]='l';        else if(s.data[i]=='n') s1.data[i]='k';        else if(s.data[i]=='o') s1.data[i]='p';        else if(s.data[i]=='p') s1.data[i]='d';        else if(s.data[i]=='q') s1.data[i]='a';        else if(s.data[i]=='r') s1.data[i]='w';        else if(s.data[i]=='s') s1.data[i]='x';        else if(s.data[i]=='t') s1.data[i]='f';        else if(s.data[i]=='u') s1.data[i]='y';        else if(s.data[i]=='v') s1.data[i]='i';        else if(s.data[i]=='w') s1.data[i]='v';        else if(s.data[i]=='x') s1.data[i]='r';        else if(s.data[i]=='y') s1.data[i]='s';        else if(s.data[i]=='z') s1.data[i]='j';    }    s1.length=i;//给定串的长度}void DecipheringStr(SqString s)//解密串{    s2.length=0;//初始化串的长度    int i;    for(i=0; i<s.length; ++i)//枚举解密    {        if(s.data[i]=='n') s2.data[i]='a';        else if(s.data[i]=='g') s2.data[i]='b';        else if(s.data[i]=='z') s2.data[i]='c';        else if(s.data[i]=='q') s2.data[i]='d';        else if(s.data[i]=='t') s2.data[i]='e';        else if(s.data[i]=='c') s2.data[i]='f';        else if(s.data[i]=='o') s2.data[i]='g';        else if(s.data[i]=='b') s2.data[i]='h';        else if(s.data[i]=='m') s2.data[i]='i';        else if(s.data[i]=='u') s2.data[i]='j';        else if(s.data[i]=='h') s2.data[i]='k';        else if(s.data[i]=='e') s2.data[i]='l';        else if(s.data[i]=='l') s2.data[i]='m';        else if(s.data[i]=='k') s2.data[i]='n';        else if(s.data[i]=='p') s2.data[i]='o';        else if(s.data[i]=='d') s2.data[i]='p';        else if(s.data[i]=='a') s2.data[i]='q';        else if(s.data[i]=='w') s2.data[i]='r';        else if(s.data[i]=='x') s2.data[i]='s';        else if(s.data[i]=='f') s2.data[i]='t';        else if(s.data[i]=='y') s2.data[i]='u';        else if(s.data[i]=='i') s2.data[i]='v';        else if(s.data[i]=='v') s2.data[i]='w';        else if(s.data[i]=='r') s2.data[i]='x';        else if(s.data[i]=='s') s2.data[i]='y';        else if(s.data[i]=='j') s2.data[i]='z';    }    s2.length=i;//给定串的长度}int main(){    char c[MaxSize];    cout<<"请输入原文串:";    gets(c);    StrAssign(str,c);//将一个char c[]赋给串str    cout<<"加密解密如下:"<<endl;    cout<<"原文串:";    Disstr(str);//输出原文串    cout<<"加密串:";    EncryptStr(str);//加密    Disstr(s1);//输出加密串    cout<<"解密串:";    DecipheringStr(s1);//解密    Disstr(s2);//输出解密串}//测试用例:encrypt


运行结果:



练习了一下顺序串~

一开始忘记加密解密后给定串的长度…导致结果各种出不来~
还有就是判断的时候是单引号 ‘ ’ 中间加字符~


0 0
原创粉丝点击