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

Commit 82fb9e5

Browse files
authored
Merge pull request #697 from kannteliss/features/lab1
Features/lab1
2 parents e403f21 + 6067e72 commit 82fb9e5

File tree

64 files changed

+1905
-1
lines changed

Some content is hidden

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

64 files changed

+1905
-1
lines changed

students/23K0135/23K0135-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="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>ru.mirea.practice</groupId>
8+
<artifactId>23K0135</artifactId>
9+
<version>2024.1</version>
10+
</parent>
11+
12+
<artifactId>23K0135-p21</artifactId>
13+
</project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package ru.mirea.practice.s23k0135.task1;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public abstract class Task1 {
7+
public static <E> List<E> convertArrayToList(E[] elements) {
8+
List<E> resultList = new ArrayList<>(elements.length);
9+
for (E element : elements) {
10+
resultList.add(element);
11+
}
12+
return resultList;
13+
}
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package ru.mirea.practice.s23k0135.task1;
2+
3+
import java.util.List;
4+
5+
import static ru.mirea.practice.s23k0135.task1.Task1.convertArrayToList;
6+
7+
public abstract class Test {
8+
public static void main(String[] args) {
9+
Integer[] numbers = {5, 6, 8, 3, 2, 1};
10+
List<Integer> numberList = convertArrayToList(numbers);
11+
System.out.println(numberList);
12+
13+
Integer[] emptyArray = new Integer[15];
14+
}
15+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package ru.mirea.practice.s23k0135.task2and3;
2+
3+
import java.util.Arrays;
4+
5+
public class Task2and3<T> {
6+
private final T[] array;
7+
private final int length;
8+
9+
Task2and3(T[] array) {
10+
this.array = array;
11+
this.length = array.length;
12+
}
13+
14+
public T get(int index) {
15+
return array[index];
16+
}
17+
18+
public int getLength() {
19+
return length;
20+
}
21+
22+
@Override
23+
public String toString() {
24+
return Arrays.toString(array);
25+
}
26+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package ru.mirea.practice.s23k0135.task2and3;
2+
3+
public abstract class Test {
4+
public static void main(String[] args) {
5+
Integer[] arr = {5, 6, 8, 3, 2, 1};
6+
Task2and3<Integer> array = new Task2and3<>(arr);
7+
System.out.println(array);
8+
}
9+
}

students/23K0135/23K0135-p22/pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>ru.mirea.practice</groupId>
8+
<artifactId>23K0135</artifactId>
9+
<version>2024.1</version>
10+
</parent>
11+
12+
<artifactId>23K0135-p22</artifactId>
13+
14+
</project>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package ru.mirea.practice.s23k0135;
2+
3+
import java.util.StringTokenizer;
4+
5+
public class RpnCalculator {
6+
private Stack stack;
7+
8+
public RpnCalculator(int size) {
9+
stack = new Stack(size);
10+
}
11+
12+
public double evaluate(String expression) {
13+
StringTokenizer tokenizer = new StringTokenizer(expression);
14+
15+
while (tokenizer.hasMoreTokens()) {
16+
String token = tokenizer.nextToken();
17+
18+
if (isNumeric(token)) {
19+
stack.push(Double.parseDouble(token));
20+
} else {
21+
performOperation(token);
22+
}
23+
}
24+
25+
return stack.pop();
26+
}
27+
28+
private void performOperation(String operator) {
29+
double secondOperand = stack.pop();
30+
double firstOperand = stack.pop();
31+
32+
switch (operator) {
33+
case "+":
34+
stack.push(firstOperand + secondOperand);
35+
break;
36+
case "-":
37+
stack.push(firstOperand - secondOperand);
38+
break;
39+
case "*":
40+
stack.push(firstOperand * secondOperand);
41+
break;
42+
case "/":
43+
if (secondOperand == 0) {
44+
throw new ArithmeticException("Ошибка: Деление на ноль");
45+
}
46+
stack.push(firstOperand / secondOperand);
47+
break;
48+
default:
49+
throw new IllegalArgumentException("Недопустимый оператор: " + operator);
50+
}
51+
}
52+
53+
private boolean isNumeric(String str) {
54+
try {
55+
Double.parseDouble(str);
56+
return true;
57+
} catch (NumberFormatException e) {
58+
return false;
59+
}
60+
}
61+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package ru.mirea.practice.s23k0135;
2+
3+
import java.util.EmptyStackException;
4+
5+
public class Stack {
6+
private int maxSize;
7+
private double[] stackArray;
8+
private int top;
9+
10+
public Stack(int size) {
11+
this.maxSize = size;
12+
this.stackArray = new double[maxSize];
13+
this.top = -1;
14+
}
15+
16+
public void push(double value) {
17+
if (top >= maxSize - 1) {
18+
throw new StackOverflowError("Стек переполнен");
19+
}
20+
stackArray[++top] = value;
21+
}
22+
23+
public double pop() {
24+
if (isEmpty()) {
25+
throw new EmptyStackException();
26+
}
27+
return stackArray[top--];
28+
}
29+
30+
public double peek() {
31+
if (isEmpty()) {
32+
throw new EmptyStackException();
33+
}
34+
return stackArray[top];
35+
}
36+
37+
public boolean isEmpty() {
38+
return top == -1;
39+
}
40+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package ru.mirea.practice.s23k0135;
2+
3+
import java.util.EmptyStackException;
4+
5+
public abstract class Test {
6+
public static void main(String[] args) {
7+
RpnCalculator calculator = new RpnCalculator(10);
8+
9+
System.out.println("7 3 + = " + calculator.evaluate("7 3 +"));
10+
System.out.println("4 9 - = " + calculator.evaluate("4 9 -"));
11+
System.out.println("6 8 * = " + calculator.evaluate("6 8 *"));
12+
System.out.println("2 4 / = " + calculator.evaluate("2 4 /"));
13+
System.out.println("9 2 + 3 * = " + calculator.evaluate("9 2 + 3 *"));
14+
15+
try {
16+
System.out.println("10 0 / = " + calculator.evaluate("10 0 /"));
17+
} catch (ArithmeticException e) {
18+
System.out.println(e.getMessage());
19+
}
20+
21+
try {
22+
System.out.println("1 + = " + calculator.evaluate("1 +"));
23+
} catch (EmptyStackException e) {
24+
System.out.println("Невозможно убрать элемент в начале пустого стека");
25+
}
26+
27+
try {
28+
System.out.println("18 9 % = " + calculator.evaluate("18 9 %"));
29+
} catch (IllegalArgumentException e) {
30+
System.out.println(e.getMessage());
31+
}
32+
}
33+
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>ru.mirea.practice</groupId>
8+
<artifactId>23K0135</artifactId>
9+
<version>2024.1</version>
10+
</parent>
11+
12+
<artifactId>23K0135-p23</artifactId>
13+
14+
15+
</project>

0 commit comments

Comments
 (0)