[英]Program Won't Continue to Run When Testing for Palindromes (C++)
我是C ++编码的初学者。 我正在尝试制作一个可以完成一组特定任务的程序:
我只允许使用带有特定参数的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;
}
这是我得到的输出:
原始行:lappal
加工线:lappal
线是回文。
-
原始行:拉帕尔
加工线:拉帕尔
线是回文。
-
原线:一个人,一个计划,一条运河,巴拿马!
加工线:amanaplanacanalpanama
线是回文。
-
原始行:圈
加工线:搭接
线不是回文。
当它说“ 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++;
}
一些在此处运行原始行:lap处理过的行:lap行不是回文。 start palindrom fix插入到位置0的字符是开始回文加法pa最后一个词:palap
原始行:lapin处理过的行:lapin行不是回文。 start palindrom fix插入到位置0的字符是start palidrome add nipa最后一个词:nipalapin
原始行:lapal处理过的行:lapal行是回文。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.