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

Commit 3f37792

Browse files
authored
Merge pull request #673 from banbanovich/lab1-32
Лабораторная 1-32
2 parents a75d52c + e481c6a commit 3f37792

File tree

58 files changed

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

58 files changed

+1738
-0
lines changed

students/23K0690/23K0690-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" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>23K0690</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0690-p21</artifactId>
12+
<description>21 работа</description>
13+
</project>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package ru.mirea.practice.s23k0690;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
public abstract class Task1 {
8+
9+
public static <T> List<T> convertArrayToList(T[] array) {
10+
return new ArrayList<>(Arrays.asList(array));
11+
}
12+
13+
public static void main(String[] args) {
14+
15+
String[] names = {"Сигарета",
16+
"Пафнутий",
17+
"Мемема"};
18+
List<String> nameList = convertArrayToList(names);
19+
System.out.println("Список Имена: " + nameList);
20+
21+
Integer[] numbers = {1,
22+
35,
23+
228,
24+
93,
25+
1488};
26+
List<Integer> numberList = convertArrayToList(numbers);
27+
System.out.println("Список Числа: " + numberList);
28+
29+
}
30+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package ru.mirea.practice.s23k0690;
2+
3+
import java.util.Arrays;
4+
5+
public class Task2<T> {
6+
private T[] array;
7+
private int size;
8+
9+
@SuppressWarnings("unchecked")
10+
public Task2(int capacity) {
11+
array = (T[]) new Object[capacity];
12+
size = 0;
13+
}
14+
15+
public void add(T element) {
16+
if (size >= array.length) {
17+
resize();
18+
}
19+
array[size++] = element;
20+
}
21+
22+
public T get(int index) {
23+
if (index < 0 || index >= size) {
24+
throw new IndexOutOfBoundsException("Вне диапазона: " + index);
25+
}
26+
return array[index];
27+
}
28+
29+
public int size() {
30+
return size;
31+
}
32+
33+
@SuppressWarnings("unchecked")
34+
private void resize() {
35+
T[] newArray = (T[]) new Object[array.length * 2];
36+
System.arraycopy(array, 0, newArray, 0, size);
37+
array = newArray;
38+
}
39+
40+
@Override
41+
public String toString() {
42+
return Arrays.toString(Arrays.copyOf(array, size));
43+
}
44+
45+
public static void main(String[] args) {
46+
Task2<Integer> intArray = new Task2<>(5);
47+
intArray.add(1);
48+
intArray.add(2);
49+
intArray.add(3);
50+
System.out.println("Integer массив: " + intArray);
51+
52+
Task2<String> stringArray = new Task2<>(5);
53+
stringArray.add("Hello");
54+
stringArray.add("Сигарета!");
55+
System.out.println("String массив: " + stringArray);
56+
57+
Task2<Double> doubleArray = new Task2<>(5);
58+
doubleArray.add(1.1);
59+
doubleArray.add(2.2);
60+
System.out.println("Double массив: " + doubleArray);
61+
}
62+
}

students/23K0690/23K0690-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="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>23K0690</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0690-p22</artifactId>
12+
<description>22 работа</description>
13+
</project>
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package ru.mirea.practice.s23k0690;
2+
3+
import java.util.Stack;
4+
5+
public final class Task1 {
6+
7+
private Task1() {
8+
throw new UnsupportedOperationException("Utility class");
9+
}
10+
11+
public static double evaluaterpn(String expression) throws Exception {
12+
Stack<Double> stack = new Stack<>();
13+
String[] tokens = expression.split(" ");
14+
15+
for (String token : tokens) {
16+
if (isOperator(token)) {
17+
if (stack.size() < 2) {
18+
throw new Exception("Недостаточно операндов");
19+
}
20+
double b = stack.pop();
21+
double a = stack.pop();
22+
double result = applyOperator(token, a, b);
23+
stack.push(result);
24+
} else {
25+
try {
26+
double number = Double.parseDouble(token);
27+
stack.push(number);
28+
} catch (NumberFormatException e) {
29+
throw new Exception("Неверный формат числа: " + token, e);
30+
}
31+
}
32+
}
33+
34+
if (stack.size() != 1) {
35+
throw new Exception("Некорректное выражение");
36+
}
37+
38+
return stack.pop();
39+
}
40+
41+
private static boolean isOperator(String token) {
42+
return "+".equals(token) || "-".equals(token) || "*".equals(token) || "/".equals(token);
43+
}
44+
45+
private static double applyOperator(String operator, double a, double b) throws Exception {
46+
switch (operator) {
47+
case "+":
48+
return a + b;
49+
case "-":
50+
return a - b;
51+
case "*":
52+
return a * b;
53+
case "/":
54+
if (b == 0) {
55+
throw new Exception("Деление на ноль");
56+
}
57+
return a / b;
58+
default:
59+
throw new Exception("Неверный оператор: " + operator);
60+
}
61+
}
62+
63+
public static void main(String[] args) {
64+
String expression = "3 4 + 2 * 7 /";
65+
try {
66+
double result = evaluaterpn(expression);
67+
System.out.println("Результат: " + result);
68+
} catch (Exception e) {
69+
System.out.println("Ошибка: " + e.getMessage());
70+
}
71+
}
72+
}
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
package ru.mirea.practice.s23k0690;
2+
3+
import javax.swing.JButton;
4+
import javax.swing.JFrame;
5+
import javax.swing.JPanel;
6+
import javax.swing.JTextField;
7+
import javax.swing.WindowConstants;
8+
import java.awt.BorderLayout;
9+
import java.awt.GridLayout;
10+
import java.awt.event.ActionEvent;
11+
import java.awt.event.ActionListener;
12+
import java.util.Stack;
13+
14+
public class Task2 extends JFrame {
15+
private JTextField displayField;
16+
17+
public Task2() {
18+
setTitle("Калькулятор");
19+
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
20+
setSize(500, 500);
21+
22+
displayField = new JTextField();
23+
add(displayField, BorderLayout.NORTH);
24+
25+
JPanel buttonPanel = new JPanel();
26+
buttonPanel.setLayout(new GridLayout(4, 4));
27+
28+
String[] buttonLabels = {"7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "+", "="};
29+
for (String label : buttonLabels) {
30+
JButton button = new JButton(label);
31+
button.addActionListener(new ButtonClickHandler());
32+
buttonPanel.add(button);
33+
}
34+
35+
add(buttonPanel, BorderLayout.CENTER);
36+
setVisible(true);
37+
}
38+
39+
private class ButtonClickHandler implements ActionListener {
40+
@Override
41+
public void actionPerformed(ActionEvent e) {
42+
String command = e.getActionCommand();
43+
if ("=".equals(command)) {
44+
try {
45+
String expression = displayField.getText().replaceAll("\\s+", "");
46+
String postfixExpression = convertToPostfix(expression);
47+
System.out.println("Постфиксное выражение: " + postfixExpression);
48+
49+
double calculationResult = evaluatePostfix(postfixExpression);
50+
displayField.setText(String.valueOf(calculationResult));
51+
} catch (Exception ex) {
52+
displayField.setText("Ошибка");
53+
System.out.println("Ошибка: " + ex.getMessage());
54+
}
55+
} else {
56+
String currentText = displayField.getText();
57+
displayField.setText(currentText + " " + command);
58+
}
59+
}
60+
}
61+
62+
public static double evaluatePostfix(String expression) {
63+
Stack<Double> numberStack = new Stack<>();
64+
String[] tokens = expression.trim().split(" ");
65+
System.out.println("Токены: " + String.join(", ", tokens));
66+
67+
for (String token : tokens) {
68+
if (isNumeric(token)) {
69+
double number = Double.parseDouble(token);
70+
numberStack.push(number);
71+
} else {
72+
if (numberStack.size() < 2) {
73+
throw new IllegalArgumentException("Недостаточно операндов для операции");
74+
}
75+
double secondOperand = numberStack.pop();
76+
double firstOperand = numberStack.pop();
77+
double operationResult = 0;
78+
if ("+".equals(token)) {
79+
operationResult = firstOperand + secondOperand;
80+
} else if ("-".equals(token)) {
81+
operationResult = firstOperand - secondOperand;
82+
} else if ("*".equals(token)) {
83+
operationResult = firstOperand * secondOperand;
84+
} else if ("/".equals(token)) {
85+
operationResult = firstOperand / secondOperand;
86+
} else {
87+
throw new IllegalArgumentException("Недопустимая операция: " + token);
88+
}
89+
numberStack.push(operationResult);
90+
}
91+
}
92+
93+
if (numberStack.size() != 1) {
94+
throw new IllegalArgumentException("Недопустимое выражение");
95+
}
96+
return numberStack.pop();
97+
}
98+
99+
private static boolean isNumeric(String str) {
100+
try {
101+
Double.parseDouble(str);
102+
return true;
103+
} catch (NumberFormatException e) {
104+
return false;
105+
}
106+
}
107+
108+
private String convertToPostfix(String expression) {
109+
StringBuilder result = new StringBuilder();
110+
Stack<Character> operatorStack = new Stack<>();
111+
112+
for (int i = 0; i < expression.length(); i++) {
113+
char character = expression.charAt(i);
114+
115+
if (Character.isDigit(character)) {
116+
result.append(character).append(" ");
117+
} else if (character == '(') {
118+
operatorStack.push(character);
119+
} else if (character == ')') {
120+
while (!operatorStack.isEmpty() && operatorStack.peek() != '(') {
121+
result.append(operatorStack.pop()).append(" ");
122+
}
123+
operatorStack.pop();
124+
} else if (isOperator(character)) {
125+
while (!operatorStack.isEmpty() && precedence(operatorStack.peek()) >= precedence(character)) {
126+
result.append(operatorStack.pop()).append(" ");
127+
}
128+
operatorStack.push(character);
129+
}
130+
}
131+
132+
while (!operatorStack.isEmpty()) {
133+
result.append(operatorStack.pop()).append(" ");
134+
}
135+
136+
return result.toString().trim();
137+
}
138+
139+
private boolean isOperator(char character) {
140+
return character == '+' || character == '-' || character == '*' || character == '/';
141+
}
142+
143+
private int precedence(char operator) {
144+
if (operator == '+' || operator == '-') {
145+
return 1;
146+
}
147+
if (operator == '*' || operator == '/') {
148+
return 2;
149+
}
150+
return -1;
151+
}
152+
153+
public static void main(String[] args) {
154+
new Task2();
155+
}
156+
}

students/23K0690/23K0690-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="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>23K0690</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0690-p23</artifactId>
12+
<description>23 работа</description>
13+
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package ru.mirea.practice.s23k0690;
2+
3+
public abstract class AbstractQueue<E> implements Queue<E> {
4+
protected int size; // Размер очереди
5+
6+
@Override
7+
public boolean isEmpty() {
8+
return size == 0;
9+
}
10+
11+
@Override
12+
public int size() {
13+
return size;
14+
}
15+
16+
@Override
17+
public void clear() {
18+
size = 0;
19+
}
20+
}

0 commit comments

Comments
 (0)