标题:我的 括号匹配有什么问题??? 求大家帮忙!!!
只看楼主
m21wo
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:4
帖 子:440
专家分:1905
注 册:2010-9-23
得分:0 
程序代码:
void init(struct brack*m)
{
    m->base=new char[SIZE];// 由于你后面涉及  m->base 字符指针引动,故要先申请内存
    m->top=new char[SIZE];  
    if(!m->base)
    {printf("cant creat!\n");
    exit(0);
    }
   
    m->top=m->base;
    m->stacksize=SIZE;
}
void push(struct brack*m,char c)
{
    if((m->top-m->base)==m->stacksize)
        printf("stack full\n");
    else
        *m->top++=c;  
}
收到的鲜花
  • lizjohn2010-11-01 21:42 送鲜花  1朵  

If You Want Something, Go Get It, Period.
2010-11-01 16:40
七有新人
Rank: 3Rank: 3
来 自:哈尔滨
等 级:论坛游侠
帖 子:64
专家分:132
注 册:2010-10-12
得分:0 
括号匹配,瞎写的,只考虑‘(’比‘)’多的情况,代码如下
//头文件
程序代码:
#ifndef LINK_STACK_MODEL_H_
    #define LINK_STACK_MODEL_H_
    
    #include<iostream>
    #include "string.h"
    using namespace std;
    
    const int maxsize = 50;
    
    template<class type> struct linknode
    {
        type data;
        struct linknode *next;
        //节点的构造函数,将数据“赋值”给data,并指向“所给定的地址”
        linknode(const type &x,struct linknode *ptr = NULL)
        {
            data = x;
            next = ptr;
        }
        //节点的默认构造函数,默认“结点”为空
        linknode(struct linknode *ptr = NULL)
        {
            //ptr = NULL;
        }
    };


    template<class type> class link_stack
    {
        private:
            struct linknode<type> *top;
        public:
            link_stack();
            ~link_stack();
        
            void push(const type &item);        
            void pop(type &item);
        
            bool isempty();
            bool isfull();        
            int getsize();
            void makeEmpty();
    };
    
    #include "link_stack_model.cpp"
#endif    

//头文件实现
程序代码:
#ifdef LINK_STACK_MODEL_H_

template<class type> link_stack<type>::link_stack()
{
    top = NULL;
}

template<class type> link_stack<type>::~link_stack()
{
    makeEmpty();
}

template<class type> void link_stack<type>::push( const type &item )
{
    if(isfull() != 1)
    {
        struct linknode<type> *temp = new struct linknode<type> (item,top);//使用构造函数对“类和结构”赋值的的格式
        top = temp;
    }
    else
        cout<<"the stack is full!can not be pushed"<<endl;
}

template<class type> void link_stack<type>::pop(type &item)
{
    if(isempty() != 1)
    {
        item = top->data;
        top = top->next;
    }
    else
        cout<<"the stack doesn't have unit to pop"<<endl;
}

template<class type> bool link_stack<type>::isempty()
{
    if(0 == getsize())
        return 1;
    else 
        return 0;    
}

template<class type> bool link_stack<type>::isfull()
{
    if(maxsize == getsize())
        return 1;
    else
        return 0;    
}

template<class type> int link_stack<type>::getsize()
{
    int count=0;
    
    struct linknode<type> *current = top;
    while(current != NULL)
    {
        current = current->next;
        count++;
    }
    
    return count;
        
}

template<class type> void link_stack<type>::makeEmpty()
{
    struct linknode<type> *current = new struct linknode<type>;
    while(top != NULL)
    {
        current = top;
        top = top->next;
        current = NULL;
    }
    delete current;    
}

#endif

//主函数
程序代码:
#include "link_stack_model.h"
#include "string.h"
int main(void)
{
    string string_to_check;    
    char temp ;
     cout<<"please input a string "<<endl;
     cin>>string_to_check;            
     
     link_stack<char> bracket_marry;
     for(string::size_type index = 0 ; index != string_to_check.size() ; index++)
     {
         if(string_to_check[index] == '(')
         bracket_marry.push( string_to_check[index] );
     }
     
     for(string::size_type index = 0 ; index != string_to_check.size() ; index++)
     {
         if(string_to_check[index] == ')')
         bracket_marry.pop( temp );
     }
     if(bracket_marry.getsize() != 0)
         cout<<"the bracket aren't matched"<<endl;
     else
         cout<<"the bracket is matched"<<endl;
         
     return 1;
}

图!
不一定是真相!
2010-11-01 16:56
lizjohn
Rank: 1
等 级:新手上路
帖 子:54
专家分:0
注 册:2010-10-28
得分:0 
回复 10楼 m21wo
对啊!那是怎么回事呢?
2010-11-01 18:04
gmac
Rank: 2
等 级:论坛游民
帖 子:174
专家分:85
注 册:2010-9-28
得分:0 
回复 楼主 lizjohn
1.因为你结构体力base定义的是char*,在VC中只是WARNING
2..是直接成员选择运算符,->是间接成员运算符,也就是说->用在指针变量和成员名之间
3.很确定的告诉你,不能
2010-11-01 18:18
lizjohn
Rank: 1
等 级:新手上路
帖 子:54
专家分:0
注 册:2010-10-28
得分:0 
回复 14楼 gmac
感谢
2010-11-01 18:31
lizjohn
Rank: 1
等 级:新手上路
帖 子:54
专家分:0
注 册:2010-10-28
得分:0 
回复 3楼 五当家
果然对了!!!谢谢,是我学艺不精,误会你了,谢谢!!!
2010-11-01 21:36
思恋到心碎
Rank: 2
等 级:论坛游民
帖 子:13
专家分:27
注 册:2010-10-22
得分:0 
正好我们才做了这个的实验,先回答楼主第三个问题,&是c++中的引用,目的是让它指向主函数的变量,可以把它看成变量的别名,但也可以向楼主一样写成指针方式,第二个问题,因为你设定的m是指针,所以调用结构体中的元素时,用两种方法:1.*m.top 2.m->top ,第三个问题是因为楼主设的top和base是struct brack中的一个元素,而他们是char类型,并不是struct brack,所以用malloc时要用char *er而不是struct brack *  以上个人意见
2010-11-03 11:16
思恋到心碎
Rank: 2
等 级:论坛游民
帖 子:13
专家分:27
注 册:2010-10-22
得分:0 
#include<stdio.h>
#include<stdlib.h>
#define SIZE 10
struct brack
{
    char*base;
    char*top;
    int stacksize;
};
void init(struct brack *m);
void push(struct brack *m,char c);
int pop(struct brack *m,char *e);
void main()
{
    char ch,t;
    int flag;
    struct brack sq;
     init(&sq);
    while((ch=getchar())!='\n')
    {
        if(ch=='('||ch=='['||ch=='{')
            push(&sq,ch);
        else if(ch==')'||ch==']'||ch=='}')
        {
            flag=pop(&sq,&t);
            if(flag==0)
                break;
            else if((ch==')')&&(t=='('))
                flag=1;
            else if((ch==']')&&(t=='['))
                flag=1;
            else if((ch=='}')&&(t=='{'))
                flag=1;
            else {
                flag=0;
                break;
            }
        }
    }
   
    if(flag==1)
        printf("match!!!\n");
    else
        printf("not match!!!\n");
}
void init(struct brack *m)
{
    m->base=m->top=(char *)malloc(SIZE*sizeof(char));      //就是这一行: : cannot convert from 'struct brack *' to 'char *'
   
        if(!m->base)
    {printf("cant creat!\n");
    exit(0);
    }
    m->top=m->base;
    m->stacksize=SIZE;
}

void push(struct brack *m,char c)
{
    if((m->top-m->base)==m->stacksize)
        printf("stack full\n");
    else
        *(m->top++)=c;
}
int pop(struct brack *m,char *e)
{
    int FLAG;
    if(m->top==m->base)
        FLAG=0;
    else{
        *e=*(--m->top);
        FLAG=1;
    }
    return FLAG;
}
2010-11-03 11:19



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




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

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