可以做一下的C语言题目(三)——括号匹配
括号匹配
说明
栈的实现基于C语言骚操作(一)(二)
核心代码
根据ascll码表,发现成对的括号ascll相差1或2,当前输入与出栈的括号同时不满足相差一和相差二,就没是没有配对。
c = sqlist->listDelete_Sq(sqlist,sqlist->length);
if(e != c+1 && e != c+2){
return -1;
}
代码
#include <stdio.h>
#include <string.h>
#include "list.h"
int isMatch(Sqlist *sqlist)
{
Elemtype e = 0;
Elemtype c = 0;
while((e = getchar()) != '\n'){
if(e == '(' || e == '[' || e == '{'){
sqlist->listInsert_Sq(sqlist,sqlist->length+1,e);
}else if(e == ')' || e == ']' || e == '}'){
if(sqlist->length == 0){
return -1;
}
c = sqlist->listDelete_Sq(sqlist,sqlist->length);
if(e != c+1 && e != c+2){
return -1;
}
}
}
if(sqlist->length !=0){
return -1;
}else{
return 1;
}
}
int main()
{
Sqlist sqlist;
addFunction(&sqlist);
sqlist.init_Sq(&sqlist);
printf(" result : %d \n",isMatch(&sqlist));
return 0;
}