魔王語言(棧和隊列的應用)

//Stack.h

 

#include <iostream>
using namespace std;

#define Type      char
#define Status    bool

#define MAXSIZE   100
#define OK        1
#define ERROR     0
#define OVERFLOW  -1

class Stack
{
private:
 Type *base;                            //棧底指針
 Type *top;                             //棧頂指針
 unsigned int stacksize;                //棧的大小
public:
 Stack();
 Stack(unsigned int size);              //固定大小的棧
 ~Stack();

 //棧的基本操作
 Status Push(const Type e);
 Status Pop(Type& e);
 Type GetTop() const;
 Status Empty();
 Status Full();
 Status Clear();
 Status Traverse();
 unsigned int GetStacksize();
};

 

//Stack.cpp

#include "stdafx.h"


#include "Stack.h"

Stack::Stack()
{
 base=new Type[MAXSIZE];
 top=base;
 stacksize=0;
}

Stack::Stack(unsigned int size)
{
 base=new Type[size];
 top=base;
 stacksize=0;
}

Stack::~Stack()
{
 delete []base;
}

Status Stack::Push(const Type e)
{
 if(stacksize<MAXSIZE-1)
 {
  *top++=e;
  stacksize++;
  return true;
 }
 return false;
}

Status Stack::Pop(Type& e)
{
 if(stacksize>0)
 {
  e=*--top;
  stacksize--;
  return true;
 }
 return false;
}

Type Stack::GetTop() const
{
 if(stacksize>0)
 {
  return *(top-1);
 }
 return 0;
}

Status Stack::Empty()
{
 if(((top-base)==0) && (stacksize==0))
  return true;
 return false;
}

Status Stack::Clear()
{
 stacksize=0;
 top=base;
 return true;
}

Status Stack::Full()
{
 if((5<=(top-base)) && stacksize>0)
  return true;
 return false;
}

unsigned int Stack::GetStacksize()
{
 return stacksize;
}

Status Stack::Traverse()
{
 Type* pt;
 if(!Empty())
 {
  pt=top;
  while(pt!=base)
  {
   cout<<' '<<*(pt-1);
   pt--;
  }
  return OK;
 }

 cout<<"Stack::Traverse(): Stack Empty!"<<endl;
 return ERROR;
}

 

 

//Queue.h

 

#include <iostream>
using namespace std;

#define QElemType char
#define Status    bool
#define OK        1
#define ERROR     0
#define OVERFLOW  -1


typedef struct QNode
{
 QElemType data;
 struct QNode *next;
}QNode,*QueuePtr;

class LinkQueue
{
private:
 QueuePtr front;
 QueuePtr rear;
public:
 LinkQueue();
    ~LinkQueue();

 //隊列的基本操作
 Status ClearQueue();
 Status QueueEmpty() const;
 unsigned int QueueLength() const;
 Status GetHead(QElemType& e) const;
 Status EnQueue(const QElemType e);
 Status DeQueue(QElemType& e);
 Status QueueTraverse() const;
};

 

//Queue.cpp

 

#include "stdafx.h"

#include "Queue.h"

LinkQueue::LinkQueue()
{
 front=rear=new QNode;
 if(!front)
 {
  cout<<"LinkQueue::LinkQueue(): Malloc Failure!"<<endl;
  exit(OVERFLOW);
 }
 front->next=NULL;
}

LinkQueue::~LinkQueue()
{
 while(front)
 {
  rear=front->next;
  delete front;
  front=rear;
 }
}

//隊列的基本操作
Status LinkQueue::ClearQueue()
{
 QueuePtr pt=front;
 
 if(!QueueEmpty())
 {
  front->next=NULL;
  while(pt)
  {
   rear=pt->next;
   delete pt;
   pt=rear;
  }
  rear=front;
  return OK;
 }

 cout<<"LinkQueue::ClearQueue(): QueueEmpty()"<<endl;
 return ERROR;
}

Status LinkQueue::QueueEmpty() const
{
 if(front==rear)
  return OK;
 return ERROR; 
}
 
unsigned int LinkQueue::QueueLength() const
{
 unsigned int l=0;
 QueuePtr pt=front;
 while(!(pt->next))
 {
  pt=pt->next;
  l++;
 }
 return l;
}

Status LinkQueue::GetHead(QElemType& e) const
{
 if(!QueueEmpty())
 {
  e=front->next->data;
  return OK;
 }

 cout<<"LinkQueue::GetHead(QElemType& e) const: QueueEmpty!"<<endl;
 return ERROR;
}

Status LinkQueue::EnQueue(const QElemType e)
{
 QueuePtr pt=new QNode;
 if(!pt)
 {
  cout<<"LinkQueue::EnQueue(const QElemType e): Malloc Failure!"<<endl;
  exit(OVERFLOW);
 }
 pt->data=e;
 pt->next=NULL;

 //隊尾插入
 rear->next=pt;
 rear=pt;

 return OK;
}

Status LinkQueue::DeQueue(QElemType& e)
{
 QueuePtr pt;
 if(!QueueEmpty())
 {
  e=front->next->data;
  pt=front;
  front=front->next;
  delete pt;
  return OK;
 }

 else
 {
  cout<<"LinkQueue::DeQueue(QElemType& e): Queue is Empty!"<<endl;
  return ERROR;
 }
}

Status LinkQueue::QueueTraverse() const
{
 cout<<"Hello,World 2 !"<<endl;

 QueuePtr pt;

 pt=front->next;

 while(pt)
 {
  cout<<' '<<pt->data;
  pt=pt->next;
 }

 return OK;
}

 

//main.cpp

 

// Queue.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include "Queue.h"
#include "Stack.h"
//#include <iostream>
//using namespace std;


bool Process(const char* all,const unsigned int size,Stack* s,LinkQueue* q,
    const char* transB,const unsigned int sizeB,
    const char* transA,const unsigned int sizeA);//const char* transB 指針的值不能改變

bool ProcessB(Stack* s,const char* transB,const unsigned int sizeB,const char* transA,const unsigned int sizeA);
bool ProcessA(Stack* s,const char* trnasA,const unsigned int sizeA);
bool Translate(Stack* s,LinkQueue* q);

//整個字符串處理(含隊列的操作)
bool Process(const char* all,const unsigned int sizeAll,Stack* s,LinkQueue* q,
    const char* transB,const unsigned int sizeB,
    const char* transA,const unsigned int sizeA)
{
 for(int i=sizeAll-1;i>=0;i--)                   //從右向左讀入字符
 {
  if('B'==all[i])
  {
   ProcessB(s,transB,sizeB,transA,sizeA);             //出現字母B的情況
  }
  else
  {
   if('('==all[i] || ')'==all[i])        //出現括號的情況
   {
    if( '('==all[i] )
    {
     s->Push(all[i+1]);           //標記括號內第一個元素
     s->Push(all[i]);             //'('入棧
     q->EnQueue(all[i]+1);
    }
    else if( ')'==all[i] )
    {
     s->Push(all[i]);             //')'入棧
     q->EnQueue(all[i]-1);
    }
                                      //壓入')'之前先將括號後第一個元素壓入棧
   }

   else
   {                                     //隊尾入隊
    q->EnQueue(all[i]);

   }//else
  }//else
 }//for

 return true;
}

//處理B->tAdA
bool ProcessB(Stack* s,const char* transB,const unsigned int sizeB,const char* transA,const unsigned int sizeA)
{
 for(int i=sizeB-1 ; i>=0 ; i--)
 {
  if('A'==transB[i])
  {
   ProcessA(s,transA,sizeA);          //出現字母A的情況
  }

     else
  {
   s->Push(transB[i]);
  }
 }
 return true;
}

//處理A->sae
bool ProcessA(Stack* s,const char* transA,const unsigned int sizeA)
{
 for(int i=sizeA-1;i>=0;i--)
 {
  s->Push(transA[i]);
 }
 return true;
}

//輸出自然語言
bool Translate(Stack* s,LinkQueue* q)
{
 char ch,theta;
 while(!s->Empty())
 {
  s->Pop(ch);
  if( '('==ch )                            //遇到'('開始出隊
  {
   s->Pop(theta);
   if(!q->QueueEmpty())
   {
    q->DeQueue(ch);                  // '('出隊
    q->DeQueue(ch);
    while( ')'!=ch )
    {
     cout<<' '<<theta<<' '<<ch;
     q->DeQueue(ch);
    }
    cout<<' '<<theta;               //最後一個字符
   }
  }

  else if( ')'==ch)                       // ')'結束出隊
  {
   continue;
  }

  else                                    // 普通字母直接輸出
  {
   cout<<' '<<ch;
  }
 }

 //剩餘隊列的元素直接輸出
 if(!q->QueueEmpty())
 {
  q->DeQueue(ch);                         // '('出隊
  q->DeQueue(ch);
  while(!q->QueueEmpty())
  {
   cout<<' '<<ch;
   q->DeQueue(ch);
  }
 }
 return true;
}

int main(int argc, char* argv[])
{
 Stack* SS=new Stack();
 LinkQueue* QQ=new LinkQueue();

 char *TransB, *TransA,*All;
 int sizeB,sizeA,size;

 cout<<"Please enter sizeA,sizeB and size:";
 cin>>sizeA>>sizeB>>size;

 cout<<"/nEnter TransA:";
 TransA=new char[sizeA];
 for(int i=0;i<sizeA;i++)
 {
  cin>>TransA[i];
 }

 cout<<"Enter TransB:";
 TransB=new char[sizeB];
 for(i=0;i<sizeB;i++)                      //複製成SizeA了
 {
  cin>>TransB[i];
 }

 All=new char[size];
 cout<<"Enter All:";

 for(i=0;i<size;i++)
 {
  cin>>All[i];
 }

 //開始翻譯
    Process(All,size,SS,QQ,TransB,sizeB,TransA,sizeA);

 //輸出翻譯
 cout<<"/nTranslating..."<<endl;
 Translate(SS,QQ);
    cout<<endl;

 return 0;
}

發佈了31 篇原創文章 · 獲贊 3 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章