注册 登录
编程论坛 C++教室

数据结构括号匹配问题,循环跳不出去 求改错,急

柒夏辰 发布于 2016-10-13 21:59, 1374 次点击
#include<iostream>
using namespace std;
const int max=100;
template <class T>
class stack
{
    T data[max];
    int top;
    public:
        stack(){top=-1;}
        void push(T x)
        {
            if(top==max-1) return;
            top++;
            data[top]=x;
        }
        void pop()
        {
            T x;
            if(top==-1) exit(0);
            x=data[top];
            top--;
        }
        T gettop()
        {
            return data[top];
        }
        bool empty()
        {
            return top=-1?1:0;
        }
};
int main()
{
    char c[200];
    int d=0,i=0,j;
    stack<char>  s;
    while(c[i]=getchar()&&c[i]!='\n')
    {
        if(c[i]=='(')
        {
            s.push(c[i]);
        }
        else if(c[i]=='[')
        {
            s.push(c[i]);
        }
        else if(c[i]=='{')
        {
            s.push(c[i]);
        }
        else if(c[i]==')')
        {
            if(s.gettop()=='(')
            {
                s.pop();
            }
        }
            else if(c[i]==']')
        {
            if(s.gettop()==']')
            {
                s.pop();
            }
        }
            else if (c[i]=='}')
        {
            if(s.gettop()=='}')
            {
                s.pop();
            }
        }
                i++;
                d++;
    }
    j=s.empty();
    if(j==0)
    {
        cout<<"括号匹配成功"<<endl;
    }
    if(j==1)
    {
        cout<<"括号匹配不成功"<<endl;
    }
    return 0;
}
3 回复
#2
柒夏辰2016-10-13 22:10
#include<iostream>
using namespace std;
const int max=100;
template <class T>
class stack
{
    T data[max];
    int top;
    public:
        stack(){top=-1;}
        void push(T x)
        {
            if(top==max-1) return;
            top++;
            data[top]=x;
        }
        void pop()
        {
            T x;
            if(top==-1) exit(0);
            x=data[top];
            top--;
        }
        T gettop()
        {
            return data[top];
        }
        bool empty()
        {
            return top=-1?1:0;
        }
};
int main()
{
    char c[200];
    int d=0,i=0,j;
    stack<char>  s;
    while(c[i]=getchar()&&c[i]!='\n')
    {
        if(c[i]=='(')
        {
            s.push(c[i]);
        }
        else if(c[i]=='[')
        {
            s.push(c[i]);
        }
        else if(c[i]=='{')
        {
            s.push(c[i]);
        }
        else if(c[i]==')')
        {
            if(s.gettop()=='(')
            {
                s.pop();
            }
        }
            else if(c[i]==']')
        {
            if(s.gettop()=='{')
            {
                s.pop();
            }
        }
            else if (c[i]=='}')
        {
            if(s.gettop()=='{')
            {
                s.pop();
            }
        }
                i++;
                d++;
    }
    j=s.empty();
    if(j==0)
    {
        cout<<"括号匹配成功"<<endl;
    }
    if(j==1)
    {
        cout<<"括号匹配不成功"<<endl;
    }
    return 0;
}
有点小错
#3
rjsp2016-10-14 08:23
为什么要贴两段代码?如果不一样,为什么不告诉别人哪里不一样?
如果不一样,你想要别人帮你改哪段代码?
你的代码需要输入数据,为什么不告诉别人你的测试用例?


[此贴子已经被作者于2016-10-14 08:24编辑过]

#4
rjsp2016-10-14 08:41
程序代码:
#include <iostream>
#include <stack>
using namespace std;

int main( void )
{
    bool bgood = true;
    stack<char> s;
    for( char c; bgood && (cin>>noskipws>>c) && c!='\n'; )
    {
        switch( c )
        {
        case '(':
            s.push( ')' );
            break;
        case '[':
            s.push( ']' );
            break;
        case '{':
            s.push( '}' );
            break;
        case ')':
        case ']':
        case '}':
            if( s.empty() || s.top()!=c )
                bgood = false;
            else
                s.pop();
            break;
        }
    }
    bgood = bgood && s.empty();

    cout << (bgood?"括号匹配成功":"括号匹配不成功") << endl;
    return 0;
}
1