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

标签 c++ function c++11 palindrome

我是 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 线是回文。

关于c++ - 测试回文时程序不会继续运行 (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49852028/

相关文章:

javascript - 仅使用一个 JavaScript 函数更改标签中的字体大小和文本

c++ - 记录代码头和源代码

c++ 当你在双端队列中 push_back 或 push_front 时真正发生了什么

无法将两个作为文件名的命令行参数传递给函数

c++ - 缓存 std::vector 的大小是否更快

c - 关于在数组中查找最大的函数问题

c++ - 无法使用迭代器删除 vector 元素, vector 元素实现移动构造函数

c++ - 如何通过英特尔 C++ 编译器 (ICC) 在三种方法上使用 SFINAE?

C++如何在for循环中计算一次而不是多次

c++ - WinAPI 函数 LoadLibrary() 导致函数在执行错误期间失败