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

Commit 3bb31f1

Browse files
committed
Лабораторные работы №21-30
1 parent 149523f commit 3bb31f1

File tree

47 files changed

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

47 files changed

+890
-0
lines changed
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>23K0350</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0350-p21</artifactId>
12+
<description>Стирание типов</description>
13+
</project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package ru.mirea.practice.s23k0350.task1;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class AnyArray<T> {
7+
public List<T> array;
8+
9+
public AnyArray() {
10+
this.array = new ArrayList<>();
11+
}
12+
13+
public void add(T item) {
14+
array.add(item);
15+
}
16+
17+
18+
public void printElements() {
19+
for (T item: array) {
20+
System.out.println(item.toString());
21+
}
22+
}
23+
24+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package ru.mirea.practice.s23k0350.task1;
2+
3+
4+
import ru.mirea.practice.s23k0350.task2.TestHuman;
5+
6+
abstract class Main {
7+
public static void main(String[] args) {
8+
AnyArray<Object> testing = new AnyArray<>();
9+
TestHuman female = new TestHuman("Natasha", 27);
10+
testing.add(10);
11+
testing.add("Hello");
12+
testing.add(true);
13+
testing.add(3.14);
14+
testing.add(female);
15+
System.out.println("Elements:");
16+
testing.printElements();
17+
}
18+
}
19+
20+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package ru.mirea.practice.s23k0350.task2;
2+
3+
4+
import ru.mirea.practice.s23k0350.task1.AnyArray;
5+
6+
public class AnyArrayElecBug<T> extends AnyArray<T> {
7+
public T returnElement(int index) {
8+
return array.get(index);
9+
}
10+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package ru.mirea.practice.s23k0350.task2;
2+
3+
4+
abstract class Main {
5+
public static void main(String[] args) {
6+
AnyArrayElecBug<Object> test = new AnyArrayElecBug<>();
7+
TestHuman male = new TestHuman("Oleg", 35);
8+
test.add(10);
9+
test.add(male);
10+
test.add("Hello");
11+
test.add(true);
12+
test.add(3.14);
13+
System.out.println("Elements:");
14+
test.printElements();
15+
System.out.println("w/ Index:\n" + test.returnElement(1));
16+
17+
}
18+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package ru.mirea.practice.s23k0350.task2;
2+
3+
public class TestHuman {
4+
String name;
5+
int age;
6+
7+
public TestHuman(String name, int age) {
8+
this.age = age;
9+
this.name = name;
10+
}
11+
12+
public String toString() {
13+
return "Name: " + name + "\n" + "Age:" + age;
14+
}
15+
}

students/23K0350/23K0350-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>23K0350</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0350-p22</artifactId>
12+
<description>Стек</description>
13+
</project>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package ru.mirea.practice.s23k0350;
2+
3+
abstract class Main {
4+
public static void main(String[] args) {
5+
String expression = "2 3 * 4 5 * + ";
6+
RpnCalculator rp = new RpnCalculator(expression);
7+
System.out.println("Результат: " + rp.evaluate(expression));
8+
}
9+
}
10+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package ru.mirea.practice.s23k0350;
2+
3+
import java.util.Stack;
4+
5+
public class RpnCalculator {
6+
String expression;
7+
8+
public RpnCalculator(String expression) {
9+
this.expression = expression;
10+
}
11+
12+
public static double evaluate(String expression) {
13+
Stack<Double> stack = new Stack<>();
14+
String[] tokens = expression.split(" ");
15+
16+
for (String token : tokens) {
17+
if (isNumber(token)) {
18+
stack.push(Double.parseDouble(token));
19+
} else {
20+
double b = stack.pop();
21+
double a = stack.pop();
22+
stack.push(applyOperation(a, b, token));
23+
}
24+
}
25+
26+
return stack.pop();
27+
}
28+
29+
private static boolean isNumber(String token) {
30+
try {
31+
Double.parseDouble(token);
32+
return true;
33+
} catch (NumberFormatException e) {
34+
return false;
35+
}
36+
}
37+
38+
private static double applyOperation(double a, double b, String operator) {
39+
if ("+".equals(operator)) {
40+
return a + b;
41+
} else if ("-".equals(operator)) {
42+
return a - b;
43+
} else if ("*".equals(operator)) {
44+
return a * b;
45+
} else if ("/".equals(operator)) {
46+
if (b == 0) {
47+
throw new UnsupportedOperationException("Cannot divide by zero");
48+
}
49+
return a / b;
50+
}
51+
throw new UnsupportedOperationException("Unsupported operation: " + operator);
52+
}
53+
}
54+
55+

students/23K0350/23K0350-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>23K0350</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0350-p23</artifactId>
12+
<description>Очередь</description>
13+
</project>

0 commit comments

Comments
 (0)