算术表达式的转换

http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2484&cid=1182

写的代码很复杂

先转换成后缀式再建立表达式树

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <ctype.h>
  4 
  5 #define MAXN 100
  6 int comp(char a, char b)
  7 {
  8     if(a == '(') return 0;
  9     if(a=='*' || a=='/') return 1;
 10     else if((a=='+' || a=='-') && (b == '+' || b == '-')) return 1;
 11     else return 0;
 12 
 13 }
 14 char st1[MAXN], st2[MAXN];
 15 
 16 typedef struct node
 17 {
 18     union
 19     {
 20         char operator1;
 21         char data;
 22     };
 23     struct node *l;
 24     struct node *r;
 25 } tree;
 26 typedef struct Tree_Stack
 27 {
 28     tree *buf[MAXN];
 29     int n;
 30 } treestack;
 31 treestack *create_empty_stack()
 32 {
 33     treestack *pstack;
 34 
 35     pstack = (treestack *)malloc(sizeof(treestack));
 36     pstack->n = -1;
 37 
 38     return pstack;
 39 }
 40 int push_stack(treestack *p, tree *data)
 41 {
 42     p->n++;
 43     p->buf[p->n] = data;
 44 
 45     return 0;
 46 }
 47 tree *pop_stack(treestack *p)
 48 {
 49     tree *data;
 50 
 51     data = p->buf[p->n];
 52     p->n --;
 53 
 54     return data;
 55 }
 56 int is_empty_stack(treestack *p)
 57 {
 58     if(p->n == -1)
 59         return 1;
 60     else
 61         return 0;
 62 }
 63 void srea1(char s[])
 64 {
 65     int i=0,top1=0,top2=0;
 66     while(s[i] != '#')
 67     {
 68         if(isalpha(s[i])) st1[top1++] = s[i];
 69         else if(top2 == 0) st2[top2++] = s[i];
 70         else if(s[i] == '(') st2[top2++] = s[i];
 71         else if(s[i] == ')')
 72         {
 73             while(st2[top2-1] != '(') st1[top1++] = st2[--top2];
 74             --top2;
 75         }
 76         else if(comp(st2[top2-1], s[i]))
 77         {
 78             st1[top1++] = st2[--top2];
 79             st2[top2++] = s[i];
 80         }
 81         else
 82         {
 83             st2[top2++] = s[i];
 84         }
 85         i++;
 86     }
 87     while(top2 != 0)
 88     {
 89         st1[top1++] = st2[--top2];
 90     }
 91 }
 92 tree *create_express_tree(char *str, treestack *p)
 93 {
 94     int i = 0;
 95     tree *current;
 96     tree *left, *right;
 97 
 98     while(str[i])
 99     {
100         if(str[i] >= 'a' && str[i] <= 'z')
101         {
102             current = (tree *)malloc(sizeof(tree));
103             current->data = str[i];
104             current->l = NULL;
105             current->r = NULL;
106             push_stack(p, current);
107         }
108         else
109         {
110             current = (tree *)malloc(sizeof(tree));
111             current->operator1 = str[i];
112             right = pop_stack(p);
113             left = pop_stack(p);
114             current->l = left;
115             current->r = right;
116             push_stack(p, current);
117         }
118         i++;
119     }
120     return p->buf[p->n];
121 }
122 void print_node(tree *p)
123 {
124     if(p->l== NULL && p->r == NULL)
125         printf("%c", p->data);
126     else
127         printf("%c", p->operator1);
128 }
129 
130 int visit_node(tree *p)
131 {
132     print_node(p);
133 
134     return 0;
135 }
136 
137 void PostOrder(tree *p)
138 {
139     if(p != NULL)
140     {
141         PostOrder(p->l);
142         PostOrder(p->r);
143         visit_node(p);
144     }
145 }
146 void InOrder(tree *p)
147 {
148     if(p != NULL)
149     {
150         InOrder(p->l);
151         visit_node(p);
152         InOrder(p->r);
153     }
154 }
155 
156 void PreOrder(tree *p)
157 {
158     if(p != NULL)
159     {
160         visit_node(p);
161         PreOrder(p->l);
162         PreOrder(p->r);
163     }
164 }
165 
166 int main()
167 {
168     tree *thead;
169     treestack *pstack;
170     int i = 0;
171     char buf[100];
172 
173     scanf("%s", buf);
174     srea1(buf);
175     pstack = create_empty_stack();
176     thead = create_express_tree(st1, pstack);
177 
178     PreOrder(thead);
179 
180     printf("\n");
181     InOrder(thead);
182     printf("\n");
183     PostOrder(thead);
184     printf("\n");
185     return 0;
186 }
View Code

 

posted @ 2013-07-29 15:11  null1019  阅读(217)  评论(0编辑  收藏  举报