1 Replies - 111 Views - Last Post: 08 February 2012 - 06:30 AM Rate Topic: -----

Topic Sponsor:

#1 GDubz  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 87
  • Joined: 06-January 12

question about evaluting and printing trees

Posted 08 February 2012 - 01:57 AM

Im trying to take the hand coded tree and print it, but I know what node of the tree to print from, and then the expression needs to be evaluated, my question is would i use a loop to run through the tree?




#include <iostream>
#include <string>
#include <cmath>
#include "math_tree.h"
#include "calc_parser.h"
using namespace std;

double evalOp(string op, double val)
{
    // for evaluating functions like sin, cos, etc.
    }

double evalOp(string op, double val1, double val2)
{
    // for evaluating operators like +, -, etc.

}

double eval(Tree *root)
{
    // for (recursively) evaluating a tree; this function will refer
    // back to itself (for evaluating subtrees) and will use the
    // evalOp() functions




}

// this is a global variable used by the parser
Tree* math_tree;

int main()
{
    //  sin(3.14159/4.0)+cos(8.0*2.0)

    Tree p1;
    p1.op = "+";

    Tree p2;
    p2.op = "sin";

    Tree p3;
    p3.op = "cos";

    Tree p4;
    p4.op = "/";

    Tree p5;
    p5.op = "*";

    Tree v1;
    v1.val = 3.145159;

    Tree v2;
    v2.val = 4.0;

    Tree v3;
    v3.val = 8.0;

    Tree v4;
    v4.val = 2.0;

    p1.left = &p2;
    p1.right = &p3;
    p2.left = &p4;
    p3.left = &p5;
    p4.left = &v1;
    p4.right = &v2
    p5.left = &v3;
    p5.right = &v4;



    //print your hand-coded tree
    print_ascii_tree();

    //evaluate the hand-coded tree (should equal 2.0)
    cout << "Result: " << eval() << endl << endl << endl;


    
    while(true)
    {
        Parser parser;
        cout << "Enter expression: ";

        // this function gets the input and does the parsing
        parser.yyparse();

        // the yyparse() function sets the global variable math_tree
        // to a new tree; if that tree is NULL (no tree), just quit
        if(math_tree == NULL)
            break;

        // otherwise, print the tree
        print_ascii_tree(math_tree);

        // and evaluate it
        cout << "Result: " << eval(math_tree) << endl << endl;

        // reset the tree back to NULL before looping
        math_tree = NULL;
    }

    return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: question about evaluting and printing trees

#2 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 438
  • View blog
  • Posts: 849
  • Joined: 17-March 11

Re: question about evaluting and printing trees

Posted 08 February 2012 - 06:30 AM

Most commonly, you would use a recursion, a evaluate function could look something like this. (in pseudocode, for binary expression)

evaluate(expression) {
    double left = evaluate(expression.left)
    double right = evaluate(expression.right)
    if (exression.operator == +)
       return left + right;
    elsif (exression.operator == -)
       return left - right;
    ...
}


This post has been edited by Karel-Lodewijk: 08 February 2012 - 03:19 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1