这是数据结构的实验~~
这是实现一个魔王语言解释器。
这里是采用栈,队列,线性表的综合实现的。
这个程序没上交,所以是中文注释的。
其实这个程序本身实用性不强,但是是个很不错的题目。
Experiment2_2.h
/* Experiment2_2.h */
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// 实验题2.2:魔王语言解释。
// 问题描述
// 有一个魔王总是使用自已的一种非常精练而抽象的语言讲话,没有人能听得懂。
// 但他的语言是可以逐步解释成人能懂的语言的,
// 因为他的语言是由以下两种形式的规则由人的语言逐步抽象上去的:
// (1) α→β1β2…βm
// (2) (θβ1β2…βm)→(θβm…β2θβ1θ)
// 在这两种形式中,从左到右均表示解释; 从右到左均表示抽象。
// 写一个魔王解释程序,将魔王的话解释成人能听懂的话。
// 基本要求:设大写字母表示魔王语言的词汇,小写字母表示人的词汇,
// 希腊字母表示可以用大写字母或小写字母代换的变量。用下述两种规则和下述规则(2)实现。
// (1) B→tAdA
// (2) A→sae
// 测试数据:B(einxgz)B
// B(einxgz)B=>tAdA(einxgz)tAdA=>tsaedsae(einxgz)tsaedsae
// => tsaedsaeezegexeneietsaedsae
// 字母-汉字对应表:
// t d s a e z g x n i
// 天 地 上 一个 鹅 追 赶 下 蛋 恨
// 则魔王说的是:"天上一个鹅地上一个鹅鹅追鹅赶鹅下鹅蛋鹅恨鹅天上一个鹅地上一个鹅"。
//
// 提示:该题中涉及栈、队列和线性表。
// (1)利用队列处理B:tsaedsae (由B→tAdA, A→sae得)
// (2)利用栈处理:(θβ1β2…βm)→(θβm…β2θβ1θ)
// (3)利用线性表(字母-汉字对应表)进行魔王语言解释。
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
// - - - - - - 基本常量 - - - - - -
#define OK 1
#define FAIL 0
// - - - - - - 数据结构 - - - - - -
// 魔王语言类型
typedef char* DevileLanguage;
// 魔王语言字符集
const char DevileLetter[14] = { 'A', 'B', 'a', 'd', 'e', 'g', 'i', 'n', 's', 't', 'x', 'z', '(', ')' };
// 字母-汉字对应表的元素类型
// letter代表字母,chinese代表中文
typedef struct
{
char letter;
char* chinese;
}ElemType;
// 字母-汉字对应表
typedef struct SqList
{
ElemType* elem;
int length;
}SqList;
// - - - - - - 基本函数 - - - - - -
// 生成字母-汉字对应表
void BulidLookupTable(SqList*& sq);
// 销毁字母-汉字对应表
int DestoryLookupTable(SqList*& sq);
// 定位元素e在字母-汉字对应表中的位置
int LocateElem(SqList* sq, char e);
// 翻译魔王语言,并输出翻译结果
int TranslateDevileLang(DevileLanguage sentence);
void BulidLookupTable(SqList*& sq)
{
sq = new SqList;
sq->length = 10;
sq->elem = new ElemType[10];
sq->elem[0].letter = 't';
sq->elem[0].chinese = (char*)"天";
sq->elem[1].letter = 'd';
sq->elem[1].chinese = (char*)"地";
sq->elem[2].letter = 's';
sq->elem[2].chinese = (char*)"上";
sq->elem[3].letter = 'a';
sq->elem[3].chinese = (char*)"一个";
sq->elem[4].letter = 'e';
sq->elem[4].chinese = (char*)"鹅";
sq->elem[5].letter = 'z';
sq->elem[5].chinese = (char*)"追";
sq->elem[6].letter = 'g';
sq->elem[6].chinese = (char*)"赶";
sq->elem[7].letter = 'x';
sq->elem[7].chinese = (char*)"下";
sq->elem[8].letter = 'n';
sq->elem[8].chinese = (char*)"蛋";
sq->elem[9].letter = 'i';
sq->elem[9].chinese = (char*)"恨";
}
int DestoryLookupTable(SqList*& sq)
{
delete[] sq->elem;
delete sq;
return OK;
}
int LocateElem(SqList* sq, char e)
{
int i;
for (i = 0; i < 10; i++) {
if (sq->elem[i].letter == e) {
return i;
}
}
return -1;
}
bool IsDevilLetter(char word, const char DevileLetter[14])
{
int i;
for (i = 0; i < 14; i++) {
if (word == DevileLetter[i]) {
return true;
}
}
return false;
}
int TranslateDevileLang(DevileLanguage sentence)
{
// 魔王语言翻译器,sentence为要翻译的魔王句子
// 若sentence为空,返回错误
if (sentence == NULL) {
return FAIL;
}
// 队列Q和栈S,用于处理魔王句子
queue<char> Q;
stack<char> S;
// word用于字符逐个扫描魔王句子
// sita为括号中要重复的字符
char* word;
char sita;
word = sentence;
// 将魔王句子翻译成字母序列
while (*word != '/0') {
if (!IsDevilLetter(*word, DevileLetter)) {
return FAIL;
}
// 将B翻译成"tsaedsae"
if (*word == 'B') {
Q.push('t');
Q.push('s');
Q.push('a');
Q.push('e');
Q.push('d');
Q.push('s');
Q.push('a');
Q.push('e');
}
// 将A翻译成"sae"
else if (*word == 'A') {
Q.push('s');
Q.push('a');
Q.push('e');
}
// 处理括号里的字符
else if (*word == '(') {
sita = *++word;
word++;
while (*word != ')' && *word != '/0') {
S.push(*word);
word++;
}
while (!S.empty()) {
Q.push(sita);
Q.push(S.top());
S.pop();
}
Q.push(sita);
}
// 处理一般字符
else {
Q.push(*word);
}
word++;
}
// 生成字母-汉字表
SqList* sq;
BulidLookupTable(sq);
// 将字母序列翻译成汉字,并输出
word = new char;
cout << "/ - ";
while (!Q.empty()) {
*word = Q.front();
cout << sq->elem[LocateElem(sq, *word)].chinese;
Q.pop();
}
delete word;
DestoryLookupTable(sq);
// 返回成功
return OK;
}
test.cpp
/* test.cpp */
#include "Experiment2_2.h"
int main()
{
cout << "/ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - " << endl;
cout << "/ - 实验题2.2:魔王语言解释。" << endl;
cout << "/ - " << endl;
cout << "/ - 魔王语言支持下列字符:" << endl;
cout << "/ - A B a d e g i n s t x z ( )" << endl;
cout << "/ - 备注:括号里不能包含大写字母或嵌套括号" << endl;
cout << "/ - " << endl;
cout << "/ - 字母-汉字对应表:" << endl;
cout << "/ - t d s a e z g x n i" << endl;
cout << "/ - 天 地 上 一个 鹅 追 赶 下 蛋 恨" << endl;
cout << "/ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - " << endl;
cout << endl;
char* lang;
lang = new char[100];
cout << "/ - 输入魔王句子:";
cin >> lang;
cout << endl;
int result;
cout << "/ - 魔王说的是:" << endl;
result = TranslateDevileLang(lang);
if (result == FAIL) {
cout << "/ - 你的输入有误" << endl;
}
cout << endl;
return 0;
}