括号匹配

#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;
}

 

全部评论

相关推荐

05-20 21:57
已编辑
门头沟学院 Java
喜欢吃卤蛋的悲伤蛙在...:建信融通没消息吧,我2说有实习挂简历不理了
点赞 评论 收藏
分享
墨西哥大灰狼:如果你的校友卤馆还在的话,他肯定会给你建议的,可是卤馆注销了@ 程序员卤馆
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务