FILE expressh CLASS PROVIDED ExpressionTree This class pr

// FILE: express.h

// CLASS PROVIDED: ExpressionTree

// This class provides only NONEMPTY expression trees

// from Project 1 of Chapter 10 in first edition of

// \"Data Structures and Other Objects Using C++\".

// This current version just slightly modified for CS 24

// by cmc, 11/17/2016.

//

// CONSTRUCTORS for the ExpressionTree class:

// ExpressionTree(double value = 0.0)

// Postcondition: The ExpressionTree has one leaf node with the specified

// value. The default argument is 0.0.

//

// CONST Member Functions for the ExpressionTree class:

// double evaluate( ) const

// Postcondition: The return value is the value of the expression tree.

//

// void printPre() const;

// Postcondition: The expression is printed in prefix form to standard output.

//

// void printPost() const;

// Postcondition: The expression is printed in postfix form to standard output.

//

// void printIn() const;

// Postcondition: The expression is printed in infix form to standard output,

// with add operations enclosed in parentheses.

//

// NONMEMBER FUNCTIONS for the ExpressionTree class:

// ExpressionTree operator +(const ExpressionTree& e1, const ExpressionTree& e2)

// Postcondition: The ExpressionTree returned has \'+\' at the root, e1 as the

// left subtree, and e2 as the right subtree.

//

// ExpressionTree operator *(const ExpressionTree& e1, const ExpressionTree& e2)

// Postcondition: The ExpressionTree returned has \'*\' at the root, e1 as the

// left subtree, and e2 as the right subtree.

//

// VALUE SEMANTICS for the ExpressionTree class:

// Assignments and the copy constructor may be used with ExpressionTree

// objects.

#ifndef EXPRESS_H

#define EXPRESS_H

#include <cstdlib> // Provides NULL

class ExpressionTree

{

public:

// CONSTRUCTORS and DESTRUCTOR

ExpressionTree(double value = 0.0);

ExpressionTree(const ExpressionTree& source);

~ExpressionTree( );

// ASSIGNMENT OPERATOR

ExpressionTree& operator =(const ExpressionTree& source);

// CONSTANT MEMBER FUNCTIONS

double evaluate( ) const;

void printPre() const;

void printPost() const;

void printIn() const;

// NONMEMBER FUNCTIONS FOR BUILDING BIGGER EXPRESSION TREES:

friend

ExpressionTree operator +(const ExpressionTree& e1, const ExpressionTree& e2);

friend

ExpressionTree operator *(const ExpressionTree& e1, const ExpressionTree& e2);

  

private:

double data; // Used for leaf nodes only.

char operation; // Used for non-leaf nodes only.

ExpressionTree *left; // Will be NULL for a leaf.

ExpressionTree *right; // Will be NULL for a leaf.

};

#endif

1. Implement class Expression Tree as it is defined in express.h. Your implementation must be in a new file that you create named expr Cpp This is a variation of Programming Project 1, pp. 534-535 of the textbook. Read the instructions (and o hints) presented there. This variation adds three print methods to the problem presented in the text o Implement each of the member functions (including both constructors, the destructor and the assignment operator) of class ExpressionTest. o Be sure to Hinclude \"express.h\" and remember to type your name(s) and the date in the comment at the start. o The print functions must show the expression parts (numbers and operators and parentheses) separated by one space from each other, and there must be one blank space at the end of each printed expression. Print to cout. o Function printPre must print the expression in prefix notation, and printPost must print it in postfix notation. Function println must print parentheses surrounding each addition operation, but multiplication operations must not be parenthesized, and of course this function must print the expression in infix notation

Solution

Answer:

//express.h

#ifndef EXPRESS_H

#define EXPRESS_H

#include <cstdlib> // Provides NULL

using namespace std;

class ExpressionTree

{

public:

    // CONSTRUCTORS and DESTRUCTOR

    ExpressionTree(double value = 0.0);

    ExpressionTree(const ExpressionTree& source);

    ~ExpressionTree( );

    // ASSIGNMENT OPERATOR

    ExpressionTree& operator =(const ExpressionTree& source);

    // CONSTANT MEMBER FUNCTIONS

    double evaluate( ) const;

    void printPre() const;

    void printPost() const;

    void printIn() const;

// NONMEMBER FUNCTIONS FOR BUILDING BIGGER EXPRESSION TREES:

    friend

ExpressionTree operator +(const ExpressionTree& e1, const ExpressionTree& e2);

    friend

ExpressionTree operator *(const ExpressionTree& e1, const ExpressionTree& e2);

   

private:

    double data;            // Used for leaf nodes only.

    char operation;         // Used for non-leaf nodes only.

    ExpressionTree *left;   // Will be NULL for a leaf.

   ExpressionTree *right; // Will be NULL for a leaf.

};

#endif

//express.cpp

#include<iostream>

#include<string>

#include \"express.h\"

using namespace std;

//default-constructor

ExpressionTree::ExpressionTree(double value)

{

     //data

    data = value;

     //set operation

    operation = \'\\0\';

     //set right and left expression-tree as NULL

    left = NULL;

    right = NULL;

}

//copy-constructor

ExpressionTree::ExpressionTree(const ExpressionTree& source)

{

     //Check left and right expression tree doesn\'t exist

    if ((source.left == NULL) && (source.right == NULL))

    {

          //get data

        data = source.data;

          //set operation as NULL

        operation = \'\\0\';

          //set left and right expression tree as NULL

        left = NULL;

        right = NULL;

    }

     //if left and right expression tree exist

     else

    {

          //get data

        data = source.data;

          //set operation

        operation = source.operation;

          //set left expression tree

        left = new ExpressionTree(*(source.left));

          //set right expression tree

        right = new ExpressionTree(*(source.right));

    }

}

//destructor

ExpressionTree::~ExpressionTree()

{

     //delete left expression-tree

    delete left;

     //delete right expression-tree

    delete right;

}

//overloading = operator

ExpressionTree& ExpressionTree::operator =(const ExpressionTree& source)

{

     //Check left and right expression tree doesn\'t exist

    if ((source.left == NULL) && (source.right == NULL))

    {

          //get data

        data = source.data;

          //set operation as NULL

        operation = \'\\0\';

          //set left and right expression tree as NULL

        left = NULL;

        right = NULL;

    }

     //if left and right expression tree exist

     else

    {

          //get data

        data = source.data;

          //set operation

        operation = source.operation;

          //set left and right expression tree

        left = new ExpressionTree(*(source.left));

        right = new ExpressionTree(*(source.right));

    }

}

//recursive-function to evaluate an expression

double ExpressionTree::evaluate() const

{

    //check right and left expression-tree doesnt exist

    if ((left == NULL) && (right == NULL))

          //return data

        return data;

    else

    {

          //get left expression

        ExpressionTree* lft = left;

          //get right expression

        ExpressionTree* rgt = right;

          //for addition operation

        if (operation == \'+\')

return (lft->evaluate()) + (rgt-> evaluate());

          //for multiplication opeartion

        else

return (lft->evaluate()) * (rgt-> evaluate());

    }

}

//print expression in prefix-order

void ExpressionTree::printPre() const

{

     //check right and left expression-tree doesnt exist

    if ((left == NULL) && (right == NULL))

          //return data

        cout<<data<<\" \";

     //if child expression-trees are available

    else

    {

          //for addition

        if (operation == \'+\')

        {

              //addition symbol

            cout<<\"+ \";

              //traverse left expression-tree

            left->printPre();

              //traverse right expression-tree

            right->printPre();

        }

          //for multiplication

        else if (operation == \'*\')

        {

              //multiplication symbol

            cout<<\"*\";

            cout<<\" \";

              //traverse left expression-tree

            left->printPre();

              //traverse right expression-tree

            right->printPre();

        }

    }

}

//print expression in postfix-order

void ExpressionTree::printPost() const

{

    //check right and left expression-tree doesnt exist

    if ((left == NULL) && (right == NULL))

          //return data

        cout<<data<<\" \";

     //if child expression-trees are available

    else

    {

        //for addition

        if (operation == \'+\')

        {            

              //traverse left expression-tree

            left->printPost();

              //traverse right expression-tree

            right->printPost();

              //addition symbol

            cout<<\"+ \";

        }

          //for multiplication

        else if (operation == \'*\')

        {            

              //traverse left expression-tree

            left->printPost();

              //traverse right expression-tree

            right->printPost();

              //multiplication symbol

            cout<<\"*\";

            cout<<\" \";

        }

    }

}

//print expression in infix-order

void ExpressionTree::printIn() const

{

    //check right and left expression-tree doesnt exist

    if ((left == NULL) && (right == NULL))

          //return data

        cout<<data<<\" \";

     //if child expression-trees are available

    else

    {

        //for addition

        if (operation == \'+\')

        {

            cout<<\"( \";

              //traverse left expression-tree

            left->printIn();

            cout<<\"+ \";

              //traverse right expression-tree

            right->printIn();

            cout<<\") \";

        }

          //for multiplication

        else if (operation == \'*\')

        {

              //traverse left expression-tree

            left->printIn();

            cout<<\"* \";

              //traverse right expression-tree

              right->printIn();

        }

    }

}

//overloaded + operator

ExpressionTree operator +(const ExpressionTree& e1, const ExpressionTree& e2)

{

    //create new expression-trees

    ExpressionTree* lftSubtr = new ExpressionTree(e1);

    ExpressionTree* rghtSubtr = new ExpressionTree(e2);

    ExpressionTree* lgrTr = new ExpressionTree;

    //head expression

    lgrTr->operation = \'+\';

    lgrTr->left = lftSubtr;

    lgrTr->right = rghtSubtr;

    return *lgrTr;

}

//overloaded * operator

ExpressionTree operator *(const ExpressionTree& e1, const ExpressionTree& e2)

{

    //create new expression-trees

    ExpressionTree* lftSubtr = new ExpressionTree(e1);

    ExpressionTree* rghtSubtr = new ExpressionTree(e2);

    ExpressionTree* lgrTr = new ExpressionTree;

    //head expression

    lgrTr->operation = \'*\';

    lgrTr->left = lftSubtr;

    lgrTr->right = rghtSubtr;

    return *lgrTr;

}

//main.cpp

#include<iostream>

#include<string>

#include \"express.h\"

using namespace std;

//main

int main()

{

   

    double xx, yy;

    cout << \"enter two double values, xx then yy\ \";

    cin >> xx >> yy;

    ExpressionTree ee1(xx), ee2(yy), product = ee1 * ee2;

     //postorder

     cout<<\"Post Order:\"<<endl;

     product.printPost();

     //preorder

     cout<<\"\ Pre Order:\"<<endl;

     product.printPre();

     //Inorder

     cout<<\"\ In Order:\"<<endl;

     product.printIn();   

    return 0;

}

Sample Output:

enter two double values, xx then yy

10

20

Post Order:

10 20 *

Pre Order:

* 10 20

In Order:

10 * 20  

// FILE: express.h // CLASS PROVIDED: ExpressionTree // This class provides only NONEMPTY expression trees // from Project 1 of Chapter 10 in first edition of /
// FILE: express.h // CLASS PROVIDED: ExpressionTree // This class provides only NONEMPTY expression trees // from Project 1 of Chapter 10 in first edition of /
// FILE: express.h // CLASS PROVIDED: ExpressionTree // This class provides only NONEMPTY expression trees // from Project 1 of Chapter 10 in first edition of /
// FILE: express.h // CLASS PROVIDED: ExpressionTree // This class provides only NONEMPTY expression trees // from Project 1 of Chapter 10 in first edition of /
// FILE: express.h // CLASS PROVIDED: ExpressionTree // This class provides only NONEMPTY expression trees // from Project 1 of Chapter 10 in first edition of /
// FILE: express.h // CLASS PROVIDED: ExpressionTree // This class provides only NONEMPTY expression trees // from Project 1 of Chapter 10 in first edition of /
// FILE: express.h // CLASS PROVIDED: ExpressionTree // This class provides only NONEMPTY expression trees // from Project 1 of Chapter 10 in first edition of /
// FILE: express.h // CLASS PROVIDED: ExpressionTree // This class provides only NONEMPTY expression trees // from Project 1 of Chapter 10 in first edition of /
// FILE: express.h // CLASS PROVIDED: ExpressionTree // This class provides only NONEMPTY expression trees // from Project 1 of Chapter 10 in first edition of /
// FILE: express.h // CLASS PROVIDED: ExpressionTree // This class provides only NONEMPTY expression trees // from Project 1 of Chapter 10 in first edition of /
// FILE: express.h // CLASS PROVIDED: ExpressionTree // This class provides only NONEMPTY expression trees // from Project 1 of Chapter 10 in first edition of /
// FILE: express.h // CLASS PROVIDED: ExpressionTree // This class provides only NONEMPTY expression trees // from Project 1 of Chapter 10 in first edition of /

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site