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

Commit e767011

Browse files
Лабораторная 31-32
1 parent afc0faf commit e767011

File tree

8 files changed

+402
-0
lines changed

8 files changed

+402
-0
lines changed

students/23K0364/23K0364-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>23K0364</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0364-p31</artifactId>
12+
<description>31 задание</description>
13+
</project>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package ru.mirea.practice.s0000001;
2+
3+
import java.io.IOException;
4+
import java.io.BufferedReader;
5+
import java.io.FileReader;
6+
import java.io.BufferedWriter;
7+
import java.io.FileWriter;
8+
import java.util.List;
9+
import java.io.File;
10+
11+
final class FileManager {
12+
private FileManager() {
13+
throw new UnsupportedOperationException("Utility class");
14+
}
15+
16+
public static void loadFromFile(String filename, TwoThreeTree tree) throws IOException {
17+
File file = new File(filename);
18+
if (!file.exists()) {
19+
throw new IOException("Файл не найден: " + filename);
20+
}
21+
22+
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
23+
String line;
24+
while ((line = reader.readLine()) != null) {
25+
String[] data = line.split(", ");
26+
Processor processor = new Processor(Integer.parseInt(data[0]), data[1], Double.parseDouble(data[2]),
27+
Integer.parseInt(data[3]), Double.parseDouble(data[4]),
28+
Integer.parseInt(data[5]), Integer.parseInt(data[6]));
29+
tree.insert(processor);
30+
}
31+
}
32+
}
33+
34+
public static void saveToFile(String filename, TwoThreeTree tree) throws IOException {
35+
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename))) {
36+
List<Processor> processors = tree.toList();
37+
for (Processor processor : processors) {
38+
writer.write(processor.toString());
39+
writer.newLine();
40+
}
41+
}
42+
}
43+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package ru.mirea.practice.s0000001;
2+
3+
import java.io.IOException;
4+
import java.util.Scanner;
5+
6+
public abstract class Main {
7+
private static TwoThreeTree tree = new TwoThreeTree();
8+
9+
public static void main(String[] args) {
10+
try (Scanner scanner = new Scanner(System.in)) {
11+
try {
12+
FileManager.loadFromFile("/Users/viktoriapermakova/Desktop/PROCS.TXT", tree);
13+
System.out.println("Данные загружены.");
14+
} catch (IOException e) {
15+
System.err.println("Ошибка загрузки данных: " + e.getMessage());
16+
}
17+
18+
while (true) {
19+
System.out.print("Введите команду (L, D n, A n, S, E): ");
20+
String command = scanner.nextLine();
21+
22+
if ("L".equalsIgnoreCase(command)) {
23+
tree.printTree();
24+
} else if (command.startsWith("D")) {
25+
try {
26+
int key = Integer.parseInt(command.split(" ")[1]);
27+
tree.delete(key);
28+
System.out.println("Запись с ключом " + key + " удалена.");
29+
} catch (Exception e) {
30+
System.err.println("Ошибка удаления: " + e.getMessage());
31+
}
32+
} else if (command.startsWith("A")) {
33+
try {
34+
int key = Integer.parseInt(command.split(" ")[1]);
35+
System.out.print("Введите данные процессора (название, тактовая частота, кеш, частота шины, SPECint, SPECfp): ");
36+
String[] data = scanner.nextLine().split(", ");
37+
Processor processor = new Processor(key, data[0], Double.parseDouble(data[1]), Integer.parseInt(data[2]),
38+
Double.parseDouble(data[3]), Integer.parseInt(data[4]), Integer.parseInt(data[5]));
39+
tree.insert(processor);
40+
System.out.println("Запись добавлена: " + processor);
41+
} catch (Exception e) {
42+
System.err.println("Ошибка добавления: " + e.getMessage());
43+
}
44+
} else if ("S".equalsIgnoreCase(command)) {
45+
try {
46+
FileManager.saveToFile("/Users/viktoriapermakova/Desktop/PROCS.TXT", tree);
47+
System.out.println("Данные сохранены в файл.");
48+
} catch (IOException e) {
49+
System.err.println("Ошибка сохранения данных: " + e.getMessage());
50+
}
51+
} else if ("E".equalsIgnoreCase(command)) {
52+
break;
53+
} else {
54+
System.out.println("Неизвестная команда.");
55+
}
56+
}
57+
}
58+
}
59+
}
60+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package ru.mirea.practice.s0000001;
2+
3+
class Processor {
4+
int key;
5+
String name;
6+
double clockSpeed;
7+
int cacheSize;
8+
double busFrequency;
9+
int specInt;
10+
int specFp;
11+
12+
public Processor(int key, String name, double clockSpeed, int cacheSize, double busFrequency, int specInt, int specFp) {
13+
this.key = key;
14+
this.name = name;
15+
this.clockSpeed = clockSpeed;
16+
this.cacheSize = cacheSize;
17+
this.busFrequency = busFrequency;
18+
this.specInt = specInt;
19+
this.specFp = specFp;
20+
}
21+
22+
@Override
23+
public String toString() {
24+
return key + ", " + name + ", " + clockSpeed + ", " + cacheSize + ", " + busFrequency + ", " + specInt + ", " + specFp;
25+
}
26+
}
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
package ru.mirea.practice.s0000001;
2+
3+
import java.util.List;
4+
import java.util.ArrayList;
5+
6+
class TwoThreeTree {
7+
class Node {
8+
List<Integer> keys = new ArrayList<>();
9+
List<Node> children = new ArrayList<>();
10+
Processor processor;
11+
boolean isLeaf;
12+
13+
Node(Processor processor) {
14+
this.processor = processor;
15+
this.keys.add(processor.key);
16+
this.isLeaf = true;
17+
}
18+
19+
Node(int key) {
20+
this.keys.add(key);
21+
}
22+
23+
@Override
24+
public String toString() {
25+
if (isLeaf) {
26+
return String.valueOf(keys.get(0));
27+
} else if (keys.size() == 1) {
28+
return keys.get(0) + " -";
29+
} else {
30+
return keys.get(0) + " " + keys.get(1);
31+
}
32+
}
33+
}
34+
35+
Node root;
36+
37+
public void insert(Processor processor) {
38+
if (root == null) {
39+
root = new Node(processor);
40+
} else {
41+
Node newRoot = insertRecursive(root, processor);
42+
if (newRoot != null) {
43+
Node oldRoot = root;
44+
root = new Node(newRoot.keys.get(0));
45+
root.children.add(oldRoot);
46+
root.children.add(newRoot);
47+
root.isLeaf = false;
48+
}
49+
}
50+
}
51+
52+
private Node insertRecursive(Node node, Processor processor) {
53+
if (node.isLeaf) {
54+
node.keys.add(processor.key);
55+
node.keys.sort(Integer::compareTo);
56+
57+
if (node.keys.size() > 2) {
58+
return splitNode(node);
59+
}
60+
return null;
61+
} else {
62+
int key = processor.key;
63+
64+
Node child;
65+
if (key < node.keys.get(0)) {
66+
child = node.children.get(0);
67+
} else if (node.keys.size() == 1 || key < node.keys.get(1)) {
68+
child = node.children.get(1);
69+
} else {
70+
child = node.children.get(2);
71+
}
72+
73+
Node newChild = insertRecursive(child, processor);
74+
if (newChild != null) {
75+
node.keys.add(newChild.keys.get(0));
76+
node.keys.sort(Integer::compareTo);
77+
node.children.add(node.children.indexOf(child) + 1, newChild);
78+
79+
if (node.keys.size() > 2) {
80+
return splitNode(node);
81+
}
82+
}
83+
return null;
84+
}
85+
}
86+
87+
private Node splitNode(Node node) {
88+
Node newNode = new Node(node.keys.get(2));
89+
newNode.isLeaf = node.isLeaf;
90+
91+
if (!node.isLeaf) {
92+
newNode.children.add(node.children.remove(2));
93+
newNode.children.add(node.children.remove(2));
94+
}
95+
96+
node.keys.remove(2);
97+
return newNode;
98+
}
99+
100+
public void printTree() {
101+
if (root == null) {
102+
System.out.println("Дерево пустое.");
103+
return;
104+
}
105+
printTreeRecursively(root);
106+
}
107+
108+
private void printTreeRecursively(Node node) {
109+
if (node == null) {
110+
return;
111+
}
112+
System.out.println(node);
113+
for (Node child : node.children) {
114+
printTreeRecursively(child);
115+
}
116+
}
117+
118+
public void delete(int key) {
119+
root = deleteRecursive(root, key);
120+
}
121+
122+
private Node deleteRecursive(Node node, int key) {
123+
if (node == null) {
124+
System.out.println("Запись с ключом " + key + " не найдена.");
125+
return null;
126+
}
127+
128+
if (node.isLeaf) {
129+
if (node.keys.contains(key)) {
130+
node.keys.remove(Integer.valueOf(key));
131+
if (node.keys.isEmpty()) {
132+
return null;
133+
}
134+
} else {
135+
System.out.println("Запись с ключом " + key + " не найдена.");
136+
}
137+
return node;
138+
}
139+
140+
if (key < node.keys.get(0)) {
141+
Node child = deleteRecursive(node.children.get(0), key);
142+
node.children.set(0, child);
143+
} else if (node.keys.size() == 1 || key < node.keys.get(1)) {
144+
Node child = deleteRecursive(node.children.get(1), key);
145+
node.children.set(1, child);
146+
} else {
147+
Node child = deleteRecursive(node.children.get(2), key);
148+
node.children.set(2, child);
149+
}
150+
return node;
151+
}
152+
153+
public List<Processor> toList() {
154+
List<Processor> result = new ArrayList<>();
155+
collectProcessors(root, result);
156+
return result;
157+
}
158+
159+
private void collectProcessors(Node node, List<Processor> result) {
160+
if (node == null) {
161+
return;
162+
}
163+
164+
if (node.isLeaf) {
165+
if (node.processor != null) {
166+
result.add(node.processor);
167+
}
168+
}
169+
170+
for (Node child : node.children) {
171+
collectProcessors(child, result);
172+
}
173+
}
174+
}

students/23K0364/23K0364-p32/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>23K0364</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0364-p32</artifactId>
12+
<description>32 задание</description>
13+
</project>

0 commit comments

Comments
 (0)