括号匹配检验(含/**/双字节符号)即数据结构3.18.b答案(Pearson版)

write a program to check for banlancing symbools in the following languages:C(/**/,(),{},[]).

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define Maxsize 50
typedef char Datatype;
typedef struct{
Datatype data[Maxsize];
int top;
}SqStack;
void InitStack(SqStack *S)
{
    S->top=-1;
}
int StackEmpty(SqStack *S)
{
    if(S->top==-1)
        return 0;
    else
        return 1;
}
int Push (SqStack *S,Datatype x)
{
    if(S->top==Maxsize-1)
        return 0;
    else
        S->data[++(S->top)]=x;
        return 1;
}
int Pop(SqStack *S, Datatype *x)
{
    if(S->top==-1)
        return 0;
    else
        *x=S->data[(S->top)--];
    return 1;
}
int GetTop(SqStack S,Datatype *x)
{
    if(S.top==-1)
        return 0;
    else
        *x=S.data[S.top];
    return 1;
}
int Bracketscheck(SqStack *S,char *str)
{
     InitStack(S);
     char e;
     int i=0;
     while(str[i]!='\0')
     {
         switch(str[i]){
     case '(':
            Push(S,'(');
            break;
     case '{':
        Push(S,'{');
        break;
     case '[':
        Push(S,'[');
        break;
     //case'/'://本行正确,无需注释
            if(str[i+1]=='*'){    
            Push(S,'/');
            Push(S,'*');
        }
        
        else if(str[i-1]=='*'){
    Pop(S,&e);
    if(e!='*')
    return 0;
    Pop(S,&e);
    if(e!='/ ')
    return 0;
         }
         break;
     case ')':
        Pop(S,&e);
        if(e!='(')
            return 0;
        break;
     case '}':
        Pop(S,&e);
        if(e!='{')
            return 0;
        break;
     case ']':
        Pop(S,&e);
        if(e!='[')
            return 0;
        break;
       default:
        break;

         }
         i++;
     }
     int h=StackEmpty(S);
     if(h==1)
        return 0;
     else
        return 1;
}
int main()
{    SqStack S;
    char str[Maxsize];
 
    
    printf("请输入你要收入的字符串:");
    scanf("%s",str);
    int h=Bracketscheck(&S,str);
    if(h==0)
        printf("括号不匹配");
    else
        printf("括号匹配");

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值