用栈实现中心对称符号的检验!一

来源:互联网 发布:笑果软件融合 编辑:程序博客网 时间:2024/04/29 21:46

如:

输入:12@21#

输出:该序列对称

刚学的,有什么请指教!!

#include <stdio.h>//实现中心对称的字符的匹配
#include <malloc.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
typedef char SElemType;
typedef int Status;
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef  struct{
 SElemType *base;
 SElemType *top;
 int stacksize;
}SqStack;
Status InitStack(SqStack &S)
{
   S.base=(SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType));
   if(!S.base)return ERROR;
   S.top=S.base;
   S.stacksize=STACK_INIT_SIZE;
   return OK;
}
Status push(SqStack &S,SElemType &c1)
{
 if(S.top-S.base>=S.stacksize){
  S.base=(SElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT) * sizeof(SElemType));
  if(!S.base)return ERROR;
  S.top=S.base+S.stacksize;
  S.stacksize+=STACKINCREMENT;
  } 
 *S.top++=c1;
 return OK;
}
Status gettop(SqStack &S,SElemType &e1)
{
 if(S.top==S.base)return ERROR;
 e1=*(S.top-1);
 return OK;
}//获得栈顶元素
Status pop(SqStack &S,SElemType e)
{
 if(S.top==S.base)return ERROR;
 e=*--S.top;
 return OK;
}
Status StackEmpty(SqStack &S)
{
 if(S.top==S.base)
  return OK;
 else
  return ERROR;
}
int main()
{
   SqStack S;
 InitStack(S);
 int a[10];int i;
 int b[10];int j;
 int k=0;int k1=0;
 char c1,e1,e;
 printf("请输入数据:\n");
 while(c1!='#')
 {
  scanf("%c",&c1);
  push(S,c1);
 }
 printf("输入完毕\n");
 pop(S,e1);//为什么,#都没入栈
 for(i=0;i<=10;i++)
 {
  gettop(S,e1);
  if(e1=='@')break;
        a[i]=e1;k++;
  pop(S,e1);
  
 }
 pop(S,e1);
 for(j=0;j<10;j++)//12@21#
 {
  if(!StackEmpty(S))
  {
   gettop(S,e);
      b[j]=e;
   k1++;
   pop(S,e);
  }
 else
  if(StackEmpty(S))break;
 }
 for(i=0;i<=k-1;i++)
 printf("%c",a[i]);
 printf("\n");
 for(j=0;j<=k1-1;j++)
  printf("%c",b[j]);
// if(k!=k1)printf("该序列不对称");
 //else
 k--;k1--;
    for(i=0,j=k1;i<=k,j>=0;i++,j--)
 { if(a[i]!=b[j])
          {
            printf("该序列不对称");
            break;
            }
  else
   if(a[i]==b[j]&&i==k)printf("该序列对称");
 }
 system("pause");
 return OK;
}
原创粉丝点击