#括号匹配
给定一串字符,不超过100个字符,可能包括括号、数字、字母、标点符号、空格,编程检查这一串字符中的( ) ,[ ],{ }是否匹配。
输入格式:
输入在一行中给出一行字符串,不超过100个字符,可能包括括号、数字、字母、标点符号、空格。
输出格式:
如果括号配对,输出yes,否则输出no。
#include<stdio.h>
int main(){
char a[102];
char stack[102];
char mapping[255];
mapping[']']='[';
mapping['}']='{';
mapping[')']='(';
int top = -1;
scanf("%s",a);
int index= 0;
if(a[index]=='\0'||a[index]=='\n'){
printf("no");
return 0;
}
while(a[index]!='\0'){
if(a[index] == '(' || a[index] =='{' || a[index]=='[')
{
stack[++top] = a[index];
}
if (a[index] == ')' || a[index] == '}' || a[index] == ']'){
if(top == -1){
printf("no");
return 0;
}
if(a[index]==']' && stack[top] !='['){
printf("no");
return 0;
}
if(a[index]=='}' && stack[top] !='{'){
printf("no");
return 0;
}
if(a[index]=='(' && stack[top] !=')'){
printf("no");
return 0;
}
top--;
}
index++;
}
printf("yes");
return 0;
}