Expression calculation (replace an infix expression with a suffix prefix expression)

Source: Internet
Author: User

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 ;}
    
   
  
 





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.