📄 魔王语言.cpp
字号:
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#define Null 0
#define MAXQSIZE 100
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
struct SqStack //定义栈类型
{
char *base; //栈底指针
char *top; //栈顶指针
int stacksize; //栈的当前可使用最大容量
}SqStack;
struct Squeue //定义队列类型
{
char *base;
int *front; //队头指针
int *rear; //队尾指针
}Q;
struct Squeue q[31];
char *rule[31]; //定义数组rule[]存放魔王语言的翻译规则
struct SqStack InitStack() //建立一个空栈
{
struct SqStack S;
S.base=(char *)malloc(STACK_INIT_SIZE*sizeof(char));
if(!S.base) exit(0); //存储分配失败
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return(S);
}
void Push(struct SqStack S,char ch) //元素进栈
{
if(S.top-S.base>=S.stacksize) //栈满,追加存储空间
{
S.base=(char *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(char));
if(!S.base) exit(0); //存储分配失败
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top++=ch;
}
char Pop(struct SqStack S) //栈顶元素出栈
{
char ch;
if(S.top==S.base) exit(0);//栈为空,返回错误
ch=*--S.top; //若栈不空,则删除S的栈顶元素,并返回其值;
return(ch);
}
struct Squeue InitQueue() //建立一个空队列
{
struct Squeue Q;
Q.base=(char *)malloc(MAXQSIZE*sizeof(char));
if(!Q.base) exit(0); //存储分配失败
Q.front=(int *)malloc(MAXQSIZE*sizeof(int));
*Q.front=0;
Q.rear=(int *)malloc(MAXQSIZE*sizeof(int));
*Q.rear=0;
return(Q);
}
void EnQueue(struct Squeue Q,char e) //元素入队列
{
if((*Q.rear+1)>MAXQSIZE)
Q.base=(char *)realloc(Q.base,(*Q.rear-*Q.front+STACKINCREMENT)*sizeof(char));
Q.base[*Q.rear]=e; //插入元素e为新的队列的队尾元素
(*Q.rear)++;
}
void InitRules() // 读入魔王语言的翻译规则
{
int i;
char ch;
for(i=0;i<=31;i++) rule[i]=Null; //置数组元素为0
cout<<"请输入魔王语言的翻译规则"<<endl
<<endl
<<"提示:只输入回车键完成翻译规则的输入"<<endl;
printf("\n请输入一个魔王语言的字符:");
ch=getchar();
while(ch<='Z'&&ch>='A') //检验输入字符是否为魔王语言的字符
{
printf("\n请输入这个字符对应的人的语言:");
rule[ch-'A']=(char *)malloc(MAXQSIZE*sizeof(char)); //把魔王语言和人的语言相对应
scanf("%s",rule[ch-'A']);
printf("\n请输入一个魔王语言的字符:");
scanf("%c",&ch);
ch=getchar();
}
}
void CopyQueue(struct Squeue Q1,struct Squeue Q2) //复制队列
{
int k;
char ch;
k=*Q2.front;
while(k!=*Q2.rear)
{
ch=Q2.base[k];
EnQueue(Q1,ch);
k++;
}
}
void TranslateRules( ) //将魔王的语言翻译为人的语言
{
int i;
char *p;
for(i=0;i<=31;i++)
{
p=rule[i];
if(p)
{
q[i]=InitQueue();
while(*p!='\0')
{
if(*p<='z'&&*p>='a')
EnQueue(q[i],*p);
else
CopyQueue(q[i],q[*p-'A']);
p++;
}
}
}
}
char *TranslateSpecialRules(char *mm) //对应于带括号的魔王语言的翻译方法
{
char ch,th;
struct SqStack S;
S=InitStack();
ch=*mm;
mm++;
while(*mm!=')')
{
Push(S,*mm);
S.top++;mm++;
}
while(!(S.top==S.base))
{
EnQueue(Q,ch);
th=Pop(S);S.top--;
EnQueue(Q,th);
}
EnQueue(Q,ch);
return(mm);
}
void main()
{
int i;
char *elem,*tt;
Q=InitQueue();
InitRules(); //输入魔王语言的翻译规则
TranslateRules(); //读取规则
printf("\n请输入一句魔王的语言:");
tt=(char *)malloc(MAXQSIZE*sizeof(char));
scanf("%s",tt); //输入魔王语言
elem=tt;
while(*elem!='\0') //翻译过程
{
if(*elem<='z'&&*elem>='a') EnQueue(Q,*elem);
else if(*elem<='Z'&&*elem>='A')
CopyQueue(Q,q[*elem-'A']);
else if(*elem=='(')
{
elem++;
elem=TranslateSpecialRules(elem);}
elem++;
}
for(i=*Q.front;i<*Q.rear;i++) //输出魔王语言的转换字符
printf("%c",Q.base[i]);
printf("\n");
for(i=*Q.front;i<*Q.rear;i++) //把魔王语言和汉字对应
{
switch(Q.base[i])
{
case 'e':printf("鹅");break;
case 't':printf("天");break;
case 's':printf("上");break;
case 'a':printf("一只");break;
case 'd':printf("地");break;
case 'z':printf("追");break;
case 'g':printf("赶");break;
case 'x':printf("下");break;
case 'n':printf("蛋");break;
case 'h':printf("恨");break;
default:printf("%c",Q.base[i]);break;
}
}
printf("\n");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -