Problem Description and Requirement Analysis
There was a monster who always spoke in his own very refined and abstract language that no one could understand. But his language can gradually be interpreted as a language that adults can understand, because his language is gradually abstracted from human language by the following two forms of rules:
(1) α→β1...βm
(2) θδ 1... δ n) θδ n θδ n-1... θδ 1 θ In both forms, explanations are expressed from left to right. Abstraction is represented from right to left. Try to write an interpretation system for the Devil's language, explaining what he said to adults.
Test data:
Use the following two specific rules and the above rule form (2). Set uppercase letters to represent words interpreted by the Devil King's language and lowercase letters to represent words in human's language. The Greek alphabet represents a variable that can be replaced with uppercase or lowercase letters. The Devil's language can contain human words.
(1).Β→ tΑdΑ
(2).Α→ sae
Test data:
B(einxgz)B
Interpreted as tsaedsaeezegexeneietsaedsae if the lowercase letters correspond to the Chinese characters shown in the table below, the Devil King said: "A goose chases a goose on the ground of a goose in the sky, a goose chases a goose under a goose, an egg hates a goose on the ground of a goose in the sky."
t d s a e z g x n i
A goose on the heavens chased down the eggs and hated them
requirement analysis
1. Create a queue and stack.
2. Require a list of King's languages to be entered.
3. Decoding method: B->tAdA, A->sae
4. Print the output according to the relationship between the King's language and the corresponding Chinese characters
Problem Analysis and Implementation Route (Queue+Stack)
Tip: Put the King's language on the stack from right to left, always at the top of the stack. If open brackets, step out of the stack one by one, queue alphabetically until closing brackets leave the stack, and step out of the queue one by one as required by the rules before processing. The other scenarios are simpler and ask the reader to think about what to do. The basic operations of the stack and queue should be implemented first.
Structural Core Code
//Define the nodes of the queue and stack typedef struct node{ char data; struct node *next; }LinkNode; //Define stack top pointer typedef struct { LinkNode *top; }LinkStack; //Define the queue head and end typedef struct{ LinkNode * rear,*front; }LinkQueue;
Queue Core Code
Entry
//Entry void InQueue(LinkQueue *queue,char x){ LinkNode *p = (LinkNode*) malloc(sizeof (LinkNode*)); p->data = x; p->next = NULL; if(queue->front ==NULL){ queue->front = queue->rear = p; } else { queue->rear->next = p; queue->rear = p; } }
Queue
//Stack Out char pop(LinkStack *stack ){ if(stack->top==NULL){ return 0; } // Stack Out LinkNode *p = stack->top; stack->top = p->next; char temp = p->data; //Save stack values free(p); return temp; }
Core Code of Stack
Push
//Stack (Chain Stack) void push(LinkStack *stack,char x){ LinkNode *p = (LinkNode *)malloc(sizeof (LinkNode)); p->data = x; p->next = stack->top; stack->top = p; } //Entry
Stack Out
//Stack Out char pop(LinkStack *stack ){ if(stack->top==NULL){ return 0; } // Stack Out LinkNode *p = stack->top; stack->top = p->next; char temp = p->data; //Save stack values free(p); return temp; }
All Code
The code is as follows (example):
c #include <stdio.h> #include <string.h> #include <malloc.h> //Define the nodes of the queue and stack typedef struct node{ char data; struct node *next; }LinkNode; //Define stack top pointer typedef struct { LinkNode *top; }LinkStack; //Define the queue head and end typedef struct{ LinkNode * rear,*front; }LinkQueue; //Initialize Queue void InitQueue(LinkQueue *queue) { queue->front = queue->rear = NULL; } //Initialization stack void InitStack(LinkStack *stack){ stack->top = NULL; } //Push void push(LinkStack *stack,char x){ LinkNode *p = (LinkNode *)malloc(sizeof (LinkNode)); p->data = x; p->next = stack->top; stack->top = p; } //Entry void InQueue(LinkQueue *queue,char x){ LinkNode *p = (LinkNode*) malloc(sizeof (LinkNode*)); p->data = x; p->next = NULL; if(queue->front ==NULL){ queue->front = queue->rear = p; } else { queue->rear->next = p; queue->rear = p; } } //Stack Out char pop(LinkStack *stack ){ if(stack->top==NULL){ return 0; } // Stack Out LinkNode *p = stack->top; stack->top = p->next; char temp = p->data; //Save stack values free(p); return temp; } //Out of Queue char DeQueue(LinkQueue *queue){ if(queue->front ==NULL){ return 0; } LinkNode *p = queue->front; char temp = p->data; //Save Queue Values queue->front = p->next; free(p); return temp; } //Determine the end of stack void deal(LinkQueue *queue,LinkStack *stack){ char res = pop(stack); // End of stack exit when encountering closing parentheses while(res !=')') { InQueue(queue,res); res = pop(stack); } char first = DeQueue(queue); //Keep the first Greek letter push(stack,first); while(queue->front!=NULL){ res = DeQueue(queue); // Press in the original letter and the first Greek letter and push(stack,res); push(stack,first); } } //Match text, output results void metch(char x){ char password[11] = "tdsaezgxni"; char chinese[10][8] = {"day","land","upper","One","goose","chase","Rush","lower","egg","hate"}; for(int i = 0;i< strlen(password);i++){ if(x ==password[i]){ printf("%s",chinese[i]); } } } //Translate the translated password and get the final result void printresult(char x){ char A_string[4] ="sae"; if(x =='A'){ for(int k = 0;k< strlen(A_string);k++){ metch(A_string[k]); //Match Characters } } else{ metch(x); } } int main() { printf("Explain the Devil's language, please enter the Devil's language!\n"); char language[100]; // Get Input gets(language); char B_string[5] ="tAdA"; LinkStack ptr; InitStack(&ptr); //Initialization stack LinkQueue queue; InitQueue(&queue); // Initialize Queue int count =0; for(int i = strlen(language)-1;i>=0;i--){ // Determine if all are stacked if(language[i]=='\000') { break; } // From right to left, stack to'(', start out, queue else if(language[i] =='('){ deal(&queue,&ptr); } else{ // Processing'B'first if(language[i] =='B'){ for(int j = strlen(B_string)-1;j>=0;j--){ // Correspond B to password, press in reverse order push(&ptr,B_string[j]); } } else{ // Stack push(&ptr,language[i]); } } count++; } char temp; while(ptr.top!=NULL){ // Print results, test data B(einxgz)B temp = pop(&ptr); printresult(temp); //Print results } return 0; }
One-way circular queues and stacks (change the corresponding password for B)
All Code
#include <stdio.h> #include <stdlib.h> #include <string.h> //Define the node of the stack #define Max 100 typedef struct{ char *base; char *top; int size; }seqStack; //Define Queue typedef struct{ char *data; int front; int rear; }seqQueue; //Initial Stack void initseqStack(seqStack *stack){ stack->base = (char*)malloc(Max*sizeof (char)); if(stack->base==NULL){ exit(1); } stack->top = stack->base; stack->size = Max; } //Initialize Queue void initseqQueue(seqQueue *queue){ queue->data =(char*)malloc(Max*sizeof (char)); if(queue->data==NULL){ exit(1); } queue->front = queue->rear =0; } //Entry void inQueue(seqQueue *queue,char x){ if((queue->rear+1)%Max ==queue->front ){ printf("The queue is full"); exit(1); } queue->data[queue->rear] =x; queue->rear = (queue->rear+1)%Max; } //Queue char deQueue(seqQueue *queue){ if((queue->rear+1)%Max ==queue->front){ printf("????????"); exit(1); } char temp = queue->data[queue->front]; queue->front = ((queue->front+1)%Max); return temp; } //Push void push(seqStack *stack,char x){ if(stack->top - stack->base ==stack->size){ stack->base = (char*) realloc(stack->base,(stack->size+Max)*sizeof (char)); if(stack->base ==NULL){ exit(1); } stack->top = stack->base+stack->size; stack->size +=Max; } *(stack->top++) =x; } //Stack Out char pop(seqStack *stack){ if(stack->base ==stack->top){ return 0; } char temp= *(--stack->top); return temp; } //Processing void deal(seqQueue *queue,seqStack *stack){ char res = pop(stack); // Save the first character on the stack while(res !=')') { inQueue(queue,res); res = pop(stack); } char first = deQueue(queue); //Keep the first element of the queue push(stack,first); while(queue->front!=queue->rear){ res = deQueue(queue); // Push three elements on the stack, and you have to press that added element push(stack,res); push(stack,first); } } //Matching results void metch(char x){ char password[11] = "tdsaezgxni"; char chinese[10][8] = {"day","land","upper","One","goose","chase","Rush","lower","egg","hate"}; for(int i = 0;i< strlen(password);i++){ if(x ==password[i]){ printf("%s",chinese[i]); } } } //Print results void printresult(char x){ char A_string[4] ="sae"; if(x =='A'){ for(int k = 0;k< strlen(A_string);k++){ metch(A_string[k]); //Match Characters } } else{ metch(x); } } int main() { printf("Explain the Devil's language, please enter the Devil's language!!\n"); char language[100]; // Get Input gets(language); char B_string[100]; printf("Please enter'B'Corresponding Characters"); gets(B_string); seqStack stack; initseqStack(&stack); seqQueue queue; initseqQueue(&queue); int count =0; for(int i = strlen(language)-1;i>=0;i--) { // Determine if all are stacked if (language[i] == '\000') { break; } else if (language[i] == '(') { deal(&queue, &stack); } // From right to left, stack to'(', start out, queue else { // Processing'B'first if (language[i] == 'B') { for (int j = strlen(B_string) - 1; j >= 0; j--) { // Correspond B to password, press in reverse order if(B_string[j]!='\000') { push(&stack, B_string[j]); } } } else { // Stack push(&stack, language[i]); } } } char temp; while(stack.top!=stack.base){ // Print results, test data B(einxgz)B temp = pop(&stack); printresult(temp); //Print results } return 0; }
summary
Here is a summary of the article, with two interpretations of the Devil King's language and different structures. The code is rotten and can be used as a reference. I hope the big guys will point to the little ones and get confused in the commentary area.