我正在编写一个程序来确定用户输入的字符串是否是回文。该程序编译,但是当输出打印时,即使不是,也将所有字符串确定为回文。我读了不同时间的教科书,审查和调试了几十次代码,看了其他类似的回文问题,但我还是输了。
我的代码如下:
#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;
}
}
如果有人能解释为什么会这样,我将不胜感激。谢谢!
答案 0 :(得分:3)
这个条件
if(isPalindrome==false && (s.empty() && q.empty()))
可以等于真的永远。:)
因为你在循环中将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;
}
答案 1 :(得分:0)
在最后一个if
语句中,您不必要地检查堆栈/队列是否为空,因为如果上面的isPalindrome
错误,它们很可能不会。
另外,看起来你想要放
cout<<"True or false: "<<isPalindrome<<endl;
在if
语句之前,以便它始终运行。
答案 2 :(得分:0)
嗯,对于初学者来说,你推动整数而不是字符。要推送字符串中的字符,请使用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];
}
}
答案 3 :(得分:0)
#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;
}