用java设计一个GUI程序,基本设计要求: (1)随意给出一段英文短文作为输入; (2)针对短文中各字符(包括标点符号)的统计其出现的频率值; (3)根据统计出的频率值,自动生成01哈夫曼编码,并将该短文编码成01编码; (4)能够输出该编码表; (5)任意给出一组长度不小于1000位二进制编码,根据刚才所得的编码系统,进行翻译。
时间: 2024-01-13 12:04:39 浏览: 64
对任意输入的一段英文,为每个字符编制其相应的赫夫曼编码;并利用该编码为任意输入的0、1序列进行解码.
4星 · 用户满意度95%
以下是一个简单的GUI程序设计,实现输入短文,统计字符频率,生成哈夫曼编码,编码短文,输出编码表和解码短文的功能。
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.util.Map.Entry;
public class HuffmanGUI extends JFrame implements ActionListener {
private JTextArea textArea;
private JButton encodeButton, decodeButton;
private JTextArea codeArea, decodeArea;
public HuffmanGUI() {
setTitle("Huffman Encoder");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建组件
textArea = new JTextArea(10, 20);
JScrollPane scrollPane1 = new JScrollPane(textArea);
encodeButton = new JButton("Encode");
encodeButton.addActionListener(this);
codeArea = new JTextArea(10, 20);
JScrollPane scrollPane2 = new JScrollPane(codeArea);
codeArea.setEditable(false);
decodeButton = new JButton("Decode");
decodeButton.addActionListener(this);
decodeArea = new JTextArea(10, 20);
JScrollPane scrollPane3 = new JScrollPane(decodeArea);
decodeArea.setEditable(false);
// 添加组件
JPanel panel1 = new JPanel(new BorderLayout());
panel1.add(new JLabel("Input text:"), BorderLayout.NORTH);
panel1.add(scrollPane1, BorderLayout.CENTER);
panel1.add(encodeButton, BorderLayout.SOUTH);
JPanel panel2 = new JPanel(new BorderLayout());
panel2.add(new JLabel("Huffman code:"), BorderLayout.NORTH);
panel2.add(scrollPane2, BorderLayout.CENTER);
JPanel panel3 = new JPanel(new BorderLayout());
panel3.add(new JLabel("Binary code:"), BorderLayout.NORTH);
panel3.add(scrollPane3, BorderLayout.CENTER);
panel3.add(decodeButton, BorderLayout.SOUTH);
JPanel panel = new JPanel(new GridLayout(1, 3));
panel.add(panel1);
panel.add(panel2);
panel.add(panel3);
getContentPane().add(panel);
pack();
setLocationRelativeTo(null);
}
private void encode() {
String text = textArea.getText();
if (text.isEmpty()) {
JOptionPane.showMessageDialog(this, "Please enter text to encode", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
Map<Character, Integer> freqMap = new HashMap<>();
for (char c : text.toCharArray()) {
freqMap.put(c, freqMap.getOrDefault(c, 0) + 1);
}
HuffmanTree huffmanTree = new HuffmanTree(freqMap);
Map<Character, String> codeMap = huffmanTree.getCodeMap();
StringBuilder sb = new StringBuilder();
for (char c : text.toCharArray()) {
sb.append(codeMap.get(c));
}
codeArea.setText(sb.toString());
StringBuilder codeTable = new StringBuilder();
for (Entry<Character, String> entry : codeMap.entrySet()) {
codeTable.append(entry.getKey()).append(": ").append(entry.getValue()).append('\n');
}
JOptionPane.showMessageDialog(this, codeTable.toString(), "Huffman Code Table", JOptionPane.INFORMATION_MESSAGE);
}
private void decode() {
String code = decodeArea.getText();
if (code.isEmpty()) {
JOptionPane.showMessageDialog(this, "Please enter binary code to decode", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
Map<String, Character> codeMap = new HashMap<>();
String[] lines = codeArea.getText().split("\n");
for (String line : lines) {
String[] parts = line.split(": ");
codeMap.put(parts[1], parts[0].charAt(0));
}
StringBuilder sb = new StringBuilder();
String prefix = "";
for (int i = 0; i < code.length(); i++) {
prefix += code.charAt(i);
if (codeMap.containsKey(prefix)) {
sb.append(codeMap.get(prefix));
prefix = "";
}
}
textArea.setText(sb.toString());
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == encodeButton) {
encode();
} else if (e.getSource() == decodeButton) {
decode();
}
}
public static void main(String[] args) {
new HuffmanGUI().setVisible(true);
}
}
```
运行程序后,可以先输入一段英文短文,然后点击 Encode 按钮,即可生成哈夫曼编码,编码后的二进制数会显示在中间的文本框中,编码表会以对话框的形式弹出。然后可以将编码后的二进制数复制到下面的文本框中,点击 Decode 按钮,即可解码成原始的英文短文。
阅读全文