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

Лабораторная 1-32 #673

Merged
merged 1 commit into from
Dec 13, 2024
Merged
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
13 changes: 13 additions & 0 deletions students/23K0690/23K0690-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="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>23K0690</artifactId>
<groupId>ru.mirea.practice</groupId>
<version>2024.1</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>23K0690-p21</artifactId>
<description>21 работа</description>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package ru.mirea.practice.s23k0690;

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

public abstract class Task1 {

public static <T> List<T> convertArrayToList(T[] array) {
return new ArrayList<>(Arrays.asList(array));
}

public static void main(String[] args) {

String[] names = {"Сигарета",
"Пафнутий",
"Мемема"};
List<String> nameList = convertArrayToList(names);
System.out.println("Список Имена: " + nameList);

Integer[] numbers = {1,
35,
228,
93,
1488};
List<Integer> numberList = convertArrayToList(numbers);
System.out.println("Список Числа: " + numberList);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package ru.mirea.practice.s23k0690;

import java.util.Arrays;

public class Task2<T> {
private T[] array;
private int size;

@SuppressWarnings("unchecked")
public Task2(int capacity) {
array = (T[]) new Object[capacity];
size = 0;
}

public void add(T element) {
if (size >= array.length) {
resize();
}
array[size++] = element;
}

public T get(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Вне диапазона: " + index);
}
return array[index];
}

public int size() {
return size;
}

@SuppressWarnings("unchecked")
private void resize() {
T[] newArray = (T[]) new Object[array.length * 2];
System.arraycopy(array, 0, newArray, 0, size);
array = newArray;
}

@Override
public String toString() {
return Arrays.toString(Arrays.copyOf(array, size));
}

public static void main(String[] args) {
Task2<Integer> intArray = new Task2<>(5);
intArray.add(1);
intArray.add(2);
intArray.add(3);
System.out.println("Integer массив: " + intArray);

Task2<String> stringArray = new Task2<>(5);
stringArray.add("Hello");
stringArray.add("Сигарета!");
System.out.println("String массив: " + stringArray);

Task2<Double> doubleArray = new Task2<>(5);
doubleArray.add(1.1);
doubleArray.add(2.2);
System.out.println("Double массив: " + doubleArray);
}
}
13 changes: 13 additions & 0 deletions students/23K0690/23K0690-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="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>23K0690</artifactId>
<groupId>ru.mirea.practice</groupId>
<version>2024.1</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>23K0690-p22</artifactId>
<description>22 работа</description>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package ru.mirea.practice.s23k0690;

import java.util.Stack;

public final class Task1 {

private Task1() {
throw new UnsupportedOperationException("Utility class");
}

public static double evaluaterpn(String expression) throws Exception {
Stack<Double> stack = new Stack<>();
String[] tokens = expression.split(" ");

for (String token : tokens) {
if (isOperator(token)) {
if (stack.size() < 2) {
throw new Exception("Недостаточно операндов");
}
double b = stack.pop();
double a = stack.pop();
double result = applyOperator(token, a, b);
stack.push(result);
} else {
try {
double number = Double.parseDouble(token);
stack.push(number);
} catch (NumberFormatException e) {
throw new Exception("Неверный формат числа: " + token, e);
}
}
}

if (stack.size() != 1) {
throw new Exception("Некорректное выражение");
}

return stack.pop();
}

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

private static double applyOperator(String operator, double a, double b) throws Exception {
switch (operator) {
case "+":
return a + b;
case "-":
return a - b;
case "*":
return a * b;
case "/":
if (b == 0) {
throw new Exception("Деление на ноль");
}
return a / b;
default:
throw new Exception("Неверный оператор: " + operator);
}
}

public static void main(String[] args) {
String expression = "3 4 + 2 * 7 /";
try {
double result = evaluaterpn(expression);
System.out.println("Результат: " + result);
} catch (Exception e) {
System.out.println("Ошибка: " + e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package ru.mirea.practice.s23k0690;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;

public class Task2 extends JFrame {
private JTextField displayField;

public Task2() {
setTitle("Калькулятор");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(500, 500);

displayField = new JTextField();
add(displayField, BorderLayout.NORTH);

JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4));

String[] buttonLabels = {"7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "+", "="};
for (String label : buttonLabels) {
JButton button = new JButton(label);
button.addActionListener(new ButtonClickHandler());
buttonPanel.add(button);
}

add(buttonPanel, BorderLayout.CENTER);
setVisible(true);
}

private class ButtonClickHandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if ("=".equals(command)) {
try {
String expression = displayField.getText().replaceAll("\\s+", "");
String postfixExpression = convertToPostfix(expression);
System.out.println("Постфиксное выражение: " + postfixExpression);

double calculationResult = evaluatePostfix(postfixExpression);
displayField.setText(String.valueOf(calculationResult));
} catch (Exception ex) {
displayField.setText("Ошибка");
System.out.println("Ошибка: " + ex.getMessage());
}
} else {
String currentText = displayField.getText();
displayField.setText(currentText + " " + command);
}
}
}

public static double evaluatePostfix(String expression) {
Stack<Double> numberStack = new Stack<>();
String[] tokens = expression.trim().split(" ");
System.out.println("Токены: " + String.join(", ", tokens));

for (String token : tokens) {
if (isNumeric(token)) {
double number = Double.parseDouble(token);
numberStack.push(number);
} else {
if (numberStack.size() < 2) {
throw new IllegalArgumentException("Недостаточно операндов для операции");
}
double secondOperand = numberStack.pop();
double firstOperand = numberStack.pop();
double operationResult = 0;
if ("+".equals(token)) {
operationResult = firstOperand + secondOperand;
} else if ("-".equals(token)) {
operationResult = firstOperand - secondOperand;
} else if ("*".equals(token)) {
operationResult = firstOperand * secondOperand;
} else if ("/".equals(token)) {
operationResult = firstOperand / secondOperand;
} else {
throw new IllegalArgumentException("Недопустимая операция: " + token);
}
numberStack.push(operationResult);
}
}

if (numberStack.size() != 1) {
throw new IllegalArgumentException("Недопустимое выражение");
}
return numberStack.pop();
}

private static boolean isNumeric(String str) {
try {
Double.parseDouble(str);
return true;
} catch (NumberFormatException e) {
return false;
}
}

private String convertToPostfix(String expression) {
StringBuilder result = new StringBuilder();
Stack<Character> operatorStack = new Stack<>();

for (int i = 0; i < expression.length(); i++) {
char character = expression.charAt(i);

if (Character.isDigit(character)) {
result.append(character).append(" ");
} else if (character == '(') {
operatorStack.push(character);
} else if (character == ')') {
while (!operatorStack.isEmpty() && operatorStack.peek() != '(') {
result.append(operatorStack.pop()).append(" ");
}
operatorStack.pop();
} else if (isOperator(character)) {
while (!operatorStack.isEmpty() && precedence(operatorStack.peek()) >= precedence(character)) {
result.append(operatorStack.pop()).append(" ");
}
operatorStack.push(character);
}
}

while (!operatorStack.isEmpty()) {
result.append(operatorStack.pop()).append(" ");
}

return result.toString().trim();
}

private boolean isOperator(char character) {
return character == '+' || character == '-' || character == '*' || character == '/';
}

private int precedence(char operator) {
if (operator == '+' || operator == '-') {
return 1;
}
if (operator == '*' || operator == '/') {
return 2;
}
return -1;
}

public static void main(String[] args) {
new Task2();
}
}
13 changes: 13 additions & 0 deletions students/23K0690/23K0690-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="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>23K0690</artifactId>
<groupId>ru.mirea.practice</groupId>
<version>2024.1</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>23K0690-p23</artifactId>
<description>23 работа</description>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ru.mirea.practice.s23k0690;

public abstract class AbstractQueue<E> implements Queue<E> {
protected int size; // Размер очереди

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

@Override
public int size() {
return size;
}

@Override
public void clear() {
size = 0;
}
}
Loading
Loading