데이터 구조 재 미 있 는 문제 - 언어 번역
15768 단어 데이터 구조
1: #include <stdio.h>
2: #include <stdlib.h>
3: #define STACK_INIT_SIZE 20
4: #define STACKINCREMENT 10
5:
6: typedef char ElemType; /* char ElemType*/
7: typedef struct { /* */
8: ElemType *base;
9: ElemType *top;
10: int stacksize;
11: } sqStack;
12:
13:
14: void initStack(sqStack *s)
15: {
16: /* , s->base*/
17: s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
18:
19: if(!s->base) exit(0); /* */
20:
21: s->top = s->base; /* , */
22: s->stacksize = STACK_INIT_SIZE; /* STACK_INIT_SIZE */
23: }
24:
25: void Push(sqStack *s, ElemType e) { /* */
26: if(s->top - s->base >= s->stacksize) {
27: /* , */
28: s->base = (ElemType *)realloc(s->base, (s->stacksize +
29: STACKINCREMENT) * sizeof(ElemType));
30:
31: if(!s->base) exit(0); /* */
32:
33: s->top = s->base + s->stacksize;
34: s->stacksize = s->stacksize + STACKINCREMENT; /* */
35: }
36:
37: *(s->top) = e; /* */
38: s->top++;
39: }
40:
41: void Pop(sqStack *s , ElemType *e) { /* */
42: if(s->top == s->base) return; /* */
43:
44: *e = *--(s->top); /* */
45: }
46:
47: int StackLen(sqStack s) { /* s */
48: return (s.top - s.base) ;
49: }
50:
51:
52: void translate(ElemType e, sqStack *s) {
53: ElemType c , a;
54: sqStack ss1;
55:
56: if(e >= 97 && e <= 122 ) printf("%c", e);
57: else if(e == 'A') printf("%s", "sae");
58: else if(e == 'B') printf("%s", "tsaedsae");
59: else if(e == '(')
60: {
61:
62: initStack(&ss1); /* ss1, */
63: Pop(&(*s), &c); /* *s s*/
64: a = c; /* */
65: Pop(&(*s), &c);
66:
67: while(c != ')') {
68: Push(&ss1, a);
69: Push(&ss1, c);
70: Pop(&(*s), &c);
71: }
72:
73: Push(&ss1, a); /* ss1*/
74:
75: while(StackLen(ss1)) /* */
76: {
77: Pop(&ss1, &c); /* ss1 c*/
78: translate(c, &ss1); /* translate c */
79: }
80: }
81: }
82:
83: int main()
84: {
85: ElemType e;
86: sqStack s1, s2;
87: initStack(&s1); /* s1*/
88:
89: printf("Please input Devil language:
");
90: scanf("%c", &e);
91:
92: while(e != '#')
93: {
94: if(e == 'A' || e == 'B' || (e >= 97 && e <= 122) || e == '(' || e == ')')
95: { /* */
96: Push(&s1, e); /* */
97:
98: }
99:
100: scanf("%c", &e);
101: }
102:
103:
104: initStack(&s2); /* s2*/
105:
106: while(StackLen(s1))
107: {
108: Pop(&s1, &e);
109: //printf("%c
",e);
110: Push(&s2, e); /* s2*/
111: }
112:
113: printf("The mankind language is:
");
114:
115: while(StackLen(s2))
116: {
117: Pop(&s2, &e);
118: translate(e, &s2) ;
119: }
120:
121: return 0;
122:
123: }