//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;
}