| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1740 人关注过本帖
标题:魔王语言C语言代码
只看楼主 加入收藏
zhengdiandia
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2009-9-13
收藏
 问题点数:0 回复次数:1 
魔王语言C语言代码
#include <stdio.h>
#define StackElementType char
#define Stack_Size 100
typedef struct
{StackElementType elem[Stack_Size];
int top;
}SeqStack;

void InitStack(SeqStack *s);/*初始化栈*/
void Push(SeqStack *s,StackElementType x); /*进栈操作*/
void Pop(SeqStack *s,StackElementType *x);/*出栈操作*/
void GetTop(SeqStack *s,StackElementType *x);/*取栈顶元素*/
int IsEmpty(SeqStack *s);/*判断栈是否为空*/
void GhostLanage();/*魔王语言翻译函数*/


/*主函数*/
main()
{
GhostLanage();
printf("\n\t按任意键退出\n\n");

}


/*初始化栈*/
void InitStack(SeqStack *s)
{s->top=-1;
}


/*进栈操作*/
void Push(SeqStack *s,StackElementType x)
{if(s->top==Stack_Size-1)
    printf("\n\t栈已满!");
else {s->top++;s->elem[s->top]=x;}
}


/*出栈操作*/
void Pop(SeqStack *s,StackElementType *x)
{if(s->top==-1)
printf("\n\t栈为空!");
else {*x=s->elem[s->top];s->top--;}
}


/*取栈顶元素*/
void GetTop(SeqStack *s,StackElementType *x)
{if(s->top==-1)
printf("\n\t栈为空!");
else *x=s->elem[s->top];
}


/*判断栈是否为空*/
int IsEmpty(SeqStack *s)
{if(s->top==-1) return(0);
else return(1);
}


/*魔王语言翻译函数*/
void GhostLanage()
{SeqStack B,A,s,B1,A1,r,M;
StackElementType ch,ch1,ch2,x;
char aa[100];
int choice,i=0,n;
InitStack(&B);InitStack(&A);InitStack(&s);InitStack(&r);InitStack(&M);
printf("\n\n 请选择魔王语言的转换形式:\n\t1.B->tAdA   A->sae\n\t2.^^^^\n\t3.^^^^\n");
scanf("%d",&choice);
switch(choice)
{case 1: Push(&B,'t');Push(&B,'A');Push(&B,'d');Push(&B,'A');
           Push(&A,'s');Push(&A,'a');Push(&A,'e');break;
   case 2:;break;
   case 3:;break;
}

if(choice==1)
printf("\n\t你选择的是:B->tAdA   A->sae\n");
if(choice==2)
printf("\n\t你选择的是:^^^\n");
if(choice==3)
printf("\n\t你选择的是:^^^\n");
printf("\n\t请输入要翻译的魔王语言\n");
scanf("%s",aa);
for(i=0;aa[i]!='\0';i++)
Push(&s,aa[i]);
while(IsEmpty(&s))
{Pop(&s,&ch);
     if(ch=='B')
       {B1=B;
        while(IsEmpty(&B1))
   {Pop(&B1,&ch1);
           if(ch1=='A')
             {A1=A;
              while(IsEmpty(&A1))
                    {Pop(&A1,&ch2);
                     Push(&r,ch2);
                    }
             }
           else Push(&r,ch1);
          }
       }
     else if(ch=='A')
             {A1=A;
              while(IsEmpty(&A1))
                    {Pop(&A1,&ch2);
                     Push(&r,ch2);
                    }
             }
     else if(ch==')')
           {Pop(&s,&ch2);
              while(ch2!='(')
                 {Push(&M,ch2);
                  Pop(&s,&ch2);
                 }
      GetTop(&M,&ch2);
      x=ch2;
             Pop(&M,&ch2);
              while(IsEmpty(&M))
                 {Push(&r,x);
                  Pop(&M,&ch2);
    Push(&r,ch2);
                 }
               Push(&r,x);
           }
     else Push(&r,ch);
}
M=r;
printf("\n\n\t翻译的结果为:");
while(IsEmpty(&M))
   {Pop(&M,&ch);
    printf("%c",ch);
   }
printf("\n\n\t是否继续翻译为汉语:(1-继续,0-不继续)");
scanf("%d",&n);
if(n==1)
{ printf("\n\n\t翻译为汉语的结果为:\n\n\t");
     M=r;   
     while(IsEmpty(&M))
      {Pop(&M,&ch);
       if(ch=='t') printf("天");
       else if(ch=='d') printf("地");
       else if(ch=='s') printf("上");
       else if(ch=='a') printf("一只");
       else if(ch=='e') printf("鹅");
       else if(ch=='z') printf("追");
       else if(ch=='g') printf("赶");
       else if(ch=='x') printf("下");
       else if(ch=='n') printf("蛋");
       else if(ch=='h') printf("恨");
      }
    printf("\n");
}
else ;
}
搜索更多相关主题的帖子: C语言 代码 魔王 
2009-09-30 00:04
wbajieng
Rank: 1
等 级:新手上路
帖 子:20
专家分:1
注 册:2010-3-28
收藏
得分:0 
没办法自己设置翻译规则吗
2010-05-09 16:44
快速回复:魔王语言C语言代码
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020666 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved