关闭

小错误变成大错误 不知道该怎么改了 应该是指针悬挂问题吧

269人阅读 评论(0) 收藏 举报

不能正常退出,以确定是析构函数的问题,而且是当前指针current的事,先放着做遗留问题吧,真没时间耗了,先往下做,会变好的 ,  那位要是看到了 ,希望给指点,帮改之,谢了!

///////////"ListNode"/////////////////////
#ifndef NULL
#define NULL 0
#endif

class List;
class ListNode{
friend class List;
private:
char element;
ListNode* Next;
public:
ListNode(const char& e,ListNode* N=NULL):element(e),Next(N){}
ListNode():Next(NULL){}
~ListNode(){}
};

/////////////"List"/////////////////

#include"ListNode.h"

#include"h.h"


class Bracket;
class List{
friend class Bracket;
private:
ListNode* head;
ListNode* current;
public:
List(){head=new ListNode();current=head;}

~List(){MakeEmpty();delete head;delete current; }
int Is_Empty()const{return head->Next==NULL;}
void MakeEmpty(){while(!Is_Empty()) PopFirst();}
void EnList(const char& e)
{
  ListNode* p=new ListNode(e,current->Next);
  current=current->Next=p;
}
const char& ShowFirst(){ return head->Next->element;}
void PopFirst()
{
  ListNode* p=head->Next;
  head->Next=p->Next;
  delete p;
}
};

/////////////"Stack"////////////


#include"h.h"

class Bracket;
static const int InitSize=10;
class Stack{
friend class Bracket;
private:
char* Array;
int top;
int MaxSize;
void DoubleSize(int Max);
public:
Stack():top(-1),MaxSize(InitSize){Array=new char[MaxSize];}
~Stack(){delete []Array;}
int Is_StackEmpty()const{return top==-1;}
int Is_StackFull()const{return top==MaxSize-1;}
void EnStack(const char& e);
void PopStack();
const char& ShowTop(){ return Array[top];}
};

void Stack::EnStack(const char& e)
{
if(++top==MaxSize) DoubleSize(2*MaxSize);
Array[top]=e;
}

void Stack::DoubleSize(int Max)
{
char* oldArray=Array;
Array=new char[Max];
int j;
for(j=0;j<=top;j++) Array[j]=oldArray[j];
MaxSize=Max;
delete[]oldArray;
}

void Stack::PopStack()
{
top--;
}

/////////////"Bracket"/////////////////

#include"Stack.h"
#include"List.h"
#include"h.h"

class Bracket{
private:
List L;
Stack S;
public:
Bracket(const List& Li,const Stack& St):L(Li),S(St){}
~Bracket(){}
friend istream& operator>>(istream& is,Bracket& b);
friend ostream& operator<<(ostream& os,Bracket& b);
};


istream& operator>>(istream& is,Bracket& b)
{
cout<<"Please input words:"<<endl;
char c;
while(cin.get(c)&&c!='#')
{
  if(c=='('||c=='{'||c=='['){ b.S.EnStack(c);   b.L.EnList(c);}
  else if(c==')'||c=='}'||c==']')
  {
   if(b.S.Is_StackEmpty())cout<<"ERROR!!!The first one can not be RIGHT!!"<<endl;
   else if(c==')'&&b.S.ShowTop()=='('){b.S.PopStack();b.L.EnList(c);}
   else if(c=='}'&&b.S.ShowTop()=='{'){b.S.PopStack();b.L.EnList(c);}
   else if(c==']'&&b.S.ShowTop()=='['){b.S.PopStack();b.L.EnList(c);}
   else cout<<"ERROR! Mach to NOONE!!"<<endl;
  }
  else b.L.EnList(c);
}//end while
return is;
}


ostream& operator<<(ostream& os,Bracket& b)
{
cout<<"Output the word:"<<endl;
char c;
while(!b.L.Is_Empty())
{
  c=b.L.ShowFirst();
  b.L.PopFirst();
  cout.put(c);
}
cout<<endl;
return os;
}

////////////////"h"///////////////////////

#ifndef _H_H
#define _H_H
#endif


#include<iostream>
using namespace std;

//////////////////"main"/////////////////////

#include"Bracket.h"

int main()
{

List L;
Stack S;
Bracket B(L,S);
cin>>B;
cout<<"--------->"<<endl;
cout<<B;
cout<<endl;

return 0;
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3563次
    • 积分:134
    • 等级:
    • 排名:千里之外
    • 原创:5篇
    • 转载:2篇
    • 译文:5篇
    • 评论:0条
    文章分类