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

Commit d808279

Browse files
committed
Лабораторная работа №21-30
1 parent 8b4c066 commit d808279

File tree

32 files changed

+900
-0
lines changed

32 files changed

+900
-0
lines changed

students/23K9006/23K9006-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>23K9006</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K9006-p21</artifactId>
12+
<description>двадцать первое задание</description>
13+
</project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package ru.mirea.practice.s0000001;
2+
3+
//второй main
4+
5+
public class GenericArray<T> {
6+
private T[] array;
7+
8+
public GenericArray(T[] array) {
9+
this.array = array;
10+
}
11+
12+
public T get(int index) {
13+
if (index < 0 || index >= array.length) {
14+
throw new IndexOutOfBoundsException("Invalid index: " + index);
15+
}
16+
return array[index];
17+
}
18+
19+
public void set(int index, T value) {
20+
if (index < 0 || index >= array.length) {
21+
throw new IndexOutOfBoundsException("Invalid index: " + index);
22+
}
23+
array[index] = value;
24+
}
25+
26+
public int size() {
27+
return array.length;
28+
}
29+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package ru.mirea.practice.s0000001;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
abstract class Main1 {
7+
public static <T> List<T> convertArrayToList(T[] array) {
8+
return Arrays.asList(array);
9+
}
10+
11+
public static void main(String[] args) {
12+
String[] stringArray = {"one", "two", "three"};
13+
Integer[] intArray = {1, 2, 3};
14+
15+
List<String> stringList = convertArrayToList(stringArray);
16+
List<Integer> intList = convertArrayToList(intArray);
17+
18+
System.out.println(stringList);
19+
System.out.println(intList);
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package ru.mirea.practice.s0000001;
2+
3+
abstract class Main2 {
4+
public static void main(String[] args) {
5+
Integer[] intArray = {1, 2, 3, 4, 5};
6+
GenericArray<Integer> genericIntArray = new GenericArray<>(intArray);
7+
8+
System.out.println("Элемент по индексу 2: " + genericIntArray.get(2));
9+
genericIntArray.set(2, 10);
10+
System.out.println("Обновленный элемент по индексу 2: " + genericIntArray.get(2));
11+
System.out.println("Размер массива: " + genericIntArray.size());
12+
13+
String[] stringArray = {"A", "B", "C"};
14+
GenericArray<String> genericStringArray = new GenericArray<>(stringArray);
15+
16+
System.out.println("Элемент по индексу 0: " + genericStringArray.get(0));
17+
genericStringArray.set(0, "Z");
18+
System.out.println("Обновленный элемент по индексу 0: " + genericStringArray.get(0));
19+
System.out.println("Размер массива: " + genericStringArray.size());
20+
}
21+
}

students/23K9006/23K9006-p22/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>23K9006</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K9006-p22</artifactId>
12+
<description>двадцать второе задание</description>
13+
</project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package ru.mirea.practice.s0000001;
2+
3+
abstract class Main1 {
4+
public static void main(String[] args) {
5+
String rpnExpression = "3 4 + 2 * 7 /";
6+
double result = RPnCalculator.calculate(rpnExpression);
7+
System.out.println("Результат: " + result);
8+
}
9+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package ru.mirea.practice.s0000001;
2+
3+
import java.util.Stack;
4+
5+
abstract class RPnCalculator {
6+
public static double calculate(String expression) {
7+
Stack<Double> stack = new Stack<>();
8+
String[] tokens = expression.split("\\s+");
9+
10+
for (String token : tokens) {
11+
if (isOperator(token)) {
12+
double b = stack.pop();
13+
double a = stack.pop();
14+
stack.push(applyOperation(a, b, token));
15+
} else {
16+
stack.push(Double.parseDouble(token));
17+
}
18+
}
19+
20+
return stack.pop();
21+
}
22+
23+
private static boolean isOperator(String token) {
24+
return "+".equals(token) || "-".equals(token) || "*".equals(token) || "/".equals(token);
25+
}
26+
27+
private static double applyOperation(double a, double b, String operator) {
28+
switch (operator) {
29+
case "+":
30+
return a + b;
31+
case "-":
32+
return a - b;
33+
case "*":
34+
return a * b;
35+
case "/":
36+
if (b == 0) {
37+
throw new ArithmeticException("Деление на ноль");
38+
}
39+
return a / b;
40+
default:
41+
throw new IllegalArgumentException("Недопустимый оператор: " + operator);
42+
}
43+
}
44+
}

students/23K9006/23K9006-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>23K9006</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K9006-p23</artifactId>
12+
<description>двадцать третье задание</description>
13+
</project>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package ru.mirea.practice.s0000001;
2+
3+
public class ArrayQueue {
4+
private Object[] elements = new Object[10];
5+
private int head = 0;
6+
private int tail = 0;
7+
private int size = 0;
8+
9+
public void enqueue(Object element) {
10+
ensureCapacity(size + 1);
11+
elements[tail] = element;
12+
tail = (tail + 1) % elements.length;
13+
size++;
14+
}
15+
16+
public Object element() {
17+
if (isEmpty()) {
18+
throw new IllegalStateException("Queue is empty");
19+
}
20+
return elements[head];
21+
}
22+
23+
public Object dequeue() {
24+
if (isEmpty()) {
25+
throw new IllegalStateException("Queue is empty");
26+
}
27+
final Object result = elements[head];
28+
elements[head] = null;
29+
head = (head + 1) % elements.length;
30+
size--;
31+
return result;
32+
}
33+
34+
public int size() {
35+
return size;
36+
}
37+
38+
public boolean isEmpty() {
39+
return size == 0;
40+
}
41+
42+
public void clear() {
43+
elements = new Object[10];
44+
head = 0;
45+
tail = 0;
46+
size = 0;
47+
}
48+
49+
private void ensureCapacity(int capacity) {
50+
if (capacity > elements.length) {
51+
Object[] newArray = new Object[2 * elements.length];
52+
for (int i = 0; i < size; i++) {
53+
newArray[i] = elements[(head + i) % elements.length];
54+
}
55+
elements = newArray;
56+
head = 0;
57+
tail = size;
58+
}
59+
}
60+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package ru.mirea.practice.s0000001;
2+
3+
public class ArrayQueueAdt {
4+
private Object[] elements = new Object[10];
5+
private int head = 0;
6+
private int tail = 0;
7+
private int size = 0;
8+
9+
public static void enqueue(ArrayQueueAdt queue, Object element) {
10+
ensureCapacity(queue, queue.size + 1);
11+
queue.elements[queue.tail] = element;
12+
queue.tail = (queue.tail + 1) % queue.elements.length;
13+
queue.size++;
14+
}
15+
16+
public static Object element(ArrayQueueAdt queue) {
17+
if (isEmpty(queue)) {
18+
throw new IllegalStateException("Queue is empty");
19+
}
20+
return queue.elements[queue.head];
21+
}
22+
23+
public static Object dequeue(ArrayQueueAdt queue) {
24+
if (isEmpty(queue)) {
25+
throw new IllegalStateException("Queue is empty");
26+
}
27+
final Object result = queue.elements[queue.head];
28+
queue.elements[queue.head] = null;
29+
queue.head = (queue.head + 1) % queue.elements.length;
30+
queue.size--;
31+
return result;
32+
}
33+
34+
public static int size(ArrayQueueAdt queue) {
35+
return queue.size;
36+
}
37+
38+
public static boolean isEmpty(ArrayQueueAdt queue) {
39+
return queue.size == 0;
40+
}
41+
42+
public static void clear(ArrayQueueAdt queue) {
43+
queue.elements = new Object[10];
44+
queue.head = 0;
45+
queue.tail = 0;
46+
queue.size = 0;
47+
}
48+
49+
private static void ensureCapacity(ArrayQueueAdt queue, int capacity) {
50+
if (capacity > queue.elements.length) {
51+
Object[] newArray = new Object[2 * queue.elements.length];
52+
for (int i = 0; i < queue.size; i++) {
53+
newArray[i] = queue.elements[(queue.head + i) % queue.elements.length];
54+
}
55+
queue.elements = newArray;
56+
queue.head = 0;
57+
queue.tail = queue.size;
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)