这个程序是判断是不是回文的。可是我运行时,不管输入的是什么,判断结果都是回文。
#ifdef__BORLANDC__//suppressthewarningmessageaboutcomparingsignedandunsignedvalues#pr...
#ifdef __BORLANDC__
// suppress the warning message about comparing signed and unsigned values
#pragma warn -8012
#endif // __BORLANDC__
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#ifdef _MSC_VER
// compensates for VC++ 6.0 bug in getline() from <string>
#include "d_util.h"
#endif // _MSC_VER
using namespace std;
int main()
{
string text;
char ch;
// q records characters in order of occurrence
queue<char> q;
// s records characters in reverse order of occurrence
stack<char> s;
int i;
cout << "Enter a line of text in lowercase:" << endl;
getline(cin, text);
// cycle through the text
for (i=0;i < text.length();i++)
{
ch = text[i];
// if ch is a letter, push it on the queue and the stack
//每个字母分别加入到队列和栈中
if (ch >= 'a' && ch <= 'z')
{
q.push(ch);
s.push(ch);
}
}
// compare the front of the queue with the top
// of the stack until both are empty or we find
// two characters that are not equal
//比较队首元素和栈顶元素,直到队列和堆栈都为空,或者发现两者的值不相等
while (!q.empty())
{
if (q.front() != s.top())
break;
q.pop();
s.pop();
}
// if the queue is empty, we have a palindrome
if (!q.empty())
cout << text << " is a palindrome" << endl;
else
cout << text << " is not a palindrome" << endl;
return 0;
}
/*
Run 1:
Enter a line of text in lowercase:
go hang a salami i'm a lasagna hog
go hang a salami i'm a lasagna hog is a palindrome
Run 2:
Enter a line of text in lowercase:
abcdefdcba
abcdefdcba is not a palindrome
*/ 展开
// suppress the warning message about comparing signed and unsigned values
#pragma warn -8012
#endif // __BORLANDC__
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#ifdef _MSC_VER
// compensates for VC++ 6.0 bug in getline() from <string>
#include "d_util.h"
#endif // _MSC_VER
using namespace std;
int main()
{
string text;
char ch;
// q records characters in order of occurrence
queue<char> q;
// s records characters in reverse order of occurrence
stack<char> s;
int i;
cout << "Enter a line of text in lowercase:" << endl;
getline(cin, text);
// cycle through the text
for (i=0;i < text.length();i++)
{
ch = text[i];
// if ch is a letter, push it on the queue and the stack
//每个字母分别加入到队列和栈中
if (ch >= 'a' && ch <= 'z')
{
q.push(ch);
s.push(ch);
}
}
// compare the front of the queue with the top
// of the stack until both are empty or we find
// two characters that are not equal
//比较队首元素和栈顶元素,直到队列和堆栈都为空,或者发现两者的值不相等
while (!q.empty())
{
if (q.front() != s.top())
break;
q.pop();
s.pop();
}
// if the queue is empty, we have a palindrome
if (!q.empty())
cout << text << " is a palindrome" << endl;
else
cout << text << " is not a palindrome" << endl;
return 0;
}
/*
Run 1:
Enter a line of text in lowercase:
go hang a salami i'm a lasagna hog
go hang a salami i'm a lasagna hog is a palindrome
Run 2:
Enter a line of text in lowercase:
abcdefdcba
abcdefdcba is not a palindrome
*/ 展开
2个回答
展开全部
将主函数中的如下程序段:
while(s[d.top])
{
if ((c=s[d.top])!=' ')
push( &d,c);
else d.top++;
}
改为如下形式:
for (i = 0; s[i]; i++)
if (s[i]!=' ') push(&d,s[i]);
while(s[d.top])
{
if ((c=s[d.top])!=' ')
push( &d,c);
else d.top++;
}
改为如下形式:
for (i = 0; s[i]; i++)
if (s[i]!=' ') push(&d,s[i]);
追问
我的主函数哪里有
while(s[d.top])
{
if ((c=s[d.top])!=' ')
push( &d,c);
else d.top++;
}
这一段???
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询