魔王语言源代码.docx

上传人:b****6 文档编号:6181264 上传时间:2023-01-04 格式:DOCX 页数:9 大小:32.87KB
下载 相关 举报
魔王语言源代码.docx_第1页
第1页 / 共9页
魔王语言源代码.docx_第2页
第2页 / 共9页
魔王语言源代码.docx_第3页
第3页 / 共9页
魔王语言源代码.docx_第4页
第4页 / 共9页
魔王语言源代码.docx_第5页
第5页 / 共9页
点击查看更多>>
下载资源
资源描述

魔王语言源代码.docx

《魔王语言源代码.docx》由会员分享,可在线阅读,更多相关《魔王语言源代码.docx(9页珍藏版)》请在冰豆网上搜索。

魔王语言源代码.docx

魔王语言源代码

题目:

魔王语言

[源代码]

#include

#include

 

#defineSTACK_INIT_SIZE100

#defineSTACK_INCREMENT10

 

structStack{

char*base;

char*top;

intstacksize;

};

 

voidInitStack(structStack&s){//构造栈

s.base=(char*)malloc(STACK_INIT_SIZE*sizeof(char));

s.top=s.base;

s.stacksize=STACK_INIT_SIZE;

}

 

voidPush(structStack&s,chare){//压入元素

if(s.top-s.base>=STACK_INIT_SIZE){

s.base=(char*)realloc(s.base,(s.stacksize+STACK_INCREMENT)*sizeof(char));

s.top=s.base+s.stacksize;

s.stacksize+=STACK_INCREMENT;

}

*(s.top)=e;

s.top++;

}

 

voidPop(structStack&s,char&e){//取出元素

e=*--s.top;

}

intStackEmpty(structStacks){//栈是否为空

if(s.top==s.base){

return1;

}

else{

return0;

}

}

voidClearStack(structStack&s){

s.top=s.base;

}

 

structQueue{

chardata;

structQueue*next;

};

structLinkQueue{

structQueue*front;

structQueue*rear;

};

 

voidInitQueue(structLinkQueue&q){//构造队

q.front=q.rear=(structQueue*)malloc(sizeof(structQueue));

q.front->next=NULL;

 

}

voidEnQueue(structLinkQueue&q,chare){//元素入队

structQueue*p;

p=(structQueue*)malloc(sizeof(structQueue));

p->data=e;

p->next=NULL;

q.rear->next=p;

q.rear=p;

}

voidDeQueue(structLinkQueue&q,char&e){//元素出队

structQueue*p;

p=q.front->next;

e=p->data;

q.front->next=p->next;

if(q.rear==p){

q.rear=q.front;

}

free(p);

}

intQueueEmpty(structLinkQueueq){//队是否为空

if(q.front==q.rear){

return1;

}

else{

return0;

}

}

 

voidInStack(char*ch,structStack&s){//把字符数组从右至左压入栈中

inti,L=0;

while(ch[L]!

='\0'){

L++;

}

for(i=L-1;i>=0;i--){

Push(s,ch[i]);

}

}

 

intmain(){

inti=0;

charA[]="sae";

charB[]="tsaedsae";

charflag='0';//flag用来标记处理括号

intmark=1;

intf=0;

 

structStackS;

structStacktemp;//用来处理括号外的元素

InitStack(S);

InitStack(temp);

 

structLinkQueueQ;

InitQueue(Q);

 

charMoWang[100]="\0";

chare1,key,e2,e;

printf("**************************************************************\n");

printf("**欢迎光临广东工业大学**\n");

printf("******************************\n");

printf("**魔王语言解释系统**\n");

printf("******************************\n");

printf("*班级:

计算机学院网络工程2007级4班*\n");

printf("*姓名:

黄文龙学号:

3107007087*\n");

printf("**************************************************************\n\n");

 

printf("请输入你想要解释的魔王语言(最多含有一个括号):

\n");

gets(MoWang);

InStack(MoWang,S);//把要解释的魔王语言压入栈中

while(!

StackEmpty(S)){

Pop(S,e1);

if(e1=='('){

if(StackEmpty(S)){

printf("魔王语言错误!

\n");

mark=0;

break;

}

while(!

StackEmpty(S)){

Pop(S,e1);

if(e1==')'){

f=1;

break;

}

elseif(!

(e1>='a'&&e1<='z')&&!

(e1>='A'&&e1<='Z')){

printf("魔王语言错误!

\n");

mark=0;

break;

}

}

if(mark==0){

break;

}

if(f!

=1){

printf("魔王语言错误!

\n");

break;

}

}

elseif(e1==')'){

printf("魔王语言错误!

\n");

mark=0;

break;

}

elseif(!

(e1>='a'&&e1<='z')&&!

(e1>='A'&&e1<='Z')){

printf("魔王语言错误!

\n");

mark=0;

break;

}

}

if(mark==1&&f==1){

ClearStack(S);

InStack(MoWang,S);

while(!

StackEmpty(S)){//栈不空时

Pop(S,e1);

if(e1=='B'){

Push(temp,e1);

}

elseif(e1=='A'){

Push(temp,e1);

}

elseif(e1=='('){//用队存储括号中的元素

Push(temp,flag);//有括号的话就用flag标记

Pop(S,e1);

while(e1!

=')'){

EnQueue(Q,e1);

Pop(S,e1);

}

if(!

QueueEmpty(Q)){

DeQueue(Q,key);

}

}

else{

Push(temp,e1);

f=0;

}

}

while(!

StackEmpty(temp)){//边处理边进栈

Pop(temp,e1);

if(e1!

=flag){//把括号外的元素压入中

Push(S,e1);

}

else{

while(!

QueueEmpty(Q)){//处理括号中的元素进栈

DeQueue(Q,e2);

Push(S,key);

Push(S,e2);

}

if(f!

=0){//最后还要压一个key

Push(S,key);

}

}

}

 

printf("解释后的语言为:

\n");

while(!

StackEmpty(S)){//依次出栈输出处理后的元素

Pop(S,e);

EnQueue(Q,e);//元素进队是为了输出对应汉字

if(e=='B'){

printf("%s",B);

}

elseif(e=='A'){

printf("%s",A);

}

else{

printf("%c",e);

}

}

printf("\n");

while(!

QueueEmpty(Q)){//输出对应汉字

DeQueue(Q,e);

switch(e){

case't':

printf("天");break;

case'd':

printf("地");break;

case's':

printf("上");break;

case'a':

printf("一只");break;

case'e':

printf("鹅");break;

case'z':

printf("追");break;

case'g':

printf("赶");break;

case'x':

printf("下");break;

case'n':

printf("蛋");break;

case'h':

printf("恨");break;

case'B':

printf("天上一只鹅地上一只鹅");break;

case'A':

printf("上一只鹅");break;

default:

printf("*");break;

}

}

printf("\n");

}

 

system("pause");

return0;

}

 

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 表格模板 > 合同协议

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1