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

Commit 77e53a7

Browse files
authored
Merge pull request #658 from Felxe/features/lab21-32
Лабораторные 21-32
2 parents 779c268 + e0b6c3a commit 77e53a7

File tree

66 files changed

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

66 files changed

+2045
-0
lines changed

students/23K0140/23K0140-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>23K0140</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0140-p21</artifactId>
12+
<description>Лабораторная 21</description>
13+
</project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package ru.mirea.practice.s23k0140.task1;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
public abstract class Main {
8+
9+
public static <T> List<T> arrayToList(T[] array) {
10+
return new ArrayList<>(Arrays.asList(array));
11+
}
12+
13+
public static void main(String[] args) {
14+
String[] stringArray = {"Apple", "Banana", "Cherry"};
15+
List<String> stringList = arrayToList(stringArray);
16+
System.out.println("String list: " + stringList);
17+
18+
Integer[] intArray = {1, 2, 3, 4, 5};
19+
List<Integer> intList = arrayToList(intArray);
20+
System.out.println("Integer list: " + intList);
21+
}
22+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package ru.mirea.practice.s23k0140.task2;
2+
3+
public class Main<T> {
4+
private T[] elements;
5+
6+
public Main(T[] elements) {
7+
this.elements = elements;
8+
}
9+
10+
public T get(int index) {
11+
if (index >= 0 && index < elements.length) {
12+
return elements[index];
13+
} else {
14+
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + elements.length);
15+
}
16+
}
17+
18+
public void set(int index, T element) {
19+
if (index >= 0 && index < elements.length) {
20+
elements[index] = element;
21+
} else {
22+
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + elements.length);
23+
}
24+
}
25+
26+
public int size() {
27+
return elements.length;
28+
}
29+
30+
@Override
31+
public String toString() {
32+
StringBuilder sb = new StringBuilder("[");
33+
for (int i = 0; i < elements.length; i++) {
34+
sb.append(elements[i]);
35+
if (i < elements.length - 1) {
36+
sb.append(", ");
37+
}
38+
}
39+
sb.append("]");
40+
return sb.toString();
41+
}
42+
43+
public static void main(String[] args) {
44+
Integer[] intArray = {1, 2, 3, 4, 5};
45+
Main<Integer> intStorage = new Main<>(intArray);
46+
System.out.println("Integer array: " + intStorage);
47+
48+
String[] stringArray = {"Apple", "Banana", "Cherry"};
49+
Main<String> stringStorage = new Main<>(stringArray);
50+
System.out.println("String array: " + stringStorage);
51+
}
52+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package ru.mirea.practice.s23k0140.task3;
2+
3+
public abstract class Main {
4+
5+
public static <E> E getElement(E[] array, int index) {
6+
if (index < 0 || index >= array.length) {
7+
throw new IndexOutOfBoundsException("Индекс вне диапазона: " + index);
8+
}
9+
return array[index];
10+
}
11+
12+
public static void main(String[] args) {
13+
String[] stringArray = {"Apple", "Banana", "Cherry"};
14+
System.out.println("Элемент по индексу 1: " + getElement(stringArray, 1));
15+
16+
Integer[] integerArray = {10, 20, 30};
17+
System.out.println("Элемент по индексу 0: " + getElement(integerArray, 0));
18+
}
19+
}

students/23K0140/23K0140-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>23K0140</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0140-p22</artifactId>
12+
<description>Лабораторная 22</description>
13+
</project>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package ru.mirea.practice.s23k0140.task1;
2+
3+
import java.util.Stack;
4+
5+
public abstract class Main {
6+
7+
public static double evaluaterpn(String[] tokens) {
8+
Stack<Double> stack = new Stack<>();
9+
10+
for (String token : tokens) {
11+
switch (token) {
12+
case "+":
13+
stack.push(stack.pop() + stack.pop());
14+
break;
15+
case "-":
16+
double subtrahend = stack.pop();
17+
stack.push(stack.pop() - subtrahend);
18+
break;
19+
case "*":
20+
stack.push(stack.pop() * stack.pop());
21+
break;
22+
case "/":
23+
double divisor = stack.pop();
24+
stack.push(stack.pop() / divisor);
25+
break;
26+
default:
27+
stack.push(Double.parseDouble(token));
28+
break;
29+
}
30+
}
31+
return stack.pop();
32+
}
33+
34+
public static void main(String[] args) {
35+
String[] expression = {"2", "3", "+", "4", "5", "*", "+"};
36+
System.out.println("Result: " + evaluaterpn(expression));
37+
}
38+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package ru.mirea.practice.s23k0140.task2;
2+
3+
import java.awt.event.ActionEvent;
4+
import java.awt.event.ActionListener;
5+
6+
public class CalculatorController {
7+
private CalculatorView view;
8+
private CalculatorModel model;
9+
10+
public CalculatorController(CalculatorView view, CalculatorModel model) {
11+
this.view = view;
12+
this.model = model;
13+
14+
this.view.addButtonListener(new ButtonClickListener());
15+
}
16+
17+
class ButtonClickListener implements ActionListener {
18+
public void actionPerformed(ActionEvent e) {
19+
String command = e.getActionCommand();
20+
if ("=".equals(command)) {
21+
String input = view.getInput().trim();
22+
if (input.isEmpty()) {
23+
view.setResult("Ошибка: пустой ввод.");
24+
return;
25+
}
26+
try {
27+
double result = model.evaluateRpn(input);
28+
view.addToHistory(input);
29+
view.setResult("Результат: " + result);
30+
view.clearInput();
31+
} catch (Exception ex) {
32+
view.setResult("Ошибка: " + ex.getMessage());
33+
}
34+
} else {
35+
String currentInput = view.getInput();
36+
view.setInput(currentInput + (currentInput.isEmpty() ? "" : " ") + command);
37+
}
38+
}
39+
}
40+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package ru.mirea.practice.s23k0140.task2;
2+
3+
import java.util.Stack;
4+
5+
public class CalculatorModel {
6+
public double evaluateRpn(String expression) {
7+
Stack<Double> stack = new Stack<>();
8+
String[] tokens = expression.trim().split("\\s+");
9+
10+
for (String token : tokens) {
11+
switch (token) {
12+
case "+":
13+
stack.push(stack.pop() + stack.pop());
14+
break;
15+
case "-":
16+
double subtrahend = stack.pop();
17+
double minuend = stack.pop();
18+
stack.push(minuend - subtrahend);
19+
break;
20+
case "*":
21+
stack.push(stack.pop() * stack.pop());
22+
break;
23+
case "/":
24+
double divisor = stack.pop();
25+
double dividend = stack.pop();
26+
if (divisor == 0) {
27+
throw new ArithmeticException("Деление на ноль.");
28+
}
29+
stack.push(dividend / divisor);
30+
break;
31+
default:
32+
try {
33+
double number = Double.parseDouble(token);
34+
stack.push(number);
35+
} catch (NumberFormatException e) {
36+
throw new IllegalArgumentException("Неправильный ввод: '" + token + "'.", e);
37+
}
38+
break;
39+
}
40+
}
41+
42+
if (stack.size() != 1) {
43+
throw new IllegalArgumentException("Неправильное выражение.");
44+
}
45+
return stack.pop();
46+
}
47+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package ru.mirea.practice.s23k0140.task2;
2+
3+
import javax.swing.JButton;
4+
import javax.swing.JFrame;
5+
import javax.swing.JTextArea;
6+
import javax.swing.JTextField;
7+
import javax.swing.JPanel;
8+
import javax.swing.JScrollPane;
9+
import java.awt.BorderLayout;
10+
import java.awt.GridLayout;
11+
import java.awt.event.ActionListener;
12+
import java.awt.Component;
13+
14+
public class CalculatorView {
15+
private JFrame frame;
16+
private JTextField inputField;
17+
private JTextArea historyArea;
18+
private JTextArea resultArea;
19+
private JPanel buttonPanel;
20+
21+
public CalculatorView() {
22+
frame = new JFrame("RPN Calculator");
23+
inputField = new JTextField(20);
24+
historyArea = new JTextArea(10, 20);
25+
resultArea = new JTextArea(5, 20);
26+
resultArea.setEditable(false);
27+
historyArea.setEditable(false);
28+
29+
buttonPanel = new JPanel();
30+
buttonPanel.setLayout(new GridLayout(4, 4));
31+
32+
setupUI();
33+
}
34+
35+
private void setupUI() {
36+
frame.setLayout(new BorderLayout());
37+
frame.add(inputField, BorderLayout.NORTH);
38+
addButtons();
39+
frame.add(buttonPanel, BorderLayout.CENTER);
40+
41+
JPanel outputPanel = new JPanel();
42+
outputPanel.setLayout(new BorderLayout());
43+
outputPanel.add(new JScrollPane(historyArea), BorderLayout.CENTER);
44+
outputPanel.add(new JScrollPane(resultArea), BorderLayout.SOUTH);
45+
frame.add(outputPanel, BorderLayout.EAST);
46+
47+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
48+
frame.pack();
49+
frame.setVisible(true);
50+
}
51+
52+
private void addButtons() {
53+
String[] buttons = {
54+
"7", "8", "9", "/",
55+
"4", "5", "6", "*",
56+
"1", "2", "3", "-",
57+
"0", ".", "=", "+"
58+
};
59+
60+
for (String text : buttons) {
61+
JButton button = new JButton(text);
62+
buttonPanel.add(button);
63+
}
64+
}
65+
66+
public String getInput() {
67+
return inputField.getText();
68+
}
69+
70+
public void setInput(String input) {
71+
inputField.setText(input);
72+
}
73+
74+
public void addToHistory(String expression) {
75+
historyArea.append(expression + "\n");
76+
}
77+
78+
public void setResult(String result) {
79+
resultArea.setText(result);
80+
}
81+
82+
public void addButtonListener(ActionListener listener) {
83+
for (Component component : buttonPanel.getComponents()) {
84+
if (component instanceof JButton) {
85+
((JButton) component).addActionListener(listener);
86+
}
87+
}
88+
}
89+
90+
public void clearInput() {
91+
inputField.setText("");
92+
}
93+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package ru.mirea.practice.s23k0140.task2;
2+
3+
public abstract class Main {
4+
public static void main(String[] args) {
5+
CalculatorView view = new CalculatorView();
6+
CalculatorModel model = new CalculatorModel();
7+
new CalculatorController(view, model);
8+
}
9+
}

0 commit comments

Comments
 (0)