2011-11-02 29 views
0

我使用堆棧和隊列來檢查給定單詞是否是迴文。我可以將一個新角色推入堆棧,但我不能將多個角色推入隊列。我看不出代碼中的錯誤。任何幫助將不勝感激。以下是使用Dev-C++的C++代碼。謝謝你的時間!找不到爲什麼堆棧工作,但不排隊檢查給定的單詞是否是迴文。

#include <iostream> 
#include <stack> 
#include <queue> 
#include <string> 

using namespace std; 

void push_char() 
{ 

    string givenword; int sizeword, countchar; 
    string letter; 
    stack<string> stackword; string stawo1; 
    queue<string> queueword; string quewo1; 

    cout<<"enter the word to test "<<endl; 
    getline(cin,givenword); 
    string str (givenword); 
    sizeword=str.size(); 
    cout<<" the word given "<<givenword<<" size of word= "<<sizeword <<endl; 
    countchar=0; 
    bool pali=true; 

    while ((countchar<sizeword)) 
    {   
    stackword.push(str.substr(countchar,1)); 
    queueword.push(str.substr(countchar,1)); 
    cout<<" stack letter= "<<stackword.top()<<" queue letter= "<<queueword.front()<<endl; 
    countchar++; 

    if(stackword.top()==queueword.front()) 
     cout<<"same letter found !"<<endl; 
    else 
     pali=false; 

    if (pali==false) 
     cout<<"not a palindrome"<<endl; 
    else 
     cout<<"palindrome!"<<endl; 
    } 
} 

int main() 
{ 
    push_char(); 
} 
+0

你確定你的隊列只推送一個字母嗎?當您執行queuewordfront()時,您將始終在打印輸出中獲得相同的字母,因爲隊列可用作First in First Out。將其更改爲.back(),我敢打賭你不會認爲你得到一個錯誤。 – Genzume

+0

爲了清晰起見,我在代碼中添加了縮進,並且我相信您的一些代碼在循環中不應該是。不知道你打算寫什麼。 –

+0

我嘗試在堆棧和隊列中每次都有新字母時顯示。在顯示屏上,堆棧顯示不同的字母,而隊列只顯示第一個字母。 – T4000

回答

0

僅供參考,您可以使用STR [1 countchar]代替str.substr的(countchar,

幾點:

1)你的代碼是正確的,你的算法是不是。拿起一張紙,並逐步瀏覽你正在做的事情。

2)我看到你想要做的事情......類似這樣的未經測試的代碼,對吧?

for(int i=0; i<sizeword; ++i) { 
    stackword.push(str[i]); 
    queueword.push(str[i]); 
} 

pali = true; 
for(int i=0; i<sizeword; ++i) { 
    if(stackword.top() != queueword.front()) { 
    pali = false; 
    break; 
    } 
    stackword.pop(); 
    queueword.pop(); 
} 
+0

是的。每當堆棧中的當前字符與隊列中的字符不同時,我想停止循環。 – T4000

+0

嘗試上面的更改,我得到了從'char'到'const char *'的無效轉換 – T4000

+0

使堆棧字符和隊列字符堆棧和隊列,而不是字符串 – fileoffset

0

有更好的方法找回文...但這不是你問的問題。

在這一行: 如果(stackword.top()== queueword.front())

是你的錯誤。當你進入一個隊列時,你會在隊列的最後加入。前面不會改變。所以對你來說,它似乎只有一件事。

當您推入堆棧時,它將推到現有堆棧的頂部。

+0

甚至queueword.back()沒有給我一個不同的結果。當我顯示剛插入隊列的字符時,仍然有相同的輸出。 – T4000

0

你在這裏有兩個選擇。第一個是aleph_null建議的 - double pass在第一遍中將字符添加到容器中,然後在第二遍中比較它們。第二種選擇是比較他們的方式,但看看這個詞的另一端。你實際上並不需要任何容器,但用最小的變化你的while循環代碼應該看起來像這樣:

while ((countchar<sizeword)) 
{   
    stackword.push(str.substr(countchar,1)); 
    queueword.push(str.substr(sizeword - 1 - countchar,1)); // adding from the back 
    cout<<" stack letter= "<<stackword.top(); 
    cout<<" queue letter= "<<queueword.back()<<endl; // .back() not .front() 
    countchar++; 

    if(stackword.top()==queueword.back()) // again, looking at the last added char 
    cout<<"same letter found !"<<endl; 
    else 
    pali=false; 

    // as Joe McGrath pointed out this should probably be outside the loop... 
    if (pali==false) 
    cout<<"not a palindrome"<<endl; 
    else 
    cout<<"palindrome!"<<endl; 
} 

希望有所幫助。

羅馬

相關問題