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

Commit 5db1022

Browse files
committed
Лабораторная №21-32
1 parent f72e743 commit 5db1022

File tree

79 files changed

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

79 files changed

+2743
-0
lines changed

students/23K0505/23K0505-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>23K0505</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0505-p21</artifactId>
12+
<description>Лабораторная 21</description>
13+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package ru.mirea.practice.s23k0505;
2+
3+
public final class Main {
4+
5+
private Main() {
6+
7+
}
8+
9+
public static void main(String[] args) {
10+
System.out.println("Двадцать первая практическая работа!");
11+
}
12+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package ru.mirea.practice.s23k0505.task1;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public abstract class Main {
7+
public static <T> List<T> arrToList(T[] array) {
8+
List<T> list = new ArrayList<>(array.length);
9+
for (T e : array) {
10+
list.add(e);
11+
}
12+
return list;
13+
}
14+
15+
public static void main(String[] args) {
16+
Integer[] arr = {1, 2, 3, 4, 5};
17+
List<Integer> list = arrToList(arr);
18+
System.out.println(list);
19+
20+
Integer[] arr1 = new Integer[10];
21+
}
22+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package ru.mirea.practice.s23k0505.task2;
2+
3+
import java.util.Arrays;
4+
5+
public class Array<T> {
6+
private final T[] array;
7+
private final int length;
8+
9+
Array(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.s23k0505.task2;
2+
3+
public abstract class Main {
4+
public static void main(String[] args) {
5+
Integer[] arr = {5, 4, 3, 2, 1};
6+
Array<Integer> array = new Array<>(arr);
7+
System.out.println(array);
8+
}
9+
}

students/23K0505/23K0505-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>23K0505</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0505-p22</artifactId>
12+
<description>Лабораторная 22</description>
13+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package ru.mirea.practice.s23k0505;
2+
3+
public final class Main {
4+
5+
private Main() {
6+
7+
}
8+
9+
public static void main(String[] args) {
10+
System.out.println("Двадцать первая практическая работа!");
11+
}
12+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package ru.mirea.practice.s23k0505.task1;
2+
3+
import ru.mirea.practice.s23k0505.task1.utilclasses.Number;
4+
import ru.mirea.practice.s23k0505.task1.utilclasses.Operation;
5+
6+
import java.util.Stack;
7+
8+
public class Calculator {
9+
private final Stack<ComputingElement> stack;
10+
11+
public Calculator() {
12+
this.stack = new Stack<>();
13+
}
14+
15+
public void clear() {
16+
stack.clear();
17+
}
18+
19+
public void addNumber(Number number) {
20+
stack.push(number);
21+
}
22+
23+
public void addOperation(Operation operation) {
24+
stack.push(operation);
25+
}
26+
27+
public Stack<ComputingElement> getStack() {
28+
return stack;
29+
}
30+
31+
public Number compute() {
32+
Number result = new Number();
33+
Number b;
34+
Number a;
35+
ComputingElement popped = stack.pop();
36+
if (popped instanceof Operation) {
37+
b = compute();
38+
a = compute();
39+
result = ((Operation) popped).compute(a, b);
40+
} else if (popped instanceof Number) {
41+
result = (Number) popped;
42+
}
43+
44+
return result;
45+
}
46+
47+
@Override
48+
public String toString() {
49+
return stack.toString();
50+
}
51+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package ru.mirea.practice.s23k0505.task1;
2+
3+
import ru.mirea.practice.s23k0505.task1.utilclasses.Number;
4+
import ru.mirea.practice.s23k0505.task1.utilclasses.Operation;
5+
import ru.mirea.practice.s23k0505.task1.utilclasses.OperationType;
6+
7+
import java.util.LinkedList;
8+
import java.util.List;
9+
10+
public class CalculatorController {
11+
private final Calculator calculator;
12+
private final List<ComputingElement> fakeStack;
13+
private int numbersCount;
14+
private int operationCount;
15+
16+
public CalculatorController() {
17+
this.calculator = new Calculator();
18+
fakeStack = new LinkedList<>();
19+
numbersCount = 0;
20+
operationCount = 0;
21+
}
22+
23+
public void addNumber(double number) {
24+
fakeStack.add(new Number(number));
25+
numbersCount += 1;
26+
}
27+
28+
public void addOperation(OperationType operationType) {
29+
if (operationCount < numbersCount - 1) {
30+
fakeStack.add(new Operation(operationType));
31+
operationCount += 1;
32+
}
33+
}
34+
35+
public void clear() {
36+
fakeStack.clear();
37+
}
38+
39+
public ComputingElement delete() {
40+
ComputingElement element = null;
41+
if (!fakeStack.isEmpty()) {
42+
element = fakeStack.remove(fakeStack.size() - 1);
43+
if (element instanceof Number) {
44+
numbersCount -= 1;
45+
} else {
46+
operationCount -= 1;
47+
}
48+
}
49+
return element;
50+
}
51+
52+
public double compute() {
53+
toRealStack();
54+
fakeStack.clear();
55+
final Number result = calculator.compute();
56+
fakeStack.addAll(calculator.getStack());
57+
calculator.clear();
58+
numbersCount -= operationCount + 1;
59+
operationCount = 0;
60+
return result.getValue();
61+
}
62+
63+
public int getStackSize() {
64+
return fakeStack.size();
65+
}
66+
67+
private void toRealStack() {
68+
for (ComputingElement element : fakeStack) {
69+
if (element instanceof Number) {
70+
calculator.addNumber((Number) element);
71+
} else if (element instanceof Operation) {
72+
calculator.addOperation((Operation) element);
73+
}
74+
}
75+
}
76+
77+
@Override
78+
public String toString() {
79+
StringBuilder fakeStackStr = new StringBuilder();
80+
for (ComputingElement element : fakeStack) {
81+
fakeStackStr.append(element.toString());
82+
fakeStackStr.append(" ");
83+
}
84+
return fakeStackStr.toString();
85+
}
86+
87+
public String printStack() {
88+
return String.format("Numbers: %d, Operations: %d", numbersCount, operationCount);
89+
}
90+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package ru.mirea.practice.s23k0505.task1;
2+
3+
public interface ComputingElement {
4+
}

0 commit comments

Comments
 (0)