#include <iostream>
#include <string.h>
#define MAXSIZE 100
#define ERROR 0
#define OK 1
#define OVERFLOW -2
using namespace std;
typedef int Status;
typedef char selemtype;
typedef struct{
selemtype *base;
selemtype *top;
int stacksize;
}SqStack;
Status InitStack(SqStack &S)
{
S.base=new selemtype[MAXSIZE];
if(!S.base) return OVERFLOW;
S.top=S.base;
S.stacksize=MAXSIZE;
return OK;
}
Status Push(SqStack &S,selemtype e)
{
if(S.top-S.base==S.stacksize)
return ERROR;
*S.top++=e;
return OK;
}
Status Pop(SqStack &S,selemtype &e)
{
if(S.top==S.base) return ERROR;
e=*--S.top;
return OK;
}
bool StackEmpty(SqStack S)
{
if(S.top!=S.base)
return true;
else
return false;
}
Status HuiWenShu(char *t)
{
int flag=0;
SqStack s;
int i,n;
selemtype e,temp;
InitStack(s);
n=strlen(t);
for(i=0;i<n/2;i++)
{
Push(s,t[i]);
}
for(i=(n%2==0? n/2:(n+1)/2);i<n;i++)
{
Pop(s,e);
temp=e;
if(temp==t[i])
flag=1;
else
{
flag=0;
break;
}
}
return flag;
}
int main()
{
cout<<"请输入长度不大于100的字符序列:\n";
char str[MAXSIZE];
cin>>str;
if(HuiWenShu(str))
cout<<"此序列是回文数!\n";
else
cout<<"此序列不是回文数!\n";
return 0;
}
用栈判断是否回文数
最新推荐文章于 2022-11-03 13:04:00 发布