如何使这个程序看起来比第一个角色更多?

时间:2015-06-02 22:26:01

标签: vector decoding

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
const int setNum = 26;
vector<char> normalV(setNum);
vector<char> cipherV(setNum);
string toDec = "";
string beenDec = "";
int i = 0;

normalV.at(i) = 'a'; cipherV.at(i) = '!'; ++i;
normalV.at(i) = 'b'; cipherV.at(i) = '^'; ++i;
normalV.at(i) = 'c'; cipherV.at(i) = '&'; ++i;
normalV.at(i) = 'd'; cipherV.at(i) = '*'; ++i;
normalV.at(i) = 'e'; cipherV.at(i) = '@'; ++i;
normalV.at(i) = 'f'; cipherV.at(i) = '('; ++i;
normalV.at(i) = 'g'; cipherV.at(i) = ')'; ++i;
normalV.at(i) = 'h'; cipherV.at(i) = '-'; ++i;
normalV.at(i) = 'i'; cipherV.at(i) = '#'; ++i;
normalV.at(i) = 'j'; cipherV.at(i) = '_'; ++i;
normalV.at(i) = 'k'; cipherV.at(i) = '='; ++i;
normalV.at(i) = 'l'; cipherV.at(i) = '+'; ++i;
normalV.at(i) = 'm'; cipherV.at(i) = '['; ++i;
normalV.at(i) = 'n'; cipherV.at(i) = '{'; ++i;
normalV.at(i) = 'o'; cipherV.at(i) = '$'; ++i;
normalV.at(i) = 'p'; cipherV.at(i) = ']'; ++i;
normalV.at(i) = 'q'; cipherV.at(i) = '}'; ++i;
normalV.at(i) = 'r'; cipherV.at(i) = ';'; ++i;
normalV.at(i) = 's'; cipherV.at(i) = ':'; ++i;
normalV.at(i) = 't'; cipherV.at(i) = ','; ++i;
normalV.at(i) = 'u'; cipherV.at(i) = '%'; ++i;
normalV.at(i) = 'v'; cipherV.at(i) = '<'; ++i;
normalV.at(i) = 'w'; cipherV.at(i) = '.'; ++i;
normalV.at(i) = 'x'; cipherV.at(i) = '>'; ++i;
normalV.at(i) = 'y'; cipherV.at(i) = '/'; ++i;
normalV.at(i) = 'z'; cipherV.at(i) = '?'; ++i;

// User inputs message
do {
    cout << "Enter a secret message: ";
    getline(cin, toDec);
} while (toDec.length() == 0);

beenDec = toDec;

//decodes user's message
for (i = 0; i < setNum; ++i){
if (toDec.at(0) == cipherV.at(i)) {
        beenDec.at(0) = normalV.at(i);
    }
}

//diplays decoded message
cout << "Decrypted message: " << beenDec << endl;

//command drive to stay open
cin.get();
cin.get();
return 0;
}

任何帮助都可以得到赞赏。该代码应该解码一个字符串,例如&#34;!^&amp;&#34;应该输出到&#34; abc&#34;。现在它只是像这样解码第一个字母&#34; a ^&amp;&#34;。我觉得这是因为它被定义为char而不是字符串但是如果我改变它会给我错误。有关如何修改此功能的任何想法?提前致谢

1 个答案:

答案 0 :(得分:2)

我认为循环应该是这样的 - :

//decodes user's message
for ( unsigned j = 0; j < toDec.length(); j++ )
{
    for ( i = 0; i < setNum; ++i )
    {
        if ( toDec.at( j ) == cipherV.at( i ) )
        {
            beenDec.at( j ) = normalV.at( i );
        }
    }
}