括号匹配

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef struct sqstack{
  char *base;
  char *top;
  int stacksize;
}sqstack;
void creat_stack(sqstack &a){
   a.base=(char *)malloc(100*sizeof(char));
   a.top=a.base;
   a.stacksize=100;
}
void push(sqstack &a,char e){
   *a.top++=e;
}
void pop(sqstack &a){
    if (a.base==a.top){//主要空的时候指针不能再往下减小
        return;
    }else {
      a.top--;
    }
}
bool isempty(sqstack a){
  if (a.base==a.top)return 1;
  else return 0;
}
char get_top(sqstack a){
  if (a.base!=a.top)
  return *(a.top-1);//栈顶指针比当前地址高一位
  else return -1;
}
int main(){
  char s[1000];
  sqstack a;
  creat_stack(a);
  scanf("%s",s);
  int len=strlen(s);
  for (int i=0;i<len;i++){
     if (get_top(a)=='(' && s[i]==')'){
        pop(a);
     }else {
        push(a,s[i]);
     }
  }
  while(!isempty(a)){
    printf("%c ",get_top(a));
    pop(a);
  }
  return 0;
}

 

posted @ 2018-12-27 22:07  bluefly-hrbust  阅读(161)  评论(0编辑  收藏  举报