Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have a problem in my main where my outputting the way it should suppose to be. I also need help in outputting the value

I have a problem in my main where my outputting the way it should suppose to be. I also need help in outputting the value of the operation.

The requirements are as follows:

The input will be in a text file whose name is given by arg[0] of main().

It will contain a fully parenthesized infix expression containing only: "(", ")", "+", "-" and integers.

Must use arg[0] as the name of the input file

//My Main:

import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; import java.util.Scanner;

public class AssignmentSeven { public static void main(String[] args) { ArrayList input = getInput(args[0]); printInput(input); } // Prints the expression for each input. private static void printInput(ArrayList input) { for (String s : input) { System.out.println("Expression is: " + s); inflixToPostfix(s); } } // Read the file input text taken from Assignment one. private static ArrayList getInput(String fileName) { try { ArrayList input = new ArrayList(); Scanner sc = new Scanner(new BufferedReader(new FileReader(fileName))); while(sc.hasNextLine()) input.add(sc.nextLine()); sc.close(); return input; } catch(Exception exc) { System.out.println("Exception: " +exc.getMessage() + " opening input file " + fileName); System.exit(0); } return null; }

private static void inflixToPostfix(String item) { String[] tokens = item.split("\\s+"); Queue myQueue = new Queue(); Stack myStack = new Stack(); for(String s : tokens) { if (s.equals("(") || isBinary(s)) { myStack.push(s); } else if(s.equals(")")) { String temp = myStack.getTop(); while (!temp.equals("(")) { myQueue.add(temp); myStack.pop(); //myQueue.dump("current postfix: "); temp = myStack.getTop(); } myStack.pop(); } else { myQueue.add(s); } } myQueue.dump("Postfix is"); eval(myQueue); } private static void eval(Queue item) { Stack evalStack = new Stack(); while (!item.isEmpty()) { String front = item.remove(); if(isBinary(front)) { double result; double operand2 = Double.parseDouble(evalStack.getTop()); evalStack.pop(); double operand1 = Double.parseDouble(evalStack.getTop()); evalStack.pop(); result = evalBinary(operand1, operand2, front); evalStack.push(Double.toString(result)); } else { evalStack.push(front); } } } // Check if the operand is Binary private static boolean isBinary(String item) { boolean divide = item.equals("/"); boolean multiply = item.equals("*"); boolean add = item.equals("+"); boolean subtract = item.equals("-"); if (divide || multiply || add || subtract) { return true; } return false; } // Evaluates the Binary operators. private static double evalBinary(double operand1,double operand2, String s) { double result; if (s.equals("+")) { result = operand1 + operand2; } else if (s.equals("-")) { result = operand1 - operand2; } else if (s.equals("*")) { result = operand1 * operand2; } else { result = operand1 / operand2; } return result; }

}

//Input:

( ( 1 + 2 ) - ( ( 3 - 4 ) + ( 7 - 2 ) ) ) ( ( 1 + 2 ) - ( 3 - 4 ) ) ( ( ( 1 + 2 ) - 3 ) - 4 ) ( ( 1 + ( 2 - 3 ) - 4 ) ) ( 1 + ( 2 - ( 3 - 4 ) ) )

//My Output:

Expression is: ( ( 1 + 2 ) - ( ( 3 - 4 ) + ( 7 - 2 ) ) )

Postfix is 1 2 + 3 4 - 7 2 - + - Expression is: ( ( 1 + 2 ) - ( 3 - 4 ) )

Postfix is 1 2 + 3 4 - - Expression is: ( ( ( 1 + 2 ) - 3 ) - 4 )

Postfix is 1 2 + 3 - 4 - Expression is: ( ( 1 + ( 2 - 3 ) - 4 ) )

Postfix is 1 2 3 - 4 - + Expression is: ( 1 + ( 2 - ( 3 - 4 ) ) )

Postfix is 1 2 3 4 - - +

// Desired Output:

Expression is: ( ( 1 + 2 ) - ( ( 3 - 4 ) + ( 7 - 2 ) ) ) Postfix is 1 2 + 3 4 - 7 2 - + - Value is -1.0 Expression is: ( ( 1 + 2 ) - ( 3 - 4 ) ) Postfix is 1 2 + 3 4 - - Value is 4.0 Expression is: ( ( ( 1 + 2 ) - 3 ) - 4 ) Postfix is 1 2 + 3 - 4 - Value is -4.0 Expression is: ( ( 1 + ( 2 - 3 ) - 4 ) ) Postfix is 1 2 3 - 4 - + Value is -4.0 Expression is: ( 1 + ( 2 - ( 3 - 4 ) ) ) Postfix is 1 2 3 4 - - + Value is 4.0 

// List.java

import java.util.ArrayList; import java.util.Collections;

public class List { /** * Create an empty List */ public List() { mFront = new Node(Node.DUMMY); mRear = new Node(Node.DUMMY); mFront.mNext = mRear; mRear.mPrev = mFront; } /** * Create a new List that is a copy of the list input * @param list the List to copy */ public List(List list) { this(); for(Node current = list.mFront.mNext; current != list.mRear; current = current.mNext) { addToRear(current.mData); } } /** * Item becomes the new front element * @param item the item to add */ public void addToFront(String item) { Node newNode = new Node(item); newNode.mPrev = mFront; newNode.mNext = mFront.mNext; newNode.mPrev.mNext = newNode; newNode.mNext.mPrev = newNode; } /** * Item becomes the new rear element * @param item the item to add */ public void addToRear(String item) { Node newNode = new Node(item); newNode.mNext = mRear; newNode.mPrev = mRear.mPrev; newNode.mPrev.mNext = newNode; newNode.mNext.mPrev = newNode; } /** * If beforeItem is not in List, prints "Item Not Found" else add item in List before beforeItem * @param beforeItem the item in the list to add item before * @param item the item to add */ public void addBeforeItem(String beforeItem, String item) { Node beforeNode = find(beforeItem); if(beforeNode == null) { sop("Item Not Found"); return; } Node newNode = new Node(item); newNode.mPrev = beforeNode.mPrev; newNode.mNext = beforeNode; newNode.mPrev.mNext = newNode; newNode.mNext.mPrev = newNode; } /** * Item becomes the element after afterItem * @param afterItem the item in the list to add item before * @param item the item to add */ public void addAfterItem(String afterItem, String item) { Node afterNode = find(afterItem); if(afterNode == null) { sop("Item Not Found"); return; } Node newNode = new Node(item); newNode.mPrev = afterNode; newNode.mNext = afterNode.mNext; newNode.mNext.mPrev = newNode; newNode.mPrev.mNext = newNode; } /** * Returns the item at the front of the List (List is not altered) * @return the item at the front of the List */ public String getFront() { if(askCount() == 0) { sop("List Empty"); return ""; } return(mFront.mNext.mData); } /** * Returns the item at the rear of the List (List is not altered) * @return the item at the rear of the List */ public String getRear() { if(askCount() == 0) { sop("List Empty"); return ""; } return(mRear.mPrev.mData); } /** * Return true if item is in List, false otherwise * @param item to check presence in List * @return true if item is in List, false otherwise */ public boolean isPresent(String item) { if(find(item) != null) return true; else return false; } /** * Returns the number of items in the List * @return the number of items in the List */ public int askCount() { int count = 0; Node current = mFront.mNext; while(current != mRear) { current = current.mNext; ++count; } return count; } /** * If the List is empty, prints "List Empty" * If the List is not empty, removes the item at the front of the List */ public void removeFront() { if(askCount() == 0) { sop("List Empty"); return; } removeItem(mFront.mNext.mData); } /** * If the List is empty, prints "List Empty" * If the List is not empty, removes the item at the rear of the List */ public void removeRear() { if(askCount() == 0) { sop("List Empty"); return; } removeItem(mRear.mPrev.mData); } /** * If the List is empty, prints "List Empty" * If item is not present in List, prints "Item not found" * Otherwise, item is removed from the List * @param item the item to remove */ public void removeItem(String item) { if(askCount() == 0) { sop("List Empty"); return; } Node removeNode = find(item); if(removeNode == null) { sop("Item not found"); return; } removeNode.mPrev.mNext = removeNode.mNext; removeNode.mNext.mPrev = removeNode.mPrev; } /** * Print title on a line by itself * Prints the List from front to rear with 1 space between each item * @param title the description of the List */ public void print(String title) { System.out.println(" " + title); for(Node current = mFront.mNext; current != mRear; current = current.mNext) { System.out.print(current.mData + " "); } System.out.println(""); } /** * Print title on a line by itself * Prints the Sorted List with 1 space between each item * Does not alter the List * @param title the description of the List */ public void printSorted(String title) { ArrayList tempList = new ArrayList(); for(Node current = mFront.mNext; current != mRear; current = current.mNext) { tempList.add(current.mData); } Collections.sort(tempList); System.out.println(" " + title); for(String s : tempList) { System.out.print(s + " "); } System.out.println(""); } private class Node { public Node(String data) { mData = data; mPrev = null; mNext = null; } private static final String DUMMY= "dummy"; private String mData; private Node mPrev; private Node mNext; } private Node find(String item) { for(Node current = mFront.mNext; current != mRear; current = current.mNext) { if(current.mData.equals(item)) return current; } return null; } private static void sop(String s) { System.out.println(s); } private Node mFront; private Node mRear; }

//Stack.java

public class Stack { private List mStack; /** * Create an empty Stack * @param myList Create an empty Stack */ public Stack() { mStack = new List(); } /** Make item the Top of the Stack * * @param item Make item the Top of the Stack */ public void push(String item) { mStack.addToRear(item); } /** * Remove the Top of the Stack * @param i */ public void pop() { mStack.removeRear(); } /** * * @return the Top of the Stack do not remove it */ public String getTop() { return mStack.getRear(); } /** * * @return true if Stack is empty, false otherwise */ public boolean isEmpty() { return mStack.askCount() == 0; } }

//Queue.java

public class Queue { private List mQueue; public Queue() { mQueue= new List(); } public void add(String item) { mQueue.addToRear(item); } /** * Get the first item in the list and removes it * @return the front of the Queue */ public String remove() { String item = mQueue.getFront(); mQueue.removeFront(); return item;

} public boolean isEmpty() { return mQueue.askCount() == 0; } public void dump(String title) { mQueue.print(title); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DB2 Universal Database V7.1 Application Development Certification Guide

Authors: Steve Sanyal, David Martineau, Kevin Gashyna, Michael Kyprianou

1st Edition

0130913677, 978-0130913678

More Books

Students also viewed these Databases questions

Question

=+ c. How would the change you describe in part

Answered: 1 week ago

Question

=+ (b) affect the world interest rate?

Answered: 1 week ago