请大家帮我挑挑错
绿皮车 2003-04-23 11:19:01 各位好:
我是c++的初学者,写了一段程序,主要是用来匹配花括号的,并指明在什么位置匹配,什么位置不匹配,不过在执行时出现了一点问题,请指点一下,谢谢!
/****************myBraceRules*********************/
1.BraceNode.cc
#include "myBraceNode.h"
BraceNode * OpeBrace::head;
BraceNode::BraceNode()
{next=NULL;}
OpeBrace::OpeBrace()
{
head=NULL;
}
void OpeBrace::push(BraceNode &n,char &ch)
{
if(head==NULL)
{
n.next=NULL;
n.ch=ch;
head=&n;
}
else
{
n.ch=ch;
n.next=head->next;
head->next=&n;
}
}
char * OpeBrace::pop()
{
if(head!=NULL)
{
BraceNode *tmp=head;
if(tmp->ch=='{')
{
head=head->next;
delete tmp;
return "Match";//我跟踪时,系统到此就断掉了,报告"User
//BreakPoint called from code at 0x77f7x570"
//请指点!
}
}
else
{return "Error";}
return "Error";
}
void OpeBrace::Delete()
{
while(head!=NULL)
{
BraceNode * t=head->next;
head=t->next;
delete t;
}
}
//myBraceNode.h
#include <iostream>
using namespace std;
class BraceNode
{
private:
char ch;
BraceNode * next;
public:
BraceNode();
friend class OpeBrace;
};
class OpeBrace
{
private:
static BraceNode * head;
public:
OpeBrace();
void push(BraceNode &,char &);
void Delete();
char * pop();
};
//Main_BraceNode.cc
#include "myBraceNode.h"
#include <string.h>
static OpeBrace OB;
void main()
{
char ch;
int i=0;
int count=0;
cout<<"Please Enter chars including {/}:"<<endl;
while(cin>>ch)
{
if(ch==97)
{
if(count==0)
{
cout<<"There is no { or }"<<endl;
}
break;
}
BraceNode n;
if(ch=='{')
{
OB.push(n,ch);
count++;
}
if(ch=='}')
{
//OpeBrace Opop;
char * Recieve=new char[10];
strcpy(Recieve,OB.pop());
cout<<Recieve<<" at "<<i<<endl;
delete Recieve;
count++;
}
i++;
}
}