主题:[讨论]跪求一道数据结构的上机题~~
zxlly
[专家分:30] 发布于 2006-04-03 12:43:00
小弟是计算机的初学者~~现在有一道题~上机就是调试不出来~~有哪位高手可以教教我~`先谢谢了~~~[em5]
假设称正读和反读都相同的字符序列为回文,例如:abba 和 abcba都是回文~,
abcde和ababab,则不是回文,试写一个算法判别读如一个以@为结束符的字符是否是回文~~~
就是这个题~要上机可以运行出来的哦~~
回复列表 (共12个回复)
11 楼
中国台湾 [专家分:2140] 发布于 2006-04-06 22:02:00
是严蔚敏习题上面的题目吧 我也写过
上次 写的很麻烦 还有点错误 tianyuan008帮我修改真确了 我发给你看
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define OK 1
#define ERROR 0
#define TURE 1
#define FALSE 0
#define OVERFLOW -1
#define INIT_STACK_SIZE 10
#define STACKINCREMENT 10
#define N 7 /*确定输入的字符长度*/
typedef char SElemType; /* 从你的下文看,栈里保存的是字符型 */
typedef int status ;
typedef struct{
SElemType *base; /*栈结构*/
SElemType *top;
int stacksize;
}SqStack;
status InitStack (SqStack *S) /*初始化一个栈*/
{
S->base=(SElemType*)malloc(INIT_STACK_SIZE*sizeof(SElemType));
if(!S->base)
{
printf("out of memory");
exit(OVERFLOW);
}
S->base=S->top;
S->stacksize=INIT_STACK_SIZE;
return OK;
}
status PushStack(SqStack *S,char e) /*进栈操作*/
{
if(S->top-S->base>=S->stacksize)
{
S->base=(SElemType*)realloc(S->base,(S->stacksize+STACKINCREMENT )*sizeof(SElemType));
if(!S->base)
{
printf("out of memory");
exit(OVERFLOW);
}
S->top=S->base+S->stacksize;
S->stacksize+=STACKINCREMENT;
}
*++S->top=e; /*这里你出错了*/
return OK;
}
status PopStack(SqStack *S,char *e) /*出栈操作*//*这里你声明有问题*/
{
if(S->base==S->top)
return ERROR;
*e=*S->top--; /*这里你出错了*/
return OK;
}
status StackEmpty(SqStack S)
{
if(S.base==S.top)
return TURE;
return FALSE;
}
/*status strcmp(char *pointer1,char *pointer2) /*判断字符串是否相等*/
/*{
int i=0;
for(;i<N;pointer1++,pointer2++)
{
if(*pointer1==*pointer2)
continue;
else
return 1;
}
return 0;
}*//* 上面这个函数完全可以用 string.h 中的标准 C 函数 strncmp 代替*/
int main(void)
{
char a[N]={0},b[N]={0};
char ch;
char e;
int i=0,j=0;
SqStack S;
if(!InitStack(&S)){ /*必须先初始化栈*/
printf("Error in initialling stack!");
system("pause");
return -1;
}
printf("please input the string ");
scanf("%c",&ch);
while(ch!='@'&&i<7)
{
PushStack(&S,ch);
b[i++]=ch;
scanf("%c",&ch);
}
while(!StackEmpty(S)) /* 这里又出错了 */
{
PopStack(&S,&e);
a[j++]=e;
}
printf("%d",strncmp(a,b,N));
if(!strncmp(a,b,N))
printf("输入的字符串是回文的");
else
printf("输入的字符串不是回文的");
system("pause");
return 0;
}
12 楼
中国台湾 [专家分:2140] 发布于 2006-04-06 22:06:00
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define OK 1
#define ERROR 0
#define TURE 1
#define FALSE 0
#define OVERFLOW -1
#define INIT_STACK_SIZE 10
#define STACKINCREMENT 10
#define N 7 /*确定输入的字符长度*/
typedef char SElemType; /* 从你的下文看,栈里保存的是字符型 */
typedef int status ;
typedef struct{
SElemType *base; /*栈结构*/
SElemType *top;
int stacksize;
}SqStack;
status InitStack (SqStack *S) /*初始化一个栈*/
{
S->base=(SElemType*)malloc(INIT_STACK_SIZE*sizeof(SElemType));
if(!S->base)
{
printf("out of memory");
exit(OVERFLOW);
}
S->base=S->top;
S->stacksize=INIT_STACK_SIZE;
return OK;
}
status PushStack(SqStack *S,char e) /*进栈操作*/
{
if(S->top-S->base>=S->stacksize)
{
S->base=(SElemType*)realloc(S->base,(S->stacksize+STACKINCREMENT )*sizeof(SElemType));
if(!S->base)
{
printf("out of memory");
exit(OVERFLOW);
}
S->top=S->base+S->stacksize;
S->stacksize+=STACKINCREMENT;
}
*++S->top=e; /*这里你出错了*/
return OK;
}
status PopStack(SqStack *S,char *e) /*出栈操作*//*这里你声明有问题*/
{
if(S->base==S->top)
return ERROR;
*e=*S->top--; /*这里你出错了*/
return OK;
}
status StackEmpty(SqStack S)
{
if(S.base==S.top)
return TURE;
return FALSE;
}
/*status strcmp(char *pointer1,char *pointer2) /*判断字符串是否相等*/
/*{
int i=0;
for(;i<N;pointer1++,pointer2++)
{
if(*pointer1==*pointer2)
continue;
else
return 1;
}
return 0;
}*//* 上面这个函数完全可以用 string.h 中的标准 C 函数 strncmp 代替*/
int main(void)
{
char a[N]={0},b[N]={0};
char ch;
char e;
int i=0,j=0;
SqStack S;
if(!InitStack(&S)){ /*必须先初始化栈*/
printf("Error in initialling stack!");
system("pause");
return -1;
}
printf("please input the string ");
scanf("%c",&ch);
while(ch!='@'&&i<7)
{
PushStack(&S,ch);
b[i++]=ch;
scanf("%c",&ch);
}
while(!StackEmpty(S)) /* 这里又出错了 */
{
PopStack(&S,&e);
a[j++]=e;
}
printf("%d",strncmp(a,b,N));
if(!strncmp(a,b,N))
printf("输入的字符串是回文的");
else
printf("输入的字符串不是回文的");
system("pause");
return 0;
}
我来回复