#include<stdio.h>
#include<stdlib.h>
#define N 100
//仿照栈进行存储左括号,与字符串内的数组进行对比
int main(int args,char *argv[])
{
char str[N];
char str1[N];
printf("input a new backet :\n");
gets(str);
int top=-1;
int i=0;
while(str[i]!='\0')
{
//左括号入"栈",
if(str[i]=='('||str[i]=='['||str[i]=='{')
{
++top;
str1[top]=str[i];
}
//右括号出"栈"
if(str[i]==')')
{
if(str1[top]=='(')
--top;
else
printf("缺少左小括号!");
}
if(str[i]=='}')
{
if(str1[top]=='{')
--top;
else
printf("缺少左大括号!");
}
if(str[i]==']')
{
if(str1[top]=='[')
--top;
else
printf("缺少左中括号!");
}
/*
if((str[i]==')'&&str1[top]=='(')||(st
The First Blog-数组模拟栈进行括号匹配-c语言版
最新推荐文章于 2021-04-02 12:19:05 发布