ソースを参照

Added Lab6, finished Lab5

Lara Luu 10 年 前
コミット
f2b4b606e6

+ 2 - 1
Lab5/src/AQueue.java

@@ -46,7 +46,7 @@ public class AQueue <T>
 			front = 0;
 			end = count - 1;
 		}
-		end++;
+		end = (end + 1) % arr.length;
 		arr[end] = item;
 		count ++;
 	}
@@ -60,6 +60,7 @@ public class AQueue <T>
 		T tmp = arr[front];
 		arr[front] = null;
 		front = (front + 1) % arr.length;
+		count --;
 		return tmp;
 	}
 	

+ 22 - 10
Lab5/src/AQueueClient.java

@@ -1,18 +1,30 @@
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
 import java.util.Scanner;
-	
+
 public class AQueueClient 
 {
-	Scanner input = new Scanner(System.in);
-	new floatArr = AQueue
-	
-	while(input.hasNext())
+	public static void main(String[] args)
 	{
-		if(input.hasNextFloat())
+		Scanner input = new Scanner(System.in);
+		AQueue<Float> floatArr =  new AQueue<Float>(5);
+
+		while(input.hasNext())
 		{
-			
+			if(input.hasNextFloat())
+			{
+				floatArr.enqueue(input.nextFloat());
+			}
+			else
+			{
+				input.next();
+			}
 		}
+		
+		while (!floatArr.isEmpty())
+		{
+			System.out.print(floatArr.dequeue() + " ");
+		}
+		
+		System.out.println();
+		
 	}
 }
-		

+ 2 - 2
Lab5/src/AQueueDriver.java

@@ -7,7 +7,7 @@ public class AQueueDriver {
 		printHelp();
 		boolean running = true;
 		while (running)
-		{
+		{   System.out.println("Enter a menu choice: ");
 			String line = in.nextLine();
 			if (line.length() != 1)
 			{
@@ -37,7 +37,7 @@ public class AQueueDriver {
 					{
 						System.out.println(queue.dequeue() +" dequeued");
 					}
-					catch (LQueue.MyException me)
+					catch (AQueue.MyException me)
 					{
 						System.out.println("Invalid operation.  Queue is empty.");
 					}

+ 6 - 0
Lab6/.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
Lab6/.project

@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>Lab6</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>

+ 12 - 0
Lab6/.settings/org.eclipse.jdt.core.prefs

@@ -0,0 +1,12 @@
+#Wed Oct 07 11:29:47 PDT 2015
+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

+ 61 - 0
Lab6/src/AStack.java

@@ -0,0 +1,61 @@
+public class AStack<T> {
+	public static class MyException extends RuntimeException
+	{
+		private static final long serialVersionUID = -244806549310243756L;
+		public MyException()
+		{
+			super();
+		}
+		public MyException(String message)
+		{
+			super(message);
+		}
+	}
+	
+	private T[] arr;
+	private int top;
+
+	@SuppressWarnings("unchecked")
+	public AStack(int size)
+	{
+		arr = (T[]) new Object[size];
+		top = -1;
+	}
+
+	@SuppressWarnings("unchecked")
+	public void push(T item)
+	{
+		if (top+1 == arr.length)
+		{
+			T[] newArr = (T[]) new Object[2*arr.length];
+			for (int i = 0; i <= top; i++)
+			{
+				newArr[i] = arr[i];
+			}
+			arr = newArr;
+		}
+		arr[++top] = item;
+	}
+
+	public T pop()
+	{
+		if (isEmpty())
+		{
+			throw new MyException();
+		}
+		return arr[top--];
+	}
+	public T peek()
+	{
+		if (isEmpty())
+		{
+			throw new MyException();
+		}
+		return arr[top];
+	}
+	
+	public boolean isEmpty()
+	{
+		return top < 0;
+	}
+}

+ 63 - 0
Lab6/src/StringChecker.java

@@ -0,0 +1,63 @@
+import java.util.Scanner;
+
+
+
+public class StringChecker 
+{
+
+	public static boolean isBalanced(String input)
+	{
+		AStack<Character> charStack = new AStack<Character>(5);
+		try
+		{
+			for (int i = 0; i < input.length(); i++)
+			{
+				char c = input.charAt(i);
+				
+				switch(c)
+				{
+					case '{':
+					case '(':
+					case '[':
+						charStack.push(c);
+						break;
+					case '}':
+						if (charStack.pop() != '{')
+						{
+							return false;
+						}
+						break;
+					case ')':
+						if (charStack.pop() != '(')
+						{
+							return false;
+						}
+						break;
+					case ']':
+						if (charStack.pop() != '[')
+						{
+							return false;
+						}
+						break;
+				}
+			}
+		}
+		catch (AStack.MyException me)
+		{
+			return false;
+		}
+		return charStack.isEmpty();
+	}
+	
+	public static void main(String[] args)
+	{
+		System.out.println("Hey baby. Can you please give me a string to check? :)");
+		Scanner in = new Scanner(System.in);
+		while(in.hasNextLine())
+		{
+			System.out.println(isBalanced(in.nextLine())? "Congrats, your string is indeed balanced (:" : 
+														  "I'm sorry, but you suck at balancing strings ):");
+		}
+		
+	}
+}