该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
package com;
public class encode
extends Thread {
NewFrame app;
String source;
double p[];
double F[];
int weishu;
public encode(NewFrame app, String source, double[] p) {
super();
this.source = source;
this.p = p;
this.app = app;
}
public void run() {
init();
double total = 0;
for (int k = 0; k
double sum = 1;
for (int t = 0; t
sum *=
p[Integer.parseInt(new Character(source.charAt(t)).toString())];
}
sum *= F[Integer.parseInt(new Character(source.charAt(k)).toString())];
total += sum;
}
double pp = 1;
for (int i = 0; i
pp *= p[Integer.parseInt(new Character(source.charAt(i)).toString())];
}
weishu = (int) Math.ceil( ( (Math.log(1 / pp) / Math.log(2))));
app.tip("编码完成");
app.setResult(convert(total));
try {
Thread.sleep(2500);
app.tip("算术编码");
}
catch (Exception ex) {
}
}
private void init() {
F = new double[p.length];
F[0] = 0;
for (int i = 1; i
double sum = 0;
for (int j = 0; j
sum += p[j];
}
F[i] = sum;
}
}
private String convert(double total) {
StringBuffer su = new StringBuffer();
for (int i = 0; i
total *= 2;
if (total > 1 && (total - 1) > 1E-15) {
su.append("1");
total--;
}
else if (total 1E-15) {
su.append("0");
}
else {
break;
}
}
String str = su.toString();
str = str.trim();
if (str.length()
int len = str.length();
for (int i = 0; i
str += "0";
}
}
return str;
}
}