数据结构实验之栈四:括号匹配

来源:互联网 发布:vb datagrid设置列数 编辑:程序博客网 时间:2024/06/04 22:12
数据结构实验之栈四:括号匹配

Time Limit: 1000MS Memory limit: 65536K

题目描述

 给你一串字符,不超过50个字符,可能包括括号、数字、字母、标点符号、空格,你的任务是检查这一串字符中的( ) ,[ ],{ }是否匹配。

输入

 输入数据有多组,处理到文件结束。

输出

 如果匹配就输出“yes”,不匹配输出“no”

示例输入

sin(20+10){[}]

示例输出

yes
no
#include <stdio.h>#include <string.h>int top;char s[60];void push(char n){    s[++top]=n;    //printf("  %c\n",s[top]);}int pop(){    return s[top--];}int empty(){    return top==-1;}int main(){    int l,i,f;    char t[60];    while(gets(t)!=NULL)    {        top=-1;        f=0;        l=strlen(t);        for(i=0; i<l; i++)        {            if(t[i]=='('||t[i]=='['||t[i]=='{')                push(t[i]);            if(t[i]==')')            {                if(s[top]=='(')                    pop();                else                    f=1;            }            if(t[i]==']')            {                if(s[top]=='[')                    pop();                else                    f=1;            }            if(t[i]=='}')            {                if(s[top]=='{')                    pop();                else                    f=1;            }        }        if(!empty()||f==1)            printf("no\n");        else            printf("yes\n");    }}

0 0