魔王语言解释
问题描述
有一个魔王总是使用自己的一种非常精炼而抽象的语言讲话,没有人能听懂。但他的语言是可以逐步解释成人能懂的语言的,因为他的语言是由以下两种形式的规则由人的语言逐步抽象上去的:
(1)
α
\alpha
α→
β
1
\beta_1
β1…
β
m
\beta_m
βm
(2) (
θ
δ
1
.
.
.
δ
n
θδ_1...δ_n
θδ1...δn) →
θ
δ
n
θ
δ
n
−
1
.
.
.
θ
δ
1
θ
θδ_nθδ_{n-1}...θδ_1θ
θδnθδn−1...θδ1θ 在这两种形式中,从左到右均表示解释;从右到左均表示抽象。试写一个魔王语言的解释系统,把他的话解释成人能听得懂的话。
基本要求
用下述两条具体规则和上述规则形式(2)实现。设大写字母表示魔王语言解释的词汇,小写字母表示人的语言的词汇;希腊字母表示可以用大写或小写字母代换的变量。魔王语言可含人的词汇。
(1).Β→ tΑdΑ
(2).Α→ sae
测试数据:
B(einxgz)B
解释成 tsaedsaeezegexeneietsaedsae 若将小写字母与汉字建立下表所示的对应关系,则魔王说的话是:“天上一个鹅地上一个鹅鹅追鹅赶鹅下鹅蛋鹅恨鹅天上一个鹅地上一个鹅。”
t | d | s | a | e | z | g | x | n | i |
---|---|---|---|---|---|---|---|---|---|
天 | 地 | 上 | 一个 | 鹅 | 追 | 赶 | 下 | 蛋 | 恨 |
实现提示
将魔王的语言自右至左进栈,总是处理栈顶。若是开括号,则逐一出栈,将字母顺序入队列,直至闭括号出栈,并按规则要求逐一出队列再处理后入栈。其他情形较简单,请读者思考应如何处理。应首先实现栈和队列的基本运算。
#include <stdio.h>
#include <malloc.h>
#include <string.h>
typedef struct node {
char data;
struct node * next;
}stacks, queues;
struct node * ch[26];
void InitQueue() {
//创建并初始队列
char c;
char cc[20];
printf("输入被替换的字符\n");
scanf("%c", &c);
printf("输入要替换的字符串\n");
scanf("%s", cc);
queues * head;
head = (queues *)malloc(sizeof(queues));
int len = strlen(cc);
queues * pr = head;
pr -> data = cc[0];
for (int i = 1; i < len; i++) {
queues *p;
p = (queues *)malloc(sizeof(queues));
p -> data = cc[i];
pr -> next = p;
pr = p;
}
pr -> next = NULL;
ch[c - 'A'] = head;
}
stacks * createStack(char c, stacks * head) {
//创建栈
head -> data = c;
stacks * p;
p = (stacks *) malloc(sizeof(stacks));
p -> next = head;
return p;
}
stacks * InitStack() {
//初始化栈
stacks * head;
head = (stacks *) malloc(sizeof(stacks));
head -> next = NULL;
return head;
}
queues * deal(queues * head) {
//大写字母替换的处理
queues * HEAD, * TAIL;
queues * q1 = head;
queues * q3 = (queues*) malloc(sizeof(queues));
queues * q4 = q3;
while (q1 != NULL) {
if (q1->data >= 'A' && q1->data <= 'Z') {
HEAD = deal(ch[q1->data - 'A']);
queues *q5 = (queues*) malloc(sizeof(queues));
q5 = HEAD;
q4 -> next = q5;
TAIL = HEAD;
while (TAIL != NULL) {
TAIL = TAIL -> next;
q4 = q4 -> next;
}
}
else {
queues *q5 = (queues*) malloc(sizeof(queues));
q4 -> next = q5;
q5 -> data = q1 -> data;
q5 -> next = NULL;
q4 = q4 -> next;
}
q1 = q1 -> next;
}
return q3 -> next;
}
int main() {
int n;
char language[105];
char trans[100005];
printf("输入要替换的字符的个数:");
scanf("%d", &n);
while(n--) {
getchar();
InitQueue();
}
printf("输入魔王的话\n");
scanf("%s", language);
int len = strlen(language);
int flag = 0, q = 0, pos1 = 0, pos2 = 0;
char key;
stacks * stack_head = InitStack();
for (int i = 0; i < len; ++i) {
if (language[i] == '(') {
flag = 1;
continue;
}
if (language[i] == ')') {
flag = 0;
q = 0;
pos2 = i - 1;
}
if (flag && !q) {
key = language[i];
q = 1;
pos1 = i + 1;
}
if (flag && q) {
stack_head = createStack(language[i], stack_head);
}
}
for (int i = 0; i < 26; i++) {
if (ch[i] != NULL) {
ch[i] = deal(ch[i]);
}
}
int sum = 0;
stack_head = stack_head -> next;
for (int i = 0; i < len; i++) {
if (i >= pos1 && i <= pos2) {
trans[sum++] = stack_head -> data;
stack_head = stack_head -> next;
trans[sum++] = key;
}
else {
if (language[i] <= 'Z' && language[i] >= 'A') {
queues * head = ch[language[i] - 'A'];
while (head != NULL) {
trans[sum++] = head -> data;
head = head -> next;
}
}
else if (language[i] == '(' || language[i] == ')') {
continue;
}
else {
trans[sum++] = language[i];
}
}
}
for (int i = 0; i < sum; ++i) {
printf("%c", trans[i]);
}
printf("\n");
char chh[26][18];
int e;
char c;
printf("输入要替换的字符的个数:");
scanf("%d", &e);
getchar();
while (e--) {
printf("输入字符\n");
scanf("%c", &c);
printf("输入汉字\n");
scanf("%s", chh[c - 'a']);
getchar();
}
for (int i = 0; i < sum; ++i) {
printf("%s", chh[trans[i] - 'a']);
}
return 0;
}