2015-04-29 87 views
-2

我有一個包含一個文本文件:爲什麼我的迴文功能會繼續返回錯誤?

1457887541 

Madam 

Able was I ere I saw Elba 

Straw? No, too stupid a fad. I put soot on warts. 

Class is cancelled today 

,當我運行該程序,我得到的所有字符串返回false和想不通這是爲什麼。

#include <iostream> 
#include <fstream> 
#include <string> 
#include <cstring> 
#include <cctype> 

using namespace std; 

bool isPalindrome(string); 

int main() 
{ 
string input; 
fstream nameFile; 


nameFile.open("Xample.txt", ios::in); 


if (nameFile) 
{ 
    cout << "Now reading from file: " << endl; 
    // Read an item from the file. 
    getline(nameFile, input); 

    // While the last read operation 
    // was successful, continue. 
    while (nameFile) 
    { 

    cout << input << endl; 
    //Palindrome function call 
    if(isPalindrome(input)){ 
    cout << "It is a Palindrome :)/> " << endl; 
     } 
    else { 
    cout << "It is not a Palindrome :'(" << endl; 
    } 

    // Read the next string. 
    getline(nameFile, input); 


} 
    //Close when completed 
    cout << "Done!" << endl; 
    nameFile.close(); 
} 
else 
{ 
    cout << "ERROR: Cannot open file.\n"; 
} 
return 0; 
} 



bool isPalindrome(string input){ 


int first = 0; 
int last = input.length() - 1; 



//begin loop to compare first position with last 
while(last > first){ 
//loop if it is not a number or letter 
while(!isalnum(input[first]) && !isalnum(input[last])){ 
    first++; 
    last--; 
    } 
if(tolower(input[first]) != tolower(input[last])){ 
    return false; 
    } 

last--; 
first++; 

} 


return true; 





} 
+5

當您使用調試器逐行執行程序時,您會觀察到哪些異常? –

+0

在函數調用期間,您還沒有在if語句中添加「)」。 –

+0

哎呀,我不知何故增加了一個,但我現在已經修好了。我不知道它是否是我正在使用的在線編譯器,但是我的代碼可以正常使用putty .. – user3247712

回答

2

沒有運行/調試你的代碼,我認爲問題出在你的算法中。代碼的這一部分看起來像是爲了解決空間和標點符號的問題。這是行不通的。如果兩個字符都不是字母字符,那麼您將跳過當前的第一個字符和最後一個字符,但是大多數情況下您只應該跳過一個或另一個字符,因此應該將其分成兩個if語句而不是一個循環。

2

我只是試圖編輯你的代碼並運行它,因爲比爾說這個問題是你的邏輯

while(!isalnum(input[first]) && !isalnum(input[last])){ 
    first++; 
    last--; 
} 

如果輸入[首頁]不是字母數字,你應該只增加它,並在同一時間最後下降。
這是函數的修正versione,看看這樣會更clearn

bool isPalindrome(string input){ 
    int first = 0; 
    int last = input.length() - 1; 

    while(last > first) 
    { 
    if(!isalnum(input[first])) 
     first++; //Increment only first 
    else if(!isalnum(input[last])) 
     last--; //Decrement only last 
    else if(tolower(input[first]) != tolower(input[last])){ 
     return false; 
    } 
    else 
    { 
     last--; 
     first++;  
    }   
    } 
    return true; 
} 
2

答案顯然是「因爲你的代碼不會做你認爲或希望」(不這本身非常有用)。

除非你真的需要「到位」,以檢查數據它幾乎肯定更容易,更可以理解爲複製您所關心的數據,然後檢查結果是否是迴文:

bool is_palindrome(std::string const &input) { 
    std::string temp; 

    std::copy_if(input.begin(), input.end(), std::back_inserter(temp), isalnum); 
    return temp == std::string(temp.rbegin(), temp.rend()); 
} 

如果在原地做這項工作很重要,我仍然試圖從「檢查結果是否迴文」部分中邏輯上分離出「我們不關心的跳過字符」部分。一種方法是將過濾器構建到特定的迭代器中。舉一個例子,封裝過濾到Boost filter_iterator將仍然保持代碼相對直接和可以理解。

+0

個人我會顛倒字符串然後檢查它,我不知道vector :: rbegin和vector ::撕裂,是的,我的解決方案會有效,但你的它更優雅和乾淨。那謝謝啦! – Sid

相關問題