Write the infix-to-postfix conversion algorithm

Assignment Help Data Structure & Algorithms
Reference no: EM132333617

Assignment -

Key Concepts - In this assignment, you will gain experience implementing and using a linked list and stack data structure. It should also give you a better understanding of dynamic allocation, templates, and recursion.

Problem Description - The following is a modified version of a problem taken from the text, C++ How to Program by Dietel and Dietel.

Stacks are used by compilers to help in the process of evaluating expressions and generating machine language code. We will investigate how compilers evaluate arithmetic expressions consisting only of constants, operators, and parentheses.

Humans generally write expressions like 3 + 4 and 7 / 9 in which the operator (+ or / here) is written between its operands - this is called infix notation. Computers "prefer" postfix notation in which the operator is written to the right of its two operands. The preceding infix expressions would appear in postfix notation as 3 4 + and 7 9 /, respectively.

To evaluate a complex infix expression, a compiler would first convert the expression to postfix notation, and then evaluate the postfix version of the expression. Each of these algorithms requires only a single left-to-right pass of the expression. Each algorithm uses a stack object in support of its operation.

You will write a C++ version of the infix-to-postfix conversion algorithm. You will discover that code you write to do this exercise can help you implement a complete working compiler.

The program you will write should convert an ordinary infix arithmetic expression read from a text file (assume a valid expression is entered) with single digit integers such as (6 + 2) * 5 - 8 / 4 to a postfix expression. The postfix version of the preceding infix expression is 6 2 + 5 * 8 4 / -

The program will then further evaluate the postfix expression into an integer.

In order for this problem to be solved, we will need to utilize a stack that should be maintained with stack nodes that each contain a data member and a pointer to the next stack node. Furthermore, you will implement a template class for linked lists. Essentially, you must use the code provided to you in the course slides and add new functions to the existing List class as described below. Therefore, your List must be a class template that is implemented using pointers. If you use another approach other than what I have stated, you will receive a failing grade even if your code manages to implement the required functionality.

A sample test file, testlist[1].cpp is provided. A sample output file, program1_output.txt is also provided. You can use this sample test file to make sure you define your functions properly. However, you should add other tests to ensure that your program is working correctly.

You can create your own test file for the Stack classes that you will build.

Requirements - In order to not get overwhelmed, please do the work in the order listed in this document.

Part 1 - Enhance the List template class discussed in the course by adding the functions listed below. Note that in no instance must any of the functions swap the data contents of the nodes - the node pointers themselves must be changed when swapping elements. Please use the exact names that have been listed below in order for my test program to work. Use the testfile to determine the return type and parameter list for each function based on it is used.

  • Write a function, called add_n(int n, ListNode<NODETYPE> &value), that adds an element after the nth element of a list.
  • Write a function, called concat(List<NODETYPE> &list2), that concatenates two linked lists. (Connects the two linked lists so the second follows the first.) Once this function is executed, list2 should end up being empty because the calling List object should take its nodes as part of its own list.
  • Write a function, called reverse(), that reverses a list, so the last node becomes the first, etc.
  • Write a function, called remove_n(int n), that removes the nth element of a list.
  • Write a function, called sort(), that sorts a list in increasing order.
  • Write a function, called merge(List<NODETYPE> &list2), that merges one sorted list into another sorted list to create a single list.
  • Write a function, called add_in(ListNode<NODETYPE> &value), that inserts an element in the correct position in a sorted linked list. This assumes the list IS SORTED already.
  • Write a function, called remove(ListNode<NODETYPE> &value), that removes an element with the given value.
  • Write a function, called count(), that returns the number of elements in the list.
  • Overload the assignment operator= and the output operator<< to allow one List to be assigned to another List and to print a List using cout respectively.

Part 2 - You have been provided with Stack.h, StackNode.h and Stack2.h. You must complete all missing code from those classes in order to use class Stack2 which inherits from Stack. Again, feel free to use your own driver program to test that your stack classes work properly before moving on to the next step.

Part 3 - Once you have completed this step, then you must implement all the missing functions in class MathExpression which has also been provided to you and which uses both Stack.h and Stack2.

A MathExpression object is comprised of three data members, a char array representing an infix expression, a char array representing the same infix expression but in postfix notation, and finally an int that represents the postfix expression after it has been evaluated.

The functions that require further explanation are described in attached file.

Part 4 - After you have completed the MathExpression class, you must test it using the driver program provided to you. The driver program is very simple and only attempts to test some of the more vital functions of the class. At this point, you should have used the driver program for the List, Stack and MathExpression classes in order to test each of those classes independently.

Part 5 - You must now combine all of these classes into one project and create a driver program to achieve the intended goal of the assignment. The driver program must do the following:

Create a List of type MathExpression. You will then open a file called infixData.dat and read its data. The file is comprised of an arbitrary number of infix expressions, one per line. You must read each line using the List's overloaded fstream operator>>. As you read each line of the file, you must create a MathExpression object by passing the infix expression read into its constructor. The constructor for MathExpression should automatically call the functions that will then initialize its other two data members, postfixExpression_ and value_ respectively using the functions it has. When you are done, you should have a List of MathExpression objects, each composed of an infix expression, a postfix expression that is based upon the infix expression and finally, an int which is the evaluated value of the postfix expression. You are to then print these values to the screen using the overloaded output operator <<.

Attachment:- External Files & Classes Assignment Files.rar

Reference no: EM132333617

Questions Cloud

Community-based corrections presentation : Create a Microsoft PowerPoint presentation of at least 10 slides, List and briefly discuss a minimum of two to three future trends.
Victimization and delinquency essay : Which you discuss whether you agree or disagree that juvenile delinquent behavior is due to some young people being victimized by factors such as poverty,
How they relate to adaptations to water stress : Describe your questions, hypotheses, and rationale and how they relate to adaptations to water stress.
Develop real-world professional and academic skills : HPS111 - Psychology A: Fundamentals of Human Behaviour - Deakin University - Conduct a literature search to help identify empirical studies to support
Write the infix-to-postfix conversion algorithm : You will write a C++ version of the infix-to-postfix conversion algorithm. You will discover that code you write to do this exercise
Explain the features of effective team performance : LM1c-H/602/3171-Lead and Manage a Team within a Health and Social Care or Children and Young People’s Setting-Pearson Edexcel Level 5 Diplomas.
Research interspecific and intraspecific interactions : Research interspecific and intraspecific interactions using the module readings, the Argosy University online library resources, and the Internet.
How the frameworks are used to improve the life chances : P4-A/602/3175-Lead and Manage Group Living for Children-Pearson Edexcel Level 5 Diplomas in Leadership for Health and Social Care and Children and Young People.
Write a program that find the shortest ladder : You are to write a program named ladderq.cpp that uses a file of 5-letter words to find the shortest ladder from one word to another

Reviews

Write a Review

Data Structure & Algorithms Questions & Answers

  Implement an open hash table

In this programming assignment you will implement an open hash table and compare the performance of four hash functions using various prime table sizes.

  Use a search tree to find the solution

Explain how will use a search tree to find the solution.

  How to access virtualised applications through unicore

How to access virtualised applications through UNICORE

  Recursive tree algorithms

Write a recursive function to determine if a binary tree is a binary search tree.

  Determine the mean salary as well as the number of salaries

Determine the mean salary as well as the number of salaries.

  Currency conversion development

Currency Conversion Development

  Cloud computing assignment

WSDL service that receives a request for a stock market quote and returns the quote

  Design a gui and implement tic tac toe game in java

Design a GUI and implement Tic Tac Toe game in java

  Recursive implementation of euclids algorithm

Write a recursive implementation of Euclid's algorithm for finding the greatest common divisor (GCD) of two integers

  Data structures for a single algorithm

Data structures for a single algorithm

  Write the selection sort algorithm

Write the selection sort algorithm

  Design of sample and hold amplifiers for 100 msps by using n

The report is divided into four main parts. The introduction about sample, hold amplifier and design, bootstrap switch design followed by simulation 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