From 13db841de369ea5ae3c8c85a2cea699d61b985fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C5=A1per=20Dobrovoljc?= Date: Sat, 1 Jun 2024 13:23:09 +0200 Subject: [PATCH] DN11 --- src/DN11.java | 56 ++++++++++++++ src/Kviz4.java | 189 +++++++++++++++++++++++++++++++++++++++++++++ viri/besedilo1.txt | 1 + viri/skrito.dat | 1 + viri/style.css | 93 ++++++++++++++++++++++ 5 files changed, 340 insertions(+) create mode 100644 src/DN11.java create mode 100644 src/Kviz4.java create mode 100644 viri/besedilo1.txt create mode 100644 viri/skrito.dat create mode 100644 viri/style.css diff --git a/src/DN11.java b/src/DN11.java new file mode 100644 index 0000000..67a6b4e --- /dev/null +++ b/src/DN11.java @@ -0,0 +1,56 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class DN11 { + public static void main(String[] args) { + var frame = new JFrame(); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setTitle("VELIKE ČRKE"); + frame.setSize(600, 400); + frame.setLocationRelativeTo(null); + frame.setLayout(new GridBagLayout()); + + var textArea1 = new JTextArea(); + + var gbc = new GridBagConstraints(); + gbc.gridx = 1; + gbc.gridy = 0; + gbc.weightx = 1; + gbc.weighty = 1; + gbc.fill = GridBagConstraints.BOTH; + gbc.insets = new Insets(10, 10, 10, 10); + frame.add(new JScrollPane(textArea1), gbc); + + var button = new JButton("--> pretvori"); + + gbc = new GridBagConstraints(); + gbc.gridx = 2; + gbc.gridy = 0; + gbc.weightx = 0; + gbc.weighty = 0; + gbc.fill = GridBagConstraints.NONE; + frame.add(button, gbc); + + var textArea2 = new JTextArea(); + + gbc = new GridBagConstraints(); + gbc.gridx = 3; + gbc.gridy = 0; + gbc.weightx = 1; + gbc.weighty = 1; + gbc.fill = GridBagConstraints.BOTH; + gbc.insets = new Insets(10, 10, 10, 10); + frame.add(new JScrollPane(textArea2), gbc); + + button.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + textArea2.setText(textArea1.getText().toUpperCase()); + } + }); + + frame.setVisible(true); + } +} diff --git a/src/Kviz4.java b/src/Kviz4.java new file mode 100644 index 0000000..84231a0 --- /dev/null +++ b/src/Kviz4.java @@ -0,0 +1,189 @@ +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +public class Kviz4 { + public static void main(String[] args) { + System.out.println(Arrays.toString(getVrstica(20))); + } + + static java.util.Map cache = new java.util.HashMap<>(); + + public static int[] getVrstica(int n) { + if (n == 1) { + return new int[]{1}; + } + if (cache.containsKey(n)) { + return cache.get(n); + } + + int[] vrstica = new int[n]; + vrstica[0] = n % 10; + + for (int i = 1; i < n; i++) { + vrstica[i] = (vrstica[i - 1] + getVrstica(n - 1)[i - 1]) % 10; + } + + cache.put(n, vrstica); + return vrstica; + } + + public static void statistikaStavkov(String imeDatoteke) { + class IzjemaManjkajocegaLocila extends Exception { + public IzjemaManjkajocegaLocila(String message) { + super(message); + } + } + + try (java.io.BufferedReader br = new java.io.BufferedReader(new java.io.FileReader(imeDatoteke))) { + java.util.Map dolzinaStavkov = new java.util.HashMap<>(); + String line; + int stevec = 1; // Za sledenje vrsticam + while ((line = br.readLine()) != null) { + String[] stavki = line.split("[.!?]"); + for (String stavek : stavki) { + stavek = stavek.trim(); + if (!stavek.isEmpty()) { + if (!stavek.endsWith(".") && !stavek.endsWith("!") && !stavek.endsWith("?")) { + throw new IzjemaManjkajocegaLocila("Stavek na vrstici " + stevec + " se ne konča z ustrezno ločilo."); + } + int dolzina = stavek.split("\\s+").length; + dolzinaStavkov.put(dolzina, dolzinaStavkov.getOrDefault(dolzina, 0) + 1); + } + stevec++; + } + } + // Izpis rezultatov + for (java.util.Map.Entry entry : dolzinaStavkov.entrySet()) { + System.out.println("Stavki dolzine " + entry.getKey() + " se pojavijo: " + entry.getValue() + "x."); + } + } catch (java.io.IOException e) { + System.out.println("Napaka pri branju datoteke."); + } catch (IzjemaManjkajocegaLocila e) { + System.out.println(e.getMessage()); + } + } + + public static void poisciInIzpisiBarve(String imeDatoteke) { + try (java.io.BufferedReader br = new java.io.BufferedReader(new java.io.FileReader(imeDatoteke))) { + String line; + java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("(^|[^\\-])color: #([0-9a-fA-F]{6})"); + + while ((line = br.readLine()) != null) { + java.util.regex.Matcher matcher = pattern.matcher(line); + while (matcher.find()) { + String hexColor = matcher.group(2); + int r = Integer.parseInt(hexColor.substring(0, 2), 16); + int g = Integer.parseInt(hexColor.substring(2, 4), 16); + int b = Integer.parseInt(hexColor.substring(4, 6), 16); + + // Convert RGB to HSL + float[] hsl = rgbToHsl(r, g, b); + + System.out.printf("#%s -> rgb(%d, %d, %d) -> hsl(%d, %d, %d)%n", + hexColor, r, g, b, Math.round(hsl[0]), Math.round(hsl[1]), Math.round(hsl[2])); + } + } + } catch (java.io.IOException e) { + e.printStackTrace(); + } + } + + private static float[] rgbToHsl(int r, int g, int b) { + float rNorm = r / 255.0f; + float gNorm = g / 255.0f; + float bNorm = b / 255.0f; + + float max = Math.max(rNorm, Math.max(gNorm, bNorm)); + float min = Math.min(rNorm, Math.min(gNorm, bNorm)); + float delta = max - min; + + float h = 0; + if (delta != 0) { + if (max == rNorm) { + h = ((gNorm - bNorm) / delta) % 6; + } else if (max == gNorm) { + h = ((bNorm - rNorm) / delta) + 2; + } else { + h = ((rNorm - gNorm) / delta) + 4; + } + h *= 60; + if (h < 0) { + h += 360; + } + } + + float l = (max + min) / 2; + + float s = 0; + if (delta != 0) { + s = delta / (1 - Math.abs(2 * l - 1)); + } + + return new float[]{h, s * 100, l * 100}; + } + + public static void izpisi(String imeDatoteke) { + try (java.io.FileInputStream fis = new java.io.FileInputStream(imeDatoteke); + java.nio.channels.FileChannel channel = fis.getChannel()) { + + java.nio.ByteBuffer buffer = java.nio.ByteBuffer.allocate(3); + + while (channel.read(buffer) != -1) { + buffer.flip(); + int number = buffer.getInt(); + System.out.println(formatTelefonskeStevilke(number)); + buffer.clear(); + } + + } catch (java.io.IOException e) { + e.printStackTrace(); + } + } + + public static void preveri(String stevilka, String imeDatoteke) { + int iskanaStevilka = parseTelefonskaStevilka(stevilka); + + try (java.io.FileInputStream fis = new java.io.FileInputStream(imeDatoteke); + java.nio.channels.FileChannel channel = fis.getChannel()) { + + java.nio.ByteBuffer buffer = java.nio.ByteBuffer.allocate(3); + + while (channel.read(buffer) != -1) { + buffer.flip(); + byte[] bytes = new byte[3]; + buffer.get(bytes); + + int number = ((bytes[1] << 28)) >>> 12 | (bytes[1]) | bytes[2]; + System.out.println(number); + if (number == iskanaStevilka) { + System.out.println("Številka " + stevilka + " je v datoteki."); + return; + } + buffer.clear(); + } + + System.out.println("Številke " + stevilka + " ni v datoteki."); + + } catch (java.io.IOException e) { + e.printStackTrace(); + } + } + + private static String formatTelefonskeStevilke(int number) { + int podrocnaKoda = number / 1000000; + int preostanek = number % 1000000; + int prvaTrojka = preostanek / 1000; + int drugaTrojka = preostanek % 1000; + return String.format("%d / %03d %03d", podrocnaKoda, prvaTrojka, drugaTrojka); + } + + private static int parseTelefonskaStevilka(String stevilka) { + String[] parts = stevilka.split("/"); + int podrocnaKoda = Integer.parseInt(parts[0].trim()); + parts = parts[1].trim().split(" "); + int prvaTrojka = Integer.parseInt(parts[0].trim()); + int drugaTrojka = Integer.parseInt(parts[1].trim()); + return (podrocnaKoda * 1000000) + (prvaTrojka * 1000) + drugaTrojka; + } +} diff --git a/viri/besedilo1.txt b/viri/besedilo1.txt new file mode 100644 index 0000000..9371ef8 --- /dev/null +++ b/viri/besedilo1.txt @@ -0,0 +1 @@ +V tekstovni datoteki imamo shranjeno besedilo, sestavljeno iz stavkov, ki so lahko poljubno dolgi (vsebujejo poljubno mnogo besed). Vaša naloga je, da napišete program, ki bo odprl tekstovno datoteko, podano kot parameter ukazne vrstice, prebral celotno vsebino in za podano datoteko izpisal vse najdene dolžine stavkov in število pojavitev stavkov te dolžine (kot ločila med besedami upoštevajte presledke, kot znak za konec stavka pa upoštevajte piko, klicaj oziroma vprašaj. V primeru, da se stavek ne konča s piko, klicajem oziroma vprašajem, pa sprožite izjemo IzjemaManjkajocegaLocila. Definirajte tudi ta razred in ustrezno obravnavajte to izjemo. \ No newline at end of file diff --git a/viri/skrito.dat b/viri/skrito.dat new file mode 100644 index 0000000..9e5634c --- /dev/null +++ b/viri/skrito.dat @@ -0,0 +1 @@ +<)Q':Do+v8f