java实现简单算术编码,算术编码核心类

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值