diff --git a/other/Stacks/.gitignore b/other/Stacks/.gitignore new file mode 100644 index 0000000..b1001a6 --- /dev/null +++ b/other/Stacks/.gitignore @@ -0,0 +1 @@ +.idea/** diff --git a/other/Stacks/Stacks.iml b/other/Stacks/Stacks.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/other/Stacks/Stacks.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/other/Stacks/questions/lab13a.doc b/other/Stacks/questions/lab13a.doc new file mode 100644 index 0000000..57dfd20 Binary files /dev/null and b/other/Stacks/questions/lab13a.doc differ diff --git a/other/Stacks/questions/lab13b.doc b/other/Stacks/questions/lab13b.doc new file mode 100644 index 0000000..f2f74b5 Binary files /dev/null and b/other/Stacks/questions/lab13b.doc differ diff --git a/other/Stacks/questions/lab13c.doc b/other/Stacks/questions/lab13c.doc new file mode 100644 index 0000000..92068a4 Binary files /dev/null and b/other/Stacks/questions/lab13c.doc differ diff --git a/other/Stacks/questions/lab13d.doc b/other/Stacks/questions/lab13d.doc new file mode 100644 index 0000000..41ed8e4 Binary files /dev/null and b/other/Stacks/questions/lab13d.doc differ diff --git a/other/Stacks/src/IntStack.java b/other/Stacks/src/IntStack.java new file mode 100644 index 0000000..6f0793d --- /dev/null +++ b/other/Stacks/src/IntStack.java @@ -0,0 +1,35 @@ +//© A+ Computer Science - www.apluscompsci.com +//Name - Ryan Walters +//Date - 11 December 2020 +//Class - Computer Science II PreAP +//Lab - Lab 13D (IntStack.java) + +import java.util.ArrayList; + +public class IntStack { + private ArrayList fakeStack; + + public IntStack() { + fakeStack = new ArrayList(); + } + + public void push(int item) { + fakeStack.add(item); + } + + public int pop() { + return fakeStack.remove(fakeStack.size() - 1); + } + + public boolean isEmpty() { + return fakeStack.size() == 0; + } + + public int peek() { + return fakeStack.get(fakeStack.size() - 1); + } + + public String toString() { + return fakeStack.toString(); + } +} \ No newline at end of file diff --git a/other/Stacks/src/Lab13a.java b/other/Stacks/src/Lab13a.java new file mode 100644 index 0000000..67f95c6 --- /dev/null +++ b/other/Stacks/src/Lab13a.java @@ -0,0 +1,37 @@ +//© A+ Computer Science - www.apluscompsci.com +//Name - Ryan Walters +//Date - 11 December 2020 +//Class - Computer Science II PreAP +//Lab - Lab 13A + +import java.util.Scanner; +import java.util.Stack; + +import static java.lang.System.out; + +public class Lab13a { + public static void main(String[] args) { + String sampleData = "a b c d e f g h i\n" + + "1 2 3 4 5 6 7 8 9 10\n" + + "# $ % ^ * ( ) ) _\n"; + + for (String line : sampleData.split("\n")) { + // Read in the scanner data, place into stack + Scanner scanner = new Scanner(line); + Stack stack = new Stack(); + while (scanner.hasNext()) + stack.add(scanner.next()); + + // Extract all items from the stack, build a new string + StringBuilder stackOut = new StringBuilder(stack.size() * 2); + while (!stack.isEmpty()) { + stackOut.append(stack.pop()); + stackOut.append(" "); + } + + out.println(stackOut.toString()); + } + + out.println(); + } +} diff --git a/other/Stacks/src/Lab13b.java b/other/Stacks/src/Lab13b.java new file mode 100644 index 0000000..c945928 --- /dev/null +++ b/other/Stacks/src/Lab13b.java @@ -0,0 +1,28 @@ +//© A+ Computer Science - www.apluscompsci.com +//Name - Ryan Walters +//Date - 11 December 2020 +//Class - Computer Science II PreAP +//Lab - Lab 13B + +import java.util.Locale; + +import static java.lang.System.out; + +public class Lab13b { + public static void main(String[] args) { + String sampleData = "(abc(*def) \n" + + "[{}]\n" + + "[\n" + + "[{<()>}]\n" + + "{{$x}}\n" + + "[one]{three}(four)\n" + + "car(cdr(a)(b)))\n" + + "car(cdr(a)(b))\n"; + + SyntaxChecker sc = new SyntaxChecker(); + for (String line : sampleData.split("\n")) { + sc.setExpression(line); + out.println(String.format(Locale.ENGLISH, "%s is %s", line, sc.checkExpression() ? "correct" : "incorrect")); + } + } +} \ No newline at end of file diff --git a/other/Stacks/src/Lab13c.java b/other/Stacks/src/Lab13c.java new file mode 100644 index 0000000..ef598ae --- /dev/null +++ b/other/Stacks/src/Lab13c.java @@ -0,0 +1,27 @@ +//© A+ Computer Science - www.apluscompsci.com +//Name - Ryan Walters +//Date - 11 December 2020 +//Class - Computer Science II PreAP +//Lab - Lab 13C + +import static java.lang.System.out; + +public class Lab13c +{ + public static void main ( String[] args ) + { + String sampleData = "2 7 + 1 2 + +\n" + + "1 2 3 4 + + +\n" + + "9 3 * 8 / 4 +\n" + + "3 3 + 7 * 9 2 / +\n" + + "9 3 / 2 * 7 9 * + 4 -\n" + + "5 5 + 2 * 4 / 9 +\n"; + + PostFix postFix = new PostFix(); + for (String line : sampleData.split("\n")) { + postFix.setExpression(line); + postFix.solve(); + out.println(postFix); + } + } +} \ No newline at end of file diff --git a/other/Stacks/src/Lab13d.java b/other/Stacks/src/Lab13d.java new file mode 100644 index 0000000..b0bedfe --- /dev/null +++ b/other/Stacks/src/Lab13d.java @@ -0,0 +1,24 @@ +//© A+ Computer Science - www.apluscompsci.com +//Name - Ryan Walters +//Date - 11 December 2020 +//Class - Computer Science II PreAP +//Lab - Lab 13D + +import static java.lang.System.out; + +public class Lab13d { + public static void main(String[] args) { + IntStack test = new IntStack(); + test.push(5); + test.push(7); + test.push(9); + System.out.println(test); + System.out.println(test.isEmpty()); + System.out.println(test.pop()); + System.out.println(test.peek()); + System.out.println(test.pop()); + System.out.println(test.pop()); + System.out.println(test.isEmpty()); + System.out.println(test); + } +} \ No newline at end of file diff --git a/other/Stacks/src/PostFix.java b/other/Stacks/src/PostFix.java new file mode 100644 index 0000000..79289cf --- /dev/null +++ b/other/Stacks/src/PostFix.java @@ -0,0 +1,64 @@ +//© A+ Computer Science - www.apluscompsci.com +//Name - Ryan Walters +//Date - 11 December 2020 +//Class - Computer Science II PreAP +//Lab - Lab 13C (PostFix.java) + +import java.util.Stack; +import java.util.Scanner; + +import static java.lang.System.*; + +public class PostFix { + private Stack stack; + private String expression; + + public PostFix() { + expression = ""; + stack = new Stack(); + } + + public PostFix(String exp) { + expression = exp; + } + + public void setExpression(String exp) { + expression = exp; + } + + public double calc(double one, double two, char op) { + switch (op) { + case '+': + return one + two; + case '-': + return one - two; + case '*': + return one * two; + case '/': + return one / two; + } + return 0.0; + } + + public void solve() { + stack = new Stack(); + for (char next : expression.replace(" ", "").toCharArray()) { + if (isOperator(next)) { + double second = stack.pop(); + double first = stack.pop(); + stack.push(calc(first, second, next)); + } else { + stack.push((double) Integer.parseInt(String.valueOf(next))); + } + } + } + + private boolean isOperator(char op) { + return op == '+' || op == '-' || op == '*' || op == '/'; + } + + @Override + public String toString() { + return expression + " = " + stack.peek(); + } +} \ No newline at end of file diff --git a/other/Stacks/src/StackTester.java b/other/Stacks/src/StackTester.java new file mode 100644 index 0000000..4a4221a --- /dev/null +++ b/other/Stacks/src/StackTester.java @@ -0,0 +1,26 @@ +//© A+ Computer Science - www.apluscompsci.com +//Name - +//Date - +//Class - +//Lab - + +import java.util.Stack; + +public class StackTester { + private Stack stack; + + public StackTester() { + setStack(""); + } + + public StackTester(String line) { + } + + public void setStack(String line) { + } + + public void popEmAll() { + } + + //add a toString +} \ No newline at end of file diff --git a/other/Stacks/src/SyntaxChecker.java b/other/Stacks/src/SyntaxChecker.java new file mode 100644 index 0000000..90f87da --- /dev/null +++ b/other/Stacks/src/SyntaxChecker.java @@ -0,0 +1,79 @@ +//© A+ Computer Science - www.apluscompsci.com +//Name - Ryan Walters +//Date - 11 December 2020 +//Class - Computer Science II PreAP +//Lab - Lab 13B (SyntaxChecker.java) + +import java.util.Stack; + +import static java.lang.System.*; + +public class SyntaxChecker { + private String exp; + private Stack symbols; + + private final char[] opening = {'[', '{', '(', '<'}; + private final char[] closing = {']', '}', ')', '>'}; + + public SyntaxChecker() { + exp = ""; + } + + public SyntaxChecker(String s) { + exp = s; + } + + public void setExpression(String s) { + exp = s; + } + + public boolean checkExpression() { + // create stack, stack matching symbols on + symbols = new Stack(); + for (int i = 0; i < exp.length(); i++) { + char possibleSymbol = exp.charAt(i); + switch (symbolType(possibleSymbol)) { + case 0: + break; + case 1: + symbols.add(possibleSymbol); + break; + case 2: + // Find the index to find the correlating open symbol + int index = -1; + for (int j = 0; j < closing.length; j++) { + if (closing[j] == possibleSymbol) + index = j; + } + + // If the closing symbol matches (and thus the array wasn't empty) + if (!symbols.isEmpty() && opening[index] == symbols.peek()) + symbols.pop(); + else + return false; + } + } + + return symbols.isEmpty(); + } + + /** + * @param character The character to identify + * @return A integer representing the character's symbol. 1 is open, 2 is closed, 0 is not a symbol. + */ + private int symbolType(char character) { + // Check if it's a opening character + for (char c : opening) { + if (character == c) + return 1; + } + + // Check if it's a closing character + for (char c : closing) { + if (character == c) + return 2; + } + + return 0; + } +} \ No newline at end of file