中心对称表达式的判定----《数据结构》

#include<stdio.h>  
#include<malloc.h>  
#include<stdlib.h>  
#include<string.h> 

typedef struct{
	char *base;
	char *top;
	int stacksize;
}SqStack; 

int InitStack(SqStack &S){
	S.base = (char *)malloc(100*sizeof(char));
	if(!S.base){
		printf("储存空间分配失败");
		exit(-2);
	}
	S.top = S.base;
	S.stacksize = 100;
	return 1;
}

char GetTop(SqStack S){
	if(S.top!=S.base)
 
	return *(S.top-1); 
}

int Push(SqStack &S,char e){
	if(S.top-S.base==S.stacksize){
		/**printf("栈满");
		S.base = (char *)realloc(S.base,(S.stacksize+10)*sizeof(char));
		if(!S.base){
			printf("储存空间分配失败");
			exit(-2);
		}
		S.top = S.base;
		S.stacksize+=10; */
		return 0; 
	}
	
	*S.top++ = e;
	return 1;
}
int Pop(SqStack &S,char &e){
	if(S.base!=S.top)
	e = *--S.top;
	return 1;
}

int main(){
	SqStack S,W;
	int i=0; 
	char a[100],x,y;
	InitStack(S);
	InitStack(W);
	printf("请输入表达式,以!为结束标志:"); 
	x = getchar();
	while(x!='!') {
		Push(S,x); 
		a[i]=x;
		i++; 
		x=getchar() ; 
	} 
	for(int j=0;j<i;j++){
		Pop(S,y); 
		if(a[j]!=y){
			printf("不中心对称");
			return 0; 
				} 
		} 
	
	printf("中心对称");
	return 0; 
	
}

 

posted @ 2017-11-26 21:24  自由的背包  阅读(717)  评论(0编辑  收藏  举报