标签:temp while numbers remove 存在 work 临时 -- data
我百度了一下如何读写文件:
File file = new File("D:\\20172328lxy\\20172328lxy\\src\\week_10\\homework\\text.txt");
Reader reader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(reader);
String temp = bufferedReader.readLine();
File file2 = new File("D:\\20172328lxy\\20172328lxy\\src\\week_10\\homework\\ciphertext.txt");
Writer writer = new FileWriter(file2);
writer.write(result1);
writer.close();
这样就实现了我预期中的第一步。从把字符集从文件中读出来,并保存在一个数组characters[]里面。
《SECOND》:然后进行计算每一个字符出现的频率。首先用嵌套循环来实现,并把出现的概率存在另一个数组中。真正地初次编完代码,好的,全是0。我继续改,知道在嵌套循环中逻辑没有错误,但是全是0.0。
这就让我有点百思不得其解了,为什么逻辑没有错误,但概率不出现,细细分析一遍,其实是我的除法有问题,在"/"运算中,计算机会自动忽略后面的小数位,所以需要加(float)的操作才化腐朽为神奇。。。(如下图所示)
double frequency[] = new double[27];
int numbers = 0;//空格的个数
for (int i = 0; i < characters.length; i++) {
if (characters[i] == ' ') {
numbers++;
}
frequency[26] = (float) numbers / characters.length;
}
System.out.println("字符集为");
for (int j = 97; j <= 122; j++) {
int number = 0;//给字母计数
for (int m = 0; m < characters.length; m++) {
if (characters[m] == (char) j) {
number++;
}
frequency[j - 97] = (float) number / characters.length;
}
System.out.print((char) j + ",");
}
于是我先尝试了一下,用了一个int型的变量加在了huffman树的节点类的属性中,但是出现的结果是:
public class Node implements Comparable<Node> {
private char data;
private double weight;
private Node left;
private Node right;
String codenumber;//这个就是保存0&1的变量
public Node(char data, double weight){
this.data = data;
this.weight = weight;
this.codenumber ="";
}
//对英文文件进行编码,输出一个编码后的文件
String result1 = "";
List<Node> temp1 = breadthFirstTraversal(root);
for (int i = 0; i < characters.length; i++) {
for (int j = 0; j < temp1.size(); j++) {
if (characters[i] == temp1.get(j).getData()) {
result1 += temp1.get(j).getCodenumber();
}
}
}
File file2 = new File("D:\\20172328lxy\\20172328lxy\\src\\week_10\\homework\\ciphertext.txt");
Writer writer = new FileWriter(file2);
writer.write(result1);
writer.close();
//对英文文件进行解码,输出一个解码后的文件
//将所有具有字符的叶子节点重新保存在一个newlist里面
List<String> newlist = new ArrayList<>();
for(int m=0;m < temp1.size();m++)
{
if(temp1.get(m).getData()!='无')
newlist.add(String.valueOf(temp1.get(m).getData()));
}
System.out.println("字符:"+newlist);
List<String> newlist1 = new ArrayList<>();
for(int m=0;m < temp1.size();m++)
{
if(temp1.get(m).getData()!='无')
newlist1.add(String.valueOf(temp1.get(m).getCodenumber()));
}
System.out.println("对应编码:"+newlist1);
//先从编完码的文件中读出密文
FileReader fileReader = new FileReader("D:\\20172328lxy\\20172328lxy\\src\\week_10\\homework\\ciphertext.txt");
BufferedReader bufferedReader1 = new BufferedReader(fileReader);
String secretline = bufferedReader1.readLine();
//将读出的密文存在secretText列表中
List<String> secretText = new ArrayList<String>();
for (int i = 0; i < secretline.length(); i++) {
secretText.add(secretline.charAt(i) + "");
}
//解密
String result2 = "";//最后的解码结果
String current="";// 临时的保存值
while(secretText.size()>0) {
current = current + "" + secretText.get(0);
secretText.remove(0);
for (int p = 0; p < newlist1.size(); p++) {
if (current.equals(newlist1.get(p))) {
result2 = result2 + "" + newlist.get(p);
current="";
}
}
}
System.out.println("解码后的结果:"+result2);
File file3 = new File("D:\\20172328lxy\\20172328lxy\\src\\week_10\\homework\\text1.txt");
Writer writer1 = new FileWriter(file3);
writer1.write(result2);
writer.close();
}
标签:temp while numbers remove 存在 work 临时 -- data
原文地址:https://www.cnblogs.com/LXY462283007/p/10100337.html