Create and calculate the arithmetic expression tree (for reference only)

Source: Internet
Author: User
Only to cope with the assignments assigned by the data structure teacher, the number in the formula can only be an integer: for example, input (6 + 3) * (4-2) * (41-1) output 720.00 # include # includeconstintN = 1000; intm [N], flag; doubleres; structNode {doubleval; charc; No

Only to cope with the assignments assigned by the data structure teacher, the number in the formula can only be an integer:

For example, input (6 + 3) * (4-2) * (41-1)

720.00 output

# Include
 
  
# Include
  
   
Const int N = 1000; int m [N], flag; double res; struct Node {double val; char c; Node * left; Node * right; Node () {left = right = NULL; c = 'A'-1 ;}}; Node * build (char * str, int l, int r) {int flag1 =-1, flag2 =-1, p = 0; Node * node = new Node; if (r-l = 1) {node-> val = m [str [l]; return node;} for (int I = l; I <r; I ++) {switch (str [I]) {case '(': p ++; break; case ')': p --; break; case '+': case '-': if (! P) flag1 = I; break; case '*': case '/': if (! P) flag2 = I; break;} if (flag1 <0) flag1 = flag2; if (flag1 <0) return build (str, l + 1, r-1 ); node-> left = build (str, l, flag1); node-> right = build (str, flag1 + 1, r); node-> c = str [flag1]; return node;} double preOrder (Node * root) {if (root = NULL) return 0; if (root-> c = 'A'-1) return root-> val; double a; switch (root-> c) {case '+': a = preOrder (root-> left) + preOrder (root-> right ); brea K; case '-': a = preOrder (root-> left)-preOrder (root-> right); break; case '*': a = preOrder (root-> left) * preOrder (root-> right); break; case '/': if (preOrder (root-> right )! = 0) a = preOrder (root-> left)/preOrder (root-> right); elseflag = 1; break;} return a;} int main () {Node * root; char str [N], s [N]; while (scanf ("% s", str )! = EOF) {memset (m, 0, sizeof (m); res = flag = 0; int len = strlen (str); int n = 0; char a = 'a'; if (str [0] = '-') s [n ++] = a ++; for (int I = 0; I <len; I ++) {if (str [I]> '9' | str [I] <'0 ') s [n ++] = str [I]; else {int sum = 0; while (str [I]> = '0' & str [I] <= '9') {sum = sum * 10 + str [I]-'0 '; I ++;} I --; m [a] = sum; s [n ++] = a ++;} s [n] = '\ 0 '; root = build (s, 0, n); res = preOrder (root); if (! Flag) printf ("expression result: %. 2lf \ n", res); elseprintf ("error! \ N ") ;}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.