**TStack.py below** # CMPT 145: Linear ADTs # Defines the Stack ADT # # A stack (also called a pushdown or LIFO stack) is a compound # data structure in which the data values are ordered according # to the LIFO (last-in first-out) protocol. # # Implementation: # This implementation was designed to point out when ADT operations are # used incorrectly. def create(): """ Purpose creates an empty stack Return an empty stack """ return '__Stack__',list() def is_empty(stack): """ Purpose checks if the given stack has no data in it Pre-conditions: stack is a stack created by create() Return: True if the stack has no data, or false otherwise """ if type(stack) is tuple: t,s = stack assert t == '__Stack__', 'Type Error : Expected __Stack__ but received '+t return len(s) == 0 else: assert False, 'Type Error: Expected __Stack__ but received '+str(type(stack)) def size(stack): """ Purpose returns the number of data values in the given stack Pre-conditions: stack: a stack created by create() Return: The number of data values in the queue """ if type(stack) is tuple: t,s = stack assert t == '__Stack__', 'Type Error: Expected __Stack__ but received '+t return len(s) else: assert False, 'Type Error: Expected __Stack__ but received '+str(type(stack)) def push(stack, value): """ Purpose adds the given data value to the given stack Pre-conditions: queue: a stack created by create() value: data to be added Post-condition: the value is added to the stack Return: (none) """ if type(stack) is tuple: t,s = stack assert t == '__Stack__', 'Type Error: Expected __Stack__ but received '+t s.append(value) else: assert False, 'Type Error: Expected __Stack__ but received '+str(type(stack)) def pop(stack): """ Purpose removes and returns a data value from the given stack Pre-conditions: stack: a stack created by create() Post-condition: the top value is removed from the stack Return: returns the value at the top of the stack """ if type(stack) is tuple: t,s = stack assert t == '__Stack__', 'Type Error: Expected __Stack__ but received '+t return s.pop() else: assert False, 'Type Error: Expected __Stack__ but received '+str(type(stack)) def peek(stack): """ Purpose returns the value from the front of given stack without removing it Pre-conditions: stack: a stack created by create() Post-condition: None Return: the value at the front of the stack """ if type(stack) is tuple: t,s = stack assert t == '__Stack__', 'Type Error: Expected __Stack__ but received '+t return s[-1] else: assert False, 'Type Error: Expected __Stack__ but received '+str(type(stack))
NOTE: Since you have not provided isfloat() function, I have used my own isfloat() which is compatible with the code.
CODE:
# CMPT 145: Linear ADTs # Defines the Stack ADT # # A stack (also called a pushdown or LIFO stack) is a compound # data structure in which the data values are ordered according # to the LIFO (last-in first-out) protocol. # # Implementation: # This implementation was designed to point out when ADT operations are # used incorrectly. def create(): """ Purpose creates an empty stack Return an empty stack """ return '__Stack__',list() def is_empty(stack): """ Purpose checks if the given stack has no data in it Pre-conditions: stack is a stack created by create() Return: True if the stack has no data, or false otherwise """ if type(stack) is tuple: t,s = stack assert t == '__Stack__', 'Type Error : Expected __Stack__ but received '+t return len(s) == 0 else: assert False, 'Type Error: Expected __Stack__ but received '+str(type(stack)) def size(stack): """ Purpose returns the number of data values in the given stack Pre-conditions: stack: a stack created by create() Return: The number of data values in the queue """ if type(stack) is tuple: t,s = stack assert t == '__Stack__', 'Type Error: Expected __Stack__ but received '+t return len(s) else: assert False, 'Type Error: Expected __Stack__ but received '+str(type(stack)) def push(stack, value): """ Purpose adds the given data value to the given stack Pre-conditions: queue: a stack created by create() value: data to be added Post-condition: the value is added to the stack Return: (none) """ if type(stack) is tuple: t,s = stack assert t == '__Stack__', 'Type Error: Expected __Stack__ but received '+t s.append(value) else: assert False, 'Type Error: Expected __Stack__ but received '+str(type(stack)) def pop(stack): """ Purpose removes and returns a data value from the given stack Pre-conditions: stack: a stack created by create() Post-condition: the top value is removed from the stack Return: returns the value at the top of the stack """ if type(stack) is tuple: t,s = stack assert t == '__Stack__', 'Type Error: Expected __Stack__ but received '+t return s.pop() else: assert False, 'Type Error: Expected __Stack__ but received '+str(type(stack)) def peek(stack): """ Purpose returns the value from the front of given stack without removing it Pre-conditions: stack: a stack created by create() Post-condition: None Return: the value at the front of the stack """ if type(stack) is tuple: t,s = stack assert t == '__Stack__', 'Type Error: Expected __Stack__ but received '+t return s[-1] else: assert False, 'Type Error: Expected __Stack__ but received '+str(type(stack)) def isfloat(val): try: v = float(val) return True except: return False def evaluate(exp): # Create empty stacks value_stack = create() operator_stack = create() lst = exp.split() # Making list of characters in the expression for char in lst: if isfloat(char): val = float(char) push(value_stack,val) elif char == '+' or char == '-' or char == '*' or char == '/': push(operator_stack,char) elif char == ')': val1 = pop(value_stack) val2 = pop(value_stack) operator = pop(operator_stack) if operator == '+': result = val1+val2 elif operator == '-': val1,val2 = val2,val1 result = val1-val2 elif operator == '*': result = val1*val2 elif operator == '/': val1,val2 = val2,val1 result = val1/val2 else: print("Unknown operator") break push(value_stack,result) else: pass return(pop(value_stack)) def test(): print("--------TESTING INDIVIDUAL OPERATORS----------") print('"( 4 + 5 )" =',evaluate("( 4 + 5 )")) print('"( 4 - 5 )" =',evaluate("( 4 - 5 )")) print('"( 4 * 5 )" =',evaluate("( 4 * 5 )")) print('"( 4 / 5 )" =',evaluate("( 4 / 5 )")) assert evaluate("( 4 + 5 )") == 9 assert evaluate("( 4 - 5 )") == -1 assert evaluate("( 4 * 5 )") == 20 assert evaluate("( 4 / 5 )") == 0.8 print("--------OPERATORS WORK CORRECTLY----------") print() print("--------TESTING EXPRESSIONS------------") print('"( ( 11 / 12 ) * 13 ) " = ',evaluate("( ( 11 / 12 ) * 13 ) ")) print('"( ( 11 + 12 ) - 13 ) " = ',evaluate("( ( 11 + 12 ) - 13 ) ")) print('"( ( 11 + 12 ) * 13 ) " = ',evaluate("( ( 11 + 12 ) * 13 ) ")) assert evaluate("( ( 11 / 12 ) * 13 ) ") == 11.916666666666666 assert evaluate("( ( 11 + 12 ) - 13 ) ") == 10.0 assert evaluate("( ( 11 + 12 ) * 13 ) ") == 299.0 print("--------EXPRESSIONS WORK CORRECTLY---------") test()
OUTPUT:
**TStack.py below** # CMPT 145: Linear ADTs # Defines the Stack ADT # # A stack (also called a pushdown or LIFO stack)...
In c++ Section 1. Stack ADT – Overview Data Items The data items in a stack are of generic DataType. This means use should use templating and your Node class. Structure The stack data items are linearly ordered from the most recently added (the top) to the least recently added (the bottom). This is a LIFO scheme. Data items are inserted onto (pushed) and removed from (popped) the top of the stack. Operations Constructor. Creates an empty stack. Copy constructor....
By using PYTHON language Postfix to Infix using Stack Develop a stack application that can convert Postfix notation to Infix notation using the following algorithm. In your stack application, you can use only two stacks, one for a stack that can store Postfix notation, and the other is a stack to store infix notation. Also, it would help if you had a function to distinguish between an operation or an operand. Input A B C * + D E /...
Suppose we decide to add a new operation to our Stack ADT called sizeIs, which returns a value of primitive type int equal to the number of items on the stack. The method signature for sizeIS is public int sizeIs() a.) Write the code for sizeIs for the ArrayStack class b.) Write the code for sizeIs for the LinkedStack class (do not add any instance variables to the class; each time sizeIs is called you must "walk" through the stack...
Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...
Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...
We as humans write math expression in infix notation, e.g. 5 + 2 (the operators are written in-between the operands). In a computer’s language, however, it is preferred to have the operators on the right side of the operands, i.e. 5 2 +. For more complex expressions that include parenthesis and multiple operators, a compiler has to convert the expression into postfix first and then evaluate the resulting postfix. Write a program that takes an “infix” expression as input, uses...
Dynamic Implementation of Stack - The purpose is to use our dynamic implementation of stack. The application will be to add large numbers. Review adding large numbers Remember that we can use stacks to safely add integer values that overflow the int data type g. in Java, the maximum possible int value Integer.MAX_VALUE is: 2147483647 so any int addition larger than this will overflow and fail Using stacks to add large numbers safely Will actually represent the large integers to...
Stacks are used by compilers to help in the process of evaluating expressions and generating machine language code.In this exercise, we investigate how compilers evaluate arithmetic expressions consisting only of constants, operators and parentheses. Humans generally write expressions like 3 + 4and 7 / 9in 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...
EVALUATING GENERAL INFIX EXPRESSIONS INTRODUCTION The notation in which we usually write arithmetic expressions is called infix notation; in it, operators are written between their operands: X + Y. Such expressions can be ambiguous; do we add or multiply first in the expression 5 + 3 * 2? Parentheses and rules of precedence and association clarify such ambiguities: multiplication and division take precedence over addition and subtraction, and operators associate from left to right. This project implements and exercises a stack-based algorithm that evaluates...
Total point: 15 Introduction: For this assignment you have to write a c program that will take an infix expression as input and display the postfix expression of the input. After converting the postfix expression, the program should evaluate the expression from the postfix and display the result. What should you submit? Write all the code in a single file and upload the .c file. Problem We as humans write math expression in infix notation, e.g. 5 + 2 (the...