魔王语言源代码.docx
- 文档编号:6181264
- 上传时间:2023-01-04
- 格式:DOCX
- 页数:9
- 大小:32.87KB
魔王语言源代码.docx
《魔王语言源代码.docx》由会员分享,可在线阅读,更多相关《魔王语言源代码.docx(9页珍藏版)》请在冰豆网上搜索。
魔王语言源代码
题目:
魔王语言
[源代码]
#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;
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 魔王 语言 源代码