• 大小: 6KB
    文件类型: .zip
    金币: 2
    下载: 1 次
    发布日期: 2021-06-23
  • 语言: Java
  • 标签: 压缩算法  

资源简介

算术压缩算法是Java实现,写大作业要求的算法,可以运行。

资源截图

代码片段和文件信息

/*
 * To change this template choose Tools | Templates
 * and open the template in the editor.
 */

package Arithmetic;
import java.io.*;
import java.util.*;
/**
 *
 * @author Administrator
 */
public class Arithmetic {
     int character[]=new int[256];
     double low=0.0;
     double high=1.0;
     double range=1.0;
     double range_low[]=new double[256];
     double range_high[]=new double[256];
     int totalNumber=0;
     
     public void count(String inFileName) throws FileNotFoundException IOException{
         //初始化
         for(int i=0;i         {character[i]=0;
          range_low[i]=0;
          range_high[i]=0;
         }
         
         //读取流中的数据
         BufferedInputStream fp=new BufferedInputStream(new FileInputStream(inFileName));
         int value;
         while((value=fp.read())!=-1){
                 character[value]++;
                 totalNumber++;
             }
         
         fp.close();
         double temp=0.0;
         int j=0;
         for(int i=0;i<256;i++){ 
             if(character[i]!=0){
             range_low[i]=temp;                                                 
             temp=range_high[i]=Math.round((double)character[i]/totalNumber*1000)/1000.0+temp;
            }
         }
         if(range_high[j]<1.0)
             range_high[j]=1.0;    
     }
     public void Compress(String inFileNameString outFileName) throws FileNotFoundException IOException{
          count(inFileName);
          BufferedInputStream fp=new BufferedInputStream(new FileInputStream(inFileName));
          FileOutputStream ou=new FileOutputStream(outFileName);
         int value;
         while((value=fp.read())!=-1){
            high=low+range*range_high[value];
            low=low+range*range_low[value];       
            range=high-low;
         }
         String result=Code();
         byte arr[]=new byte[(int)(Math.ceil(result.length()/8.0))];
         int point=0;
         for(int i=0;i             String temp=““;
             for(int j=0;j<8;j++point++){                
                 if(point                       temp+=String.valueOf(result.charAt(point));
                 else
                     temp+=“0“;
             }
             arr[i]=convert(temp);
         }      
         ou.write(arr);
     }
     
     byte convert(String s){
         byte x=0;
         for(int i=0;i<8;i++){
             if(s.charAt(i)!=‘0‘)
                 x+=1<<(7-i);
         }
         return x;
     }
     public String Code(){
         String result=““;
         int k=0;
         while(Value(result)             result+=“1“;
             if(Value(result)>=high)
             result=result.replaceFirst(result result.substring(0result.length()-1)+“0“);
             k=k+1;
         }
        return result;
     }
     
     public double Value(String result){
       
         double x=0;
         for(int i=0;i             if(resul

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-12-27 18:20  Arithmetic\
     目录           0  2017-12-27 18:20  Arithmetic\1\
     文件         268  2016-11-28 19:24  Arithmetic\1\Compare.java
     文件        6655  2016-11-28 19:46  Arithmetic\1\HuffmanCompress.java
     文件         832  2016-11-28 19:32  Arithmetic\1\HufTree.java
     文件         413  2016-11-28 19:50  Arithmetic\1\Test.java
     文件        5429  2009-06-24 15:24  Arithmetic\Arithmetic.java
     文件         506  2009-06-23 18:16  Arithmetic\Node.java

评论

共有 条评论