mirror of
https://github.com/Xevion/contest.git
synced 2025-12-06 01:14:36 -06:00
Stacks finished data/programs/questions
This commit is contained in:
1
other/Stacks/.gitignore
vendored
Normal file
1
other/Stacks/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
.idea/**
|
||||
11
other/Stacks/Stacks.iml
Normal file
11
other/Stacks/Stacks.iml
Normal file
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
BIN
other/Stacks/questions/lab13a.doc
Normal file
BIN
other/Stacks/questions/lab13a.doc
Normal file
Binary file not shown.
BIN
other/Stacks/questions/lab13b.doc
Normal file
BIN
other/Stacks/questions/lab13b.doc
Normal file
Binary file not shown.
BIN
other/Stacks/questions/lab13c.doc
Normal file
BIN
other/Stacks/questions/lab13c.doc
Normal file
Binary file not shown.
BIN
other/Stacks/questions/lab13d.doc
Normal file
BIN
other/Stacks/questions/lab13d.doc
Normal file
Binary file not shown.
35
other/Stacks/src/IntStack.java
Normal file
35
other/Stacks/src/IntStack.java
Normal file
@@ -0,0 +1,35 @@
|
||||
//<2F> 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<Integer> fakeStack;
|
||||
|
||||
public IntStack() {
|
||||
fakeStack = new ArrayList<Integer>();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
37
other/Stacks/src/Lab13a.java
Normal file
37
other/Stacks/src/Lab13a.java
Normal file
@@ -0,0 +1,37 @@
|
||||
//<2F> 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<String> stack = new Stack<String>();
|
||||
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();
|
||||
}
|
||||
}
|
||||
28
other/Stacks/src/Lab13b.java
Normal file
28
other/Stacks/src/Lab13b.java
Normal file
@@ -0,0 +1,28 @@
|
||||
//<2F> 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" +
|
||||
"{<html[value=4]*(12)>{$x}}\n" +
|
||||
"[one]<two>{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"));
|
||||
}
|
||||
}
|
||||
}
|
||||
27
other/Stacks/src/Lab13c.java
Normal file
27
other/Stacks/src/Lab13c.java
Normal file
@@ -0,0 +1,27 @@
|
||||
//<2F> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
24
other/Stacks/src/Lab13d.java
Normal file
24
other/Stacks/src/Lab13d.java
Normal file
@@ -0,0 +1,24 @@
|
||||
//<2F> 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);
|
||||
}
|
||||
}
|
||||
64
other/Stacks/src/PostFix.java
Normal file
64
other/Stacks/src/PostFix.java
Normal file
@@ -0,0 +1,64 @@
|
||||
//<2F> 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<Double> stack;
|
||||
private String expression;
|
||||
|
||||
public PostFix() {
|
||||
expression = "";
|
||||
stack = new Stack<Double>();
|
||||
}
|
||||
|
||||
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<Double>();
|
||||
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();
|
||||
}
|
||||
}
|
||||
26
other/Stacks/src/StackTester.java
Normal file
26
other/Stacks/src/StackTester.java
Normal file
@@ -0,0 +1,26 @@
|
||||
//<2F> A+ Computer Science - www.apluscompsci.com
|
||||
//Name -
|
||||
//Date -
|
||||
//Class -
|
||||
//Lab -
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
public class StackTester {
|
||||
private Stack<String> stack;
|
||||
|
||||
public StackTester() {
|
||||
setStack("");
|
||||
}
|
||||
|
||||
public StackTester(String line) {
|
||||
}
|
||||
|
||||
public void setStack(String line) {
|
||||
}
|
||||
|
||||
public void popEmAll() {
|
||||
}
|
||||
|
||||
//add a toString
|
||||
}
|
||||
79
other/Stacks/src/SyntaxChecker.java
Normal file
79
other/Stacks/src/SyntaxChecker.java
Normal file
@@ -0,0 +1,79 @@
|
||||
//<2F> 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<Character> 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<Character>();
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user