Ver código fonte

Project 1 created and commented to hell.

Tom Flucke 10 anos atrás
pai
commit
06b49da025

+ 6 - 0
Project1/.classpath

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" path="src"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
+	<classpathentry kind="output" path="bin"/>
+</classpath>

+ 17 - 0
Project1/.project

@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>Project1</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+	</natures>
+</projectDescription>

+ 11 - 0
Project1/.settings/org.eclipse.jdt.core.prefs

@@ -0,0 +1,11 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=1.6
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.6

+ 68 - 0
Project1/src/ConTest.java

@@ -0,0 +1,68 @@
+/**
+ * Driver program to allow command line user to interface with the Converter methods.
+ * 
+ * Project 1
+ * 
+ * @author Thomas Flucke tflucke
+ * @author Lara Luu ljluu
+ * 
+ * @since 2015/10/07
+ * 
+ * @see Converter
+ */
+import java.util.EmptyStackException;
+import java.util.Scanner;
+
+public class ConTest
+{
+	/**
+	 * Runs the program.
+	 */
+	public static void main(String... args)
+	{
+		Scanner in = new Scanner(System.in); // class to read from the console
+		System.out.println("Choose one of the following operations:");
+		System.out.println("- Infix to postfix conversion (enter the letter i)");
+		//We removed the "," from p because none of the other options had it.
+		System.out.println("- Postfix expression evaluation (enter the letter p)");
+		System.out.println("- Arithmetic expression evaluation (enter the letter a)");
+		System.out.println("- Quit the program (enter the letter q)");
+		System.out.println("Please insert a menu choice.");
+		String line; // variable to store the read input by line
+		while (!(line = in.nextLine()).equals("q"))
+		{
+			if (line.length() != 1)
+			{
+				System.out.println("Invalid choice");
+			}
+			char option = line.charAt(0); // stores option to be read in switch statement 
+			try
+			{
+				switch (option)
+				{
+				case 'i':
+					System.out.println("Please enter the infix expression to be converted to postfix: ");
+					System.out.println("the postfix expression is: "+Converter.infixToPostfix(in.nextLine()));
+					break;
+				case 'p':
+					System.out.println("Please enter the postfix expression to be evaluated: ");
+					System.out.println("the value of the postfix expression is: "+Converter.postfixValue(in.nextLine()));
+					break;
+				case 'a':
+					System.out.println("Please enter the arithmetic expression to be evaluated: ");
+					System.out.println("the value of the arithmetic expression is: "+Converter.postfixValue(Converter.infixToPostfix(in.nextLine())));
+					break;
+				default:
+					System.out.println("Invalid choice");
+				}
+			}
+			catch (EmptyStackException ese)
+			{
+				System.out.println("Invalid operation on an empty stack");	
+			}
+			System.out.println("Please insert a menu choice.");
+		}
+		System.out.println("quitting");
+		in.close();
+	}
+}

+ 101 - 0
Project1/src/Converter.java

@@ -0,0 +1,101 @@
+/**
+ * A class containing methods for manipulating postfix expressions.
+ * 
+ * Project 1
+ * 
+ * @author Thomas Flucke tflucke
+ * @author Lara Luu ljluu
+ * 
+ * @since 2015/10/07
+ */
+import java.util.Scanner;
+
+public class Converter
+{
+	/**
+	 * Converts an infix expression to postfix notation
+	 * @return The postfix expression as a space separated character string
+	 */
+	public static String infixToPostfix (String expression)
+	{
+		MyStack<Character> operatorStack = new MyStack<Character>(); // Creates stack in which to store operators
+		Scanner in = new Scanner(expression); // class to read from console
+		String output = new String(); // creates string to be used as output
+		while (in.hasNext())
+		{
+			String num = in.next(); // used to print the number if it is not an operator
+			char operator = num.charAt(0); // switchable operator
+			if (operator == '+' || operator == '-' || operator == '(')
+			{
+				operatorStack.push(operator);
+			}
+			else if (operator == '*' || operator == '/')
+			{
+				char nextOperator = operatorStack.peek();
+				while (!(nextOperator == '+' || nextOperator == '-' || nextOperator == '('))
+				{
+					output += operatorStack.pop() + " ";
+				}
+				operatorStack.push(operator);
+			}
+			else if (operator == ')')
+			{
+				while (operatorStack.peek() != '(')
+				{
+					output += operatorStack.pop() + " ";
+				}
+				operatorStack.pop();
+			}
+			else
+			{
+				output += num + " ";	
+			}
+		}
+		in.close();
+		while (!operatorStack.isEmpty())
+		{
+			output += operatorStack.pop() + " ";
+		}
+		return output.trim();
+	}
+
+	/**
+	 * Evaluates a postfix expression into a double value.
+	 * @return The double value derived from the postfix expression
+	 */
+	public static double postfixValue (String expression)
+	{
+		MyStack<Double> valueStack = new MyStack<Double>(); // create a stack in which to load doubles to be evaluated
+		Scanner in = new Scanner(expression); // class to read from console
+		while (in.hasNext())
+		{
+			if (in.hasNextDouble())
+			{
+				valueStack.push(in.nextDouble());
+			}
+			else
+			{
+				double tmp; // used for order-sensitive operations ( "-" & "/")
+				switch (in.next().charAt(0))
+				{
+					case '+':
+						valueStack.push(valueStack.pop() + valueStack.pop());
+						break;
+					case '-':
+						tmp = valueStack.pop();
+						valueStack.push(valueStack.pop() - tmp);
+						break;
+					case '*':
+						valueStack.push(valueStack.pop() * valueStack.pop());
+						break;
+					case '/':
+						tmp = valueStack.pop();
+						valueStack.push(valueStack.pop() / tmp);
+						break;
+				}
+			}
+		}
+		in.close();
+		return valueStack.pop();
+	}
+}

+ 83 - 0
Project1/src/MyStack.java

@@ -0,0 +1,83 @@
+/**
+ * Linked list implementation of a stack.
+ * 
+ * Project 1
+ * 
+ * @author Thomas Flucke tflucke
+ * @author Lara Luu ljluu
+ * 
+ * @since 2015/10/07
+ */
+
+import java.util.EmptyStackException;
+public class MyStack<T>
+{
+	/**
+	 * A single node containing an item in the list and a reference to the next node.
+	 */
+	private class Node
+	{
+		public T elm; // contains the data in the node
+		
+		public Node next; // contains the location of the next linked node
+	}
+	
+	private Node first; // contains the first node in the linked list
+	
+	/**
+	 * Initializes an instance of MyStack
+	 */
+	public MyStack()
+	{
+		first = null;
+	}
+	
+	/**
+	 * Places an item at the front of the stack.
+	 * @param item The item to be placed on the stack.
+	 */
+	public void push(T item)
+	{
+		Node newFirst = new Node(); // creates a new node
+		newFirst.elm = item; 
+		newFirst.next = first; 
+		first = newFirst; 
+	}
+
+	/**
+	 * Removes an item from the front of the stack.
+	 * @return The item removed from the stack.
+	 */
+	public T pop()
+	{
+		if (isEmpty())
+		{
+			throw new EmptyStackException();
+		}
+		T val = first.elm; // stores object to be returned
+		first = first.next;
+		return val;
+	}
+	
+	/**
+	 * Returns the value at the front of the stack.
+	 * @return The item at the front of the stack.
+	 */
+	public T peek()
+	{
+		if (isEmpty())
+		{
+			throw new EmptyStackException();
+		}
+		return first.elm;
+	}
+	
+	/**
+	 * Checks if there are any items left on the stack.
+	 * @return true if there are no items on the stack, false otherwise.
+	 */
+	public boolean isEmpty()
+	{
+		return first == null;
+	}
+}

+ 71 - 0
Project1/src/StackTest.java

@@ -0,0 +1,71 @@
+/**
+ * Driver program to allow command line user to interface with a MyStack instance.
+ * 
+ * Project 1
+ * 
+ * @author Thomas Flucke tflucke
+ * @author Lara Luu ljluu
+ * 
+ * @since 2015/10/07
+ * 
+ * @see MyStack
+ */
+import java.util.EmptyStackException;
+import java.util.Scanner;
+
+public class StackTest
+{
+	/**
+	 * Runs the program.
+	 */
+	public static void main(String... args)
+	{
+		Scanner in = new Scanner(System.in); // class to read from console
+		MyStack<String> stack = new MyStack<String>(); // initializes stack to manipulate inputs onto
+		System.out.println("Choose one of the following operations:");
+		System.out.println("- push/add (enter the letter a)");
+		System.out.println("- pop/delete (enter the letter d)");
+		System.out.println("- peek (enter the letter p)");
+		System.out.println("- check if the list is empty (enter the letter e)");
+		System.out.println("- Quit (enter the letter q)");
+		String line; // variable to store the read input by line
+		System.out.println("Please insert a menu choice.");
+		while (!(line = in.nextLine()).equals("q"))
+		{
+			if (line.length() != 1)
+			{
+				System.out.println("Invalid choice");
+			}
+			char option = line.charAt(0);; // stores option to be read in switch statement 
+			try
+			{
+				switch (option)
+				{
+				case 'a':
+					String pushed = in.nextLine(); // saves next input to be pushed onto the stack
+					stack.push(pushed);
+					System.out.println(pushed+" pushed in");
+					break;
+				case 'd':
+					System.out.println(stack.pop()+" popped out");
+					break;
+				case 'p':
+					System.out.println(stack.peek()+" on the top");
+					break;
+				case 'e':
+					System.out.println(stack.isEmpty()? "empty":"not empty");
+					break;
+					default:
+						System.out.println("Invalid choice");
+				}
+			}
+			catch (EmptyStackException ese)
+			{
+				System.out.println("Invalid operation on an empty stack");	
+			}
+			System.out.println("Please insert a menu choice.");
+		}
+		System.out.println("quitting");
+		in.close();
+	}
+}