Evaluate the expression and print the result

Assignment Help Computer Engineering
Reference no: EM132212152

Write in PYTHON

For this assignment you will read a file expression.txt and create an expression tree. The expression will be a valid infix expression with the all the necessary parentheses so that there is no ambiguity in the order of the expression.

You will evaluate the expression and print the result.

You will also write the prefix and postfix versions of the same expression without any parentheses.

In an expression tree the nodes are either operators or operands. The operators will be in the set ['+', '-', '*', '/']. The operands will be either integers or floating point numbers. All the operand nodes will be leaves of the expression tree. All the operator nodes will have exactly two children.

The outline of your program will be as follows:

class Stack (object): class Node (object): class Tree (object): def __init__ (self): def createTree (self, expr): def evaluate (self, aNode): def preOrder (self, aNode): def postOrder (self, aNode): def main(): main()

The function createTree() will take as input parameter an infix expression with parentheses as a String and create an Expression Tree from it. Assume that the expression string is valid.

You will take the expression string and break it into tokens. There are four different kinds of tokens - left parenthesis, right parenthesis, operator, and operand. When we read a left parenthesis we are starting a new expression and when we read a right parenthesis we are ending an expression. Here is the algorithm that you will use:

If the current token is a left parenthesis add a new node as the left child of the current node. Push current node on the stack and make current node equal to the left child.

If the current token is an operator set the current node's data value to the operator. Push current node on the stack. Add a new node as the right child of the current node and make the current node equal to the right child.

If the current token is an operand, set the current node's data value to the operand and make the current node equal to the parent by popping the stack.

If the current token is a right parenthesis make the current node equal to the parent node by popping the stack if it is not empty.

For the input expression, this is what your program will output:

( ( 8 + 3 ) * ( 7 - 2 ) ) = 55 Prefix Expression: * + 8 3 - 7 2 Postfix Expression: 8 3 + 7 2 - *

Reference no: EM132212152

Questions Cloud

Calculate the amount of money a person would earn : Design a program that calculates the amount of money a person would earn over a period of time if his or her salary is one peeny the first day.
What are the detailed characteristics of the data : What are the detailed characteristics of the data (use summary statistics to explain)? How can MapReduce be used to solve the problem
Write a python 3 program-containing 2 functions : Create a FUNCTION called createMenu that creates a STRING which will produce the following output when printed by the main program.
Define mechanisms were in-place to protect against intrusion : Search "scholar.google.com" for a company, school, or person that has been the target of a network or system intrusion? What information was targeted?
Evaluate the expression and print the result : For this assignment you will read a file expression.txt and create an expression tree. Evaluate the expression and print the result.
Implement euclids extended algorithm : Write PYTHON code which implements Euclid's Extended Algorithm. A sample run of the program would look.
Describe your first experience with a computer : Please describe your first experience with a computer and with use of the internet. Minimum 250 words. The response must be typed, single spaced.
What are the steps involved in designing product : What the different manufacturing process types and their applications in business organisations. What are the steps involved in designing a product
Briefly explain the provider primary cloud function : Briefly explain the provider's primary cloud function-what advantages might the organization have over another organization that provides the same product.

Reviews

Write a Review

Computer Engineering Questions & Answers

  What value do you see in data mining in the context of this

Explain how this particular company could benefit from a data warehousing and data mining initiative.

  Write a program that uses a recursive method to check

A palindome is a string that reads the same both forward and backward. For example,the string "madam" is a palindrome.

  What is the syntax of the if statement

Find BNF OR EBNF grammars for Ada(available on the web). What is the syntax of the IF statement? Write a legal Ada if statement that look at the integer variables a and b.

  Devise an algorithm for viewing a single sphere

Devise an algorithm for viewing a single sphere using the ray-casting method. Write routine to produce surface contour plot for given surface function f(x, y).

  Discuss the challenges of maintaining information security

Discuss the challenges of maintaining information security at a remote recovery location. I recommend your initial posting to be between 200-to-300 words.

  Googlescholar only cloud solutions

Cloud-based computing allows businesses to store and access large amounts of data over the Internet rather than on in-house computer hard drives. There are several cloud-based data solutions currently available in the marketplace.

  Paste image of linear programming results window

A linear programming problem cost minimization problem has objective function: Minimize X +Y. Use QM for Windows to plot the feasible region. Paste image of Linear Programming Results window and Solution List window here

  Explain the term multiprogramming

Explain the term multiprogramming. The following terms relate to how the operating system manages multiprogramming.

  Explain basics of the rfid components that were chosen

Explain Basics of the RFID components that were chosen. Security concerns or practices associated with the RFID system.

  Describe the emerging trends of near field communication

describe the emerging trends of near field communication bluetooth wifi smartphones and rfid as they relate to the

  Why the computer storage is getting bigger

as it is true that computer storage is getting bigger and faster, how does that help us? As we start saving more and more information, it is really possible for the average person to keep all of the information organized? For large companies sure,..

  What other defenses could french have deployed to protect

What other defenses could the French have deployed to protect their entire border? What other principles did the French violate that led to such catastrophic results

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd