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

Commit a1d09e1

Browse files
committed
лаборатоная 32
1 parent 4749714 commit a1d09e1

File tree

46 files changed

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

46 files changed

+1373
-0
lines changed

students/23K0754/23K0754-p022/pom.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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>23K0754</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0754-p022</artifactId>
12+
<description>Массивы</description>
13+
<dependencies>
14+
<dependency>
15+
<groupId>org.jetbrains</groupId>
16+
<artifactId>annotations</artifactId>
17+
<version>25.0.0</version>
18+
<scope>compile</scope>
19+
</dependency>
20+
</dependencies>
21+
</project>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package ru.mirea.practice.s230k754.lab11.task1;
2+
3+
import java.util.Objects;
4+
5+
public class Stacks {
6+
private final char[] arr;
7+
private int top;
8+
private int capacity;
9+
10+
public Stacks(int capacity) {
11+
this.capacity = capacity;
12+
this.arr = new char[capacity];
13+
this.top = -1;
14+
}
15+
16+
private boolean isFull() {
17+
return Objects.equals(top,capacity - 1);
18+
}
19+
20+
private boolean isEmpty() {
21+
return -1 == top;
22+
}
23+
24+
public int getSize() {
25+
return top + 1;
26+
}
27+
28+
public void push(char x) {
29+
if (isFull()) {
30+
System.out.println("Stack is full");
31+
System.exit(1);
32+
}
33+
System.out.println("Inserting " + x);
34+
arr[++top] = x;
35+
}
36+
37+
public int pop() {
38+
if (isEmpty()) {
39+
System.out.println("STACK EMPTY");
40+
// terminates the program
41+
System.exit(1);
42+
}
43+
// pop element from top of stack
44+
return arr[top--];
45+
}
46+
47+
public void printStack() {
48+
for (int i = 0; i <= top; i++) {
49+
System.out.print(arr[i] + ", ");
50+
}
51+
}
52+
53+
public char[] getArr() {
54+
return arr;
55+
}
56+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package ru.mirea.practice.s230k754.lab11.task1;
2+
3+
import java.util.Scanner;
4+
import java.util.Stack;
5+
6+
final class Task1 {
7+
private Task1() {}
8+
9+
public static double algo(String expression) {
10+
// Разделим строку с большими пробелами
11+
String[] str = expression.split("\\s+");
12+
Stack<Double> stack = new Stack<>();
13+
14+
for (String t1 : str) {
15+
try {
16+
double num = Double.parseDouble(t1);
17+
stack.push(num);
18+
} catch (NumberFormatException e) {
19+
switch (t1) {
20+
case "+":
21+
stack.push(stack.pop() + stack.pop());
22+
break;
23+
case "-": {
24+
double operand2 = stack.pop();
25+
double operand1 = stack.pop();
26+
stack.push(operand1 - operand2);
27+
break;
28+
}
29+
case "*":
30+
stack.push(stack.pop() * stack.pop());
31+
break;
32+
case "/": {
33+
double operand2 = stack.pop();
34+
double operand1 = stack.pop();
35+
if (operand2 == 0) {
36+
System.out.println("Деление на 0");
37+
System.exit(1);
38+
}
39+
stack.push(operand1 / operand2);
40+
break;
41+
}
42+
default:
43+
System.out.println("Неизвестная операция");
44+
System.exit(1);
45+
break;
46+
}
47+
}
48+
}
49+
50+
if (stack.size() != 1) {
51+
throw new IllegalArgumentException("Некорректное выражение");
52+
}
53+
54+
return stack.pop();
55+
}
56+
57+
public static void main(String[] args) {
58+
try (Scanner scanner = new Scanner(System.in)) {
59+
String input = scanner.nextLine();
60+
try {
61+
double result = algo(input);
62+
System.out.println("Результат: " + result);
63+
} catch (Exception e) {
64+
System.err.println("Ошибка: " + e.getMessage());
65+
}
66+
}
67+
}
68+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package ru.mirea.practice.s230k754.lab11.task2;
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.SwingUtilities;
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.Objects;
13+
import java.util.Stack;
14+
15+
public final class Calculator extends JFrame implements ActionListener {
16+
private JTextField display;
17+
private String currentInput = "";
18+
19+
private Calculator() {
20+
setTitle("MyCalculator");
21+
setSize(250, 350);
22+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
23+
setLayout(new BorderLayout());
24+
25+
display = new JTextField();
26+
display.setEditable(false);
27+
display.setHorizontalAlignment(JTextField.RIGHT);
28+
add(display, BorderLayout.NORTH);
29+
30+
JPanel panel = new JPanel(new GridLayout(4, 4));
31+
32+
String[] buttons = {"7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "+", "="};
33+
34+
for (String button : buttons) {
35+
JButton button1 = new JButton(button);
36+
button1.addActionListener(this);
37+
panel.add(button1);
38+
}
39+
40+
add(panel, BorderLayout.CENTER);
41+
setVisible(true);
42+
}
43+
44+
public double algo(String expression) {
45+
String[] str = expression.split("");
46+
Stack<Double> stack = new Stack<>();
47+
48+
for (String t1 : str) {
49+
try {
50+
double num = Double.parseDouble(t1);
51+
stack.push(num);
52+
} catch (NumberFormatException e) {
53+
switch (t1) {
54+
case "+":
55+
stack.push(stack.pop() + stack.pop());
56+
break;
57+
case "-": {
58+
double operand2 = stack.pop();
59+
double operand1 = stack.pop();
60+
stack.push(operand1 - operand2);
61+
break;
62+
}
63+
case "*":
64+
stack.push(stack.pop() * stack.pop());
65+
break;
66+
case "/": {
67+
double operand2 = stack.pop();
68+
double operand1 = stack.pop();
69+
if (operand2 == 0) {
70+
System.out.println("Деление на 0");
71+
System.exit(1);
72+
}
73+
stack.push(operand1 / operand2);
74+
break;
75+
}
76+
default:
77+
System.out.println("Неизвестная операция");
78+
System.exit(1);
79+
break;
80+
}
81+
}
82+
}
83+
84+
if (stack.size() != 1) {
85+
throw new IllegalArgumentException("Некорректное выражение");
86+
}
87+
88+
return stack.pop();
89+
}
90+
91+
@Override
92+
public void actionPerformed(ActionEvent e) {
93+
String command = e.getActionCommand();
94+
95+
if (Objects.equals(command, "=")) {
96+
try {
97+
double result = algo(currentInput);
98+
display.setText(String.valueOf(result));
99+
currentInput = "";
100+
} catch (Exception ex) {
101+
display.setText("Ошибка");
102+
currentInput = "";
103+
}
104+
} else {
105+
currentInput += command;
106+
display.setText(currentInput);
107+
}
108+
}
109+
110+
111+
public static void main(String[] args) {
112+
SwingUtilities.invokeLater(() -> new Calculator().setVisible(true));
113+
}
114+
}

students/23K0754/23K0754-p023/pom.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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>23K0754</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0754-p023</artifactId>
12+
<description>Массивы</description>
13+
<dependencies>
14+
<dependency>
15+
<groupId>org.jetbrains</groupId>
16+
<artifactId>annotations</artifactId>
17+
<version>25.0.0</version>
18+
<scope>compile</scope>
19+
</dependency>
20+
</dependencies>
21+
</project>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package ru.mirea.practice.s230k754.lab11.task1;
2+
3+
public class ArrayQueueADt<T> {
4+
private T[] queue;
5+
private int head;
6+
private int tail;
7+
private int size;
8+
private int capacity;
9+
10+
public ArrayQueueADt(int capacity) {
11+
this.capacity = capacity;
12+
queue = (T[]) new Object[capacity];
13+
head = 0;
14+
tail = -1;
15+
size = 0;
16+
}
17+
18+
public boolean enqueue(T item) {
19+
if (size == capacity) {
20+
return false; // Очередь полная
21+
}
22+
tail = (tail + 1) % capacity;
23+
queue[tail] = item;
24+
size++;
25+
return true;
26+
}
27+
28+
public T dequeue() {
29+
if (size == 0) {
30+
throw new IllegalStateException("Очередь пуста");
31+
}
32+
final T item = queue[head];
33+
queue[head] = null;
34+
head = (head + 1) % capacity;
35+
size--;
36+
return item;
37+
}
38+
39+
public T element() {
40+
if (size == 0) {
41+
throw new IllegalStateException("Очередь пуста");
42+
}
43+
return queue[0];
44+
}
45+
46+
public boolean isEmpty() {
47+
return size == 0;
48+
}
49+
50+
public int size() {
51+
return size;
52+
}
53+
54+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package ru.mirea.practice.s230k754.lab11.task1;
2+
3+
public class Queue {
4+
5+
private int[] queue;
6+
private int head;
7+
private int tail;
8+
private int size;
9+
private int capacity;
10+
11+
public Queue(int capacity) {
12+
this.capacity = capacity;
13+
queue = new int[capacity];
14+
head = 0;
15+
tail = -1;
16+
size = 0;
17+
}
18+
19+
public boolean enqueue(int item) {
20+
if (size == capacity) {
21+
return false; // Очередь полная
22+
}
23+
tail = (tail + 1) % capacity; // Циклический буфер
24+
queue[tail] = item;
25+
size++;
26+
return true;
27+
}
28+
29+
public int dequeue() {
30+
if (size == 0) {
31+
throw new IllegalStateException("Очередь пуста");
32+
}
33+
int item = queue[head];
34+
head = (head + 1) % capacity; // Циклический буфер
35+
size--;
36+
return item;
37+
}
38+
39+
public boolean isEmpty() {
40+
return size == 0;
41+
}
42+
43+
public int size() {
44+
return size;
45+
}
46+
}

0 commit comments

Comments
 (0)