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

Лабораторная 21-32 #669

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions students/23K0130/23K0130-p21/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>23K0130</artifactId>
<groupId>ru.mirea.practice</groupId>
<version>2024.1</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>23K0130-p21</artifactId>
<description>21 задание</description>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ru.mirea.practice.s23k0130;

public final class Main {

private Main() {

}

public static void main(String[] args) {
System.out.println("21 практическая работа");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ru.mirea.practice.s23k0130.task1;

import java.util.ArrayList;
import java.util.List;

public abstract class Main {
public static <T> List<T> arrayToArrayList(T[] array) {
List<T> list = new ArrayList<>();
for (T element : array) {
list.add(element);
}
return list;
}

public static void main(String[] args) {
Integer[] arrayInt = {0, 1, 0, 2, 0, 6};
List<Integer> listInt = arrayToArrayList(arrayInt);
System.out.println(listInt);

String[] arrayStr = {"square", "triangle", "rectangle", "circle"};
List<String> listStr = arrayToArrayList(arrayStr);
System.out.println(listStr);

}
}
13 changes: 13 additions & 0 deletions students/23K0130/23K0130-p22/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>23K0130</artifactId>
<groupId>ru.mirea.practice</groupId>
<version>2024.1</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>23K0130-p22</artifactId>
<description>22 задание</description>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ru.mirea.practice.s23k0130;

public final class Main {

private Main() {

}

public static void main(String[] args) {
System.out.println("22 практическая работа");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ru.mirea.practice.s23k0130.task1;

abstract class Main {
public static void main(String[] args) {
String rpnExpression = "3 9 + 4 * 2 /";
double result = RPncalculator.calculate(rpnExpression);
System.out.println("Результат: " + result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package ru.mirea.practice.s23k0130.task1;

import java.util.Stack;

abstract class RPncalculator {
private static double applyOperation(double a, double b, String operator) {
switch (operator) {
case "+":
return a + b;
case "-":
return a - b;
case "*":
return a * b;
case "/":
if (b == 0) {
throw new ArithmeticException("Деление на ноль");
}
return a / b;
default:
throw new IllegalArgumentException("Недопустимый оператор: " + operator);
}
}

private static boolean isOperator(String token) {
return "+".equals(token) || "-".equals(token) || "*".equals(token) || "/".equals(token);
}

public static double calculate(String expression) {
Stack<Double> stack = new Stack<>();
String[] tokens = expression.split("\\s+");
for (String token : tokens) {
if (isOperator(token)) {
double b = stack.pop();
double a = stack.pop();
stack.push(applyOperation(a, b, token));
} else {
stack.push(Double.parseDouble(token));
}
}
return stack.pop();
}
}
13 changes: 13 additions & 0 deletions students/23K0130/23K0130-p23/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>23K0130</artifactId>
<groupId>ru.mirea.practice</groupId>
<version>2024.1</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>23K0130-p23</artifactId>
<description>23 задание</description>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ru.mirea.practice.s23k0130;

public final class Main {

private Main() {

}

public static void main(String[] args) {
System.out.println("23 практическая работа");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package ru.mirea.practice.s23k0130.task1;

public final class ArrayQueue {

private Object[] queue = new Object[16];
private int head = 0;
private int tail = 0;
private int size = 0;

public static ArrayQueue createQueue() {
return new ArrayQueue();
}

public void enqueue(Object element) {
ensureCapacity(size + 1);
queue[tail] = element;
tail = (tail + 1) % queue.length;
size++;
}

public Object element() {
return queue[head];
}

public Object dequeue() {
Object result;
result = queue[head];
queue[head] = null;
head = (head + 1) % queue.length;
size--;
return result;
}

public int size() {
return size;
}

public boolean isEmpty() {
return size == 0;
}

public void clear() {
queue = new Object[16];
head = tail = size = 0;
}

private void ensureCapacity(int capacity) {
if (capacity > queue.length) {
Object[] newQueue = new Object[queue.length * 2];
for (int i = 0; i < size; i++) {
newQueue[i] = queue[(head + i) % queue.length];
}
queue = newQueue;
head = 0;
tail = size;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package ru.mirea.practice.s23k0130.task1;

public final class ArrayQueueADt {

private Object[] queue = new Object[16];
private int head = 0;
private int tail = 0;
private int size = 0;

public static void enqueue(ArrayQueueADt queueadt, Object element) {
ensureCapacity(queueadt, queueadt.size + 1);
queueadt.queue[queueadt.tail] = element;
queueadt.tail = (queueadt.tail + 1) % queueadt.queue.length;
queueadt.size++;
}

public static Object element(ArrayQueueADt queueadt) {
return queueadt.queue[queueadt.head];
}

public static Object dequeue(ArrayQueueADt queueadt) {
Object result;
result = queueadt.queue[queueadt.head];
queueadt.queue[queueadt.head] = null;
queueadt.head = (queueadt.head + 1) % queueadt.queue.length;
queueadt.size--;
return result;
}

public static int size(ArrayQueueADt queueadt) {
return queueadt.size;
}

public static boolean isEmpty(ArrayQueueADt queueadt) {
return queueadt.size == 0;
}

public static void clear(ArrayQueueADt queueadt) {
queueadt.queue = new Object[16];
queueadt.head = queueadt.tail = queueadt.size = 0;
}

private static void ensureCapacity(ArrayQueueADt queueadt, int capacity) {
if (capacity > queueadt.queue.length) {
Object[] newQueue = new Object[queueadt.queue.length * 2];
for (int i = 0; i < queueadt.size; i++) {
newQueue[i] = queueadt.queue[(queueadt.head + i) % queueadt.queue.length];
}
queueadt.queue = newQueue;
queueadt.head = 0;
queueadt.tail = queueadt.size;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package ru.mirea.practice.s23k0130.task1;

public final class ArrayQueueModule {

private ArrayQueueModule() {
}

private static final int INITIAL_CAPACITY = 16;
private static Object[] queue = new Object[INITIAL_CAPACITY];
private static int head = 0;
private static int tail = 0;
private static int size = 0;

public static void enqueue(Object element) {
ensureCapacity(size + 1);
queue[tail] = element;
tail = (tail + 1) % queue.length;
size++;
}

public static Object element() {
return queue[head];
}

public static Object dequeue() {
Object result;
result = queue[head];
queue[head] = null;
head = (head + 1) % queue.length;
size--;
return result;
}

public static int size() {
return size;
}

public static boolean isEmpty() {
return size == 0;
}

public static void clear() {
queue = new Object[INITIAL_CAPACITY];
head = tail = size = 0;
}

private static void ensureCapacity(int capacity) {
if (capacity > queue.length) {
Object[] newQueue = new Object[queue.length * 2];
for (int i = 0; i < size; i++) {
newQueue[i] = queue[(head + i) % queue.length];
}
queue = newQueue;
head = 0;
tail = size;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package ru.mirea.practice.s23k0130.task1;

public final class Main {

private Main() {
}

public static void main(String[] args) {
System.out.println("Тестирование ArrayQueueModule:");
testarrayqueuemodule();

System.out.println("\nТестирование ArrayQueueADt:");
testarrayaueueadt();

System.out.println("\nТестирование ArrayQueue:");
testarrayqueue();
}

private static void testarrayqueuemodule() {
ArrayQueueModule.enqueue(0);
ArrayQueueModule.enqueue(1);
System.out.println("Элементы добавлены: 0, 1");
System.out.println("Первый элемент: " + ArrayQueueModule.element());
System.out.println("Удалён первый элемент: " + ArrayQueueModule.dequeue());
System.out.println("Текущий размер: " + ArrayQueueModule.size());
System.out.println("Очередь пуста? " + ArrayQueueModule.isEmpty());
ArrayQueueModule.clear();
System.out.println("Очередь очищена.");
System.out.println("Очередь пуста? " + ArrayQueueModule.isEmpty());
}

private static void testarrayaueueadt() {
ArrayQueueADt queueadt = new ArrayQueueADt();
ArrayQueueADt.enqueue(queueadt, 0);
ArrayQueueADt.enqueue(queueadt, 2);
System.out.println("Элементы добавлены: 0, 2");
System.out.println("Первый элемент: " + ArrayQueueADt.element(queueadt));
System.out.println("Удалён первый элемент: " + ArrayQueueADt.dequeue(queueadt));
System.out.println("Текущий размер: " + ArrayQueueADt.size(queueadt));
System.out.println("Очередь пуста? " + ArrayQueueADt.isEmpty(queueadt));
ArrayQueueADt.clear(queueadt);
System.out.println("Очередь очищена.");
System.out.println("Очередь пуста? " + ArrayQueueADt.isEmpty(queueadt));
}

private static void testarrayqueue() {
ArrayQueue queue = new ArrayQueue();
queue.enqueue(0);
queue.enqueue(6);
System.out.println("Элементы добавлены: 0, 6");
System.out.println("Первый элемент: " + queue.element());
System.out.println("Удалён первый элемент: " + queue.dequeue());
System.out.println("Текущий размер: " + queue.size());
System.out.println("Очередь пуста? " + queue.isEmpty());
queue.clear();
System.out.println("Очередь очищена.");
System.out.println("Очередь пуста? " + queue.isEmpty());
}
}
Loading
Loading