【问题标题】:Stack and Queue Palindrome Program堆栈和队列回文程序
【发布时间】:2015-06-12 14:13:16
【问题描述】:

我正在编写一个程序来确定用户输入的字符串是否是回文。程序可以编译,但是当输出输出时,所有字符串都被确定为回文,即使它们不是。我多次阅读教科书,检查和调试代码数十次,查看其他类似的回文问题,但我仍然迷茫。

我的代码如下:

 #include <iostream>
    #include <stack>
    #include <queue>
    #include <string>
    using namespace std;

    int main (void)
    {
      stack <char> s;
      queue <char> q;
      string letter;
      int length;

      cout<<"Please enter a series of characters."<<endl;
      getline (cin, letter);
      length = letter.size();

      for (int i=0; i<length; i++)
        {
          q.push(i);
          s.push(i);
        }

         bool isPalindrome = true;
         while (isPalindrome==true && (!s.empty() && !q.empty()))
         {
           if (s.top() != q.front())
            {
              isPalindrome = false;
            }
          else
           {
            q.pop();
            s.pop();
           }
         }

       if(isPalindrome==false && (s.empty() && q.empty()))
         {
          cout<<"True or false: "<<isPalindrome<<endl;
          cout<<"Is not a palindrome."<<endl;
         }
       else
         {
           cout<<"Is a palindrome."<<endl;
         }

    }

如果有人能解释为什么会发生这种情况,我将不胜感激。谢谢!

【问题讨论】:

  • 你不应该按“letter[i]”而不是“i”吗?

标签: c++ string queue stack palindrome


【解决方案1】:

这个条件

   if(isPalindrome==false && (s.empty() && q.empty()))

可以等于true never。:)

因为您在循环中将 isPalindrome 设置为 false,而不会从堆栈和队列中弹出相应的元素

       if (s.top() != q.front())
        {
          isPalindrome = false;
        }

因此程序控制总是传递给 else 语句

   else
     {
       cout<<"Is a palindrome."<<endl;
     }

而不是if语句中的错误条件

   if(isPalindrome==false && (s.empty() && q.empty()))

你也可以写

   if (isPalindrome == false )

或者干脆

   if ( !isPalindrome )

或者您可以完全删除可变 isPalindrome 和使用条件

   if ( s.empty() && q.empty() )

或更简单

   if ( s.empty() )

程序可以如下所示

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

int main() 
{
    while ( true )
    {
        std::string letters;
        std::cout << "Please enter a string (Enter - exit): ";
        std::getline( std::cin, letters );

        if ( letters.empty() ) break;

        std::stack<char> 
            s( std::stack<char>::container_type( letters.begin(), letters.end() ) );
        std::queue<char> 
            q( std::queue<char>::container_type( letters.begin(), letters.end() ) );

        while ( !s.empty() && s.top() == q.front() )
        {
            s.pop();
            q.pop();
        }

        if ( s.empty() ) std::cout << "The string is a palindrome" << std::endl;
        else std::cout << "The string is not a palindrome" << std::endl;
    }

    return 0;
}

【讨论】:

  • 感谢您这么快回答!我开始明白你的意思了。我改变了我的代码,但现在我做了相反的事情,让所有用户输入的字符串打印出来,即使它们也不是回文。如果可能的话,你能解释更多吗?感谢您的帮助。
  • @jvi1021 你不需要检查两个容器。 你只是用相同的数据填充了它们。并且由于没有机会从一个没有另一个(你也在控制)弹出,你可以简单地检查 one对于空状态(哪个没有区别)。此外,如果您只是在不匹配时中断循环,则回文检查的结果将反映在任一容器的空状态中。 IE。如果s(或q)为空,则为回文,否则不是。 See it live
  • @VladfromMoscow 感谢您的所有帮助!我非常感谢! (:
  • @WhozCraig 感谢您的所有帮助!我非常感谢! (:
【解决方案2】:

嗯,对于初学者来说,你推送的是整数而不是字符。要推送字符串中的字符,请使用 q.push(letters[i]) 而不是 q.push(i)

您也可以摆脱堆栈并将字符串的一半推入队列,然后将其与另一半进行比较。像这样:

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

using namespace std;

int main (void)
{
  queue <char> q;
  string letter;
  int length;

  cout<<"Please enter a series of characters."<<endl;
  getline (cin, letter);

  bool isPalindrome = false;

  if (letters.size() > 0)
  {
    int length = letter.size() / 2;

    for (int i=0; i<length; i++)
    {
      q.push(letters[i]);
    }

    isPalindrome = true;

    for (int i = 1; i <= length && isPalindrome; ++i)
    {
      isPalindrome = q.front() == letters[letters.size() - i];
      q.pop();
    }
  }

  if(!isPalindrome)
  {
    cout<<"Is not a palindrome."<<endl;
  }
  else
  {
    cout<<"Is a palindrome."<<endl;
  }

  return 0;
}

或者您实际上可以为这样一个简单的任务避免繁重的数据结构,并使用一个简单的循环来执行此操作:

bool isPalindrome = false;
int len = letters.size();

if (len > 0)
{
  isPalindrome = true;

  for (int i = 0; i < len / 2 && isPalindrome; ++i)
  {
    isPalindrome = letters[i] == letters[len - i - 1];
  }
}

【讨论】:

  • 是的!关于推送整数而不是字符的顶部部分和最后一个 if 语句解决了这个问题。谢谢您的帮助!我很感激!! (:
【解决方案3】:

在最后的if 语句中,您不必要地检查堆栈/队列是否为空,因为如果上面的isPalindrome 被设为假,它们很可能不会是。

还有,看来你想放

cout<<"True or false: "<<isPalindrome<<endl;

if 语句之前,使其始终运行。

【讨论】:

  • 谢谢!我很感激!
【解决方案4】:
#include<iostream>
#include <stack>
#include<queue>
#include<string>
using namespace std;
int main() {
queue<char> q;
stack <char> s;
string name;
int count = 0;
cout << "Please enter a name " << endl;
cin >> name;
for (int i = 0; i < name.size(); i++) {
    q.push(name[i]);
    s.push(name[i]);
}
bool check = true;
if (!q.empty() && !s.empty() && check){
    if (s.top() == q.front()) {
        q.pop();
        s.pop();
        cout << " It is a palindrome " << endl;
    }
    else
        cout << " It's not a palindrome" << endl;

    }

system("pause");
return 0;


}

【讨论】:

  • 请为您的代码添加解释,或者之前的代码有什么问题(by @jvi1021)
猜你喜欢
  • 1970-01-01
  • 2013-09-18
  • 1970-01-01
  • 2012-10-11
  • 1970-01-01
  • 1970-01-01
  • 2014-07-21
  • 2015-11-16
  • 2019-03-07
相关资源
最近更新 更多