From 380611c4df87224f3836215c7aac7dc8e22d0aee Mon Sep 17 00:00:00 2001 From: me Date: Mon, 2 Feb 2026 13:25:10 +0500 Subject: [PATCH] add solutions for hw6:Base --- java/wspp/IntList.java | 28 +++++++++++++++ java/wspp/WordScanner.java | 56 ++++++++++++++++++++++++++++++ java/wspp/Wspp.java | 71 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 155 insertions(+) create mode 100644 java/wspp/IntList.java create mode 100644 java/wspp/WordScanner.java create mode 100644 java/wspp/Wspp.java diff --git a/java/wspp/IntList.java b/java/wspp/IntList.java new file mode 100644 index 0000000..e417afe --- /dev/null +++ b/java/wspp/IntList.java @@ -0,0 +1,28 @@ +package wspp; + +public class IntList { + private int[] array; + private int size; + + public IntList() { + array = new int[10]; + size = 0; + } + + public void add(int value) { + if (size >= array.length) { + int[] newArray = new int[array.length * 2]; + System.arraycopy(array, 0, newArray, 0, size); + array = newArray; + } + array[size++] = value; + } + + public int get(int index) { + return array[index]; + } + + public int size() { + return size; + } +} diff --git a/java/wspp/WordScanner.java b/java/wspp/WordScanner.java new file mode 100644 index 0000000..9f7872d --- /dev/null +++ b/java/wspp/WordScanner.java @@ -0,0 +1,56 @@ +package wspp; + +import java.io.*; +import java.nio.charset.StandardCharsets; + +public class WordScanner { + private BufferedReader br; + private String line = null; + private int pos = 0; + + public WordScanner(String fileName) throws IOException { + br = new BufferedReader(new InputStreamReader( + new FileInputStream(fileName), StandardCharsets.UTF_8)); + } + + private boolean hasNextLine() throws IOException { + if (line != null && pos < line.length()) return true; + line = br.readLine(); + pos = 0; + return line != null; + } + + private boolean isWordChar(char c) { + return Character.isLetter(c) || c == '\'' || + Character.getType(c) == Character.DASH_PUNCTUATION; + } + + public boolean hasNextWord() throws IOException { + while (hasNextLine()) { + while (pos < line.length() && !isWordChar(line.charAt(pos))) { + pos++; + } + if (pos < line.length()) { + return true; + } + } + return false; + } + + public String nextWord() { + while (pos < line.length() && !isWordChar(line.charAt(pos))) { + pos++; + } + + int start = pos; + while (pos < line.length() && isWordChar(line.charAt(pos))) { + pos++; + } + + return line.substring(start, pos).toLowerCase(); + } + + public void close() throws IOException { + br.close(); + } +} diff --git a/java/wspp/Wspp.java b/java/wspp/Wspp.java new file mode 100644 index 0000000..0d540f6 --- /dev/null +++ b/java/wspp/Wspp.java @@ -0,0 +1,71 @@ +package wspp; + +import java.io.*; +import java.nio.charset.StandardCharsets; +import java.util.*; + +public class Wspp { + public static void main(String[] args) { + if (args.length != 2) { + System.err.println("Usage: java Wspp inputFile outputFile"); + return; + } + + String inputFile = args[0]; + String outputFile = args[1]; + + try { + WordScanner scanner = new WordScanner(inputFile); + + // LinkedHashMap сохраняет порядок вставки + Map wordMap = new LinkedHashMap<>(); + int wordPosition = 1; + + while (scanner.hasNextWord()) { + String word = scanner.nextWord(); + + if (wordMap.containsKey(word)) { + WordInfo info = wordMap.get(word); + info.count++; + info.positions.add(wordPosition); + } else { + WordInfo info = new WordInfo(); + info.count = 1; + info.positions = new IntList(); + info.positions.add(wordPosition); + wordMap.put(word, info); + } + + wordPosition++; + } + + scanner.close(); + + // Запись результата + PrintWriter writer = new PrintWriter( + new OutputStreamWriter( + new FileOutputStream(outputFile), StandardCharsets.UTF_8)); + + for (Map.Entry entry : wordMap.entrySet()) { + String word = entry.getKey(); + WordInfo info = entry.getValue(); + + writer.print(word + " " + info.count); + for (int i = 0; i < info.positions.size(); i++) { + writer.print(" " + info.positions.get(i)); + } + writer.println(); + } + + writer.close(); + + } catch (IOException e) { + System.err.println("Error: " + e.getMessage()); + } + } + + static class WordInfo { + int count; + IntList positions; + } +}