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

Practiks 21 30 #700

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
<module>students/23K0186</module>
<module>students/23K0755</module>
<module>students/23K1292</module>
<module>students/23K0342</module>
</modules>
<dependencies>
<dependency>
Expand Down Expand Up @@ -147,7 +148,7 @@
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>10.20.1</version>
<version>10.19.0</version>
</dependency>
</dependencies>
<configuration>
Expand Down
13 changes: 13 additions & 0 deletions students/23K0342/23K0342-p21/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>23K0342</artifactId>
<groupId>ru.mirea.practice</groupId>
<version>2024.1</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>23K0342-p21</artifactId>
<description>Массивы</description>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ru.mirea.practice.s0000001;

import java.util.Arrays;
import java.util.List;

public abstract class Converter {
public static <T> List<T> convertArrayToList(T[] array) {
return Arrays.asList(array);
}

public static void main(String[] args) {
String[] stringArray = {"a", "b", "c"};
List<String> stringList = convertArrayToList(stringArray);
System.out.println("Список строк: " + stringList);

Integer[] intArray = {1, 2, 3};
List<Integer> intList = convertArrayToList(intArray);
System.out.println("Список чисел: " + intList);
}
}
//
53 changes: 53 additions & 0 deletions students/23K0342/23K0342-p21/src/main/java/t2/Array.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package ru.mirea.practice.s0000001.t2;

public class Array<T> {
private T[] elements;

public Array(T[] elements) {
this.elements = elements;
}

public T get(int index) {
if (index >= 0 && index < elements.length) {
return elements[index];
} else {
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + elements.length);
}
}

public void set(int index, T element) {
if (index >= 0 && index < elements.length) {
elements[index] = element;
} else {
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + elements.length);
}
}

public int size() {
return elements.length;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder("[");
for (int i = 0; i < elements.length; i++) {
sb.append(elements[i]);
if (i < elements.length - 1) {
sb.append(", ");
}
}
sb.append("]");
return sb.toString();
}

public static void main(String[] args) {
Integer[] intArray = {1, 2, 3, 4, 5};
Array<Integer> intStorage = new Array<>(intArray);
System.out.println("Integer array: " + intStorage);

String[] stringArray = {"СТОЛ", "СТУЛ", "ЗОНТ"};
Array<String> stringStorage = new Array<>(stringArray);
System.out.println("String array: " + stringStorage);
}
}
//
13 changes: 13 additions & 0 deletions students/23K0342/23K0342-p22/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>23K0342</artifactId>
<groupId>ru.mirea.practice</groupId>
<version>2024.1</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>23K0342-p22</artifactId>
<description>Массивы</description>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package ru.mirea.practice.s0000001;

import java.util.Stack;

public class Calculator {
private Stack<Double> stack;

public Calculator() {
stack = new Stack<>();
}

public double evaluate(String expression) {
stack.clear();
String[] tokens = expression.split(" ");
for (String token : tokens) {
if (isOperator(token)) {
if (stack.size() < 2) {
throw new ArrayIndexOutOfBoundsException("Ошибка: недостаточно данных для операции");
}
double b = stack.pop();
double a = stack.pop();
stack.push(applyOperator(a, b, token));
} else {
try {
stack.push(Double.parseDouble(token));
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Ошибка: неверный формат числа", e);
}
}
}
if (stack.size() != 1) {
throw new IllegalStateException("Ошибка: выражение некорректно");
}
return stack.pop();
}

private boolean isOperator(String token) {
return "+".equals(token) || "-".equals(token) || "*".equals(token) || "/".equals(token);
}

private double applyOperator(double a, double b, String operator) {
try {
switch (operator) {
case "+":
return a + b;
case "-":
return a - b;
case "*":
return a * b;
case "/":
if (b == 0) {
throw new ArithmeticException("Ошибка: деление на ноль");
}
return a / b;
default:
throw new IllegalArgumentException("Ошибка: неизвестная операция " + operator);
}
} catch (ArithmeticException | IllegalArgumentException e) {
throw e;
}
}
}
//
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package ru.mirea.practice.s0000001;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.SwingUtilities;

public class Rpn extends JFrame {
private JTextField display;
private Calculator calculator;

public Rpn() {
calculator = new Calculator();
display = new JTextField();
display.setEditable(false);
display.setFont(new Font("Arial", Font.PLAIN, 24));
setLayout(new BorderLayout());
add(display, BorderLayout.NORTH);

JPanel panel = new JPanel(new GridLayout(5, 4, 5, 5));
String[] buttons = {
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", ".", "=", "+",
"C", "Space", "", ""
};

for (String text : buttons) {
JButton button = new JButton(text);
button.setFont(new Font("Arial", Font.PLAIN, 18));
if ("Space".equals(text)) { // Литерал теперь идет первым
button.setPreferredSize(new Dimension(80, 50));
}
button.addActionListener(new ButtonClickListener());
panel.add(button);
}
add(panel, BorderLayout.CENTER);

setTitle("RPN Calculator");
setSize(400, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}

private class ButtonClickListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
switch (command) {
case "=":
try {
double result = calculator.evaluate(display.getText().trim());
display.setText(String.valueOf(result));
} catch (ArithmeticException ex) {
display.setText("Ошибка: деление на ноль");
} catch (NumberFormatException ex) {
display.setText("Ошибка: неверный формат числа");
} catch (ArrayIndexOutOfBoundsException ex) {
display.setText("Ошибка: недостаточно данных для операции");
} catch (IllegalStateException ex) {
display.setText("Ошибка: выражение некорректно");
} catch (Exception ex) {
display.setText("Неизвестная ошибка: " + ex.getMessage());
}
break;
case "C":
display.setText("");
break;
case "Space":
display.setText(display.getText() + " ");
break;
default:
display.setText(display.getText() + command);
break;
}
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
Rpn calc = new Rpn();
calc.setVisible(true);
});
}
}
//
13 changes: 13 additions & 0 deletions students/23K0342/23K0342-p23/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>23K0342</artifactId>
<groupId>ru.mirea.practice</groupId>
<version>2024.1</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>23K0342-p23</artifactId>
<description>Массивы</description>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package ru.mirea.practice.s0000001.task1;

public class ArrayQueue {
private Object[] elements;
private int size;
private int front;
private int rear;

public ArrayQueue() {
elements = new Object[10];
size = 0;
front = 0;
rear = 0;
}

public void enqueue(Object element) {
if (size == elements.length) {
throw new IllegalStateException("Queue is full");
}
elements[rear] = element;
rear = (rear + 1) % elements.length;
size++;
}

public Object element() {
if (isEmpty()) {
throw new IllegalStateException("Queue is empty");
}
return elements[front];
}

public Object dequeue() {
if (isEmpty()) {
throw new IllegalStateException("Queue is empty");
}
Object result = elements[front];
front = (front + 1) % elements.length;
size--;
return result;
}

public int size() {
return size;
}

public boolean isEmpty() {
return size == 0;
}

public void clear() {
size = 0;
front = 0;
rear = 0;
}
}

Loading
Loading