C + + two fork Tree application: Evaluating expressions

Source: Internet
Author: User
Tags eol

Last night, I spent a lot of time studying inside the binary tree application to solve the problem of calculation expressions, has been not understood, mainly feel is not their own fault, and lazy, do not want to knock the code to the computer to see, the result of a lot of wasted time. So still remind everyone, code this kind of thing, what a lot of look, think he was wrong to knock himself to the computer to see! In fact, too much, is missing something, resulting in the original code in the parentheses completely meaningless, that is, the book's Code, although the calculation of the expression in the parentheses, but nothing to do, And that's actually a little bit of an improvement: Add a flag to store the last read char, if it's ') ', calculate the left as an operand.

OK, put the correct code on the following:

#include

using namespace Std;

Class Calc

{

Enum Type {DATA, ADD, SUB, MULTI, DIV, Oparen, Cparen, EOL};

struct node

{

Type type;

int data;

Node *lchild, *rchild;

Node (Type t, int d=0, node *lc=null, node *rc=null)

{

type=t; Data=d; LCHILD=LC; RCHILD=RC;

}

};

Node *root;

Node *create (char * &s);

Type GetToken (char * &s, int &value);

int result (node *t);

Public

Calc (char *s) {root=create (s);}

int result () {if (root==null) return 0;

return result (root);}

};

Calc::node *calc::create (char * &s)

{

Node *p, *root=null;

Type Returntype,flag=data;

int value;

while (*s)

{

Flag=returntype;

Returntype=gettoken (S,value);

Switch (returntype)

{

Case DATA:

Case Oparen:

if (returntype = = DATA) p=new node (data,value);

else P=create (s);

if (root==null) root=p;

else if (root->rchild==null) root->rchild=p;

else root->rchild->rchild=p;

Break

Case Cparen:

Case Eol:return Root;

Case ADD:

Case SUB:

Root=new node (returntype,0,root);

Break

Case MULTI:

Case DIV:

if (Root->type==data | | root->type==multi | | root->type==div | | flag==oparen)

Root=new node (returntype,0,root);

else root->rchild=new node (returntype,0,root->rchild);

}

}

return root;

}

Calc::type Calc::gettoken (char *&s, int &data)

{

char type;

while (*s== ') ++s;

if (*s>= ' 0 ' && *s<= ' 9 ')

{

data=0;

while (*s>= ' 0 ' && *s<= ' 9 ') {data=data*10+ *s-' 0 '; ++s;}

return DATA;

}

if (*s = = ") return EOL;

Type =*s; ++s;

Switch (type)

{

Case ' + ': return ADD;

Case '-': return SUB;

Case ' * ': return MULTI;

Case '/': return DIV;

Case ' (': return oparen;

Case '] ': return cparen;

Default:return EOL;

}

}

int Calc::result (node *t)

{

int num1,num2;

if (T->type = = DATA) return t->data;

Num1=result (T->lchild);

Num2=result (T->rchild);

Switch (t->type)

{

Case add:t->data=num1+num2;break;

Case sub:t->data=num1-num2;break;

Case multi:t->data=num1*num2;break;

Case div:t->data=num1/num2;break;

}

Return t->data;

}

int main ()

{

Char equation[256];

cin>>equation;

Calc exp ("3* (6/2)");

Calc exp (equation);

cout<

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.