我编写了一个函数来搜索数组中的char,如果找到则返回其后继,否则返回-1.然后,如果单词以元音结尾,则算法会添加辅音,反之亦然.
这段代码运行良好,即使文件的最后一个字:
changedChar = cipherChar(character, consonants, tConsonants);
if (changedChar != -1) charType = 'c';
else {
changedChar = cipherChar(character, CONSONANTS, tConsonants);
if (changedChar != -1) charType = 'c';
else {
changedChar = cipherChar(character, vowels, tVowels);
if (changedChar != -1) charType = 'v';
else {
changedChar = cipherChar(character, VOWELS, tVowels);
if (changedChar != -1) charType = 'v';
else {
changedChar = cipherChar(character, others, tOthers);
if (changedChar != -1) charType = 'o';
else {
changedChar = changeDigit(character);
if (changedChar != -1) charType = 'o';
else {
changedChar = cipherChar(character, punctuation, tPunctuation);
if (changedChar != -1) charType = 'o';
}
}
}
}
}
}
if (changedChar != -1) outFile << changedChar;
if (searchChar(inFile.peek(), punctuation, tPunctuation) > -1)
if (charType == 'v') {
outFile << consonants[nVowel];
nVowel < 4 ? nVowel++ : nVowel = 0;
}
else if (charType == 'c') {
outFile << vowels[nConsonant];
nConsonant < 20 ? nConsonant++ : nConsonant = 0;
}
Run Code Online (Sandbox Code Playgroud)
但是这个其他的不会在文件的最后一个字后添加一个额外的字母:
charType = 'c';
changedChar = cipherChar(character, consonants, tConsonants);
if (changedChar == -1) {
changedChar = cipherChar(character, CONSONANTS, tConsonants);
if (changedChar == -1) {
charType = 'v';
changedChar = cipherChar(character, vowels, tVowels);
if (changedChar == -1) {
changedChar = cipherChar(character, VOWELS, tVowels);
if (changedChar == -1) {
charType = 'o';
changedChar = cipherChar(character, others, tOthers);
if (changedChar == -1) {
changedChar = changeDigit(character);
if (changedChar == -1) changedChar = cipherChar(character, punctuation, tPunctuation);
}
}
}
}
}
if (changedChar != -1) outFile << changedChar;
if (searchChar(inFile.peek(), punctuation, tPunctuation) > -1)
if (charType == 'v') {
outFile << consonants[nVowel];
nVowel < 4 ? nVowel++ : nVowel = 0;
}
else if (charType == 'c') {
outFile << vowels[nConsonant];
nConsonant < 20 ? nConsonant++ : nConsonant = 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么?我真的很困惑.