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

Commit 2a4cb7d

Browse files
committed
Pr21-32
1 parent 4749714 commit 2a4cb7d

File tree

39 files changed

+799
-0
lines changed

39 files changed

+799
-0
lines changed

students/23K9901/23K9901-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>23K9901</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K9901-p21</artifactId>
12+
<description>Второе задание</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.s0000001;
2+
3+
public class ArrayElementGetter<T> {
4+
private T[] array;
5+
6+
public ArrayElementGetter(T[] array) {
7+
this.array = array;
8+
}
9+
10+
public T getElementByIndex(int index) {
11+
if (index < 0 || index >= array.length) {
12+
throw new IndexOutOfBoundsException("Invalid index: " + index);
13+
}
14+
return array[index];
15+
}
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package ru.mirea.practice.s0000001;
2+
3+
abstract class Main {
4+
public static void main(String[] args) {
5+
Integer[] numbers = {10, 20, 30, 40, 50};
6+
ArrayElementGetter<Integer> intGetter = new ArrayElementGetter<>(numbers);
7+
System.out.println("Элемент по индексу 2: " + intGetter.getElementByIndex(2));
8+
9+
String[] words = {"apple", "banana", "cherry"};
10+
ArrayElementGetter<String> stringGetter = new ArrayElementGetter<>(words);
11+
System.out.println("Элемент по индексу 1: " + stringGetter.getElementByIndex(1));
12+
}
13+
}

students/23K9901/23K9901-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>23K9901</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K9901-p22</artifactId>
12+
<description>Второе задание</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.s0000001;
2+
3+
public class ArrayElementGetter<T> {
4+
private T[] array;
5+
6+
public ArrayElementGetter(T[] array) {
7+
this.array = array;
8+
}
9+
10+
public T getElementByIndex(int index) {
11+
if (index < 0 || index >= array.length) {
12+
throw new IndexOutOfBoundsException("Invalid index: " + index);
13+
}
14+
return array[index];
15+
}
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package ru.mirea.practice.s0000001;
2+
3+
abstract class Main3 {
4+
public static void main(String[] args) {
5+
Integer[] numbers = {10, 20, 30, 40, 50};
6+
ArrayElementGetter<Integer> intGetter = new ArrayElementGetter<>(numbers);
7+
System.out.println("Элемент по индексу 2: " + intGetter.getElementByIndex(2));
8+
9+
String[] words = {"apple", "banana", "cherry"};
10+
ArrayElementGetter<String> stringGetter = new ArrayElementGetter<>(words);
11+
System.out.println("Элемент по индексу 1: " + stringGetter.getElementByIndex(1));
12+
}
13+
}

students/23K9901/23K9901-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>23K9901</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K9901-p23</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 Main {
4+
public static void main(String[] args) {
5+
String rpnExpression = "3 4 + 2 * 7 /";
6+
double result = RPNCalc.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/23K9901/23K9901-p24/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>23K9901</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K9901-p24</artifactId>
12+
<description>Второе задание</description>
13+
</project>

0 commit comments

Comments
 (0)