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

Commit aec5b84

Browse files
authored
Merge pull request #652 from murtazinail/main
Лабораторная №21-32
2 parents 33958c7 + ffc6341 commit aec5b84

File tree

69 files changed

+2167
-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.

69 files changed

+2167
-0
lines changed

students/23F0011/23F0011-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>23F0011</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23F0011-p21</artifactId>
12+
<description>Массивы</description>
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.s23f0011.task1;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public abstract class Example {
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.s23f0011.task1;
2+
3+
import java.util.List;
4+
5+
import static ru.mirea.practice.s23f0011.task1.Example.convertArrayToList;
6+
7+
public abstract class Tester {
8+
public static void main(String[] args) {
9+
Integer[] numbers = {1, 2, 3, 4, 5};
10+
List<Integer> numberList = convertArrayToList(numbers);
11+
System.out.println(numberList);
12+
13+
Integer[] emptyArray = new Integer[10];
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.s23f0011.task2andtask3;
2+
3+
import java.util.Arrays;
4+
5+
public class Example<T> {
6+
private final T[] array;
7+
private final int length;
8+
9+
Example(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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package ru.mirea.practice.s23f0011.task2andtask3;
2+
3+
public abstract class Tester {
4+
public static void main(String[] args) {
5+
Integer[] arr = {5, 4, 3, 2, 1};
6+
Example<Integer> array = new Example<>(arr);
7+
System.out.println(array);
8+
}
9+
}
10+

students/23F0011/23F0011-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>23F0011</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23F0011-p22</artifactId>
12+
<description>Массивы</description>
13+
</project>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package ru.mirea.practice.s23f0011;
2+
3+
import java.util.StringTokenizer;
4+
5+
public class RpnCalc {
6+
private Stack stack;
7+
8+
public RpnCalc(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+
}
62+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package ru.mirea.practice.s23f0011;
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.s23f0011;
2+
3+
import java.util.EmptyStackException;
4+
5+
public abstract class Tester {
6+
public static void main(String[] args) {
7+
RpnCalc calculator = new RpnCalc(10);
8+
9+
System.out.println("5 2 + = " + calculator.evaluate("5 2 +"));
10+
System.out.println("3 7 - = " + calculator.evaluate("3 7 -"));
11+
System.out.println("5 7 * = " + calculator.evaluate("5 7 *"));
12+
System.out.println("1 5 / = " + calculator.evaluate("1 5 /"));
13+
System.out.println("8 3 + 1 * = " + calculator.evaluate("8 3 + 1 *"));
14+
15+
try {
16+
System.out.println("5 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("6 2 % = " + calculator.evaluate("6 2 %"));
29+
} catch (IllegalArgumentException e) {
30+
System.out.println(e.getMessage());
31+
}
32+
}
33+
}

students/23F0011/23F0011-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>23F0011</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23F0011-p23</artifactId>
12+
<description>Массивы</description>
13+
</project>

0 commit comments

Comments
 (0)