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 在这种情况下,开关可能更紧凑: 开关(字符){ 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 firstchar 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 braceelse {ciphertext[i] = cleartext[i];}}return ciphertext;}} .... 将所有输出放入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++] 这篇关于有什么方法可以使这段代码更紧凑,和/或能够在运行时更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-20 23:27