C++ C++;编解码

C++ C++;编解码,c++,C++,// /消息示例:“hello”和移位1。加密机将每个字母上移一位:h->i,e->f,l->m,l->m,o->p。 所以encrypt(“hello”)返回“ifmmp”。decrypt函数的作用正好相反,因此decrypt(“ifmmp”)返回“hello”。 cypher加密程序使用表示字母转换的字符串作为加密解密的方式。 所以一个密码为“efyhadwzlvjnktbogrumcpiqxs”的加密程序加密a->e,b->f,c->y,d->h等等。 使用此加密程序,encrypt(“h

// /消息示例:“hello”和移位1。加密机将每个字母上移一位:h->i,e->f,l->m,l->m,o->p。 所以encrypt(“hello”)返回“ifmmp”。decrypt函数的作用正好相反,因此decrypt(“ifmmp”)返回“hello”。 cypher加密程序使用表示字母转换的字符串作为加密解密的方式。 所以一个密码为“efyhadwzlvjnktbogrumcpiqxs”的加密程序加密a->e,b->f,c->y,d->h等等。 使用此加密程序,encrypt(“hello”)返回“zannb”,decrypt(“zannb”)返回“hello”/

我运行代码时得到的结果是:

#包括
#包括
使用名称空间std;
类加密机
{
公众:
虚拟字符串编码(字符串原始)=0;
虚拟字符串解码(字符串机密)=0;
};
类移位加密机:公共加密机
{
int移位;
公众:
移位加密器(整数移位)
{
//您的代码从这里开始
这个->移位=移位;
//您的代码到此结束
}
字符串编码(字符串原始)
{
//您的代码从这里开始
字符c_original[26]={'','a','b','c','d','e','f','g','h','i','j','l','m','n','o','p','r','s','t','u','v','w','x','y','z'};
字符串原始_结果=”;
对于(int i=0;ishift;
if(最终索引>尺寸(原版)-1)
最终索引-=sizeof(c_原件);
原始结果+=c原始[最终索引];
}
返回原始结果;
//您的代码到此结束
}
字符串解码(字符串机密)
{
//您的代码从这里开始
字符c_secret[26]={'','a','b','c','d','e','f','g','h','i','j','l','m','n','o','p','r','s','t','u','v','w','x','y','z'};
字符串secret_result=“”;
for(int i=0;i移位;
如果(最终指数<0)
最终索引+=sizeof(c_secret);
secret_result+=c_secret[最终_索引];
}
返回秘密结果;
//您的代码到此结束
}
};
密码加密机类:公共加密机
{
弦密码;
公众:
密码加密机(字符串密码)
{
//您的代码从这里开始
这个->密码=密码;
//您的代码到此结束
}
字符串编码(字符串原始)
{
//您的代码从这里开始
字符c_original[26]={'','a','b','c','d','e','f','g','h','i','j','l','m','n','o','p','r','s','t','u','v','w','x','y','z'};
字符串原始_结果=”;
对于(int i=0;icypher.length();j++)
{
如果(原始[i]==此->密码[j])
{
c_指数=j;
打破
}
}
原始结果+=c_原始[c_索引];
}
返回原始结果;
//您的代码到此结束
}
字符串解码(字符串机密)
{
//您的代码从这里开始
字符c_secret[26]={'','a','b','c','d','e','f','g','h','i','j','l','m','n','o','p','r','s','t','u','v','w','x','y','z'};
字符串secret_result=“”;
for(int i=0;icypher[c_index];
}
返回秘密结果;
//您的代码到此结束
}
};
//之后
int main()
{
shift_加密机*shiftTest=新的shift_加密机(1);
字符串encodeResult=“”;
字符串decodeResult=“”;
encodeResult=shiftTest->encode(“你好世界”);

你不必费心说明你的预期输出应该是什么。有一件事你似乎没有考虑到非字母字符。你应该编码所有字符还是只编码字母?如果都是字符,你的方法是非常错误的,而不是有点错误。这个答案假设只有TTER将被编码/解码

但这里有几个错误:

  • sizeof(c_original)
    可能应该是
    c_original.length()
  • 您的移位算法不使用
    shift
    变量,而是将移位值硬编码为1,而忽略字母“k”
  • 您的移位解码似乎只是进一步编码,而不是以另一种方式向后移位(我没有运行它,但代码太相同了,即,我没有看到我认为应该存在的差异)
  • 您没有使用问题陈述中的相同消息测试您的cypher类;您如何知道它是否工作
正如我的评论所指出的,我不认为这是多态性的一个很好的例子。但是对布局的一个小小的更改可以使它成为一个更好的例子。更改实际上是依赖多态性,在这种情况下,使用一个自由函数来演示使用任何派生自加密对象的编码/解码

你的代码是
shift encode result == ifmmpaxpsme
shift encode result == jhoor  ruog
encryptor encode result == c bj aiuw
encryptor encode result == kdedkuerldcqoepujoevlnrewurrjeu
#include <iostream>
#include <string>

using namespace std;

class encryptor
{
public:
    virtual string encode(string original) = 0;
    virtual string decode(string secret) = 0;
};

class shift_encryptor : public encryptor
{
    int shift;

public:
    shift_encryptor(int shift)
    {
        // Your code starts here
        this->shift = shift;
        // Your code ends here
    }

    string encode(string original)
    {
        // Your code starts here 
        char c_original[26] = { ' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
        string original_result = "";
        for (int i = 0; i < original.length(); i++)
        {
            int c_index = 0;
            for (int j = 0; j < sizeof(c_original); j++)
            {
                if (original[i] == c_original[j])
                {
                    c_index = j;
                    break;
                }
            }
            int final_index = c_index + this->shift;
            if (final_index > sizeof(c_original) - 1)
                final_index -= sizeof(c_original);
            original_result += c_original[final_index];
        }
        return original_result;
        // Your code ends here
    }

    string decode(string secret)
    {
        // Your code starts here 
        char c_secret[26] = { ' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
        string secret_result = "";
        for (int i = 0; i < secret.length(); i++)
        {
            int c_index = 0;
            for (int j = 0; j < sizeof(c_secret); j++)
            {
                if (secret[i] == c_secret[j])
                {
                    c_index = j;
                    break;
                }
            }
            int final_index = c_index - this->shift;
            if (final_index < 0)
                final_index += sizeof(c_secret);
            secret_result += c_secret[final_index];
        }
        return secret_result;
        // Your code ends here
    }
};

class cypher_encryptor : public encryptor
{
    string cypher;
public:
    cypher_encryptor(string cypher)
    {
        // Your code starts here
        this->cypher = cypher;
        // Your code ends here
    }
    string encode(string original)
    {
        // Your code starts here 
        char c_original[26] = { ' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
        string original_result = "";
        for (int i = 0; i < original.length(); i++)
        {
            int c_index = 0;
            for (int j = 0; j < this->cypher.length(); j++)
            {
                if (original[i] == this->cypher[j])
                {
                    c_index = j;
                    break;
                }
            }
            original_result += c_original[c_index];
        }
        return original_result;
        // Your code ends here
    }

    string decode(string secret)
    {
        // Your code starts here
        char c_secret[26] = { ' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
        string secret_result = "";
        for (int i = 0; i < secret.length(); i++)
        {
            int c_index = 0;
            for (int j = 0; j < sizeof(c_secret); j++)
            {
                if (secret[i] == c_secret[j])
                {
                    c_index = j;
                    break;
                }
            }
            secret_result += this->cypher[c_index];
        }
        return secret_result;
        // Your code ends here
    }
};

//After

int main()
{
    shift_encryptor* shiftTest = new shift_encryptor(1);
    string encodeResult = "";
    string decodeResult = "";

    encodeResult = shiftTest->encode("hello world");
    cout << "shift encode result == " << encodeResult << endl;

    decodeResult = shiftTest->decode("lipps${svph");
    cout << "shift encode result == " << decodeResult << endl;

    // string encodeResult = shiftTest->en

    cypher_encryptor* crypterTest = new cypher_encryptor("efyhadwzlvjnktbogrumcpiqxs");

    encodeResult = crypterTest->encode("heyj fvmpe");
    cout << "encryptor encode result == " << encodeResult << endl;
    decodeResult = crypterTest->decode("me emt shevyp wtjp ihls ftssjkt");
    cout << "encryptor decode result == " << decodeResult << endl;
    return 0;
}

Shift by 1:
Original: Hello World
Encoded:  IFMMP XPSME
Decoded:  HELLO WORLD

Original: Zipper
Encoded:  AJQQFS
Decoded:  ZIPPER

Shift by 10:
Original: Hello World
Encoded:  ROVVY GYBVN
Decoded:  HELLO WORLD

Original: Zipper
Encoded:  JSZZOB
Decoded:  ZIPPER

Provided cypher:
Original: Hello World
Encoded:  ZANNB IBRNH
Decoded:  HELLO WORLD

'Unknown' cypher:
Original: Hello World
Encoded:  CHLLG AGVLK
Decoded:  HELLO WORLD