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

Commit 07da61f

Browse files
committed
Лабораторные работы 21-29
1 parent 44c31f5 commit 07da61f

File tree

41 files changed

+1084
-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

+1084
-0
lines changed

students/23K0116/23K0116-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>23K0116</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0116-p21</artifactId>
12+
<description>Стирание типов в Джава </description>
13+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package ru.mirea.practice.s23k0116;
2+
3+
public final class Main {
4+
5+
private Main() {
6+
7+
}
8+
9+
public static void main(String[] args) {
10+
System.out.println("Двадцать первая практическая работа!");
11+
}
12+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package ru.mirea.practice.s23k0116.task1;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
public abstract class ArrayToListConverter {
8+
9+
// Метод для конвертации массива в список
10+
public static <T> List<T> convertArrayToList(T[] array) {
11+
return new ArrayList<>(Arrays.asList(array));
12+
}
13+
14+
public static void main(String[] args) {
15+
// Пример для строк
16+
String[] stringArray = {"apple", "banana", "cherry"};
17+
List<String> stringList = convertArrayToList(stringArray);
18+
System.out.println("Список строк: " + stringList);
19+
20+
// Пример для чисел
21+
Integer[] intArray = {1, 2, 3, 4, 5};
22+
List<Integer> intList = convertArrayToList(intArray);
23+
System.out.println("Список чисел: " + intList);
24+
}
25+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package ru.mirea.practice.s23k0116.task2and3;
2+
3+
import java.util.Arrays;
4+
5+
public class GenericArray<T> {
6+
private T[] array;
7+
8+
public GenericArray(T[] array) {
9+
this.array = array;
10+
}
11+
12+
// Получить элемент по индексу
13+
public T get(int index) {
14+
if (index < 0 || index >= array.length) {
15+
throw new IndexOutOfBoundsException("Индекс вне допустимого диапазона");
16+
}
17+
return array[index];
18+
}
19+
20+
// Получить длину массива
21+
public int length() {
22+
return array.length;
23+
}
24+
25+
// Получить строковое представление массива
26+
@Override
27+
public String toString() {
28+
return Arrays.toString(array);
29+
}
30+
31+
public static void main(String[] args) {
32+
// Пример для массива целых чисел
33+
Integer[] intArray = {1, 2, 3, 4, 5};
34+
GenericArray<Integer> genericIntArray = new GenericArray<>(intArray);
35+
System.out.println("Массив целых чисел: " + genericIntArray);
36+
System.out.println("Элемент с индексом 2: " + genericIntArray.get(2));
37+
38+
// Пример для массива строк
39+
String[] stringArray = {"apple", "banana", "cherry"};
40+
GenericArray<String> genericStringArray = new GenericArray<>(stringArray);
41+
System.out.println("Массив строк: " + genericStringArray);
42+
System.out.println("Элемент с индексом 1: " + genericStringArray.get(1));
43+
}
44+
}

students/23K0116/23K0116-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>23K0116</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0116-p22</artifactId>
12+
<description>Абстрактные типы данных. Стек</description>
13+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package ru.mirea.practice.s23k0116;
2+
3+
public final class Main {
4+
5+
private Main() {
6+
7+
}
8+
9+
public static void main(String[] args) {
10+
System.out.println("Двадцать вторая практическая работа!");
11+
}
12+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package ru.mirea.practice.s23k0116.task1;
2+
3+
import java.util.Stack;
4+
5+
public abstract class Calculator {
6+
7+
// Метод для вычисления выражения в обратной польской нотации
8+
public static double evaluate(String expression) {
9+
Stack<Double> stack = new Stack<>();
10+
String[] tokens = expression.split(" ");
11+
12+
for (String token : tokens) {
13+
switch (token) {
14+
case "+":
15+
stack.push(stack.pop() + stack.pop());
16+
break;
17+
case "-":
18+
double b = stack.pop();
19+
double a = stack.pop();
20+
stack.push(a - b);
21+
break;
22+
case "*":
23+
stack.push(stack.pop() * stack.pop());
24+
break;
25+
case "/":
26+
double divisor = stack.pop();
27+
double dividend = stack.pop();
28+
if (divisor == 0) {
29+
throw new ArithmeticException("Деление на ноль");
30+
}
31+
stack.push(dividend / divisor);
32+
break;
33+
default:
34+
// Попробуем распарсить число
35+
try {
36+
stack.push(Double.parseDouble(token));
37+
} catch (NumberFormatException e) {
38+
throw new IllegalArgumentException("Некорректный токен: " + token, e);
39+
}
40+
break;
41+
}
42+
}
43+
44+
if (stack.size() != 1) {
45+
throw new IllegalArgumentException("Некорректное выражение");
46+
}
47+
48+
return stack.pop();
49+
}
50+
51+
// Пример использования
52+
public static void main(String[] args) {
53+
String rpnExpression = "3 4 + 2 * 7 /"; // (3 + 4) * 2 / 7
54+
try {
55+
double result = evaluate(rpnExpression);
56+
System.out.println("Результат выражения '" + rpnExpression + "': " + result);
57+
} catch (Exception e) {
58+
System.out.println("Ошибка вычисления: " + e.getMessage());
59+
}
60+
61+
// Дополнительный пример
62+
String anotherExpression = "5 1 2 + 4 * + 3 -"; // 5 + ((1 + 2) * 4) - 3
63+
try {
64+
double result = evaluate(anotherExpression);
65+
System.out.println("Результат выражения '" + anotherExpression + "': " + result);
66+
} catch (Exception e) {
67+
System.out.println("Ошибка вычисления: " + e.getMessage());
68+
}
69+
}
70+
}

students/23K0116/23K0116-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>23K0116</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0116-p23</artifactId>
12+
<description> Абстрактные типы данных. Очередь</description>
13+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package ru.mirea.practice.s23k0116;
2+
3+
public final class Main {
4+
5+
private Main() {
6+
7+
}
8+
9+
public static void main(String[] args) {
10+
System.out.println("Двадцать третья практическая работа!");
11+
}
12+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package ru.mirea.practice.s23k0116.task1;
2+
3+
public class ArrayQueue {
4+
private Object[] queue;
5+
private int size;
6+
private int head;
7+
private int tail;
8+
9+
public ArrayQueue(int capacity) {
10+
queue = new Object[capacity];
11+
size = head = tail = 0;
12+
}
13+
14+
public void enqueue(Object element) {
15+
assert element != null : "Элемент не может быть null";
16+
ensureCapacity(size + 1);
17+
queue[tail] = element;
18+
tail = (tail + 1) % queue.length;
19+
size++;
20+
}
21+
22+
public Object element() {
23+
assert size > 0 : "Очередь пуста";
24+
return queue[head];
25+
}
26+
27+
public Object dequeue() {
28+
assert size > 0 : "Очередь пуста";
29+
final Object result = queue[head];
30+
queue[head] = null;
31+
head = (head + 1) % queue.length;
32+
size--;
33+
return result;
34+
}
35+
36+
public int size() {
37+
return size;
38+
}
39+
40+
public boolean isEmpty() {
41+
return size == 0;
42+
}
43+
44+
public void clear() {
45+
queue = new Object[queue.length];
46+
size = head = tail = 0;
47+
}
48+
49+
private void ensureCapacity(int capacity) {
50+
if (capacity > queue.length) {
51+
Object[] newQueue = new Object[queue.length * 2];
52+
for (int i = 0; i < size; i++) {
53+
newQueue[i] = queue[(head + i) % queue.length];
54+
}
55+
queue = newQueue;
56+
head = 0;
57+
tail = size;
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)