Skip to content
This repository was archived by the owner on Dec 28, 2024. It is now read-only.

Commit 3e28c14

Browse files
authored
Merge pull request #647 from MiNareee/main
Лаба 21-30, 32
2 parents 1aee80c + 54e56ba commit 3e28c14

File tree

41 files changed

+1009
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1009
-0
lines changed

students/23K0164/23K0164-p21/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<artifactId>23K0164</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0164-p21</artifactId>
12+
<description>Лаба 21</description>
13+
</project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package ru.mirea.practice.s23k0164.t1;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public final class First {
7+
private First() {
8+
}
9+
10+
public static <T> List<T> convertArrayToList(T[] array) {
11+
List<T> list = new ArrayList<>();
12+
for (T element : array) {
13+
list.add(element);
14+
}
15+
return list;
16+
}
17+
18+
public static void main(String[] args) {
19+
String[] stringArray = {"A", "B", "C"};
20+
List<String> stringList = convertArrayToList(stringArray);
21+
System.out.println(stringList);
22+
23+
Integer[] intArray = {1, 2, 3};
24+
List<Integer> intList = convertArrayToList(intArray);
25+
System.out.println(intList);
26+
}
27+
}
28+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package ru.mirea.practice.s23k0164.t2;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class GenericArray<T> {
7+
private List<T> elements;
8+
9+
public GenericArray() {
10+
elements = new ArrayList<>();
11+
}
12+
13+
public void add(T element) {
14+
elements.add(element);
15+
}
16+
17+
public T get(int index) {
18+
if (index < 0 || index >= elements.size()) {
19+
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + elements.size());
20+
}
21+
return elements.get(index);
22+
}
23+
24+
public int size() {
25+
return elements.size();
26+
}
27+
28+
@Override
29+
public String toString() {
30+
return elements.toString();
31+
}
32+
33+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package ru.mirea.practice.s23k0164.t2;
2+
3+
public final class Two {
4+
private Two() {
5+
6+
}
7+
8+
public static void main(String[] args) {
9+
10+
GenericArray<Integer> intArray = new GenericArray<>();
11+
intArray.add(1);
12+
intArray.add(2);
13+
intArray.add(3);
14+
System.out.println("Integer Array: " + intArray);
15+
16+
17+
GenericArray<String> stringArray = new GenericArray<>();
18+
stringArray.add("Hello");
19+
stringArray.add("World");
20+
System.out.println("String Array: " + stringArray);
21+
22+
23+
GenericArray<Double> doubleArray = new GenericArray<>();
24+
doubleArray.add(3.14);
25+
doubleArray.add(2.71);
26+
System.out.println("Double Array: " + doubleArray);
27+
28+
29+
GenericArray<Object> mixedArray = new GenericArray<>();
30+
mixedArray.add(100);
31+
mixedArray.add("Test");
32+
mixedArray.add(3.14159);
33+
System.out.println("Mixed Array: " + mixedArray);
34+
}
35+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<artifactId>23K0164</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0164-p22_001</artifactId>
12+
<description>Лаба 22</description>
13+
</project>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package ru.mirea.practice.s23k0164.t1;
2+
3+
import java.util.Stack;
4+
5+
public final class RpnCalculator {
6+
private RpnCalculator() {
7+
8+
}
9+
10+
public static void main(String[] args) {
11+
String expression = "3 4 + 2 * 7 /";
12+
13+
try {
14+
double result = evaluateRpn(expression);
15+
System.out.println("Result: " + result);
16+
} catch (Exception e) {
17+
System.err.println("Error: " + e.getMessage());
18+
}
19+
}
20+
21+
public static double evaluateRpn(String expression) throws Exception {
22+
Stack<Double> stack = new Stack<>();
23+
String[] tokens = expression.split("\\s+");
24+
25+
for (String token : tokens) {
26+
if (isNumeric(token)) {
27+
stack.push(Double.parseDouble(token));
28+
} else {
29+
if (stack.size() < 2) {
30+
throw new Exception("Invalid expression: insufficient values in stack.");
31+
}
32+
double b = stack.pop();
33+
double a = stack.pop();
34+
double result = performOperation(a, b, token);
35+
stack.push(result);
36+
}
37+
}
38+
39+
if (stack.size() != 1) {
40+
throw new Exception("Invalid expression: too many values in stack.");
41+
}
42+
return stack.pop();
43+
}
44+
45+
private static boolean isNumeric(String str) {
46+
try {
47+
Double.parseDouble(str);
48+
return true;
49+
} catch (NumberFormatException e) {
50+
return false;
51+
}
52+
}
53+
54+
private static double performOperation(double a, double b, String operator) throws Exception {
55+
switch (operator) {
56+
case "+":
57+
return a + b;
58+
case "-":
59+
return a - b;
60+
case "*":
61+
return a * b;
62+
case "/":
63+
if (b == 0) {
64+
throw new Exception("Division by zero.");
65+
}
66+
return a / b;
67+
default:
68+
throw new Exception("Invalid operator: " + operator);
69+
}
70+
}
71+
}

students/23K0164/23K0164-p23/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<artifactId>23K0164</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0164-p23</artifactId>
12+
<description>Лаба 23</description>
13+
</project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package ru.mirea.practice.s23k0164.t1;
2+
3+
public abstract class AbstractQueue<T> implements Queue<T> {
4+
5+
protected int size;
6+
7+
@Override
8+
public boolean isEmpty() {
9+
return size == 0;
10+
}
11+
12+
@Override
13+
public int size() {
14+
return size;
15+
}
16+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package ru.mirea.practice.s23k0164.t1;
2+
3+
public class LinkedQueue<T> implements Queue<T> {
4+
private Node<T> head;
5+
private Node<T> tail;
6+
private int size;
7+
8+
public LinkedQueue() {
9+
head = null;
10+
tail = null;
11+
size = 0;
12+
}
13+
14+
@Override
15+
public void enqueue(T element) {
16+
Node<T> newNode = new Node<>(element);
17+
if (tail != null) {
18+
tail.next = newNode;
19+
}
20+
tail = newNode;
21+
if (head == null) {
22+
head = newNode;
23+
}
24+
size++;
25+
}
26+
27+
@Override
28+
public T dequeue() {
29+
if (isEmpty()) {
30+
throw new IllegalStateException("Queue is empty");
31+
}
32+
final T data = head.data;
33+
head = head.next;
34+
if (head == null) {
35+
tail = null;
36+
}
37+
size--;
38+
return data;
39+
}
40+
41+
@Override
42+
public T peek() {
43+
if (isEmpty()) {
44+
throw new IllegalStateException("Queue is empty");
45+
}
46+
return head.data;
47+
}
48+
49+
@Override
50+
public boolean isEmpty() {
51+
return size == 0;
52+
}
53+
54+
@Override
55+
public int size() {
56+
return size;
57+
}
58+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package ru.mirea.practice.s23k0164.t1;
2+
3+
class Node<T> {
4+
T data;
5+
Node<T> next;
6+
7+
Node(T data) {
8+
this.data = data;
9+
this.next = null;
10+
}
11+
}

0 commit comments

Comments
 (0)