diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2017-02-26 09:06:49 -0500 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2017-02-26 09:06:49 -0500 |
| commit | e8567df90f38e5f2e15fbb245aa4ac45d37a3003 (patch) | |
| tree | d0e9eb9103a45d902f26afb2e21889f45a26fa9c /BJC-Utils2/src/examples | |
| parent | 32ff2eeece956254cb8d3bb60f7d1526571ac28c (diff) | |
Example formatting
Diffstat (limited to 'BJC-Utils2/src/examples')
5 files changed, 128 insertions, 139 deletions
diff --git a/BJC-Utils2/src/examples/java/bjc/utils/examples/BinarySearchTest.java b/BJC-Utils2/src/examples/java/bjc/utils/examples/BinarySearchTest.java index 1beaeec..3c6f124 100644 --- a/BJC-Utils2/src/examples/java/bjc/utils/examples/BinarySearchTest.java +++ b/BJC-Utils2/src/examples/java/bjc/utils/examples/BinarySearchTest.java @@ -14,41 +14,31 @@ import bjc.utils.funcdata.bst.TreeLinearizationMethod; public class BinarySearchTest { private static void display(BinarySearchTree<Character> tree, Scanner input) { - System.out.print( - "What order would you like the tree to be printed in (m for options): "); - + System.out.print("What order would you like the tree to be printed in (m for options): "); char command; while (true) { command = input.nextLine().charAt(0); - TreeLinearizationMethod method = null; switch (command) { - case 'm': - System.out.println("Possible tree printing methods: "); - System.out.println( - "\tp: Preorder printing (print parent first, then left & right)."); - System.out.println( - "\ti: Inorder printing (print left first, then parent & right)."); - System.out.println( - "\to: Postorder printing (print left first, then right & parent)."); - break; - - case 'p': - method = TreeLinearizationMethod.PREORDER; - break; - - case 'i': - method = TreeLinearizationMethod.INORDER; - break; - - case 'o': - method = TreeLinearizationMethod.POSTORDER; - break; - - default: - System.out.println("ERROR: Unknown command."); + case 'm': + System.out.println("Possible tree printing methods: "); + System.out.println("\tp: Preorder printing (print parent first, then left & right)."); + System.out.println("\ti: Inorder printing (print left first, then parent & right)."); + System.out.println("\to: Postorder printing (print left first, then right & parent)."); + break; + case 'p': + method = TreeLinearizationMethod.PREORDER; + break; + case 'i': + method = TreeLinearizationMethod.INORDER; + break; + case 'o': + method = TreeLinearizationMethod.POSTORDER; + break; + default: + System.out.println("ERROR: Unknown command."); } if (method != null) { @@ -60,8 +50,7 @@ public class BinarySearchTest { return; } - System.out.print( - "What order would you like the tree to be printed in (m for options): "); + System.out.print("What order would you like the tree to be printed in (m for options): "); } } @@ -78,71 +67,58 @@ public class BinarySearchTest { char command = ' '; - BinarySearchTree<Character> tree = new BinarySearchTree<>( - (o1, o2) -> o1 - o2); + BinarySearchTree<Character> tree = new BinarySearchTree<>((o1, o2) -> o1 - o2); while (command != 'e') { System.out.print("Enter a command (m for help): "); command = input.nextLine().charAt(0); switch (command) { - case 'm': - System.out.println("Valid commands: "); - System.out.println("\tm: Display this help message."); - System.out.println("\te: Exit this program."); - System.out.println( - "\ta: Add a node to the binary tree."); - System.out.println("\td: Display the binary tree."); - System.out.println( - "\tr: Remove a node from the binary tree."); - System.out.println( - "\tf: Check if a given node is in the binary tree."); - System.out.println( - "\tt: Trim all deleted nodes from the tree."); - System.out.println( - "\tb: Balance the tree (also trims dead nodes)"); - break; - - case 'a': - System.out.print( - "Enter the letter to add to the binary tree: "); - command = input.nextLine().charAt(0); - - tree.addNode(command); - break; - - case 'r': - System.out.print( - "Enter the letter to add to the binary tree: "); - command = input.nextLine().charAt(0); - - tree.deleteNode(command); - break; - - case 'd': - display(tree, input); - break; - - case 'f': - System.out.print( - "Enter the letter to add to the binary tree: "); - command = input.nextLine().charAt(0); - - System.out.println("Node " + command + " was " - + (tree.isInTree(command) ? "" : "not ") - + "found"); - break; - - case 't': - tree.trim(); - break; - - case 'b': - tree.balance(); - break; - - default: - System.out.println("ERROR: Unrecognized command."); + case 'm': + System.out.println("Valid commands: "); + System.out.println("\tm: Display this help message."); + System.out.println("\te: Exit this program."); + System.out.println("\ta: Add a node to the binary tree."); + System.out.println("\td: Display the binary tree."); + System.out.println("\tr: Remove a node from the binary tree."); + System.out.println("\tf: Check if a given node is in the binary tree."); + System.out.println("\tt: Trim all deleted nodes from the tree."); + System.out.println("\tb: Balance the tree (also trims dead nodes)"); + break; + case 'a': + System.out.print("Enter the letter to add to the binary tree: "); + command = input.nextLine().charAt(0); + + tree.addNode(command); + break; + case 'r': + System.out.print("Enter the letter to add to the binary tree: "); + command = input.nextLine().charAt(0); + + tree.deleteNode(command); + break; + case 'd': + display(tree, input); + break; + case 'f': + System.out.print("Enter the letter to add to the binary tree: "); + command = input.nextLine().charAt(0); + + boolean inTree = tree.isInTree(command); + if(inTree) { + System.out.printf("Node %s was found\n", command); + } else { + System.out.printf("Node %s was not found\n", command); + } + break; + case 't': + tree.trim(); + break; + case 'b': + tree.balance(); + break; + default: + System.out.println("ERROR: Unrecognized command."); } } diff --git a/BJC-Utils2/src/examples/java/bjc/utils/examples/parsing/ShuntTest.java b/BJC-Utils2/src/examples/java/bjc/utils/examples/parsing/ShuntTest.java index 47bbaf8..1317db7 100644 --- a/BJC-Utils2/src/examples/java/bjc/utils/examples/parsing/ShuntTest.java +++ b/BJC-Utils2/src/examples/java/bjc/utils/examples/parsing/ShuntTest.java @@ -26,13 +26,11 @@ public class ShuntTest { String line = inputSource.nextLine(); ShuntingYard<String> yard = new ShuntingYard<>(true); - - IList<String> shuntedTokens = yard - .postfix(new FunctionalStringTokenizer(line) - .toList((strang) -> strang), (strang) -> strang); + IList<String> preTokens = new FunctionalStringTokenizer(line).toList(strang -> strang); + IList<String> shuntedTokens = yard.postfix(preTokens, strang -> strang); System.out.println(shuntedTokens.toString()); inputSource.close(); } -}
\ No newline at end of file +} diff --git a/BJC-Utils2/src/examples/java/bjc/utils/examples/parsing/TreeConstructTest.java b/BJC-Utils2/src/examples/java/bjc/utils/examples/parsing/TreeConstructTest.java index fbc3638..6559a1e 100644 --- a/BJC-Utils2/src/examples/java/bjc/utils/examples/parsing/TreeConstructTest.java +++ b/BJC-Utils2/src/examples/java/bjc/utils/examples/parsing/TreeConstructTest.java @@ -26,8 +26,7 @@ import bjc.utils.parserutils.TreeConstructor; * */ public class TreeConstructTest { - private static final class OperatorPicker - implements Predicate<String> { + private static final class OperatorPicker implements Predicate<String> { @Override public boolean test(String token) { if (StringUtils.containsOnly(token, "\\[")) { @@ -37,13 +36,13 @@ public class TreeConstructTest { } switch (token) { - case "+": - case "-": - case "*": - case "/": - return true; - default: - return false; + case "+": + case "-": + case "*": + case "/": + return true; + default: + return false; } } } @@ -61,8 +60,7 @@ public class TreeConstructTest { System.out.print("Enter a expression to parse: "); String line = inputSource.nextLine(); - IList<String> tokens = new FunctionalStringTokenizer(line) - .toList(); + IList<String> tokens = new FunctionalStringTokenizer(line).toList(); ShuntingYard<String> yard = new ShuntingYard<>(true); @@ -75,23 +73,18 @@ public class TreeConstructTest { ops.add(new Pair<>(":=", ":=")); ops.add(new Pair<>("=>", "=>")); - IList<String> semiExpandedTokens = ListUtils.splitTokens(tokens, - ops); + IList<String> semiExpandedTokens = ListUtils.splitTokens(tokens, ops); ops = new LinkedList<>(); - ops.add(new Pair<>("(", "\\(")); ops.add(new Pair<>(")", "\\)")); ops.add(new Pair<>("[", "\\[")); ops.add(new Pair<>("]", "\\]")); - IList<String> fullyExpandedTokens = ListUtils - .deAffixTokens(semiExpandedTokens, ops); - + IList<String> fullyExpandedTokens = ListUtils.deAffixTokens(semiExpandedTokens, ops); fullyExpandedTokens.removeIf((strang) -> strang.equals("")); - IList<String> shuntedTokens = yard.postfix(fullyExpandedTokens, - (token) -> token); + IList<String> shuntedTokens = yard.postfix(fullyExpandedTokens, (token) -> token); System.out.println("Shunted: " + shuntedTokens.toString()); @@ -105,8 +98,8 @@ public class TreeConstructTest { return false; }; - IMap<String, Function<Deque<ITree<String>>, - ITree<String>>> operators = new FunctionalMap<>(); + IMap<String, Function<Deque<ITree<String>>, ITree<String>>> operators = + new FunctionalMap<>(); operators.put("[", (queuedTrees) -> { return null; @@ -132,12 +125,12 @@ public class TreeConstructTest { return arrayTree; }); - ITree<String> constructedTree = TreeConstructor.constructTree( - shuntedTokens, new OperatorPicker(), specialPicker, - operators::get); + ITree<String> constructedTree = + TreeConstructor.constructTree(shuntedTokens, + new OperatorPicker(), specialPicker, operators::get); System.out.println("AST: " + constructedTree.toString()); inputSource.close(); } -}
\ No newline at end of file +} diff --git a/BJC-Utils2/src/examples/java/bjc/utils/examples/rangen/DiabloItemGen.java b/BJC-Utils2/src/examples/java/bjc/utils/examples/rangen/DiabloItemGen.java index 521d8e3..fc1d6c2 100644 --- a/BJC-Utils2/src/examples/java/bjc/utils/examples/rangen/DiabloItemGen.java +++ b/BJC-Utils2/src/examples/java/bjc/utils/examples/rangen/DiabloItemGen.java @@ -15,8 +15,10 @@ public class DiabloItemGen { private static void addCase(String ruleName, int probability, String ruleParts) { - rules.addCase(ruleName, probability, FunctionalStringTokenizer - .fromString(ruleParts).toList(s -> s)); + IList<String> parts = FunctionalStringTokenizer.fromString(ruleParts) + .toList(strang -> strang); + + rules.addCase(ruleName, probability, parts); } private static void addInfixRules() { @@ -36,7 +38,7 @@ public class DiabloItemGen { addCase(rn, 10, "<infix>"); addCase(rn, 20, "<prefix> <infix>"); addCase(rn, 30, "<infix> <suffix>"); - addCase(rn, 40, "<prefix> <infix> <suffix>"); + addCase(rn, 40, "<prefix> <infix> <suffix>"); addCase(rn, 50, "<prefix> <prefix> <infix>"); addCase(rn, 60, "<prefix> <prefix> <infix> <suffix>"); } @@ -86,8 +88,7 @@ public class DiabloItemGen { IList<String> ls = rules.generateListValues("<item>", " "); StringBuilder sb = new StringBuilder(); - - ls.forEach(sp -> sb.append(sp)); + ls.forEach(sb::append); System.out.println(sb.toString().replaceAll("\\s+", " ")); } diff --git a/BJC-Utils2/src/examples/java/bjc/utils/examples/rangen/RandomStringExamples.java b/BJC-Utils2/src/examples/java/bjc/utils/examples/rangen/RandomStringExamples.java index f9adb72..c8a7871 100644 --- a/BJC-Utils2/src/examples/java/bjc/utils/examples/rangen/RandomStringExamples.java +++ b/BJC-Utils2/src/examples/java/bjc/utils/examples/rangen/RandomStringExamples.java @@ -18,8 +18,10 @@ public class RandomStringExamples { IList<IList<String>> cses = new FunctionalList<>(); for (String strang : cases) { - cses.add(FunctionalStringTokenizer.fromString(strang) - .toList(s -> s)); + IList<String> lst = FunctionalStringTokenizer.fromString(strang) + .toList(s -> s) + + cses.add(lst); } rg.makeRule(rule, cses); @@ -34,33 +36,52 @@ public class RandomStringExamples { public static void main(String[] args) { rg = new RandomGrammar<>(); - addRule("<sentance>", "<person> <opines> <something>", + addRule("<sentance>", + "<person> <opines> <something>", "<person> thinks that I am <property>", - "I <opine> <something>", "You think that I am <property>"); - - addRule("<activity>", "dancing", "eating", "sleeping"); - - addRule("<object>", "<person>", "life", "my computer", + "I <opine> <something>", + "You think that I am <property>"); + + addRule("<activity>", + "dancing", + "eating", + "sleeping"); + + addRule("<object>", + "<person>", + "life", + "my computer", "my friends"); - addRule("<opine>", "hate", "am jealous of", "love"); + addRule("<opine>", + "hate", + "am jealous of", + "love"); - addRule("<opines>", "hates", "loves"); + addRule("<opines>", + "hates", + "loves"); - addRule("<person>", "my sister", "my father", "my girlfriend", + addRule("<person>", + "my sister", + "my father", + "my girlfriend", "the man next door"); - addRule("<property>", "creative", "intelligent"); + addRule("<property>", + "creative", + "intelligent"); - addRule("<something>", "<activity>", "<activity> with <person>", + addRule("<something>", + "<activity>", + "<activity> with <person>", "<object>"); for (int i = 0; i < 10; i++) { IList<String> ls = rg.generateListValues("<sentance>", " "); StringBuilder sb = new StringBuilder(); - - ls.forEach(sp -> sb.append(sp)); + ls.forEach(sb::append); System.out.println(sb.toString().replaceAll("\\s+", " ")); } |
