第八周项目四字符串加密

/*
* Copyright (c) 2015, 烟台大学计算机与控制工程学院
* All rights reserved.
* 文件名称: SqString.cpp,SqString.h
* 作者:巩凯强
* 完成日期:2015年10月25日
* 版本号:codeblocks
*
* 问题描述: 一个文本串可用事先编制好的字符映射表进行加密。:
* 输入描述:lao he jiao shu ju jie gou
* 程序输出:见下面的运行结果
*/
#ifndef LISTRING_H_INCLUDED
#define LISTRING_H_INCLUDED
#include <stdio.h>
#include <malloc.h>
#define MaxSize 100
typedef struct
{   char data[MaxSize];         //定义可容纳MaxSize个字符的空间
    int length;                 //标记当前实际串长
}SqString;
void StrAssign(SqString &s,char cstr[]);    //字符串常量cstr赋给串s
int StrLength(SqString s);  //求串长
void DispStr(SqString s);   //输出串
SqString EnCrypt(SqString p);
SqString UnEncrypt(SqString q);

#endif // LISTRING_H_INCLUDED

#include "liString.h"
SqString A,B;
void StrAssign(SqString &s,char cstr[]) //s为引用型参数
{   int i;
    for (i=0;cstr[i]!='\0';i++)
        s.data[i]=cstr[i];
    s.length=i;
}
int StrLength(SqString s)
{
    return s.length;
}
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 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;
}

运行结果:


知识点总结:

利用顺序串的基本运算实现加密功能。

学习心得:

为什么只输入lao he的时候加密串是lno ht没有对应一开始编好的字符映射表???疑问疑问疑问


  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值