当前位置:   article > 正文

Java课程设计——哈夫曼编码译码系统的Java语言实现(附Java源码与课程设计报告)_哈夫曼编码与译码java运行结果

哈夫曼编码与译码java运行结果

以下都是我在CSDN发布是文章好吧。

数据库课程设计——某工厂的物料管理系统(附Java源码与课程设计报告)
数据库课程设计——某商店进销存管理系统(附Java源码与课程设计报告)
数据库课程设计——某煤气公司送气管理系统(附课设报告)
数据库课程设计——基于JavaEE的企业进销存系统(附Java源码与课程设计报告)
Java课程设计——哈夫曼编码译码系统的Java语言实现
C语言课程设计——班级成绩管理系统(附课设报告)
C语言课程设计——排班系统 DFS解决(附课设报告)

0. 系统展示

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

1. 系统类图

在这里插入图片描述

2. 课程设计报告

在这里插入图片描述

3. 部分源码展示

import java.io.*;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;

public class HuffmanEncoder implements DataShare {
    private String fileName;
    private Node huffmanTree;
    private HashMap<Character, Integer> charFrequencies;
    private HashMap<Character, String> huffmanCodeMap;
    private String text;
    private String encodedText;
    /*private static final String treeExportPath = "G:\\project\\test data\\HuffmanTree.txt";
    private static final String encodedTextExportPath = "G:\\project\\test data\\EncodedText.txt";*/
    private static final String treeExportPath = "HuffmanTree.txt";
    private static final String encodedTextExportPath = "EncodedText.txt";

    HuffmanEncoder(String fileName) {
        // just don't want to fold the code
        this.fileName = fileName;
        charFrequencies = new HashMap<>();
    }

    public String getEncodedText() {
        // just don't want to fold the code
        return encodedText;
    }

    public String getText() {
        // just don't want to fold the code
        return text;
    }

    private void getCharFrequencies() {
        int ch;
        StringBuilder textBuilder = new StringBuilder();
        try {
            Reader in = new FileReader(fileName);
            while ((ch = in.read()) != -1) {
                char c = (char)ch;
                textBuilder.append(c);
                charFrequencies.put(c, charFrequencies.containsKey(c) ? charFrequencies.get(c) + 1 : 1);
            }
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        text = textBuilder.toString();
    }

    private void createHuffmanTree() {
        // 优先级队列的元素按照其自然顺序进行排序,或者根据构造队列时提供的 Comparator 进行排序,具体取决于所使用的构造方法
        PriorityQueue<Node> priorityQueue = new PriorityQueue<Node>();
        // Map.Entry<K,V>接口 映射项(键-值对)
        // 类HashMap<K,V>的方法 Set<Map.Entry<K,V>> entrySet() 返回此映射所包含的映射关系的 Set 视图
        for (Map.Entry<Character, Integer> kv : charFrequencies.entrySet()) {
            priorityQueue.add(new Node(kv.getKey(), kv.getValue(), null, null));
        }
        while (priorityQueue.size() > 1) {
            Node node1 = priorityQueue.poll();
            // System.out.print(node1.ch);
            Node node2 = priorityQueue.poll();
            // System.out.print(node2.ch);
            priorityQueue.add(new Node('\0', node1.freq + node2.freq, node1, node2));
        }
        huffmanTree = priorityQueue.poll();
    }

    private void createHuffmanCode(Node node, HashMap<Character, String> hashMap, String string) {
        if (node.lChild == null && node.rChild == null) {
            hashMap.put(node.ch, string);
            return;
        }
        createHuffmanCode(node.lChild, hashMap, string + '0');
        createHuffmanCode(node.rChild, hashMap, string + '1');
    }

    private void getHuffmanCode() {
        huffmanCodeMap = new HashMap<>();
        createHuffmanCode(huffmanTree, huffmanCodeMap, "");
    }

    private void encode() {
        StringBuilder encodedTextBuilder = new StringBuilder();
        for (char ch : text.toCharArray()) {
            encodedTextBuilder.append(huffmanCodeMap.get(ch));
        }
        encodedText = encodedTextBuilder.toString();
        /*try {
            int ch;
            Reader in = new FileReader(fileName);
            while ((ch = in.read()) != -1) {
                encodedTextBuilder.append(huffmanCodeMap.get((char) ch));
            }
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        encodedText = encodedTextBuilder.toString();*/
    }

    private void exportEncodedText() throws IOException {
        // G:\project\test data\Red history thriving along highway.txt
        File file = new File(encodedTextExportPath);
        if (file.createNewFile()) {
            RandomAccessFile out = new RandomAccessFile(file, "rw");
            out.writeBytes(encodedText);
        }
    }

    private void exportHuffmanTree() {
        File file = new File(treeExportPath);
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
            objectOutputStream.writeObject(huffmanTree);
            objectOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String calculateCompressionRatio() {
        File text = new File(fileName);
        File encodedText = new File(encodedTextExportPath);
        /*System.out.println(text.length());
        System.out.println(encodedText.length());*/
        String ratio = String.format("%.4f", (double)encodedText.length() / (double)text.length() / 16.0);
        return NumberFormat.getPercentInstance().format(Double.parseDouble(ratio));
    }

    public Object[][] getFileData() {
        File textFile = new File(fileName);
        File encodedTextFile = new File(encodedTextExportPath);

        Object[][] fileData = new Object[2][3];
        fileData[0][0] = textFile.getName();
        fileData[0][1] = textFile.length();
        fileData[0][2] = textFile.getAbsolutePath();
        fileData[1][0] = encodedTextFile.getName();
        fileData[1][1] = encodedTextFile.length() / 16;
        fileData[1][2] = encodedTextFile.getAbsolutePath();

        return fileData;
    }

    public Object[][] getCodingData() {
        Object[][] codingData = new Object[charFrequencies.size()][3];
        int count = 0;
        for (Map.Entry<Character, Integer> kv : charFrequencies.entrySet()) {
            codingData[count][0] = kv.getKey();
            codingData[count][1] = kv.getValue();
            ++count;
        }
        count = 0;
        for (Map.Entry<Character, String> kv : huffmanCodeMap.entrySet()) {
            codingData[count][2] = kv.getValue();
            ++count;
        }
        return codingData;
    }

    public void execute() throws IOException {
        getCharFrequencies();
        createHuffmanTree();
        getHuffmanCode();
        encode();
        exportHuffmanTree();
        exportEncodedText();
    }

    /*public void showHuffmanCodeMap() {
        for (Map.Entry<Character, String> kv : huffmanCodeMap.entrySet()) {
            System.out.println(kv.getKey() + " : " + kv.getValue());
        }
    }

    public void showCharFrequencies() {
        for (Map.Entry<Character, Integer> kv : charFrequencies.entrySet()) {
            System.out.println(kv.getKey() + " : " + kv.getValue());
        }
    }

    public static void main(String[] args) throws IOException {
        HuffmanEncoder en = new HuffmanEncoder();
        en.setFileName("G:\\project\\src\\test.txt");
        en.getCharFrequencies();
        en.createHuffmanTree();
        en.getHuffmanCode();
        en.encode();
        en.exportHuffmanTree();
        en.showCharFrequencies();
        en.exportEncodedText();
        System.out.println(en.encodedText);
        System.out.println(en.calculateCompressionRatio());
    }*/

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199

4. 资源展示

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/859305
推荐阅读
相关标签
  

闽ICP备14008679号