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

Commit 9aaa47e

Browse files
authored
Merge pull request #617 from MikleC/Лабораторная№19-20
Lab21-22
2 parents 9dd3da6 + 9ece824 commit 9aaa47e

File tree

14 files changed

+504
-0
lines changed

14 files changed

+504
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package ru.mirea.practice.s0000001.n1;
2+
3+
public class ArrQ {
4+
private Object[] q;
5+
private int fr;
6+
private int rr;
7+
private int sz;
8+
9+
public ArrQ(int cap) {
10+
q = new Object[cap];
11+
fr = 0;
12+
rr = 0;
13+
sz = 0;
14+
}
15+
16+
public void add(Object el) {
17+
ensureCap();
18+
q[rr] = el;
19+
rr = (rr + 1) % q.length;
20+
sz++;
21+
}
22+
23+
public Object peek() {
24+
return sz > 0 ? q[fr] : null;
25+
}
26+
27+
public Object poll() {
28+
if (sz == 0) {
29+
return null;
30+
}
31+
final Object el = q[fr];
32+
q[fr] = null;
33+
fr = (fr + 1) % q.length;
34+
sz--;
35+
return el;
36+
}
37+
38+
public int size() {
39+
return sz;
40+
}
41+
42+
public boolean isEmpty() {
43+
return sz == 0;
44+
}
45+
46+
public void clear() {
47+
q = new Object[q.length];
48+
fr = 0;
49+
rr = 0;
50+
sz = 0;
51+
}
52+
53+
private void ensureCap() {
54+
if (sz == q.length) {
55+
Object[] nq = new Object[q.length * 2];
56+
for (int i = 0; i < sz; i++) {
57+
nq[i] = q[(fr + i) % q.length];
58+
}
59+
fr = 0;
60+
rr = sz;
61+
q = nq;
62+
}
63+
}
64+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package ru.mirea.practice.s0000001.n1;
2+
3+
public class ArrQAdt {
4+
private Object[] q;
5+
private int fr;
6+
private int rr;
7+
private int sz;
8+
9+
public ArrQAdt(int cap) {
10+
q = new Object[cap];
11+
fr = 0;
12+
rr = 0;
13+
sz = 0;
14+
}
15+
16+
public static void add(ArrQAdt queue, Object el) {
17+
queue.ensureCap();
18+
queue.q[queue.rr] = el;
19+
queue.rr = (queue.rr + 1) % queue.q.length;
20+
queue.sz++;
21+
}
22+
23+
public static Object peek(ArrQAdt queue) {
24+
if (queue.sz > 0) {
25+
return queue.q[queue.fr];
26+
} else {
27+
return null;
28+
}
29+
}
30+
31+
public static Object poll(ArrQAdt queue) {
32+
if (queue.sz == 0) {
33+
return null;
34+
}
35+
final Object el = queue.q[queue.fr];
36+
queue.q[queue.fr] = null;
37+
queue.fr = (queue.fr + 1) % queue.q.length;
38+
queue.sz--;
39+
return el;
40+
}
41+
42+
public static int size(ArrQAdt queue) {
43+
return queue.sz;
44+
}
45+
46+
public static boolean isEmpty(ArrQAdt queue) {
47+
return queue.sz == 0;
48+
}
49+
50+
public static void clear(ArrQAdt queue) {
51+
queue.q = new Object[queue.q.length];
52+
queue.fr = 0;
53+
queue.rr = 0;
54+
queue.sz = 0;
55+
}
56+
57+
private void ensureCap() {
58+
if (sz == q.length) {
59+
Object[] nq = new Object[q.length * 2];
60+
for (int i = 0; i < sz; i++) {
61+
nq[i] = q[(fr + i) % q.length];
62+
}
63+
fr = 0;
64+
rr = sz;
65+
q = nq;
66+
}
67+
}
68+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package ru.mirea.practice.s0000001.n1;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public final class ArrQMod {
7+
private ArrQMod() {
8+
}
9+
10+
public static <T> List<T> convertToList(T[] array) {
11+
List<T> list = new ArrayList<>();
12+
for (T element : array) {
13+
list.add(element);
14+
}
15+
return list;
16+
}
17+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package ru.mirea.practice.s0000001.n2;
2+
3+
public class DataCr<T> {
4+
private T[] data;
5+
6+
public DataCr(T[] data) {
7+
this.data = data;
8+
}
9+
10+
public T[] getData() {
11+
return data;
12+
}
13+
14+
public void setData(T[] data) {
15+
this.data = data;
16+
}
17+
18+
public T getEl(int idx) {
19+
if (idx < 0 || idx >= data.length) {
20+
throw new RuntimeException("Индекс вне диапазона!");
21+
}
22+
return data[idx];
23+
}
24+
25+
public int getSz() {
26+
return data.length;
27+
}
28+
29+
public static void main(String[] args) {
30+
Integer[] intArr = {1, 2, 3, 4};
31+
String[] strArr = {"a", "b", "c"};
32+
33+
DataCr<Integer> intCr = new DataCr<>(intArr);
34+
DataCr<String> strCr = new DataCr<>(strArr);
35+
36+
System.out.println(intCr.getEl(2)); //вывод 3
37+
System.out.println(strCr.getEl(1)); //вывод b
38+
System.out.println("Размер intCr: " + intCr.getSz());
39+
}
40+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package ru.mirea.practice.s0000001.n3;
2+
3+
public class ArrUtils<T> {
4+
public T getByIdx(T[] arr, int idx) {
5+
if (idx < 0 || idx >= arr.length) {
6+
throw new RuntimeException("Индекс вне диапазона");
7+
}
8+
return arr[idx];
9+
}
10+
11+
public static void main(String[] args) {
12+
ArrUtils<Integer> intUtils = new ArrUtils<>();
13+
ArrUtils<String> strUtils = new ArrUtils<>();
14+
15+
Integer[] intArr = {10, 20, 30, 40};
16+
String[] strArr = {"apple", "banana", "cherry"};
17+
18+
System.out.println(intUtils.getByIdx(intArr, 2)); //вывод 30
19+
System.out.println(strUtils.getByIdx(strArr, 1)); //вывод banana
20+
}
21+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package ru.mirea.practice.s0000001.n4;
2+
3+
import java.io.File;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
public class DirUtils {
8+
public List<String> saveDir(String path) {
9+
List<String> files = new ArrayList<>();
10+
File dir = new File(path);
11+
if (dir.isDirectory()) {
12+
File[] fileList = dir.listFiles();
13+
if (fileList != null) {
14+
for (File file : fileList) {
15+
files.add(file.getName());
16+
}
17+
}
18+
} else {
19+
throw new RuntimeException("Не является каталогом:(");
20+
}
21+
return files;
22+
}
23+
24+
public void printFirst5(List<String> files) {
25+
int count = Math.min(5, files.size());
26+
for (int i = 0; i < count; i++) {
27+
System.out.println(files.get(i));
28+
}
29+
}
30+
31+
public static void main(String[] args) {
32+
DirUtils dirUtils = new DirUtils();
33+
String dirPath = ".";
34+
35+
List<String> fileList = dirUtils.saveDir(dirPath);
36+
dirUtils.printFirst5(fileList);
37+
}
38+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package ru.mirea.practice.s0000001.n5;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.HashSet;
6+
import java.util.List;
7+
import java.util.Map;
8+
import java.util.Set;
9+
10+
public final class Solution {
11+
private Solution() {
12+
}
13+
14+
public static <T> List<T> newArrayList(T... items) {
15+
List<T> list = new ArrayList<>();
16+
for (T item : items) {
17+
list.add(item);
18+
}
19+
return list;
20+
}
21+
22+
public static <T> Set<T> newHashSet(T... items) {
23+
Set<T> set = new HashSet<>();
24+
for (T item : items) {
25+
set.add(item);
26+
}
27+
return set;
28+
}
29+
30+
public static <K, V> Map<K, V> newHashMap(K[] keys, V[] values) {
31+
if (keys.length != values.length) {
32+
throw new RuntimeException("Ключи и значения имеют разный размер:(");
33+
}
34+
Map<K, V> map = new HashMap<>();
35+
for (int i = 0; i < keys.length; i++) {
36+
map.put(keys[i], values[i]);
37+
}
38+
return map;
39+
}
40+
}

students/23K0565/23K0565-p22/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="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>23K0565</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0565-p22</artifactId>
12+
<description>22 практическая</description>
13+
</project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package ru.mirea.practice.s0000001.n1;
2+
3+
public class IllegArg extends RuntimeException {
4+
public IllegArg(String msg) {
5+
super(msg);
6+
}
7+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package ru.mirea.practice.s0000001.n1;
2+
3+
import java.util.Stack;
4+
5+
public abstract class RPnCalc {
6+
public static int calcRPn(String exp) {
7+
Stack<Integer> st = new Stack<>();
8+
String[] tok = exp.split(" ");
9+
10+
for (String t : tok) {
11+
if (isNum(t)) {
12+
st.push(Integer.parseInt(t));
13+
} else {
14+
int b = st.pop();
15+
int a = st.pop();
16+
switch (t) {
17+
case "+":
18+
st.push(a + b);
19+
break;
20+
case "-":
21+
st.push(a - b);
22+
break;
23+
case "*":
24+
st.push(a * b);
25+
break;
26+
case "/":
27+
st.push(a / b);
28+
break;
29+
default:
30+
throw new IllegArg("Этот оператор неразрешён: " + t);
31+
}
32+
}
33+
}
34+
return st.pop();
35+
}
36+
37+
private static boolean isNum(String s) {
38+
try {
39+
Integer.parseInt(s);
40+
return true;
41+
} catch (NumberFormatException e) {
42+
return false;
43+
}
44+
}
45+
46+
public static void main(String[] args) {
47+
String exp = "3 5 + 2 *"; // Например
48+
System.out.println(calcRPn(exp));
49+
}
50+
}

0 commit comments

Comments
 (0)