| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1237 人关注过本帖
标题:数据结构括号匹配问题,循环跳不出去 求改错,急
取消只看楼主 加入收藏
柒夏辰
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2016-10-13
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
数据结构括号匹配问题,循环跳不出去 求改错,急
#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;
}
搜索更多相关主题的帖子: include public return 
2016-10-13 21:59
柒夏辰
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2016-10-13
收藏
得分:0 
#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;
}
有点小错
2016-10-13 22:10
快速回复:数据结构括号匹配问题,循环跳不出去 求改错,急
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020307 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved