【发布时间】:2014-04-24 23:31:44
【问题描述】:
我正在研究 C++ 中的回文检测器,它读取文件并用指示符“*”标记回文行。这就是我所拥有的。
PalindromeDetector::PalindromeDetector(const string& iFile, const string& oFile) {
myInFile = iFile;
myOutFile = oFile;
}
void PalindromeDetector::detectPalindrome() {
ifstream fin(myInFile.data());
ofstream fout(myOutFile.data());
string nLine, palLine;
while (getline(fin, nLine)){
if (isPalindrome(nLine)){
fout << nLine << " ***";
} else {
fout << nLine;
}
}
fin.close();
fout.close();
}
bool PalindromeDetector::isPalindrome(const string& str) {
Stack<char> charStack(1);
ArrayQueue<char> charQueue(1);
char ch1, ch2;
for ( unsigned i = 0; i < str.size(); i++){
if (isalnum (str[i])){
tolower(str[i]);
try {
charStack.push(str[i]);
charQueue.append(str[i]);
} catch ( StackException& se ){
charStack.setCapacity(charStack.getCapacity() * 2);
charQueue.setCapacity(charQueue.getCapacity() * 2);
charStack.push(str[i]);
charQueue.append(str[i]);
}
} else {
while ( !charStack.isEmpty() || !charQueue.isEmpty() ){
ch1 = charStack.pop();
ch2 = charQueue.remove();
if ( ch1 != ch2 ){
return false;
}
}
}
}
return true;
}
到目前为止,我遇到了 2 个问题: 1. 没有正确输出行尾带有“*”的文件;出于某种原因,它在前面做。 2. 它只标记文件每个块中的第一行,而不是回文的行。 我非常感谢您对此的帮助。
【问题讨论】:
标签: c++ stack queue palindrome