我正在处理一个项目的一部分有点麻烦。 我一直在搞乱代码,但我无法让它发挥作用。 基本上,该项目执行以下操作:
我被困在第4位。
这是我的代码:
#include <iostream>
#include <string>
using namespace std;
void fixRepeats(char array[], int size); //Function to remove duplicates
void fixSpaces(char array[], int size); //Function to remove spaces
void copy(char array[], char array2[], int size);
int main()
{
char word[25];
char modWord[25]; //User inputted word without repeated letters
char regAlpha[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char altAlpha[27]; //Alternate alphabet created from the word
char sentence[100];
int lenWord;
int lenModWord;
int lenAlpha = 27;
int lenSent;
cout << "Please enter a single word: "; //Get user input
cin >> word;
lenWord = strlen(word); //Get length of word
fixRepeats(word, lenWord); //Function call from main
fixSpaces(word, lenWord); //Function call from main
lenModWord = strlen(word); //Get length of the modified word
copy(word, modWord, lenModWord); //Function call from main
copy(modWord, altAlpha, lenModWord); //Function call from main
for (int i = 0; i < lenAlpha; i++)
{
for (int j = 0; j < lenModWord; j++)
{
if (regAlpha[i] == altAlpha[j])
{
regAlpha[i++];
}
}
altAlpha[lenModWord++] = regAlpha[i];
}
fixRepeats(altAlpha, lenAlpha);
fixSpaces(altAlpha, lenAlpha);
cout << "\nModified Word: " << word << endl;
cout << "\nAlphabet------: " << regAlpha << endl;
cout << "Encryption Key: " << altAlpha << endl;
cin.ignore(100, '\n');
cout << "\nPlease enter a sentence: "; //Get user input
cin.get(sentence, 99, '\n');
lenSent = strlen(sentence);
//-------------------------------#4------------------------------------
for (int i = 0; i < lenAlpha; i++)
{
for (int j = 0; j < lenSent; j++)
{
if (altAlpha[i] == sentence[j])
{
sentence[j] = regAlpha[i];
}
}
}
//-------------------------------#4------------------------------------
cout << "\nEncrypted message: " << sentence << endl;
cout << endl;
return 0;
}
void copy(char array[], char array2[], int size)
{
for (int i = 0; i < size; i++)
{
array2[i] = array[i];
}
}
void fixRepeats(char array[], int size)
{
//Use nested loop to check each character
for (int i = 0; i <= size; i++)
{
for (int j = i + 1; j <= size; j++)
{
if (array[i] == array[j]) //Check for duplicates
{
array[j] = '\0'; //Change duplicate to a null char
}
}
}
}
void fixSpaces(char array[], int size)
{
//Use nested loop to check each character
for (int i = 0; i < size; i++)
{
if (array[i] == '\0') //Check for space or null char
{
for (int j = i + 1; j < size; j++)
{
if (array[j] != '\0') //Check the next position
{
swap(array[j], array[i]); //Swap positions
break;
}
}
}
}
}
我在代码中用破折号和#4标记了我的问题。
期望的结果是这样的:
Please enter a single word: HELLO
Modified Word: HELO
Alphabet------: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Encryption Key: HELOABCDFGIJKMNPQRSTUVWXYZ
Please enter a sentence: HE IS HUMAN
Encrypted message: DA FS DUKHM
使用相同单词的另一个例子是:
Please enter a sentence: COMPUTER SCIENCE
Encrypted message: LNKPUTAR SLFAMLA
Please enter a sentence: PLEASE HELP
Encrypted message: PJAHSA DAJP
当我运行我的代码时,它变成了这样的东西:
Please enter a single word: HELLO
Modified Word: HELO
Alphabet------: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Encryption Key: HELOABCDFGIJKMNPQRSTUVWXYZ
Please enter a sentence: HE IS HUMAN
Encrypted message: EO OS EUOEO
和
Please enter a sentence: COMPUTER SCIENCE
Encrypted message: LHOPUTOR SLOOOLO
Please enter a sentence: PLEASE HELP
Encrypted message: PLOESO EOLP
其他一切都很好,它只是加密部分的错误。 我也知道它完全错了(这真的是一次尝试)。
有关如何修复单个部分的任何建议(假设它可以用于输入的任何单词)?
感谢。对于冗长的帖子感到抱歉。
答案 0 :(得分:0)
您正在使用目标字母表进行检查,然后使用错误的索引进行修改,使用源字母表。
这:
Key1, Key2, 7, 10
Key2, Key3, 11, 14
应该是:
System.Drawing.SystemIcons.Error.ToBitmap().Save("Error.png", System.Drawing.Imaging.ImageFormat.Png);
中断是必要的,以避免多次加密某些字符。
您还需要更改循环的顺序,以避免多次加密某些字符。所以这个:
if (altAlpha[i] == sentence[j])
{
sentence[i] = regAlpha[i];
}
成为:
if (regAlpha[i] == sentence[j])
{
sentence[j] = altAlpha[i];
break;
}
将它们拼接在一起,您的for (int i = 0; i < lenAlpha; i++)
{
for (int j = 0; j < lenSent; j++)
循环应该如下所示:
for (int j = 0; j < lenSent; j++)
{
for (int i = 0; i < lenAlpha; i++)
答案 1 :(得分:0)
您需要将句子中的字母更改为altAlpha元素,但您需要使用lenAlpha中字母的位置索引。
所以,你应该修改这个部分:
for (int i = 0; i < lenAlpha; i++)
{
for (int j = 0; j < lenSent; j++)
{
if (altAlpha[i] == sentence[j])
{
sentence[j] = regAlpha[i];
}
}
}
像
for (int i = 0; i < lenSent; i++)
{
for (int j = 0; j < lenAlpha; j++)
{
if (sentence[i] == regAlpha[j])
{
sentence[i] = altAlpha[j];
}
}
}