zoukankan      html  css  js  c++  java
  • 数据结构实验之栈与队列四:括号匹配

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

    Description

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

    Input

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

    Output

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

    Sample

    Input 

    sin(20+10)
    {[}]

    Output 

    yes
    no
     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <string.h>
     4 
     5 int date[52]; //初始化栈的大小
     6 int main()
     7 {
     8     char a[55];
     9     int i,top,base,len;
    10     while(gets(a)) //在while中输入字符串不用加“!=EOF”
    11     {
    12         top=0;
    13         base=0;
    14         len=strlen(a);
    15         for(i=0;i<len;i++)
    16         {
    17             if(a[i]=='('||a[i]=='['||a[i]=='{')
    18                 date[top++]=a[i];  //左括号入栈
    19             else if((a[i]==')'||a[i]==']'||a[i]=='}'))
    20             {
    21                if(top==base)
    22                     break; //只有右括号,括号不匹配,结束查找
    23                else
    24                {
    25                  if((date[top-1]=='('&&a[i]==')')||(date[top-1]=='['&&a[i]==']')||(date[top-1]=='{'&&a[i]=='}'))
    26                     date[--top]; //右括号与左括号匹配,左括号出栈
    27                  else
    28                     break; //没有与右括号匹配的左括号,括号不匹配,结束查找
    29                }
    30             }
    31         }
    32         if(top==0&&i==len)
    33             printf("yes
    "); //栈为空且字符串查找完成
    34         else
    35             printf("no
    ");
    36     }
    37     return 0;
    38 }
  • 相关阅读:
    hdu 5101 Select
    hdu 5100 Chessboard
    cf B. I.O.U.
    cf C. Inna and Dima
    cf B. Inna and Nine
    cf C. Counting Kangaroos is Fun
    Radar Installation 贪心
    spfa模板
    Sequence
    棋盘问题
  • 原文地址:https://www.cnblogs.com/xiaolitongxueyaoshangjin/p/12660043.html
Copyright © 2011-2022 走看看