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

Commit f20d1c5

Browse files
authored
Merge pull request #708 from w0RKintILDeaTH/lab21-32
Лабораторная работа №21-32
2 parents d56710a + 910f1a4 commit f20d1c5

File tree

13 files changed

+463
-1
lines changed

13 files changed

+463
-1
lines changed

students/23K0365/23K0365-p22/src/main/java/ru/mirea/practice/s0000001/Pln.java renamed to students/23K0365/23K0365-p22/src/main/java/ru/mirea/practice/s0000001/task1/Pln.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ru.mirea.practice.s0000001;
1+
package ru.mirea.practice.s0000001.task1;
22

33
import java.util.ArrayDeque;
44
import java.util.Deque;
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package ru.mirea.practice.s0000001.task2;
2+
3+
import javax.swing.JFrame;
4+
import javax.swing.JLabel;
5+
import javax.swing.JOptionPane;
6+
import javax.swing.JButton;
7+
import javax.swing.JPanel;
8+
import javax.swing.SwingConstants;
9+
import java.awt.BorderLayout;
10+
import java.awt.Font;
11+
import java.awt.Dimension;
12+
import java.awt.GridLayout;
13+
import java.awt.event.ActionEvent;
14+
import java.awt.event.ActionListener; //протестировано на "2", "3", "*", "4", "5", "*", "+"
15+
16+
public class Calcularot extends JFrame {
17+
private JLabel res = new JLabel("", SwingConstants.RIGHT);
18+
private String text = "";
19+
20+
Calcularot() {
21+
super("Calculator");
22+
setLayout(new BorderLayout());
23+
setSize(400, 500);
24+
res.setFont(new Font("Arial", Font.BOLD, 24));
25+
res.setPreferredSize(new Dimension(400, 50));
26+
add(res, BorderLayout.NORTH);
27+
JPanel buttonPanel = new JPanel();
28+
buttonPanel.setLayout(new GridLayout(4, 4, 5, 5));
29+
30+
String[] buttons = {
31+
"7", "8", "9", "/",
32+
"4", "5", "6", "*",
33+
"1", "2", "3", "-",
34+
"0", " ", "=", "+"
35+
};
36+
for (String btnText : buttons) {
37+
JButton button = new JButton(btnText);
38+
button.setFont(new Font("Arial", Font.PLAIN, 24));
39+
buttonPanel.add(button);
40+
button.addActionListener(new ActionListener() {
41+
@Override
42+
public void actionPerformed(ActionEvent e) {
43+
if ("=".equals(btnText)) {
44+
String formattedText = formatTextForCalculation(text);
45+
String[] tokens = formattedText.split(" ");
46+
double result = Pln.calculateSmth(tokens);
47+
if (!Double.isNaN(result)) {
48+
res.setText(String.valueOf(result));
49+
} else {
50+
JOptionPane.showMessageDialog(null, "Calculation error", "ERROR", JOptionPane.ERROR_MESSAGE);
51+
}
52+
text = "";
53+
} else if (" ".equals(btnText)) {
54+
text += " ";
55+
res.setText(text);
56+
} else if ("+".equals(btnText) | "*".equals(btnText)
57+
| "/".equals(btnText) | "-".equals(btnText)) {
58+
text += " " + btnText + " ";
59+
res.setText(text);
60+
} else {
61+
text += btnText;
62+
res.setText(text);
63+
}
64+
}
65+
});
66+
}
67+
68+
add(buttonPanel, BorderLayout.CENTER);
69+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
70+
setVisible(true);
71+
}
72+
73+
private String formatTextForCalculation(String input) {
74+
input = input.trim();
75+
StringBuilder formattedText = new StringBuilder();
76+
for (int i = 0; i < input.length(); i++) {
77+
char ch = input.charAt(i);
78+
if (Character.isDigit(ch)) {
79+
formattedText.append(ch);
80+
} else if (isOperator(ch)) {
81+
formattedText.append(" ").append(ch).append(" ");
82+
} else if (ch == ' ') {
83+
formattedText.append(" ");
84+
}
85+
}
86+
return formattedText.toString();
87+
}
88+
89+
private boolean isOperator(char ch) {
90+
return ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '^';
91+
}
92+
93+
public static void main(String[] args) {
94+
new Calcularot();
95+
}
96+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package ru.mirea.practice.s0000001.task2;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.Deque;
5+
6+
public abstract class Pln {
7+
private static boolean isOperate(String token) {
8+
return "+".equals(token) || "-".equals(token) || "*".equals(token) || "/".equals(token) || "^".equals(token);
9+
}
10+
11+
private static double operationChoose(double a, double b, String op) {
12+
switch (op) {
13+
case "+": return a + b;
14+
case "-": return a - b;
15+
case "*": return a * b;
16+
case "/":
17+
if (b == 0) {
18+
throw new ArithmeticException("INFINITY");
19+
}
20+
return a / b;
21+
default: return Double.NaN;
22+
}
23+
}
24+
25+
public static double calculateSmth(String... args) {
26+
Deque<Double> st = new ArrayDeque<>();
27+
for (String one : args) {
28+
try {
29+
double temp = Double.parseDouble(one);
30+
st.push(temp);
31+
} catch (NumberFormatException e) {
32+
if (isOperate(one)) {
33+
if (st.size() < 2) {
34+
System.err.println("too litle amount of opearnds: " + one);
35+
return Double.NaN;
36+
}
37+
double x = st.pop();
38+
double y = st.pop();
39+
try {
40+
st.push(operationChoose(y, x, one));
41+
} catch (ArithmeticException ae) {
42+
System.err.println("error: ");
43+
return Double.NaN;
44+
}
45+
} else {
46+
if (!(" ".equals(one) | one.isEmpty())) {
47+
System.err.println("not operatable");
48+
return Double.NaN;
49+
}
50+
}
51+
} catch (Exception e) {
52+
System.err.println("incorrect symbols");
53+
return Double.NaN;
54+
}
55+
}
56+
57+
if (st.size() != 1) {
58+
System.err.println("error");
59+
return Double.NaN;
60+
}
61+
return st.pop();
62+
}
63+
}
64+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package ru.mirea.practice.s0000001.tasksfrom1to3;
2+
3+
import java.util.PriorityQueue;
4+
5+
public abstract class TaskFour { //не совсем понятно задание
6+
public static void main(String[] args) {
7+
PriorityQueue<Integer> pQ = new PriorityQueue<>();
8+
for (int i = 1; i < 11; i++) {
9+
pQ.add(i);
10+
}
11+
while (!pQ.isEmpty()) {
12+
System.out.println(pQ.poll());
13+
}
14+
}
15+
}

students/23K0365/23K0365-p31/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>23K0365</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0365-p31</artifactId>
12+
<description>Массивы</description>
13+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package ru.mirea.practice.s0000001;
2+
3+
public final class Main {
4+
5+
private Main() {
6+
7+
}
8+
9+
public static void main(String[] args) {
10+
System.out.println("первая практическая работа!");
11+
}
12+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package ru.mirea.practice.s0000001.task1;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.ArrayList;
5+
import java.util.Deque;
6+
import java.util.List;
7+
8+
public abstract class MyyTree {
9+
public static void printer(Node head) {
10+
if (head == null) {
11+
return;
12+
}
13+
int leng = getHeight(head);
14+
List<String> levels = new ArrayList<>();
15+
Deque<Node> q = new ArrayDeque<>();
16+
q.offer(head);
17+
for (int level = 0; level < leng; level++) {
18+
int size = q.size();
19+
int b4 = (int) Math.pow(2, leng - level - 1) - 1;
20+
int betwen = (int) Math.pow(2, leng - level) - 1;
21+
StringBuilder answ = new StringBuilder(" ".repeat(b4));
22+
for (int i = 0; i < size; i++) {
23+
Node node = q.poll();
24+
answ.append(node.data);
25+
if (i < size - 1) {
26+
answ.append(" ".repeat(betwen));
27+
}
28+
for (int j = 0; j < node.childCnt; j++) {
29+
q.offer(node.childs[j]);
30+
}
31+
}
32+
levels.add(answ.toString());
33+
}
34+
for (int i = levels.size() - 1; i >= 0; i--) {
35+
System.out.println(levels.get(i));
36+
}
37+
}
38+
39+
private static int getHeight(Node head) {
40+
if (head == null) {
41+
return 0;
42+
}
43+
int leng = 0;
44+
Deque<Node> q = new ArrayDeque<>();
45+
q.offer(head);
46+
while (!q.isEmpty()) {
47+
int size = q.size();
48+
leng++;
49+
for (int i = 0; i < size; i++) {
50+
Node node = q.poll();
51+
for (int j = 0; j < node.childCnt; j++) {
52+
q.offer(node.childs[j]);
53+
}
54+
}
55+
}
56+
return leng;
57+
}
58+
59+
public static void main(String[] args) {
60+
Node main = new Node(5);
61+
Node a = new Node(2);
62+
Node b = new Node(6);
63+
Node c = new Node(1);
64+
final Node d = new Node(2);
65+
final Node e = new Node(5);
66+
final Node f = new Node(6);
67+
main.addChild(a);
68+
main.addChild(b);
69+
a.addChild(c);
70+
a.addChild(d);
71+
b.addChild(e);
72+
b.addChild(f);
73+
printer(main);
74+
}
75+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package ru.mirea.practice.s0000001.task1;
2+
3+
public class Node {
4+
int data;
5+
Node[] childs;
6+
int childCnt;
7+
8+
Node(int data) {
9+
this.data = data;
10+
this.childs = new Node[3];
11+
this.childCnt = 0;
12+
}
13+
14+
void addChild(Node child) {
15+
if (childCnt < 3) {
16+
childs[childCnt++] = child;
17+
}
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package ru.mirea.practice.s0000001.task2;
2+
3+
public class Node {
4+
int data;
5+
Node[] childs;
6+
int childCnt;
7+
8+
Node(int data) {
9+
this.data = data;
10+
this.childs = new Node[3];
11+
this.childCnt = 0;
12+
}
13+
14+
void addChild(Node child) {
15+
if (childCnt < 3) {
16+
childs[childCnt++] = child;
17+
}
18+
}
19+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package ru.mirea.practice.s0000001.task2;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.Deque;
5+
6+
public abstract class NotMyTree {
7+
public static void printer(Node head) {
8+
if (head == null) {
9+
return;
10+
}
11+
int leng = getHeight(head);
12+
Deque<Node> q = new ArrayDeque<>();
13+
q.offer(head);
14+
String result = "";
15+
for (int level = 1; level <= leng; level++) {
16+
int size = q.size();
17+
String currentLevelNodes = "";
18+
for (int i = 0; i < size; i++) {
19+
Node node = q.poll();
20+
currentLevelNodes += node.data;
21+
if (i < size - 1) {
22+
currentLevelNodes += ", ";
23+
}
24+
for (int j = 0; j < node.childCnt; j++) {
25+
if (node.childs[j] != null) {
26+
q.offer(node.childs[j]);
27+
}
28+
}
29+
}
30+
result += currentLevelNodes + ", ";
31+
}
32+
33+
if (result.length() > 0) {
34+
result = result.substring(0, result.length() - 2);
35+
}
36+
System.out.println(result);
37+
}
38+
39+
private static int getHeight(Node head) {
40+
if (head == null) {
41+
return 0;
42+
}
43+
int leng = 1;
44+
for (int i = 0; i < head.childCnt; i++) {
45+
if (head.childs[i] != null) {
46+
leng = Math.max(leng, 1 + getHeight(head.childs[i]));
47+
}
48+
}
49+
return leng;
50+
}
51+
52+
public static void main(String[] args) {
53+
Node main = new Node(5);
54+
Node a = new Node(2);
55+
Node b = new Node(6);
56+
Node c = new Node(1);
57+
final Node d = new Node(2);
58+
final Node e = new Node(5);
59+
final Node f = new Node(6);
60+
main.addChild(a);
61+
main.addChild(b);
62+
a.addChild(c);
63+
a.addChild(d);
64+
b.addChild(e);
65+
b.addChild(f);
66+
printer(main);
67+
}
68+
}

0 commit comments

Comments
 (0)