有什么方法可以使这段代码更紧凑,和/或能够在运行时更改? [英] Any way to make this code more compact, and/or be able to change at runtime?

查看:61
本文介绍了有什么方法可以使这段代码更紧凑,和/或能够在运行时更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写的这个程序模拟了一个36个字符,10个转子

互易转子密码,带有一个插板。我有什么方法可以使

插板功能更紧凑和/或能够在运行时更改映射




char Enigma :: plugboard(char char)

{

if(Char ==''A'')

返回''0' ';

否则if(Char ==''B'')

返回''Q'';

else if(Char = ='''C'')

返回''W'';

否则if(Char ==''D'')

返回''E'';

否则if(Char ==''E'')

返回''D'';

否则if(Char ==''F'')

返回''T'';

else if(Char ==''G'')

返回''Y'';

否则if(Char ==''H'')

返回''U'';

else if(Char ==''I'')

返回''我';

else if(Char ==''J' ')

返回''O'';

否则if(Char ==''K'')

返回''P' ';

否则if(Char ==''L'')

返回''S'';

else if(Char ==''M'')

返回''N'';

否则if(Char ==''N'')

返回''M'';

else if(Char ==''O'')

返回''J'';

else if(Char ==''P' ')

返回''K'';

否则if(Char ==''Q'')

返回''B' ';

否则if(Char ==''R'')

返回''Z'';

else if(Char = =''S'')

返回''L'';

否则if(Char ==''T'')

返回''F'';

否则if(Char ==''U'')

返回''H'';

否则if(Char ==''V'')

返回''X'';

else if(Char ==''W'')

返回''C'';

否则if(Char ==''X'')

返回''V'';

else if(Char ==''Y'')

return' G'';

否则if(Char ==''Z'')

返回''R'';

否则如果( Char ==''0'')

返回''A'';

否则if(Char ==''1'')

返回''9'';

否则if(Char ==''2'')

返回''8'';

else if(Char ==''3'')

返回''7'';

else if(Char ==''4'')

返回''6'';

else if(Char ==''5'')

返回''5'';

else if(Char ==''6'')

返回''4'';

else if(Char =='' 7'')

返回''3';

否则if(Char ==''8'')

return'' 2'';

否则if(Char ==''9'')

返回''1';

}


如果您需要更多信息,请询问,我会生产它。谢谢!!!!

I''ve written this program that simulates a 36 character, 10 rotor
reciprocal rotor cipher, w/ a plugboard. Any way I can make the
plugboard function more compact and/or be able to change the mapping
at runtime?

char Enigma::plugboard(char Char)
{
if(Char==''A'')
return ''0'';
else if(Char==''B'')
return ''Q'';
else if(Char==''C'')
return ''W'';
else if(Char==''D'')
return ''E'';
else if(Char==''E'')
return ''D'';
else if(Char==''F'')
return ''T'';
else if(Char==''G'')
return ''Y'';
else if(Char==''H'')
return ''U'';
else if(Char==''I'')
return ''I'';
else if(Char==''J'')
return ''O'';
else if(Char==''K'')
return ''P'';
else if(Char==''L'')
return ''S'';
else if(Char==''M'')
return ''N'';
else if(Char==''N'')
return ''M'';
else if(Char==''O'')
return ''J'';
else if(Char==''P'')
return ''K'';
else if(Char==''Q'')
return ''B'';
else if(Char==''R'')
return ''Z'';
else if(Char==''S'')
return ''L'';
else if(Char==''T'')
return ''F'';
else if(Char==''U'')
return ''H'';
else if(Char==''V'')
return ''X'';
else if(Char==''W'')
return ''C'';
else if(Char==''X'')
return ''V'';
else if(Char==''Y'')
return ''G'';
else if(Char==''Z'')
return ''R'';
else if(Char==''0'')
return ''A'';
else if(Char==''1'')
return ''9'';
else if(Char==''2'')
return ''8'';
else if(Char==''3'')
return ''7'';
else if(Char==''4'')
return ''6'';
else if(Char==''5'')
return ''5'';
else if(Char==''6'')
return ''4'';
else if(Char==''7'')
return ''3'';
else if(Char==''8'')
return ''2'';
else if(Char==''9'')
return ''1'';
}

If you need any more info, just ask and I''ll produce it. Thanks!!!!

推荐答案

Protoman写道:
Protoman wrote:

I 这个程序写的模拟了一个36个字符,10个转子

互易转子密码,带有一个插板。我能用什么方式使

插板功能更紧凑和/或能够在运行时更改映射


I''ve written this program that simulates a 36 character, 10 rotor
reciprocal rotor cipher, w/ a plugboard. Any way I can make the
plugboard function more compact and/or be able to change the mapping
at runtime?



在这种情况下,开关可能更紧凑:


开关(字符){

case''A'':返回''0'';

case''B'':返回''Q'';

....

}


如果你想在运行时更改:

#include< map>

class Enigma {

std :: map< char,charplug;

public:

char plugboard(char Char){

返回插头[Char];

}

....

};

-

rbh

In this situation, a switch might be more compact:

switch (Char) {
case ''A'': return ''0'';
case ''B'': return ''Q'';
....
}

If you want to change at runtime:
#include <map>

class Enigma {
std::map<char, charplug;
public:
char plugboard(char Char) {
return plug[Char];
}
....
};
--
rbh


7月2日20:10,Robert Bauck Hamar< roberth + n ... @ ifi .uio.nowrote:
On 2 Jul, 20:10, Robert Bauck Hamar <roberth+n...@ifi.uio.nowrote:

Protoman写道:
Protoman wrote:

我写了这个程序来模拟一个36个字符,10个转子

互易转子密码,带插板。我能用什么方式使

插板功能更紧凑和/或能够在运行时更改映射


I''ve written this program that simulates a 36 character, 10 rotor
reciprocal rotor cipher, w/ a plugboard. Any way I can make the
plugboard function more compact and/or be able to change the mapping
at runtime?



在这种情况下,开关可能更紧凑:


开关(字符){

case''A'':返回''0'';

case''B'':返回''Q'';

...


}


如果你想在运行时更改:

#include< map>


class Enigma {

std :: map< char,charplug;

public:

char plugboard(char Char){

返回插头[Char];

}

...


} ;


-

rbh


In this situation, a switch might be more compact:

switch (Char) {
case ''A'': return ''0'';
case ''B'': return ''Q'';
...

}

If you want to change at runtime:
#include <map>

class Enigma {
std::map<char, charplug;
public:
char plugboard(char Char) {
return plug[Char];
}
...

};

--
rbh



哦,加密()没有''工作正常...它只返回明文的第一个

字符:


字符串Enigma :: Encrypt(const string& cleartext)

{

string ciphertext;

ciphertext.resize(cleartext.size());

unsigned int i = 0;

for(; i< cleartext.length(); i ++)

{

int val = Rotor :: Ch aracterMap(plugboard(cleartext [i]));

if(val< 36)

{

char val1 = R1.GetCharacterIndex(val );

int val2 = Rotor :: CharacterMap(val1);

char val3 = R2.GetCharacterIndex(val2);

int val4 = Rotor :: CharacterMap(val3);

char val5 = R3.GetCharacterIndex(val4);

int val6 = Rotor :: CharacterMap(val5);

char val7 = R4.GetCharacterIndex(val6);

int val8 = Rotor :: CharacterMap(val7);

char val9 = R5.GetCharacterIndex(val8);

int val10 = Rotor :: CharacterMap(val9);

char val11 = R6.GetCharacterIndex(val10);

int val12 =转子: :CharacterMap(val11);

char val13 = R7.GetCharacterIndex(val12);

int val14 = Rotor :: CharacterMap(val13);

char val15 = R8.GetCharacterIndex(val14);

int val16 = Rotor :: CharacterMap(val15);

char val17 = R9.GetCharacterIndex(val16);

int val18 = Rotor :: CharacterMap(val17);

char val19 = R10.GetCharacterIndex(val18);

int val20 = Rotor :: CharacterMap(val19);

char val21 = Enigma :: Reflector [val20];

int val22 = Rotor :: CharacterMap(val21);

char val23 = R10.GetCharacterInverse(val22);

int val24 = Rotor :: CharacterMap(val23 );

char val25 = R9.GetCharacterInverse(val24);

int val26 = Rotor :: CharacterMap(val25);

char val27 = R8.GetCharacterInverse(val26);

int val28 = Rotor :: CharacterMap(val27);

char val29 = R7.GetCharacterInverse(val28);

int val30 = Rotor :: CharacterMap(val29);

char val31 = R6.GetCharacterInverse(val30);

int val32 = Rotor :: CharacterMap(val31);

char val33 = R5.GetCharacterIndex(val32);

int val34 = Rotor :: CharacterMap(val33);

char val35 = R4。 GetCharacterIndex(val34);

int val36 = Rotor :: CharacterMap(val35);

char val37 = R3.GetCharacterIndex(val36);

int val38 = Rotor :: Charac terMap(val37);

char val39 = R2.GetCharacterIndex(val38);

int val40 = Rotor :: CharacterMap(val39);

char val41 = R1.GetCharacterIndex(val40);

ciphertext [i] = plugboard(val41);

R1.AdvanceRotor(1);

if((R1.GetSteps()%36)== 0)

{

R2.AdvanceRotor(1);

if( (R2.GetSteps()%36)== 0)

{

R3.AdvanceRotor(1);

if((R3。 GetSteps()%36)== 0)

{

R4.AdvanceRotor(1);

if((R4.GetSteps() %36)== 0)

R5.AdvanceRotor(1);

{

if((R5.GetSteps()%36) == 0)

R6.AdvanceRotor(1);

{

if((R6.GetSteps()%36)== 0 )

R7.AdvanceRotor(1);

{

if((R7.GetSteps()%36)== 0)

R8.AdvanceRotor(1);

{

if((R8.GetSteps()%36)== 0)

R9.AdvanceRotor(1);

{

if((R9.GetSteps()%36)== 0)

R10.AdvanceRotor(1);

}

}

}

}

}

}

}

} //问题#2缺少大括号

else {ciphertext [i] = cleartext [i];}

}

返回密文;

}

}

Oh, and the encrypt() doesn''t work right...it just returns the first
char of the cleartext:

string Enigma::Encrypt(const string& cleartext)
{
string ciphertext;
ciphertext.resize(cleartext.size());
unsigned int i=0;
for(;i<cleartext.length();i++)
{
int val=Rotor::CharacterMap(plugboard(cleartext[i]));
if (val<36)
{
char val1 = R1.GetCharacterIndex(val);
int val2 = Rotor::CharacterMap(val1);
char val3 = R2.GetCharacterIndex(val2);
int val4 = Rotor::CharacterMap(val3);
char val5 = R3.GetCharacterIndex(val4);
int val6 = Rotor::CharacterMap(val5);
char val7 = R4.GetCharacterIndex(val6);
int val8 = Rotor::CharacterMap(val7);
char val9=R5.GetCharacterIndex(val8);
int val10 = Rotor::CharacterMap(val9);
char val11=R6.GetCharacterIndex(val10);
int val12=Rotor::CharacterMap(val11);
char val13=R7.GetCharacterIndex(val12);
int val14=Rotor::CharacterMap(val13);
char val15=R8.GetCharacterIndex(val14);
int val16=Rotor::CharacterMap(val15);
char val17=R9.GetCharacterIndex(val16);
int val18=Rotor::CharacterMap(val17);
char val19=R10.GetCharacterIndex(val18);
int val20=Rotor::CharacterMap(val19);
char val21 = Enigma::Reflector[val20];
int val22 = Rotor::CharacterMap(val21);
char val23 = R10.GetCharacterInverse(val22);
int val24 = Rotor::CharacterMap(val23);
char val25 = R9.GetCharacterInverse(val24);
int val26 = Rotor::CharacterMap(val25);
char val27 = R8.GetCharacterInverse(val26);
int val28 = Rotor::CharacterMap(val27);
char val29 = R7.GetCharacterInverse(val28);
int val30 = Rotor::CharacterMap(val29);
char val31 = R6.GetCharacterInverse(val30);
int val32=Rotor::CharacterMap(val31);
char val33=R5.GetCharacterIndex(val32);
int val34=Rotor::CharacterMap(val33);
char val35=R4.GetCharacterIndex(val34);
int val36=Rotor::CharacterMap(val35);
char val37=R3.GetCharacterIndex(val36);
int val38=Rotor::CharacterMap(val37);
char val39=R2.GetCharacterIndex(val38);
int val40=Rotor::CharacterMap(val39);
char val41=R1.GetCharacterIndex(val40);
ciphertext[i] = plugboard(val41);
R1.AdvanceRotor(1);
if((R1.GetSteps()%36)==0)
{
R2.AdvanceRotor(1);
if((R2.GetSteps()%36)==0)
{
R3.AdvanceRotor(1);
if((R3.GetSteps()%36)==0)
{
R4.AdvanceRotor(1);
if((R4.GetSteps()%36)==0)
R5.AdvanceRotor(1);
{
if((R5.GetSteps()%36)==0)
R6.AdvanceRotor(1);
{
if((R6.GetSteps()%36)==0)
R7.AdvanceRotor(1);
{
if((R7.GetSteps()%36)==0)
R8.AdvanceRotor(1);
{
if((R8.GetSteps()%36)==0)
R9.AdvanceRotor(1);
{
if((R9.GetSteps()%36)==0)
R10.AdvanceRotor(1);
}
}
}
}
}
}
}
} // problem #2 missing brace
else {ciphertext[i] = cleartext[i];}
}
return ciphertext;
}
}


Protoman写道:
Protoman wrote:

我''我写了这个程序模拟一个36个字符,10个转子

互易转子密码,带有一个插板。我有什么方法可以使

插板功能更紧凑和/或能够在运行时更改映射




char Enigma :: plugboard(char char)

{

if(Char ==''A'')

返回''0' ';

否则if(Char ==''B'')

返回''Q'';
I''ve written this program that simulates a 36 character, 10 rotor
reciprocal rotor cipher, w/ a plugboard. Any way I can make the
plugboard function more compact and/or be able to change the mapping
at runtime?

char Enigma::plugboard(char Char)
{
if(Char==''A'')
return ''0'';
else if(Char==''B'')
return ''Q'';



....


将所有输出放入char数组[] = {''0'', ''Q'',...};

返回数组[Char - ''A''];


在运行时更改映射你可以把文件读进阵列。


-

Scott McPhillips [MVP VC ++]

....

Put all the outputs in a char array[] = {''0'', ''Q'', ...};
return array[Char - ''A''];

To change the mapping at run time you could read a file into the array.

--
Scott McPhillips [MVP VC++]


这篇关于有什么方法可以使这段代码更紧凑,和/或能够在运行时更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆