📄 魔王语言解释.cpp
字号:
#include<iostream.h>
#include<stdio.h>
#include<string.h>
class stack
{
public:
stack();
char popStack(); //栈顶元素出栈
void pushStack(char x); //新元素x进栈
void print(); //调试时使用
bool isEmpty(); //判断栈空否
private:
int top; //栈顶指针
char * lan; //存放栈中元素的栈数组
};
stack::stack() //栈的构造函数
{
top=0;
lan=new char[50];
}
char stack::popStack() //退栈,并将栈顶元素附给a后返回
{
char a;
a = lan[--top]; //栈顶指针退1
return a;
}
void stack::pushStack(char x) //将x入栈
{
lan[top++]=x; //x先进栈,再使指针加1
}
void stack::print()
{ //利用辅助栈将栈中元素输出,否则会销毁栈中的元素
stack temp=*this;
while(temp.top!=0)
{
cout<<temp.popStack(); //输出辅助栈中的栈顶元素
}
}
bool stack::isEmpty () //判断栈是否为空,如果为空返回false,否则返回bool
{
if(top==0)
return true;
else
return false;
}
class queue
{
public:
queue(); //构造函数
char DeQueue(); //队头元素出队列
void EnQueue(char x); //新元素x插到队尾
void print(); //打印队列
bool isEmpty(); //判断队列空否
private:
int front; //队头指针
int rear; //队尾指针
char * q; //存放队列元素的指针
};
queue::queue() //构造函数
{
q=new char[50]; //建立给定长度50的字符数组
front=rear=0; //给队头和队尾指针赋初值
}
char queue::DeQueue()
//退掉一个队头元素并由e返回
{
char a;
a=q[front++]; //队头元素赋给e后,队头指针加1
return a;
}
void queue::EnQueue(char x)
//将新元素e插入到该队列的队尾
{
q[rear++]=x; //先将元素x插入队尾,再将队尾指针加1
}
void queue::print() //利用辅助队列输出队列元素
{
queue temp=*this;
while(temp.front!=temp.rear)
{
cout<<temp.DeQueue() ;
}
}
bool queue::isEmpty () //判断队列是否为空,如果为空返回true,否则返回false
{
if(front==rear)
return true;
else
return false;
}
int main()
{
stack s,stemp;
queue q,gz,mwyy,gztemp;
int i,n,ngz1,ngz2;
char e,a[5],temp;
char guize1[]="BtAdA",guize2[]="Asae"; //gz1[]和gz2[]存放规则
cout<<"请输入魔王语言:测试数据为:B(ehnxgz)B"<<endl;
gets(a); //将输入的字符串赋给a
n=strlen(a); //将字符串a的长度赋给n
ngz1=strlen(guize1);
ngz2=strlen(guize2);
for(i=n;i>=0;i--) //将魔王语言自右向左进栈
{
s.pushStack(a[i]);
} //s栈中存储情况:B2)zgxnhe(B1
{
e=s.popStack();
do
{
stemp.pushStack(e); //辅助栈,用来存放开括号以前的元素
e=s.popStack();
} //stemp:B
while(!(e=='('));
temp=s.popStack(); //用temp记录括号中的第一个字符
e=s.popStack();
do
{
q.EnQueue(temp);
q.EnQueue(e);
e=s.popStack();
}
while(!(e==')'));
q.EnQueue(temp); //用队列q储存中间解释结果ehenexegeze
}
while(!q.isEmpty()) //将q中元素压入栈s中
{
char c;
c=q.DeQueue();
s.pushStack(c);
}
while(!stemp.isEmpty()) //将括号前的元素进入栈s中
{
char c;
c=stemp.popStack();
s.pushStack(c);
} //s:BehenexegezeB
for(i=1;i<ngz1;i++)
if(guize1[i]==guize2[0])
for(int j=1;j<ngz2;j++)
gz.EnQueue(guize2[j]);
else
gz.EnQueue(guize1[i]); //用gz存放B的解释语言tsaedsae
gztemp=gz; //gztemp做gz的辅助队列
e=s.popStack();
while(!s.isEmpty())
{
if(e==guize1[0])
while(!gztemp.isEmpty())
{
char ctemp=gztemp.DeQueue();
mwyy.EnQueue(ctemp);
}
else
mwyy.EnQueue(e);
gztemp=gz;
e=s.popStack();
}
cout<<"******************************************************************"<<endl;
cout<<endl;
cout<<"翻译后的语言为:"<<endl;
mwyy.print();
cout<<endl;
cout<<"对应的汉字为:"<<endl;
while(!mwyy.isEmpty() ) //将对应的汉字输出
{
e=mwyy.DeQueue ();
switch(e)
{
case 't' : cout<<"天";break;
case 'd' : cout<<"地";break;
case 's' : cout<<"上"; break;
case 'a' : cout<<"一只"; break;
case 'e' : cout<<"鹅"; break;
case 'z' : cout<<"追"; break;
case 'g' : cout<<"赶"; break;
case 'x' : cout<<"下"; break;
case 'n' : cout<<"蛋"; break;
case 'h' : cout<<"恨"; break;
case 'B' : cout<<"天上一只鹅地上一只鹅";break;
case 'A' : cout<<"上一只鹅";break;
default : cout<<"*";break; //遇到没没有汉字输出的字符用*号表示
}
}
cout<<endl;
return 0;
}
//B1ehenexegezeB2
//s:easdeastehenexegezeeasdeast
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -