sinat_31829943 2015-10-25 03:51 采纳率: 60%
浏览 3243

C/C++ 简单括号匹配问题

我的程序,运行检测的括号匹配和实际不符麻烦帮忙看下问题

 #include<stdio.h>
typedef struct
{
    char ch[50];
    int top;
}SeqStack;
int Push(SeqStack &S,char x)
{
    if(S.top==49) return 0;
    S.top++;
    S.ch[S.top]=x;
    return 1;

}
int Pop(SeqStack &S,char x)
{
    if(S.top==-1) return 0;
    x=S.ch[S.top];
    S.top--;
    return 1;

}
int GetTop(SeqStack &S,char x)
{
    if(S.top==-1) return 0;
    x=S.ch[S.top];
    return 1;

}
int BracketMatch(char str[])
{
    SeqStack S;
    int i;
    char ch;
    S.top=-1;
    for(i=0;str[i]!='#';i++)
    {
        switch(str[i]){
        case'(':
        case'[':
        case'{':
            Push(S,str[i]);
            break;
        case')':
        case']':
        case'}':
            if(S.top==-1) {printf("\nÓÒÀ¨ºÅ¶àÓà");return 0;}
             else 
             {
                 GetTop(S,ch);
                 if((str[i]==')'&&ch=='(')||(str[i]==']'&&ch=='[')||(str[i]=='}'&&ch=='{')) Pop(S,ch);
                 else  {printf("\n¶ÔÓ¦µÄ×óÓÒÀ¨ºÅ²»Í¬Àà");return 0;}
             }
            }

    }if(S.top==-1)   {printf("\nÀ¨ºÅÆ¥Åä");return 1;}
        else printf("\n×óÀ¨ºÅ¶àÓ࣡");
    return 0;
}
void main()
{
    char ch[50];
    int i=0;
    char c;
    c=getchar();
    while(c!='\n')
    {
        ch[i]=c;
        c=getchar();
        i++;
    }
    BracketMatch(ch);
}

  • 写回答

1条回答 默认 最新

  • havedream_one 2015-10-25 04:28
    关注

    { if(S.top==-1) {printf("\nÓÒÀ¨ºÅ¶àÓà");return 0;}
    else
    {
    GetTop(S,ch);
    if((str[i]=='('&&ch==')')||(str[i]=='['&&ch==']')||(str[i]=='{'&&ch=='}')) Pop(S,ch);
    else {printf("\n¶ÔÓ¦µÄ×óÓÒÀ¨ºÅ²»Í¬Àà");return 0;}
    }
    }

    去掉最外层的括号试试

    评论

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来