1 /* 2 * 魔王语言 3 * 栈----先进后出-----处理带括号的部分 4 * 队列--先进先出-----将对处理后的语言 5 * 2018.10.3 fang 6 */ 7 #include<stdio.h> 8 #include<stdlib.h> 9 #define STACK_INIT_SIZE 100; //存储空间初始分配量 10 #define STACKINCREMENT 10; //存储空间分配增量 11 #define NULL 0; 12 13 /* 14 输入检验: 1. B(ehnxgz)B 输出: tsaedsaeezegexenehetsaedsae 15 2. B(ab(cde)fg)B 输出: tsaedsaeagafacadacaeacabatsaedsae 16 3. #BooooooMAoo今天天气不错! 输出: #tsaedsaeooooooMsaeoo今天天气不错! 17 */ 18 19 20 typedef struct Sqstack{ 21 char *base; 22 char *top; 23 int stacksize; 24 }sqstack; 25 26 typedef struct QNode{ 27 char data; 28 struct QNode *next; 29 }Qnode, *QueuePtr; 30 31 typedef struct LinkQueue{ 32 QueuePtr front; 33 QueuePtr rear; 34 }LinkQueue; 35 //初始化栈 36 void InitStack(sqstack *s); 37 //入栈 38 void push(sqstack *s,char e); 39 //出栈 40 char Pop(sqstack *s); 41 //初始化队列 42 void InitQueue(LinkQueue *q); 43 //入队 44 void Enqueue(LinkQueue *q,char e); 45 //出队 46 char Dequeue(LinkQueue *q); 47 48 49 int main() { 50 sqstack *s; 51 LinkQueue *q; 52 int m, j; 53 int i = 0; 54 int count = 0; 55 char c, language[1000]; 56 q = (LinkQueue*)malloc(sizeof(LinkQueue)); 57 s = (sqstack*)malloc(sizeof(sqstack)); 58 InitStack(s); 59 InitQueue(q); 60 //① 首先,将魔王语言存放在language数组中 61 printf(" *************** Devil's Language *************** "); 62 printf(" 请输入魔王语言: "); 63 while(c != ' ') { 64 scanf("%c",&c); 65 if(c == '(') count++; 66 language[i] = c; 67 i++; 68 } 69 i--; 70 71 if(count == 0){ 72 } else{ 73 while(count >= 1){ 74 int k1 = 0; 75 //② 然后,将数组中 第count重括号,(用k1来表示循环到第几重)内元素入栈(括号里的元素放在栈中逆置) 76 //从第二位数字开始,每位数字后面都插入括号里数据的首字母 77 78 for(j=0;j<=i;j++) { 79 if(language[j] == '(') { 80 k1++; 81 if(k1==count){ 82 m = j; // language[m+1]作为固定的首字母值 83 push(s,language[m+1]); 84 while(language[j+2] != ')'){ 85 push(s,language[j+2]); 86 push(s,language[m+1]); 87 j++; 88 } 89 break; 90 }else j++; 91 } 92 } 93 94 //③ 将数组中元素全部入队,遇到存入栈的()先出栈再入队 95 for(j=0; j<=i; j++) { 96 if(language[j] == '(' && j==m) { 97 while(s->base != s->top) { 98 char e = Pop(s); 99 Enqueue(q,e); 100 } 101 while(language[j] != ')'){ 102 j++; 103 continue; 104 } 105 }else if(count==1 && (language[j]=='(' || language[j]==')')){ 106 107 }else Enqueue(q,language[j]); 108 } 109 110 111 count--; 112 i = 0; 113 while(q->front != q->rear){ 114 language[i] = Dequeue(q); 115 i++; 116 } 117 118 } 119 } 120 121 //④ 将队中的元素全部取出,逐个翻译 122 printf(" 翻译之后: "); 123 for(j = 0; j<=i; j++) { 124 char ch = language[j]; 125 if(ch == 'A') printf("%s","sae"); 126 else if(ch == 'B') printf("%s","tsaedsae"); 127 else printf("%c",ch); 128 } 129 return 0; 130 131 } 132 133 134 135 136 137 138 139 140 141 142 143 //栈操作 144 void InitStack(sqstack *s) { 145 s->base = (char*) malloc(100 * sizeof(char)); 146 s->top = s->base; 147 s->stacksize = STACK_INIT_SIZE; 148 } 149 //压栈 150 void push(sqstack *s,char e) { 151 //如果栈满 152 if(s->top - s->base >= s->stacksize) { 153 s->base = (char *)realloc(s->base,(s->stacksize+10)*sizeof(char)); 154 s->stacksize += STACKINCREMENT; 155 s->top = s->base + s->stacksize; 156 } 157 *(s->top) = e; 158 s->top++; 159 } 160 //出栈 161 char Pop(sqstack *s) { 162 char e; 163 if(s->top-s->base == 0) 164 return -1; 165 --s->top; 166 e = *(s->top); 167 return e; 168 } 169 170 171 172 173 174 //队列操作 175 void InitQueue(LinkQueue *q){ 176 q->front = q->rear = (QueuePtr)malloc(sizeof(Qnode)); 177 if(!q->front) exit(-1); //内存分配失败 178 q->front->next = NULL; 179 } 180 //入队 181 void Enqueue(LinkQueue *q,char e){ 182 QueuePtr p = (QueuePtr)malloc(sizeof(Qnode)); 183 if(!p) exit(-1); //内存分配失败 184 p->data = e; 185 p->next = NULL; 186 q->rear->next = p; 187 q->rear = p; 188 } 189 //出队 190 char Dequeue(LinkQueue *q){ 191 char e; 192 if(q->front == q->rear) exit(0); 193 QueuePtr p = q->front->next; 194 e = p->data; 195 q->front->next = p->next; 196 if(q->rear == p) q->rear = q->front; 197 free(p); 198 return e; 199 }