Question

In: Computer Science

Create: post fix calculator By Using: stack concept LinkedList is original line Queue is the waitlist...

Create:

post fix calculator

By Using:

stack concept

LinkedList is original line

Queue is the waitlist

Requirements - Orchestra passes are on sale. Enter first names of the people to form the line. Assume there is certain criteria that will have to be checked before purchase of passes (must be an annual ticket holder to purchase pass). You will have to eliminate those not meeting it from the line. A lottery will be done and the outcome will insert that person at the front of the line. Dispense passes to the first 15 people in line (there are total of 20 people total in line). The others will be put into waitlist queue for the next available pass.

Solutions

Expert Solution

/*Program For PostFix PostFixCalculator*/

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class PostFixCalculator {

private static final String ADD = "+";
private static final String SUB = "-";
private static final String MUL = "*";
private static final String DIV = "/";

public void calculateFile(String fileName) throws IOException {
BufferedReader br = null;
StringBuilder sb = null;
try {
FileReader fileReader = new FileReader(fileName);
br = new BufferedReader(fileReader);

sb = new StringBuilder();
String line = br.readLine();

while (line != null) {
sb.append(line);
line = br.readLine();
}

String input = sb.toString();
System.out.println(input + " = " + calculate(input));
} catch (IOException e) {
e.printStackTrace();
} finally {
br.close();
}
}

private int calculate(String input) {
SinglyLinkedListStack<Integer> stack = new SinglyLinkedListStack<>();

String[] inputs = input.split(" ");

return handleCalculation(stack, inputs);
}

private static int handleCalculation(SinglyLinkedListStack<Integer> stack, String[] el) {
int operand1, operand2;

for(int i = 0; i < el.length; i++) {
if( el[i].equals(ADD) || el[i].equals(SUB) || el[i].equals(MUL) || el[i].equals(DIV) ) {
operand2 = stack.pop();
operand1 = stack.pop();
switch(el[i]) {
case ADD: {
int local = operand1 + operand2;
stack.push(local);
break;
}

case SUB: {
int local = operand1 - operand2;
stack.push(local);
break;
}

case MUL: {
int local = operand1 * operand2;
stack.push(local);
break;
}

case DIV: {
int local = operand1 / operand2;
stack.push(local);
break;
}
}
} else {
stack.push(Integer.parseInt(el[i]));
}
}

return stack.pop();
  
}
}


/* Another CLASS 2 */
/*SinglyLinkedListStack*/
public class SinglyLinkedListStack<T> {

private int size;
private Node<T> head;

public SinglyLinkedListStack() {
head = null;
size = 0;
}

public void push(T element) {
if(head == null) {
head = new Node(element);
} else {
Node<T> newNode = new Node(element);
newNode.next = head;
head = newNode;
}

size++;
}

public T pop() {
if(head == null)
return null;
else {
T topData = head.data;

head = head.next;
size--;

return topData;
}
}

public T top() {
if(head != null)
return head.data;
else
return null;
}

public int size() {
return size;
}

public boolean isEmpty() {
return size == 0;
}

private class Node<T> {
private T data;
private Node<T> next;

public Node(T data) {
this.data = data;
}

}

}

/*MAIN CLASS*/

import java.io.IOException;

public class PostFixCalculatorDemo {
public static void main(String[] args) throws IOException {
PostFixCalculator calc = new PostFixCalculator();
calc.calculateFile("postfix.txt");
}
}


Related Solutions

Implement in Python using stack operations. Postfix Calculator Post fix calculator • We use a stack...
Implement in Python using stack operations. Postfix Calculator Post fix calculator • We use a stack • When an operand is read, push it on statck • When an operator is read i.e +, *. /, - – Pop two from the top of the stack and apply the operator and push the result on stack if there is one value instead of two display an error message • Keep repeating until an equal sign, = is read; pop from...
In Java or C++, implement a stack and a queue using a linkedlist data structure.  You may...
In Java or C++, implement a stack and a queue using a linkedlist data structure.  You may not use any standard Java or C++ libraries. Assume your data structure only allows Strings. Implement the following operations for the data structure: Queue: enqueue, dequeue, create, isEmpty (10 points) Stack: push, pop, create, isEmpty (10 points) Here is a link to get started on transferring from Java to C++ http://www.horstmann.com/ccj2/ccjapp3.html (Links to an external site.) Upload a zip file with one implementation for...
PYTHON- create a queue class that uses a LINKEDLIST in order to store data. this queue...
PYTHON- create a queue class that uses a LINKEDLIST in order to store data. this queue will call on a linkedlist class class Queue: def __init__(self): self.items = LinkedList() #has to be O(1) def enqueue(self, item): #has to be O(1) def dequeue(self): def is_empty(self): def __len__(self):
In this lab, using C++, you will create two data structures: a stack and a queue....
In this lab, using C++, you will create two data structures: a stack and a queue. You will use STL containers to demonstrate basic ADTs. Queue For the queue, you will simulate a buffer. Remember it is first-in-first-out. The user will enter a number for the number of rounds to run your simulation. You need one function that randomly generates a number. You will also have a user specified percentage, and the function uses this percentage to randomly put the...
Using a single queue (linkedQueue), re-implement the concept of Stack ADT, what is the complexity of...
Using a single queue (linkedQueue), re-implement the concept of Stack ADT, what is the complexity of the method push, pop, top, isEmpty, and size. You should not use any extra data structure. Related codes: public interface Stack<E> { int size( ); boolean isEmpty( ); void push(E e); E top( ); E pop( ); } public class LinkedStack<E> implements Stack<E> { private SinglyLinkedList<E> list = new SinglyLinkedList<>( );    public LinkedStack( ) { }    public int size( ) { return...
Implement a stack using a single queue. In particular, you are given a queue Q that...
Implement a stack using a single queue. In particular, you are given a queue Q that provides the method Q.size() to return its size at any point and the standard methods of queues (i.e, Q.enqueue(x) and Q.dequeue()). The requirement is to use such methods of Q to implement two methods S.push(x) and S.pop() for a stack S. What are the running times of your methods? Kindly answer using python programming
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class Queue{ public Queue(){ // use the linked list } public void enqueue(int item){ // add item to end of queue } public int dequeue(){ // remove & return item from the front of the queue } public int peek(){ // return item from front of queue without removing it } public boolean isEmpty(){ // return true if the Queue is empty, otherwise false }...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class Stack{ public Stack(){ // use LinkedList class } public void push(int item){ // push item to stack } public int pop(){ // remove & return top item in Stack } public int peek(){ // return top item in Stack without removing it } public boolean isEmpty(){ // return true if the Stack is empty, otherwise false } public int getElementCount(){ // return current number...
Using Python list tools, create the standard stack (push, pop) and queue (enqueue, dequeue) operations Counting...
Using Python list tools, create the standard stack (push, pop) and queue (enqueue, dequeue) operations Counting Letter Challenge: Create a function that... Takes in a string as parameter Counts how often each letter appears in the string Returns a dictionary with the counts BONUS: make it so lowercase and uppercase letter count for the same letter
1. (10 pts) Define the nodes in the LinkedList. Create the LinkedList using the ListNode class....
1. (10 pts) Define the nodes in the LinkedList. Create the LinkedList using the ListNode class. Create a method to find a node with given value in a LinkedList. Return the value is this value exists in the LinkedList. Return null if not exists. Use these two examples to test your method. Example 1: Input: 1 -> 2 -> 3, and target value = 3 Output: 3 Example 2: Input: 1 -> 2 -> 3, and target value = 4...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT