标题:括号是否配对
只看楼主
小小小火柴
Rank: 5Rank: 5
来 自:江西赣州
等 级:职业侠客
威 望:3
帖 子:141
专家分:337
注 册:2012-9-28
结帖率:90.91%
 问题点数:0 回复次数:0 
括号是否配对
#include<iostream>
using namespace std;
class link
{
public:
    char data;
    link *next;
};
class linkstack
{
public:
    link *top;
    void inistack()//栈的初始化
    {
        top=new link;
        top->next=NULL;
    }
    void push(char x)//进栈
    {
        link *s=new link;
        s->data=x;
        s->next=top->next;
        top->next=s;
    }
    void pop()//退栈
    {
        link *s=top->next;
        if(s!=NULL)
        {
            top->next=s->next;
            delete s;
        }
    }
    char gettop()
    {
        if(top->next!=NULL)
            return (top->next->data);
        else return NULL;
    }
    bool empty()
    {
        if(top->next==NULL)
            return true;
        else return false;
    }
    int yn()
    {
        char c;
        int sign=1;
        inistack();
        cout<<"请输入表达式,并以=结束:";
        c=getchar();
        while(c!='=')
        {
            switch(c)
            {
            case '('://扫描到'('入栈
                push(c);
                break;
            case ')':
                {if(gettop()=='(')
                     pop();
                 else
                     sign=0;
                 break;}
            }
            if(sign==0)
                break;
            else
                c=getchar();

        }
        if(!empty())
            sign=0;
        return sign;
    }
    void judgeout(int a)
    {
        if(a==1)
            cout<<"括号配对正确\n";
        else if(a==0)
            cout<<"括号配对错误\n";
    }

};
void main()
{   int n;
    linkstack s;
    n=s.yn();
    s.judgeout(n);
}
就是括号配对问题。我只需要括号是否配对,不需要考虑其他问题。比如,当我输入:7+8)=时,出现的是:括号配对正确。但是明明就不配对,求高人指点,我算法错在哪里?
搜索更多相关主题的帖子: public include top next 
2012-10-16 21:42



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-383368-1-1.html




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

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