数据结构c语言版 :算法设计题 求大神解答。。在线等。。。

题目如下:

二、算法设计题(exp3-1.c)

1、回文测试

【问题描述】

试写一个算法判定给定的字符向量是否为回文(只需要把IsHuiwen函数中的__________替换成正确的语句即可)。 
题目是 栈和队列 这一章的。。。

第1个回答  2013-04-12
/*判断一段字符串是不是回文,所谓回文,也就
是正读反读都一样,如asdffdsa*/

#include<iostream>
#include<stdio.h>
using namespace std;
const int size=100;
class HuiWen
{
private:
char ch[100];
int top;
public:
HuiWen(){top = -1;};
void input(char c);
bool isHuiWen();
bool empty();
};
void HuiWen::input(char c)
{
//if(top==size-1) throw"上溢";
top++;
//cin>>ch[top];
ch[top]= c;
}
bool HuiWen::isHuiWen()
{
for(int i=0;i<top;i++)
if(ch[top--]!=ch[i])
return 0;
return 1;
}
bool HuiWen::empty()
{
if(top==-1)
return 1;
return 0;
}
int main()
{
HuiWen hw;
cout<<"请输入字符串,以回车键结束"<<endl;
int c;
while((c=getchar())!='\n')
hw.input(c); //主要就是这里做了修改。

if(hw.empty())
cout<<"对不起,无字符"<<endl;
else
{
if(hw.isHuiWen())
cout<<"该字符串是回文"<<endl;
else
cout<<"该字符串不是回文"<<endl;
}
return 0;
}
第2个回答  2013-04-12
大概的原理是这样,你的题目没给清楚代码。
//返回TRUE或者非零表示是回文,返回FALSE或者0表示不是回文
int IsHuiwen(char *str)

{
int top, len;

char stack[MAX_SIZE]; //用数组模拟顺序栈

top = 0;

len = strlen(str);

while (top <= len/2) //将前一半的字符入栈

{
stack[top++] = *str++;

}

if (len%2 != 0) //如果有奇数个字符,那中间的那个是对称点,不用判断,直接出栈即可

{
--top;

}

while (top >= 0)

{
if (stack[top--] != *str++) //挨个出栈和后一半的字符比较,有一个不匹配就说明不是回文

return FALSE;

}

return TRUE; //说明是回文

}
以上代码未测试,有问题再问。
第3个回答  2013-04-11
题目不完整,自己写一个
bool isHuiwen(char *p)
{
int l=strlen(p);
for(i=0;i<l/2;++i)
{
if(p[i]!=p[l-1-i])
return false;
}
return true;
}
相似回答