测试回文时程序不会继续运行 (C++)

Program Won't Continue to Run When Testing for Palindromes (C++)

本文关键字:运行 C++ 继续 回文 程序 测试      更新时间:2023-10-16

我是C++编码的初学者。我正在尝试制作一个完成一组特定任务的程序:

  1. 打开一个数据文件,
  2. 取文件的每一行,
  3. 通过删除所有空格和标点符号来处理每一行,
  4. 将字符串转换为全部小写,
  5. 使用递归方法来测试字符串是否为回文,
  6. 在字符串中查找一个位置,可以在其中添加字符以使其成为回文(如果尚未添加),
  7. 然后添加使其在 (6) 中指定的位置成为回文所需的字符。

我只允许使用 4 个具有特定参数的用户定义函数。到目前为止,我已经有大约 80% 的程序可以工作,但是当它检测到非回文时会出现错误。我希望有人能找出原因。这是我的代码:

// Read file data, check for palindromes, and process strings to palindromes.
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
using namespace std;
string process(string);
bool is_palindrome(string);
int palindrome_fix_location(string);
string palindrome_addition(string, int);
int main()
{
ifstream inFile;
inFile.open("data");
string preString;
int palinLoc = 0;
while(getline(inFile, preString))
{
cout << "nOriginal line: " << preString << endl;
cout << "Processed line: " << process(preString) << endl;
if (is_palindrome(preString) == true)
cout << "Line is palindrome." << endl;
else
cout << "Line is NOT a palindrome." << endl;
palindrome_fix_location(preString);
palindrome_addition(preString, palinLoc);
}
inFile.close();
return 0;
}
// Return a string that is lowercase with no punctuation or spacing.
string process(string preString)
{
string procString;
for (size_t i = 0; i < preString.length(); i++)
{
if (isalnum(preString[i]))
procString += tolower(preString[i]);
}
return procString;
}
// Uses a recursive method to determine if the processed string is a palindrome.
bool is_palindrome(string procString)
{
string temp = process(procString);
int length = temp.length();
string firstChar = temp.substr(0, 1);
string lastChar = temp.substr((length - 1), 1);
if (firstChar == lastChar)
{
temp = temp.substr((0 + 1), (length - 2));
if (temp.length() <= 1) // Base case.
return true;
return is_palindrome(temp); // Recursion.
}
else
return false;
}
// Return a location where text can be added to the non-palindrome to make it a palindrome.
int palindrome_fix_location(string procString)
{
string temp = process(procString);
if (is_palindrome(temp) == false)
{
int palinLoc;
int firstChar = 0, lastChar = temp.length() - 1;
while (firstChar < lastChar)
{
if (temp[firstChar] != temp[lastChar])
{
palinLoc = firstChar;
cout << "Characters to insert at location "
<< palinLoc << " are ";
return palinLoc;
}
}
}
return 0;
}
// Return the text that needs to be added at the "palinLoc" location.
string palindrome_addition(string procString, int palinLoc)
{
string temp = process(procString);
string addedChars;
string finalString;
if (is_palindrome(temp) == false)
{
int firstChar = 0, lastChar = temp.length() - 1;
while (firstChar < lastChar)
{
do {
addedChars += temp[lastChar];
} while (temp[firstChar] != temp[lastChar]);
firstChar++;
lastChar--;
}
finalString = temp.insert(palinLoc, addedChars);
cout << addedChars << endl;
cout << "Final word: " << finalString << endl;
return finalString;
}
else
return finalString;
}

这是我得到的输出:

原台词:拉帕

尔加工生产线:拉帕

尔线是回文。

-

原线:拉帕尔

加工生产线:拉帕尔

线是回文。

-

原文台词:一个人,一个计划,一条运河,巴拿马!

加工生产线:巴拿马阿马纳

线是回文。

-

原线:圈

加工线:圈

线不是回文。

当它说"Line 不是回文"时,它应该跟进看起来像这样的东西:

要在位置 0 插入的字符是 pa

最后一行:帕拉普

它只是止步于"线不是回文"。谁能看到我哪里出了问题?

任何帮助将不胜感激。

这里的循环(回文加法)被窃听

do {
addedChars += temp[lastChar];
} while (temp[firstChar] != temp[lastChar]);

它永远不会结束

所以你应该在里面移动 lastchar 或 firstchar 更改,例如这样

while (firstChar < lastChar)
{   
do {
addedChars += temp[lastChar];
lastChar--;
} while (temp[firstChar] != temp[lastChar])         
firstChar++;
}

有些跑到这里 原线:圈 加工线:圈 线不是回文。 启动回文修复 要在位置 0 插入的字符是 开始帕利德罗姆添加 帕 结语:帕拉普

原线:拉平 加工生产线:拉平 线不是回文。 启动回文修复 要在位置 0 插入的字符是 开始帕利德罗姆添加 尼帕 结语:尼帕拉平

原线:拉帕尔 加工生产线:拉帕尔 线是回文。