An expression consisting of addition, subtraction, multiplication, division, and parentheses is provided to calculate the value of the expression and the prefix and suffix expression of the expression.
# Include
# Include
# Define Inf 1e9struct tree {double date; char ch; tree * l, * r; tree () {ch = '\ 0'; date = 0; l = r = NULL ;}}; double judge (char * s, int x, int y, double & n) {double num = 0; for (int I = x; I
= '0' & s [I] <= '9') {num = num * 10 + s [I]-'0';} else return 0; return n = num;} tree * build (char * s, int x, int y) {tree * now = new tree; double num = Inf; judge (s, x, y, num); // printf ("% c \ n", s [x]); if (num! = Inf) {now-> date = num; return now;} int p = 0, c1 =-1, c2 =-1; for (int I = x; I
L = build (s, x, c1); now-> r = build (s, c1 + 1, y); now-> ch = s [c1]; return now;} double dfs (tree * p) {if (! P) return 0; if (p-> l = p-> r & p-> l = NULL) return p-> date; switch (p-> ch) {case '+': return p-> date = dfs (p-> l) + dfs (p-> r); break; case '-': return p-> date = dfs (p-> l)-dfs (p-> r); break; case '*': return p-> date = dfs (p-> l) * dfs (p-> r); break; case '/': return p-> date = dfs (p-> l)/dfs (p-> r); break;} return 0;} void dfs (tree * p, int choose) {if (! P) return; if (p-> l = p-> r & p-> l = NULL) {printf ("% g ", p-> date);} if (! Choose) {printf ("% c", p-> ch); dfs (p-> l, choose); dfs (p-> r, choose );} else {dfs (p-> l, choose); dfs (p-> r, choose); printf ("% c", p-> ch );}} char s [1005]; int main () {tree * root = NULL; while (gets (s) = NULL) {root = NULL; int len = strlen (s ); root = build (s, 0, len); double ans = dfs (root); puts ("prefix expression:"); dfs (root, 0 ); puts (""); puts ("suffix expression:"); dfs (root, 1); puts (""); printf ("% s = % g \ n ", s, ans);} return 0 ;}
The above cannot be calculated as 5 *-6, and the following is corrected.
# Include
# Include
# Define Inf 1e9struct tree {double date; char ch; tree * l, * r; tree () {ch = '\ 0'; date = 0; l = r = NULL ;}}; char st [1005]; double judge (char * s, int x, int y, double & n) {double num = 0; for (int I = x; I
= '0' & s [I] <= '9') {num = num * 10 + s [I]-'0';} else return 0; return n = num;} int is_operator (char c) {if (c = '+' | c = '-' | c = '*' | c = '/') return 1; return 0;} tree * build (char * s, int x, int y) {tree * now = new tree; double num = Inf; judge (s, x, y, num); // printf ("% c \ n", s [x]); if (num! = Inf) {now-> date = num; return now;} int p = 0, c1 =-1, c2 =-1; for (int I = x; I
L = build (s, x, c1); now-> r = build (s, c1 + 1, y); now-> ch = s [c1]; return now;} double dfs (tree * p) {if (! P) return 0; if (p-> l = p-> r & p-> l = NULL) return p-> date; switch (p-> ch) {case '+': return p-> date = dfs (p-> l) + dfs (p-> r); break; case '-': return p-> date = dfs (p-> l)-dfs (p-> r); break; case '*': return p-> date = dfs (p-> l) * dfs (p-> r); break; case '/': return p-> date = dfs (p-> l)/dfs (p-> r); break;} return 0;} void dfs (tree * p, int choose) {if (! P) return; if (p-> l = p-> r & p-> l = NULL) {printf ("% g ", p-> date);} if (! Choose) {printf ("% c", p-> ch); dfs (p-> l, choose); dfs (p-> r, choose );} else {dfs (p-> l, choose); dfs (p-> r, choose); printf ("% c", p-> ch );}} char s [1005]; int main () {tree * root = NULL; while (gets (s )! = NULL) {root = NULL; int len = strlen (s); root = build (s, 0, len); double ans = dfs (root ); puts ("prefix expression:"); dfs (root, 0); puts (""); puts ("suffix expression:"); dfs (root, 1 ); puts (""); printf ("% s = % g \ n", s, ans);} return 0 ;}