数据结构实验之栈四:括号匹配
生活随笔
收集整理的這篇文章主要介紹了
数据结构实验之栈四:括号匹配
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??數(shù)據(jù)結構實驗之棧四:括號匹配
Description
給你一串字符,不超過50個字符,可能包括括號、數(shù)字、字母、標點符號、空格,你的任務是檢查這一串字符中的( ) ,[ ],{ }是否匹配。Input
輸入數(shù)據(jù)有多組,處理到文件結束。Output
如果匹配就輸出“yes”,不匹配輸出“no”Sample Input
sin(20+10) {[}]Sample Output
yes noHint
#include<stdio.h> #include<string.h> char a[51]; int main(){char test[51];while(gets(test)){int len=strlen(test), ans=0, flag=0;for(int i=0; i<len; i++){if(test[i]=='[' || test[i]=='(' || test[i]=='{'){ a[ans++] = test[i]; flag = 1; }else if( ( test[i]==']'&&a[ans-1]=='[' ) || ( test[i]=='}'&&a[ans-1]=='{' ) || ( test[i]==')'&&a[ans-1]=='(' ) )ans--;else if( ( test[i]==']'&&a[ans-1]!='[' ) || ( test[i]=='}'&&a[ans-1]!='{' ) || ( test[i]==')'&&a[ans-1]!='(' ) || ( (a[ans-1]==']' || a[ans-1]=='}' || a[ans-1]==')') && ans==0 ) ){ flag=0; break; }}if(!ans && flag) printf("yes\n");else printf("no\n");}return 0; }轉載于:https://www.cnblogs.com/Genesis2018/p/8304825.html
總結
以上是生活随笔為你收集整理的数据结构实验之栈四:括号匹配的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux 命令 ——less命令
- 下一篇: 测试微信好友是否删除软件,微信测试好友是