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

Лабораторная работа №21-32 #708

Merged
merged 1 commit into from
Dec 14, 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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.mirea.practice.s0000001;
package ru.mirea.practice.s0000001.task1;

import java.util.ArrayDeque;
import java.util.Deque;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package ru.mirea.practice.s0000001.task2;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; //протестировано на "2", "3", "*", "4", "5", "*", "+"

public class Calcularot extends JFrame {
private JLabel res = new JLabel("", SwingConstants.RIGHT);
private String text = "";

Calcularot() {
super("Calculator");
setLayout(new BorderLayout());
setSize(400, 500);
res.setFont(new Font("Arial", Font.BOLD, 24));
res.setPreferredSize(new Dimension(400, 50));
add(res, BorderLayout.NORTH);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4, 5, 5));

String[] buttons = {
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", " ", "=", "+"
};
for (String btnText : buttons) {
JButton button = new JButton(btnText);
button.setFont(new Font("Arial", Font.PLAIN, 24));
buttonPanel.add(button);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if ("=".equals(btnText)) {
String formattedText = formatTextForCalculation(text);
String[] tokens = formattedText.split(" ");
double result = Pln.calculateSmth(tokens);
if (!Double.isNaN(result)) {
res.setText(String.valueOf(result));
} else {
JOptionPane.showMessageDialog(null, "Calculation error", "ERROR", JOptionPane.ERROR_MESSAGE);
}
text = "";
} else if (" ".equals(btnText)) {
text += " ";
res.setText(text);
} else if ("+".equals(btnText) | "*".equals(btnText)
| "/".equals(btnText) | "-".equals(btnText)) {
text += " " + btnText + " ";
res.setText(text);
} else {
text += btnText;
res.setText(text);
}
}
});
}

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

private String formatTextForCalculation(String input) {
input = input.trim();
StringBuilder formattedText = new StringBuilder();
for (int i = 0; i < input.length(); i++) {
char ch = input.charAt(i);
if (Character.isDigit(ch)) {
formattedText.append(ch);
} else if (isOperator(ch)) {
formattedText.append(" ").append(ch).append(" ");
} else if (ch == ' ') {
formattedText.append(" ");
}
}
return formattedText.toString();
}

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

public static void main(String[] args) {
new Calcularot();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package ru.mirea.practice.s0000001.task2;

import java.util.ArrayDeque;
import java.util.Deque;

public abstract class Pln {
private static boolean isOperate(String token) {
return "+".equals(token) || "-".equals(token) || "*".equals(token) || "/".equals(token) || "^".equals(token);
}

private static double operationChoose(double a, double b, String op) {
switch (op) {
case "+": return a + b;
case "-": return a - b;
case "*": return a * b;
case "/":
if (b == 0) {
throw new ArithmeticException("INFINITY");
}
return a / b;
default: return Double.NaN;
}
}

public static double calculateSmth(String... args) {
Deque<Double> st = new ArrayDeque<>();
for (String one : args) {
try {
double temp = Double.parseDouble(one);
st.push(temp);
} catch (NumberFormatException e) {
if (isOperate(one)) {
if (st.size() < 2) {
System.err.println("too litle amount of opearnds: " + one);
return Double.NaN;
}
double x = st.pop();
double y = st.pop();
try {
st.push(operationChoose(y, x, one));
} catch (ArithmeticException ae) {
System.err.println("error: ");
return Double.NaN;
}
} else {
if (!(" ".equals(one) | one.isEmpty())) {
System.err.println("not operatable");
return Double.NaN;
}
}
} catch (Exception e) {
System.err.println("incorrect symbols");
return Double.NaN;
}
}

if (st.size() != 1) {
System.err.println("error");
return Double.NaN;
}
return st.pop();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ru.mirea.practice.s0000001.tasksfrom1to3;

import java.util.PriorityQueue;

public abstract class TaskFour { //не совсем понятно задание
public static void main(String[] args) {
PriorityQueue<Integer> pQ = new PriorityQueue<>();
for (int i = 1; i < 11; i++) {
pQ.add(i);
}
while (!pQ.isEmpty()) {
System.out.println(pQ.poll());
}
}
}
13 changes: 13 additions & 0 deletions students/23K0365/23K0365-p31/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>23K0365</artifactId>
<groupId>ru.mirea.practice</groupId>
<version>2024.1</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>23K0365-p31</artifactId>
<description>Массивы</description>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ru.mirea.practice.s0000001;

public final class Main {

private Main() {

}

public static void main(String[] args) {
System.out.println("первая практическая работа!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package ru.mirea.practice.s0000001.task1;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

public abstract class MyyTree {
public static void printer(Node head) {
if (head == null) {
return;
}
int leng = getHeight(head);
List<String> levels = new ArrayList<>();
Deque<Node> q = new ArrayDeque<>();
q.offer(head);
for (int level = 0; level < leng; level++) {
int size = q.size();
int b4 = (int) Math.pow(2, leng - level - 1) - 1;
int betwen = (int) Math.pow(2, leng - level) - 1;
StringBuilder answ = new StringBuilder(" ".repeat(b4));
for (int i = 0; i < size; i++) {
Node node = q.poll();
answ.append(node.data);
if (i < size - 1) {
answ.append(" ".repeat(betwen));
}
for (int j = 0; j < node.childCnt; j++) {
q.offer(node.childs[j]);
}
}
levels.add(answ.toString());
}
for (int i = levels.size() - 1; i >= 0; i--) {
System.out.println(levels.get(i));
}
}

private static int getHeight(Node head) {
if (head == null) {
return 0;
}
int leng = 0;
Deque<Node> q = new ArrayDeque<>();
q.offer(head);
while (!q.isEmpty()) {
int size = q.size();
leng++;
for (int i = 0; i < size; i++) {
Node node = q.poll();
for (int j = 0; j < node.childCnt; j++) {
q.offer(node.childs[j]);
}
}
}
return leng;
}

public static void main(String[] args) {
Node main = new Node(5);
Node a = new Node(2);
Node b = new Node(6);
Node c = new Node(1);
final Node d = new Node(2);
final Node e = new Node(5);
final Node f = new Node(6);
main.addChild(a);
main.addChild(b);
a.addChild(c);
a.addChild(d);
b.addChild(e);
b.addChild(f);
printer(main);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ru.mirea.practice.s0000001.task1;

public class Node {
int data;
Node[] childs;
int childCnt;

Node(int data) {
this.data = data;
this.childs = new Node[3];
this.childCnt = 0;
}

void addChild(Node child) {
if (childCnt < 3) {
childs[childCnt++] = child;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ru.mirea.practice.s0000001.task2;

public class Node {
int data;
Node[] childs;
int childCnt;

Node(int data) {
this.data = data;
this.childs = new Node[3];
this.childCnt = 0;
}

void addChild(Node child) {
if (childCnt < 3) {
childs[childCnt++] = child;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package ru.mirea.practice.s0000001.task2;

import java.util.ArrayDeque;
import java.util.Deque;

public abstract class NotMyTree {
public static void printer(Node head) {
if (head == null) {
return;
}
int leng = getHeight(head);
Deque<Node> q = new ArrayDeque<>();
q.offer(head);
String result = "";
for (int level = 1; level <= leng; level++) {
int size = q.size();
String currentLevelNodes = "";
for (int i = 0; i < size; i++) {
Node node = q.poll();
currentLevelNodes += node.data;
if (i < size - 1) {
currentLevelNodes += ", ";
}
for (int j = 0; j < node.childCnt; j++) {
if (node.childs[j] != null) {
q.offer(node.childs[j]);
}
}
}
result += currentLevelNodes + ", ";
}

if (result.length() > 0) {
result = result.substring(0, result.length() - 2);
}
System.out.println(result);
}

private static int getHeight(Node head) {
if (head == null) {
return 0;
}
int leng = 1;
for (int i = 0; i < head.childCnt; i++) {
if (head.childs[i] != null) {
leng = Math.max(leng, 1 + getHeight(head.childs[i]));
}
}
return leng;
}

public static void main(String[] args) {
Node main = new Node(5);
Node a = new Node(2);
Node b = new Node(6);
Node c = new Node(1);
final Node d = new Node(2);
final Node e = new Node(5);
final Node f = new Node(6);
main.addChild(a);
main.addChild(b);
a.addChild(c);
a.addChild(d);
b.addChild(e);
b.addChild(f);
printer(main);
}
}
Loading
Loading