Question: plz fill out sudo code *@author - Tim Nelson package applications; import datastructures.LinkedStack; import datastructures.OurStack; public class Calculator { public static void main(String[] args) {

plz fill out sudo code

*@author - Tim Nelson

package applications;

import datastructures.LinkedStack; import datastructures.OurStack;

public class Calculator {

public static void main(String[] args) { String problem = "5*(4+3)-6+2*7"; //First step is to convert this into a form we can put on a stack String postfix = convert(problem); System.out.println(postfix); //Second step is to use a stack to solve the problem int result = solve(postfix); System.out.println(result); } /** * Converts an arithmetic expression from infix notation * to postfix notation. * Infix notation is human readable expressions * Postfix notation puts the operators after the operands * This allows us to use a stack to solve the problem * (and is how basic calculators actually work) * To read the postfix expression "3 4 2 * + 8 -" * Read it as subtract 8 from the sum of (the product of 2 and 4) and 3 * @param infix infix expression, ex. "3+4*2-8" * @return postfix expression, ex. "3 4 2 * + 8 -" */ public static String convert(String infix) { /* Input: infix string Start with: empty stack of characters Start with: empty postfix string for each character c in infix string: if c is a digit: append c to postfix string else if c is '(': push c on stack else if c is ')': while top of stack is not '(': pop stack and append to postfix string pop '(' from stack else: pop stack and append to postfix string until: 1) stack is empty, 2) top is '(', or 3) p(top) < p(c) (p(o) is the precedence of o) push c on stack while stack is not empty: pop stack and append to postfix string Output: postfix string */ } String postfix = ""; return postfix; } /** * Returns the precedence level of an operation * This is a helper method for convert * @param op the operator to check * @return the level of precedence (2 for *,/ and 1 for +,-) */ public static int p(char op) { switch(op) { case '*': case '/': return 2; case '+': case '-': return 1; default: return -1; } } /** * Solves a postfix arithmetic expression. * @param postfix posfix expression to solve, ex. "3 4 2 * + 8 -" * @return integer solution, ex. 3 */ public static int solve(String postfix) { /* Input: postfix string Start with an empty stack of integers Start with a 0 result for each character c in postfix string: if c is a digit: push c onto stack else: (c must be an op) y = pop stack x = pop stack add x op y to result result is top of stack Output: result */ int result = 0; return result; } }

package datastructures;

import java.util.EmptyStackException;

public class LinkedStack implements OurStack {

//We build our stack by linking Nodes //Note: Node is a private class - it can only be accessed by LinkedStack private class Node { E element; //Element stored in the node Node link; //Link to the next node in the stack (the node beneath this one) } private Node top; //the top node in the stack private int size; //the total number of nodes /** * Constructor - creates an empty stack */ public LinkedStack() { size = 0; top = null; } @Override /** * Removes and returns this stack's top element. * @return top element * @throws EmptyStackException if the stack is empty */ public E pop() { if (size == 0) { throw new EmptyStackException(); } E elem = top.element; top = top.link; size--; return elem; }

@Override /** * Adds the given element to the top of the stack. * @param elem - element to add to the stack */ public void push(E elem) { Node n = new Node(); n.element = elem; n.link = top; top = n; size++; }

@Override /** * Returns, but does not remove, the tope element of the stack * @return top element * @throws EmptyStackException */ public E peek() { if (size == 0) { throw new EmptyStackException(); } return top.element; }

@Override /** * Returns the number of elements in the stack * @return size of stack */ public int size() { return size; }

@Override /** * Returns true if the stack is empty * @return true if stack is empty */ public boolean isEmpty() { return top == null; }

@Override /** * Returns this stack as a string * @return string containing the stack's elements */ public String toString() { String s = ""; //This is an example of an iterator Node walker = top; while(walker != null) { s += walker.element + " "; walker = walker.link; } return s; } @Override /** * Returns true if the stack is equal to the given stack. * Two stacks are equal if they have equal elements in the same order. * @return true if stacks are equal */ public boolean equals(Object obj) { if (obj instanceof LinkedStack) { LinkedStack s = (LinkedStack) obj; if(size != s.size) { return false; }else { Node walker = top; Node sWalker = s.top; while(walker != null) { if(!walker.element.equals(sWalker.element)) { return false; } walker = walker.link; sWalker = sWalker.link; } return true; } }else { return false; } } public static void main(String[] args) { OurStack s = new LinkedStack(); s.push(5); s.push(11); s.push(14); System.out.println(s.peek()); System.out.println(s.pop()); System.out.println(s.toString()); } }

package datastructures;

public interface OurStack{

/** * Removes the top element of the stack * @return the removed element */ public E pop(); /** * Adds a new element to the top of the stack * @param elem the element to add */ public void push(E elem); /** * Returns the top element without removing it * @return the top element */ public E peek(); /** * Returns the size of the stack * @return the size of the stack */ public int size(); /** * Returns true if the stack is empty * @return true if emtpy, false otherwise */ public boolean isEmpty(); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!