/
//write by xves at 2014.11.1
//
//function : explain
//use Stack and Queue
//
#include <iostream>
#include <cstring>
#include <cstdlib>
#define STACK_SIZE 100
#define ELEM char
#define OVERFLOW -1
using namespace std;
typedef struct
{
int stacksize;
ELEM *top;
ELEM *base;
}SqStack;
typedef struct QNode
{
ELEM data;
struct QNode *next;
}QNode, *QNodePtr;
typedef struct
{
QNodePtr front;
QNodePtr rear;
}LinkQueue;
int InitStack(SqStack &S)
{
S.top= S.base = (char *)malloc(sizeof(char)* STACK_SIZE);
if (!S.base)
{
cout << "Stack malloc Wrong " << endl;
exit(OVERFLOW);
}
S.stacksize = STACK_SIZE;
return 0;
}
int Pop(SqStack &S, ELEM &e)
{
if (S.top == S.base)
{
cout << "Stack is empty " << endl;
exit(OVERFLOW);
}
S.top--;
e = *(S.top);
return 0;
}
int Push(SqStack &S, ELEM e)
{
if (S.top - S.base >=S.stacksize)
{
S.base = (char *)realloc(S.base, S.stacksize * 2);
if (!S.base)
{
cout << "realloc Wrong " << endl;
exit(OVERFLOW);
}
S.top = S.base + S.stacksize;
S.stacksize *= 2;
}
*(S.top) = e;
S.top++;
return 0;
}
int GetTop(SqStack &S, ELEM &e)
{
if (S.top == S.base)
return 0;
else
{
e = *(S.top - 1);
return 0 ;
}
}
int Stackempty(SqStack &S)
{
if(S.top == S.base )
return 1 ;
else
return 0 ;
}
int InitQueue(LinkQueue &Q)
{
Q.front = Q.rear = (QNode *)malloc(sizeof(QNode));
if (!Q.front)
{
cout << "Queue malloc Wrong" << endl;
}
Q.front->next = NULL;
return 0;
}
int EnQueue(LinkQueue &Q, ELEM &e)
{
QNodePtr p;
p = (QNode *)malloc(sizeof (QNode));
if (!p)
{
cout << "Queuenext malloc Wrong " << endl;
}
p->data = e;
p->next = NULL;
Q.rear->next = p;
Q.rear = p;
return 0;
}
int DeQueue(LinkQueue &Q, ELEM &e)
{
QNodePtr p;
if (Q.front == Q.rear)
{
cout << "the Queue is empty " << endl;
exit(OVERFLOW);
}
p = Q.front->next;
e = p->data;
Q.front->next = p->next;
if (Q.rear == p)
Q.rear = Q.front;
free(p);
return 0;
}
int print(SqStack S)
{
char e ;
while(!Stackempty(S))
{
Pop(S, e);
switch(e)
{
case 'B':
cout <<"tsaedsae";
break ;
case 'A' :
cout <<"sae";
break ;
default:
cout << e ;
}
}
return 0 ;
}
int explain(char *p, int len, SqStack &S, LinkQueue &Q)
{
int d = 0 ,qnumber = 0;
ELEM e , w ;
int flag = 0;
char *q =p ;
p = p + len-1; //指向尾部
while(d <= len-1 )
{
Push(S,*p);
p-- ;
d++ ;
GetTop(S ,e); //处理人类语言
if( e =='(')
{
Pop(S, e);
EnQueue(Q ,e );
GetTop(S,w);
DeQueue(Q,e);
Push(S ,e);
Pop(S, e);
while(e != ')')
{
if( (e !='(') && (e != ')') )
{
EnQueue(Q, e);
qnumber++;
}
Pop(S,e);
}
while (qnumber)
{
DeQueue(Q, e);
Push(S, e);
if(flag == 1)
{
Push(S, w);
}
flag = 1;
qnumber--;
}
flag = 0 ;
}
}
print(S) ;
cout << "\ngood" << endl;
return 0 ;
}
int main()
{
SqStack S ;// S1;
LinkQueue Q;
InitQueue(Q);
InitStack(S);
//InitStack(S1);
char lan[200];
int length ;
cout << "Please input the word" << endl;
<span style="white-space:pre"> </span>scanf("%s", lan);
<span style="white-space:pre"> </span>length = strlen(lan);
<span style="white-space:pre"> </span>explain(lan, length, S, Q);
<span style="white-space:pre"> </span>cout << "end" << endl;
<span style="white-space:pre"> </span>return 0 ;
}
栈 魔王语言解释
最新推荐文章于 2022-01-17 21:41:03 发布