数据结构上机4.4字符串映射加密解密

来源:互联网 发布:淘宝天猫买家注册 编辑:程序博客网 时间:2024/05/04 16:37

代码:

#include <iostream>#include<malloc.h>#include<string.h>#include<cstdio>using namespace std;#define MaxSize 100typedef struct{    char data[MaxSize];    int length;} SqString;SqString A,B;void StrAssign(SqString &s,char cstr[]){    int i;    for(i=0; cstr[i]!='\0'; i++)        s.data[i]=cstr[i];    s.length=i;}void DispStr(SqString s){    int i;   if(s.length>0)   {        for(i=0; i<s.length; i++)        printf("%c",s.data[i]);        printf("\n");   }}SqString EnCode(SqString p){    int i=0,j;    SqString q;    while(i<p.length)    {        for(j=0; p.data[i]!=A.data[j]&&j<A.length; j++);        if(j>=A.length)///不在A中找到            q.data[i]=p.data[i];        else           ///在A中找到            q.data[i]=B.data[j];        i++;    }    q.length=p.length;    return q;}SqString UnCode(SqString p){    int i=0,j;    SqString q;    while(i<p.length)    {        for(j=0; p.data[i]!=B.data[j]&&j<B.length; j++);///你之前的一直循环条件少加了&&j<A.length,导致出错        if(j>=B.length)///不在A中找到            q.data[i]=p.data[i];        else           ///在A中找到            q.data[i]=A.data[j];        i++;    }    q.length=p.length;    return q;}int main(){    SqString p,q;    char str[MaxSize];    StrAssign(A,"abcdefghijklmnopqrstuvwxyz");    StrAssign(B,"ngzqtcobmuhelkpdawxfyivrsj");    printf("请输入原来字符串:\n");    gets(str);    StrAssign(p,str);    printf("原串:\n");///不在A中找到    DispStr(p);    q=EnCode(p);    printf("加密后:\n");    DispStr(q);    p=UnCode(q);    printf("解密后:\n");    DispStr(p);    return 0;}

你的


0 0
原创粉丝点击