Joined projects

This commit is contained in:
Gašper Dobrovoljc 2024-11-29 13:11:09 +01:00
commit 74a510905e
No known key found for this signature in database
GPG Key ID: 0E7E037018CFA5A5
24 changed files with 3536 additions and 0 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

31
izzivi/.gitignore vendored Normal file
View File

@ -0,0 +1,31 @@
### 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
**.zip

8
izzivi/.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,12 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<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>
</profile>
</component>

6
izzivi/.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
izzivi/.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$/aps1.iml" filepath="$PROJECT_DIR$/aps1.iml" />
</modules>
</component>
</project>

124
izzivi/.idea/uiDesigner.xml generated Normal file
View File

@ -0,0 +1,124 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>

6
izzivi/.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="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

11
izzivi/aps1.iml Normal file
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,61 @@
package izziv1;
public class Izziv1 {
public static void main(String[] args) {
System.out.println(" n | linearno | dvojisko |");
System.out.println("---------+--------------+------------------");
for (int i = 20000; i <= 1000000; i += 20000) {
System.out.printf(" %d | %d | %d\n", i, timeLinear(i), timeBinary(i));
}
}
static int[] generateTable(int n) {
int[] table = new int[n];
for (int i = 0; i < n; i++) {
table[i] = i + 1;
}
return table;
}
static int findLinear(int[] a, int v) {
for (int i = 0; i < a.length; i++) {
if (a[i] == v) {
return i;
}
}
return -1;
}
static int findBinary(int[] a, int l, int r, int v) {
int center = l + (r - l) / 2;
if (a[center] == v) {
return center;
}
if (a[center] > v) {
return findBinary(a, l, center, v);
}
return findBinary(a, center + 1, r, v);
}
static long timeLinear(int n) {
int[] table = generateTable(n);
long startTime = System.nanoTime();
for (int i = 0; i < 1000; i++) {
int num = (int)(Math.random() * n);
int _ = findLinear(table, num);
}
return (System.nanoTime() - startTime) / 1000;
}
static long timeBinary(int n) {
int[] table = generateTable(n);
long startTime = System.nanoTime();
for (int i = 0; i < 1000; i++) {
int num = (int)(Math.random() * n) + 1;
int _ = findBinary(table, 0, n - 1, num);
}
return (System.nanoTime() - startTime) / 1000;
}
}

View File

@ -0,0 +1,23 @@
Zakaj so na časi pri vas drugačni kot v zgornji tabeli?
Ker sta procesorja različno zmogljiva in na računalnikih tečeju drugi procesi.
Kateri algoritem je hitrejši?
Binarni.
Kdaj bi lahko bil počasnejši algoritem hitrejši?
Če je iskan element na začetku tabele.
Kako je čas iskanja odvisen od velikosti naloge (linearno, kvadratno, ...)?
Linearno iskanje je linearno, binarno pa logaritemsko.
Je časovna odvisnost dvojiškega iskanja bližje linearni ali konstantni?
Konstantni.
Ali lahko napišemo boljši algoritem (za naš primer)?
Ja, ker so številke v tabeli od 1 do n, ne potrebujemo algoritma za iskanje ker je neka vrednost x na x-em mestu (4 je na 4. mestu, ...)
Katere so težave tovrstnega določanja časovne zahtevnosti?
Niso natančne.
Kako jih skušamo zaobiti?
Z večkratnim testiranjem in računanjem povprečja.

Binary file not shown.

After

Width:  |  Height:  |  Size: 65 KiB

View File

@ -0,0 +1,250 @@
package izziv2;
public class Izziv2 {
public static void main(String[] args) throws CollectionException {
System.out.println("Stack: ");
Stack<Integer> stack = new ArrayDeque<>();
int i = 0;
while (!stack.isFull()) {
stack.push(i++);
}
System.out.println(stack);
assert stack.top() == i;
while (!stack.isEmpty()) {
System.out.println(stack.pop());
}
System.out.println("Deque: ");
Deque<Integer> deque = new ArrayDeque<>();
// enqueue and dequeueBack are the same as push and pop
i = 0;
while (!deque.isFull()) {
deque.enqueueFront(i++);
}
System.out.println(deque);
assert deque.front() == i;
assert deque.back() == 0;
while (!deque.isEmpty()) {
System.out.println(deque.dequeue());
}
System.out.println("Sequence: ");
Sequence<Integer> sequence = new ArrayDeque<>();
i = 0;
while (!sequence.isFull()) {
sequence.add(i++);
}
System.out.println(sequence);
for (int j = 0; j < sequence.size(); j++) {
System.out.println(sequence.get(j));
}
}
}
class CollectionException extends Exception {
public CollectionException(String msg) {
super(msg);
}
}
interface Collection {
static final String ERR_MSG_EMPTY = "Collection is empty.";
static final String ERR_MSG_FULL = "Collection is full.";
boolean isEmpty();
boolean isFull();
int size();
String toString();
}
interface Stack<T> extends Collection {
T top() throws CollectionException;
void push(T x) throws CollectionException;
T pop() throws CollectionException;
}
interface Deque<T> extends Collection {
T front() throws CollectionException;
T back() throws CollectionException;
void enqueue(T x) throws CollectionException;
void enqueueFront(T x) throws CollectionException;
T dequeue() throws CollectionException;
T dequeueBack() throws CollectionException;
}
interface Sequence<T> extends Collection {
static final String ERR_MSG_INDEX = "Wrong index in sequence.";
T get(int i) throws CollectionException;
void add(T x) throws CollectionException;
}
class ArrayDeque<T> implements Deque<T>, Stack<T>, Sequence<T> {
private static final int DEFAULT_CAPACITY = 64;
private final T[] array;
private int front, back, size;
public ArrayDeque() {
//noinspection unchecked
array = (T[]) new Object[DEFAULT_CAPACITY];
front = back = size = 0;
}
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 front() throws CollectionException {
if (isEmpty()) {
throw new CollectionException(ERR_MSG_EMPTY);
}
return array[front];
}
@Override
public T back() throws CollectionException {
if (isEmpty()) {
throw new CollectionException(ERR_MSG_EMPTY);
}
return array[back];
}
@Override
public void enqueue(T x) throws CollectionException {
this.push(x);
}
@Override
public void enqueueFront(T x) throws CollectionException {
if (isFull()) {
throw new CollectionException(ERR_MSG_FULL);
}
front = this.prev(front);
size++;
array[front] = x;
}
@Override
public T dequeue() throws CollectionException {
if (isEmpty()) {
throw new CollectionException(ERR_MSG_EMPTY);
}
T x = array[front];
array[front] = null;
front = this.next(front);
size--;
return x;
}
@Override
public T dequeueBack() throws CollectionException {
return this.pop();
}
@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 {
return this.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;
}
@Override
public boolean isFull() {
return size == DEFAULT_CAPACITY;
}
@Override
public int size() {
return size;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
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());
}
sb.append("]");
return sb.toString();
}
}

View File

@ -0,0 +1,112 @@
package izziv3;
import java.util.ArrayList;
public class Izziv3 {
public static void main(String[] args) {
// int size = Integer.parseInt(args[0]);
int size = 20;
StdDraw.setCanvasSize(1500, 800);
StdDraw.setXscale(-1, size);
StdDraw.setYscale((int) (Math.log(size + 1) / Math.log(2)) + 1, -1);
CompleteBinaryTreeDrawer ctd = new CompleteBinaryTreeDrawer(size);
ctd.drawLevelorder();
}
}
class CompleteBinaryTreeDrawer {
char[] data;
int[] x, y;
ArrayList<Integer> inorder;
final int size;
public CompleteBinaryTreeDrawer(int size) {
this.size = size;
data = new char[size];
x = new int[size];
y = new int[size];
inorder = new ArrayList<>();
inorder(0);
for (int i = 0; i < size; i++) {
data[i] = (char) ('A' + i);
x[i] = inorder.indexOf(Integer.valueOf(i));
y[i] = (int) (Math.log(i + 1) / Math.log(2));
}
}
void inorder(int i) {
if (2 * i + 1 < size) {
inorder(i * 2 + 1);
}
inorder.add(Integer.valueOf(i));
if (2 * i + 2 < size) {
inorder(2 * i + 2);
}
}
void drawNode(int i) {
StdDraw.filledCircle(x[i], y[i], 0.1);
}
void drawEdgeToNode(int i) {
int o = (i - 1) / 2;
StdDraw.line(x[o], y[o], x[i], y[i]);
}
void drawLevelorder() {
drawNode(0);
for (int i = 0; i < size; i++) {
drawEdgeToNode(i);
drawNode(i);
}
}
void drawPreorder(int i) {
drawEdgeToNode(i);
drawNode(i);
if (2 * i + 1 < size) {
drawInorder(i * 2 + 1);
}
if (2 * i + 2 < size) {
drawInorder(2 * i + 2);
}
}
void drawInorder(int i) {
if (2 * i + 1 < size) {
drawInorder(i * 2 + 1);
}
drawEdgeToNode(i);
drawNode(i);
if (2 * i + 2 < size) {
drawInorder(2 * i + 2);
}
}
void drawPostorder(int i) {
if (2 * i + 1 < size) {
drawInorder(i * 2 + 1);
}
if (2 * i + 2 < size) {
drawInorder(2 * i + 2);
}
drawEdgeToNode(i);
drawNode(i);
}
int traverse(int i, int x, int y) {
if (2 * i + 1 < size) {
x = traverse(2 * i + 1, x, y + 1);
}
this.x[i] = x;
this.y[i] = y;
if (2 * i + 2 < size) {
x = traverse(2 * i + 2, x + 1, y + 1);
}
return x + 1;
}
}

File diff suppressed because it is too large Load Diff

BIN
naloge/.DS_Store vendored Normal file

Binary file not shown.

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();
}
}