《数据结构与算法A》实验3:字符串的简单加密

《数据结构与算法A》实验3:字符串的简单加密


Description


假设字符串中只可能包含“大小写英文字母”、“阿拉伯数字”和10种其他符号(包括:’!’、’#’、’@’、’+’,’-’,’*’,’?’,’$’,’:’,’;’)。请编写代码,当读入一个字符串(长度不超过50个字符)之后,使用顺序表存储字符串,并通过以下方式实现加密:首先,去掉字符串中的阿拉伯数字和其他符号;其次,将剩余的英文字母转换成ASCII码表中其后的第n(1≤n≤10)个字符;最后,输出加密后的字符串(可能为空字符串)。

顺序表的参考代码如下:

const  int  MaxListSize=**10**; //根据问题修改该值

class SeqCharList{

char data[MaxListSize]; //存储字符串

int size;            //元素个数

public:

    SeqCharList( );  //构造函数

    void Clear( );        //清空表

    bool IsEmpty( );     //判断如果为空表,返回true,否则返回false

    char Get(int k);    //返回第k个字符

    int Locate(char e);      //返回第一个与元素e匹配的元素位序

    char Delete(int i);        //删除第i个元素,并返回所删除元素值

    void Print( );    //输出字符串

    **void Encryption( );    //字符串加密**

}; //SeqCharList

Input


本实验包含多组测试数据,每组数据包含两行:第一行输入n(1≤n≤10),表示使用ASCII码表中其后的第n个字符进行加密;第二行输入要加密的字符串。当输入n=-1时,做为测试结束标志。

Output


输出加密后的字符串,并且每个加密字符串占一行。注意:当加密之后为空字符串时,则只输出换行。

Sample Input


  • 8
  • Hello?World!
  • 5
  • 123@456$789#
  • 1
  • Hello:World!
  • 10
  • Hello;World!
  • -1

Sample Output


  • Pmttw_wztl
  • IfmmpXpsme
  • Rovvyay|vn

代码


#include<iostream>
using namespace std;
 
const int MaxListSize = 50;
int q;
 
class SeqCharList
{
    char data[MaxListSize];
    int size;
public:
    SeqCharList(char* str);
    void Clear();
    bool IsEmpty();
    char Get(int k);
    int Locate(char e);
    char Delete(int i);
    void Print();
    void Encryption();
};
 
SeqCharList::SeqCharList(char* str)
{
    size = 0;
    while (str[size])
    {
        data[size] = str[size++];
    }
}
 
void SeqCharList::Clear()
{
    size = 0;
}
 
bool SeqCharList::IsEmpty()
{
    if (size == 0)
        return 1;
    else
        return 0;
}
 
char SeqCharList::Get(int k)
{
    return data[k];
}
 
int SeqCharList::Locate(char e)
{
    return (int)e;
}
 
char SeqCharList::Delete(int i)
{
    char ch = data[i];
    for (int j = i; j < size; j++)
    {
        data[j] = data[j + 1];
    }
    data[size - 1] = '\0';
    size--;
    return ch;
}
 
void SeqCharList::Print()
{
    for (int i = 0; i < size; i++)
    {
        cout << data[i];
    }
    cout << endl;
}
 
void SeqCharList::Encryption()
{
    for (int i = 0; i < size; i++)
    {
        if ((data[i] >= 'a'&&data[i] <= 'z') || (data[i] >= 'A'&&data[i] <= 'Z'))
        {
            data[i] += q;
        }
        else
        {
            Delete(i);
            i--;
        }
    }
    Print();
}
 
int main()
{
    while (cin >> q)
    {
        if (q == -1)
            break;
        char* ch = new char[50];
        cin >> ch;
        SeqCharList newCharList(ch);
        newCharList.Encryption();
    }
}
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值