回文数判断。称正读反读都相同的字符序列为“回文”序列

回文数判断。称正读反读都相同的字符序列为“回文”序列(栈的方式实现)

#include <stdio.h>
#include <stdlib.h>
#include<stdbool.h>
#define size 50
typedef struct{
	char elem[size];
	int top;
}seqstack ;
void initStack(seqstack *s)//初始化 
{
	printf("初始化完成\n");
	s->top=-1;
}
int Push(seqstack *s,char str)//进栈 
{	int i=0;
	if(s->top==size-1) return 1;
	else if (str!='\0')
    {
    		s->top++;
	    s->elem[s->top]=str;
	   
	}
	
	return 0;
}
int pop(seqstack *s ,char *str)//出栈 
{
	if(s->top==-1) return 1;
	else {
	
	 *str=s->elem[s->top];
	  //printf("%c",*str);
	 s->top--;
	 
	}
	return 0;
}
_Bool isempty(seqstack *s)//判空 
{	

	if(s->top==-1) return 0;
	else{
	
		//*ch=s->elem[s->top];
		
		return 1;	    
	}	
}
int main()
{   seqstack s;
    char str[50],ch,sh;
    int i,j,m=0;
	initStack(&s);
	printf("请输入一个字符串\n");
	scanf("%s",&str);//下面是直接匹配 
	for(i=0;str[i]!='&';i++)	
	{
			Push(&s,str[i]);
			
			//printf("%c",str[i]);
	    	
		}
	for(j=i+1;str[i]!='\0';j++)
	{
		if(isempty(&s))
		{
		pop(&s,&ch);
		//printf(" str[j]  %c\n",str[j]); 
		i--;}
		else{
			printf("\n栈空"); 
		}
		
		if(str[j]!=ch&&i>=0)
		{   
			printf("\n不是回文数"); 
			break;
		}
		else if(i==0)
		{
			printf("\n回文数");
			break;
		}
	}
	    		    	
	}

猜你喜欢

转载自blog.csdn.net/weixin_43904021/article/details/87397687