Examples Postfix Infix Result 2121 545-4 7 2*7 * 2 93/9/3 25 7 "(2+5) * 7 49 3

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Part 1: Stack

As mentioned in class, Python 3 provides the LifoQueue class as part of the Queue library.  To see how this is really just a wrapper around the basic list class below is our own Stack class that uses a list.

Copy this code into file lab5.py

You MAY NOT change the below code anyway.

class Stack:

    def __init__(self):

        self.__S = []

    def __str__(self):

        return str(self.__S)

    def push(self,x):

        self.__S.append(x)

    def pop(self):

        return self.__S.pop()

    def top(self):

        return self.__S[-1]

Part 2: Postfix Math

You are probably most familiar with math written in infix notation. For example, "4 + 3 - 7". In infix notation, operators are placed between their inputs. This is a very nice way to read math, but it is nontrivial for a computer to parse.

An alternative representation is called postfix notation. In postfix, we write the two inputs followed by the operator. The previous example would be "4 3 + 7 -". This is much easier to parse because we have both inputs before the operator. We also don't have to worry about parenthesis.

Postfix notation works well with a Stack.

  1. Push Numbers onto Stack as read from input.
  2. When operator seen:
    1. Pop top 2 items on stack.
    2. Complete Operation.
    3. Push result onto Stack.
  3. Repeat until end of input.
  4. Final Result will be on top of Stack.

 

Implement a function postfix(exp) that takes a string exp containing a postfix math expression as input and returns the result as a float.

You MUST use the provided Stack class to implement this function.

Examples
Postfix Infix Result
2121
545-4
7 2*7 * 2
93/9/3
25 7 "(2+5) * 7 49
3
Transcribed Image Text:Examples Postfix Infix Result 2121 545-4 7 2*7 * 2 93/9/3 25 7 "(2+5) * 7 49 3
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Lists
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education