用C语言编....想了很久还是不知道怎么做
[CODE]
#include<iostream.h>
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#define slen 100
struct stack
{
int stacksize;
char *base,*top;
}
initstack(stack &s)
{
s.base=new char;
if(!s.base) cout<<"初始化空间失败!"<<endl;
s.top=s.base;
s.stacksize=100;
return s ;
}
void push(stack &s,char e)
{
if(s.top-s.base>=s.stacksize)
{
s.base=(char *)realloc(s.base,(s.stacksize+10)*sizeof(char));
if(!s.base) cout<<"堆栈已经满!"<<endl;
s.top=s.base+s.stacksize;
s.stacksize+=10;
}
*s.top++=e;
}
pop(stack &s,char e)
{
if(s.top==s.base) cout<<"stack if empty!"<<endl;
else e=*--s.top;
return e;
}
main()
{
stack t;
int i=0,j;
char ch[30],c;
char str[]={"输入格式不正确!"};
cout<<"输入字符串并且以@字符结束例如: 123a&a321@ "<<endl;
initstack(t);
cin.getline(ch,30);
j=strlen(ch);
if(ch[j-1]!='@'||ch[j/2-1]!='&') {cout<<str<<endl; return; }
for(i=0;i<j-1;i++)
{ if(ch[i]=='&') break;
push(t,ch[i]);
}
for(++i;i<j-1;i++)
{ if(ch[i]!=pop(t,c))
{cout<<"该字符串不是回文!"<<endl; return ;}
}
cout<<"该字符串是回文!"<<endl;
return;
}
[/CODE]