测试回文时程序不会继续运行 (C++)
Program Won't Continue to Run When Testing for Palindromes (C++)
我是C++编码的初学者。我正在尝试制作一个完成一组特定任务的程序:
- 打开一个数据文件,
- 取文件的每一行,
- 通过删除所有空格和标点符号来处理每一行,
- 将字符串转换为全部小写,
- 使用递归方法来测试字符串是否为回文,
- 在字符串中查找一个位置,可以在其中添加字符以使其成为回文(如果尚未添加),
- 然后添加使其在 (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 插入的字符是 开始帕利德罗姆添加 尼帕 结语:尼帕拉平
原线:拉帕尔 加工生产线:拉帕尔 线是回文。
相关文章:
- 运行同一解决方案的另一个项目的项目
- CMake-按正确顺序将项目与C运行时对象文件链接
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- 代码在main()中运行,但在函数中出现错误
- 我在c++代码中生成了一个运行时#3异常
- 如何在linux终端中同时编译和运行c++代码
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何在运行中期切换GTK CSS style_context
- 如何在MS Visual Studio 2019中运行QT UI
- 如何通过cpp程序运行shell脚本
- IPC使用多个管道和分支进程来运行Python程序
- 删除指向指针的指针是运行时错误吗
- 如何用参数值调用函数(仅在运行时已知)
- 为什么即使使用-cudart-static进行编译,库用户仍然需要链接到cuda运行时
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- c++中的指针和运行时错误
- 在C应用程序中运行C++(带有STL)函数
- 运行程序时出现问题
- 控制允许动态运行c++的并发操作数
- 无法获取菜单选择以运行函数.C++