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

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

我是 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;
}

这是我得到的输出:

Original line: lappal

Processed line: lappal

Line is palindrome.

-

Original line: lapal

Processed line: lapal

Line is palindrome.

-

Original line: A man, a plan, a canal, Panama!

Processed line: amanaplanacanalpanama

Line is palindrome.

-

Original line: lap

Processed line: lap

Line is NOT a palindrome.

就在那里,当它说 "Line is NOT a palindrome," 时,它应该跟进如下所示的内容:

Characters to insert at location 0 are pa

Final line: palap

它停在 "Line is NOT a palindrome." 谁能看出我哪里出错了?

如有任何帮助,我们将不胜感激。

你这里的循环(回文加法)有问题

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++;
        }

一些 运行 这里 原线:lap 加工线:圈 线不是回文。 开始回文修复 要在位置 0 插入的字符是 开始回文添加 帕 最后一句话:palap

原文:lapin 加工线:lapin 线不是回文。 开始回文修复 要在位置 0 插入的字符是 开始回文添加 尼帕 最后一句话:尼帕拉平

原文:lapal 加工线:lapal 线是回文。