Joined projects

This commit is contained in:
Gašper Dobrovoljc
2024-11-29 13:11:09 +01:00
commit 74a510905e
24 changed files with 3536 additions and 0 deletions

29
naloge/naloga1/.gitignore vendored Normal file
View File

@@ -0,0 +1,29 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

8
naloge/naloga1/.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@@ -0,0 +1,16 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="ExpressionComparedToItself" enabled="true" level="WARNING" enabled_by_default="true">
<option name="ignoreSideEffectConditions" value="true" />
</inspection_tool>
<inspection_tool class="PyUnresolvedReferencesInspection" enabled="true" level="WARNING" enabled_by_default="true">
<option name="ignoredIdentifiers">
<list>
<option value="tests.smoke.test_absences_sliding.TestAbsencesSliding.*" />
</list>
</option>
</inspection_tool>
<inspection_tool class="UNCHECKED_WARNING" enabled="false" level="WARNING" enabled_by_default="false" />
</profile>
</component>

6
naloge/naloga1/.idea/misc.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_23" default="true" project-jdk-name="homebrew-23" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
naloge/naloga1/.idea/modules.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/naloga1.iml" filepath="$PROJECT_DIR$/naloga1.iml" />
</modules>
</component>
</project>

6
naloge/naloga1/.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

View 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>

View File

@@ -0,0 +1,522 @@
import java.util.Scanner;
public class Naloga1 {
public static void main(String[] args) {
Calculator calculator;
try {
calculator = new Calculator();
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) {
calculator.reset();
String line = scanner.nextLine();
calculator.run(line);
}
} catch (CollectionException e) {
System.out.println(e.getMessage());
}
}
}
class Calculator {
Sequence<Stack<String>> stacks = new ArrayDeque<>();
Stack<String> mainStack, currStack;
boolean condition;
int copyCount = 0;
Calculator() throws CollectionException {
reset();
}
void reset() throws CollectionException {
stacks.clear();
for (int i = 0; i < 42; i++) {
stacks.add(new ArrayDeque<>());
}
mainStack = stacks.get(0);
currStack = null;
copyCount = 0;
condition = false;
}
void run(String input) throws CollectionException {
run(input.split("\\s+"));
}
void run(String[] tokens) throws CollectionException {
for (String token : tokens) {
runToken(token);
}
}
void runToken(String token) throws CollectionException {
if (copyCount > 0) {
currStack.push(token);
copyCount--;
return;
}
if (token.startsWith("?")) {
if (!condition) {
return;
}
token = token.substring(1);
}
switch (token) {
case "echo":
echo();
break;
case "pop":
pop();
break;
case "dup":
dup();
break;
case "dup2":
dup2();
break;
case "swap":
swap();
break;
case "char":
charr();
break;
case "even":
even();
break;
case "odd":
odd();
break;
case "!":
factorial();
break;
case "len":
len();
break;
case "<>":
neq();
break;
case "<":
lt();
break;
case "<=":
lte();
break;
case "==":
eq();
break;
case ">":
gt();
break;
case ">=":
gte();
break;
case "+":
add();
break;
case "-":
sub();
break;
case "*":
mul();
break;
case "/":
div();
break;
case "%":
mod();
break;
case ".":
concat();
break;
case "rnd":
rnd();
break;
case "then":
then();
break;
case "else":
elsee();
break;
case "print":
print();
break;
case "clear":
clear();
break;
case "run":
run();
break;
case "loop":
loop();
break;
case "fun":
fun();
break;
case "move":
move();
break;
case "reverse":
reverse();
break;
default:
mainStack.push(token);
break;
}
}
int popInt() throws CollectionException {
return Integer.parseInt(mainStack.pop());
}
void pushInt(int value) throws CollectionException {
mainStack.push(String.valueOf(value));
}
Stack<String> cloneStack(Stack<String> stack) {
return new ArrayDeque<>((ArrayDeque<String>) stack);
}
Stack<String> getStack() throws CollectionException {
return stacks.get(Integer.parseInt(mainStack.pop()));
}
void echo() throws CollectionException {
if (mainStack.isEmpty()) {
System.out.println();
return;
}
System.out.println(mainStack.top());
}
void pop() throws CollectionException {
mainStack.pop();
}
void dup() throws CollectionException {
mainStack.push(mainStack.top());
}
void dup2() throws CollectionException {
String top = mainStack.pop();
String bottom = mainStack.top();
mainStack.push(top);
mainStack.push(bottom);
mainStack.push(top);
}
void swap() throws CollectionException {
String top = mainStack.pop();
String bottom = mainStack.pop();
mainStack.push(top);
mainStack.push(bottom);
}
void charr() throws CollectionException {
mainStack.push(Character.toString((char) popInt()));
}
void even() throws CollectionException {
pushInt(popInt() % 2 == 0 ? 1 : 0);
}
void odd() throws CollectionException {
pushInt(popInt() % 2 != 0 ? 1 : 0);
}
void factorial() throws CollectionException {
int top = popInt();
int factorial = 1;
while (top > 0) {
factorial *= top--;
}
pushInt(factorial);
}
void len() throws CollectionException {
mainStack.push(String.valueOf(mainStack.pop().length()));
}
void neq() throws CollectionException {
pushInt(mainStack.pop().equals(mainStack.pop()) ? 0 : 1);
}
void lt() throws CollectionException {
pushInt(popInt() > popInt() ? 1 : 0);
}
void lte() throws CollectionException {
pushInt(popInt() >= popInt() ? 1 : 0);
}
void eq() throws CollectionException {
String right = mainStack.pop();
String left = mainStack.pop();
pushInt(left.equals(right) ? 1 : 0);
}
void gt() throws CollectionException {
pushInt(popInt() < popInt() ? 1 : 0);
}
void gte() throws CollectionException {
pushInt(popInt() <= popInt() ? 1 : 0);
}
void add() throws CollectionException {
pushInt(popInt() + popInt());
}
void sub() throws CollectionException {
int right = popInt();
pushInt(popInt() - right);
}
void mul() throws CollectionException {
pushInt(popInt() * popInt());
}
void div() throws CollectionException {
int right = popInt();
pushInt(popInt() / right);
}
void mod() throws CollectionException {
int right = popInt();
pushInt(popInt() % right);
}
void concat() throws CollectionException {
String right = mainStack.pop();
mainStack.push(mainStack.pop() + right);
}
void rnd() throws CollectionException {
int y = popInt();
int x = popInt();
pushInt((int) (Math.random() * (y - x + 1) + x));
}
void then() throws CollectionException {
condition = popInt() != 0;
}
void elsee() {
condition = !condition;
}
void print() throws CollectionException {
System.out.println(getStack());
}
void clear() throws CollectionException {
getStack().clear();
}
void run() throws CollectionException {
Stack<String> stack = cloneStack(getStack());
stack.reverse();
while (!stack.isEmpty()) {
runToken(stack.pop());
}
}
void loop() throws CollectionException {
Stack<String> stack = cloneStack(getStack());
stack.reverse();
int loop = popInt();
for (int i = 0; i < loop; i++) {
Stack<String> runStack = cloneStack(stack);
while (!runStack.isEmpty()) {
runToken(runStack.pop());
}
}
}
void fun() throws CollectionException {
currStack = getStack();
copyCount = popInt();
}
void move() throws CollectionException {
Stack<String> stack = getStack();
int count = popInt();
for (int i = 0; i < count; i++) {
stack.push(mainStack.pop());
}
}
void reverse() throws CollectionException {
Stack<String> stack = getStack();
stack.reverse();
}
}
class CollectionException extends Exception {
public CollectionException(String msg) {
super(msg);
}
}
interface Collection {
String ERR_MSG_EMPTY = "Collection is empty.";
String ERR_MSG_FULL = "Collection is full.";
boolean isEmpty();
int size();
void clear();
void reverse();
String toString();
}
interface Stack<T> extends Collection {
T top() throws CollectionException;
void push(T value) throws CollectionException;
T pop() throws CollectionException;
}
interface Sequence<T> extends Collection {
String ERR_MSG_INDEX = "Wrong index in sequence.";
T get(int i) throws CollectionException;
void add(T x) throws CollectionException;
}
class ArrayDeque<T> implements Stack<T>, Sequence<T> {
private static final int DEFAULT_CAPACITY = 64;
private final T[] array;
private int front, back, size;
@SuppressWarnings("unchecked")
public ArrayDeque() {
array = (T[]) new Object[DEFAULT_CAPACITY];
front = back = size = 0;
}
@SuppressWarnings("unchecked")
public ArrayDeque(ArrayDeque<T> deque) {
array = (T[]) new Object[DEFAULT_CAPACITY];
System.arraycopy(deque.array, 0, array, 0, deque.array.length);
front = deque.front;
back = deque.back;
size = deque.size;
}
private int index(int i) {
return (front + i) % DEFAULT_CAPACITY;
}
private int next(int i) {
return (i + 1) % DEFAULT_CAPACITY;
}
private int prev(int i) {
return (i + DEFAULT_CAPACITY - 1) % DEFAULT_CAPACITY;
}
@Override
public T get(int i) throws CollectionException {
if (isEmpty()) {
throw new CollectionException(ERR_MSG_EMPTY);
}
if (i < 0 || i >= size) {
throw new CollectionException(ERR_MSG_INDEX);
}
return array[index(i)];
}
@Override
public void add(T x) throws CollectionException {
this.push(x);
}
@Override
public T top() throws CollectionException {
if (isEmpty()) {
throw new CollectionException(ERR_MSG_EMPTY);
}
return array[prev(back)];
}
@Override
public void push(T x) throws CollectionException {
if (isFull()) {
throw new CollectionException(ERR_MSG_FULL);
}
array[back] = x;
back = this.next(back);
size++;
}
@Override
public T pop() throws CollectionException {
if (isEmpty()) {
throw new CollectionException(ERR_MSG_EMPTY);
}
back = this.prev(back);
T x = array[back];
array[back] = null;
size--;
return x;
}
@Override
public boolean isEmpty() {
return size == 0;
}
private boolean isFull() {
return size == DEFAULT_CAPACITY;
}
@Override
public int size() {
return size;
}
@Override
public void clear() {
for (int i = 0; i < DEFAULT_CAPACITY; i++) {
array[i] = null;
}
front = back = size = 0;
}
@Override
public void reverse() {
@SuppressWarnings("unchecked")
T[] tmp = (T[]) new Object[DEFAULT_CAPACITY];
System.arraycopy(array, 0, tmp, 0, array.length);
for (int i = 0; i < array.length; i++) {
array[array.length - 1 - i] = tmp[i];
}
int tmpFront = front;
front = (array.length - back) % DEFAULT_CAPACITY;
back = (array.length - tmpFront) % DEFAULT_CAPACITY;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
if (size > 0) {
sb.append(array[front].toString());
}
for (int i = 0; i < size - 1; i++) {
sb.append(" ").append(array[this.next(front + i)].toString());
}
return sb.toString();
}
}