導航:首頁 > 源碼編譯 > java字元串壓縮演算法

java字元串壓縮演算法

發布時間:2022-08-23 15:14:27

㈠ 用java 編程 如何將 字元串原地壓縮

對字元做累加,如果遇到相同字元則加1,否則輸出累加的數

㈡ java使用zip壓縮演算法將字元串壓縮之後生成二維碼,用手機掃描二維碼可以解析出來嗎

手機端的軟體,是自己寫的,那是可以的。。。。。。否則就是一串二進制亂碼
~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~
~~
~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~

㈢ java中使用壓縮演算法

public ArrayList fun(int[] arr) {
//我只考慮正整數的情況
ArrayList<Integer> list = new ArrayList<Integer>();
int flag = -1;
int ct = 0;
for(int i=0;i<arr.length;i++) {
if(arr[i]!=flag) {
if(ct!=0) {
list.add(ct);
ct = 0;
}
flag = arr[i];
list.add(0);
list.add(arr[i]);
}
ct++;
}
list.add(ct);
return list;
}

㈣ 求助:用java實現哈夫曼編碼壓縮與解壓縮演算法。

你好,由於內容比較多,先概述一下先。如圖所示,為我寫的一個壓縮軟體,原理是利用哈弗曼演算法實現的。我將資料整理好稍後就發到你郵箱,但在這里簡要說明一下代碼。

請看我的空間

http://hi..com/%D2%B6%BF%C6%C1%BC/blog

中的文章共5篇(太長了)

http://hi..com/%D2%B6%BF%C6%C1%BC/blog/item/93c35517bb528146f2de32fd.html

1.HuffmanTextEncoder類完成壓縮功能,可直接運行,壓縮測試用文本文件。

2.HuffmanTextDecoder類完成解壓縮功能,可直接運行,解壓縮壓縮後的文本文件。

3.BitReader,工具類,實現對BufferedInputStream的按位讀取。

4.BitWriter,工具類,實現按位寫入的功能。該類來自網路。

5.MinHeap<T>,模板工具類,實現了一個最小堆。生成Huffman樹時使用。

㈤ Java,把字元竄壓縮成十六進制,並且壓縮一半

12345678910111213141516171819小豬,已解答,採納即可publicclassRectangle{publicstaticvoidmain(String[]args){StringS1="0412134FFFFFFFFFF";StringT1="00007770000200";longsi=Long.parseLong(S1,16);longti=Long.parseLong(T1);longst=si^//很顯然是十進制0~9//4693090553043039415System.out.println(st);Stringhex=Long.toHexString(st);//412134fe30df34b7System.out.println(hex);}}

追問

按照異或的演算法,首位0和0異或的結果不是應該是0嗎?為什麼算出來後結果的第一位是4?還是說內部執行的機制不是這樣的?求教,謝謝

㈥ 用java如何實現壓縮字元串

package javase1.day02;
/**
* 1)一種字元串壓縮演算法
* str ="aaaabbccccddeaaa"
* 壓縮為:"4a2b4c2d1e3a"
* 原理實現:
* str = "aaaabbccccddeaaa"
*
* c = str.charAt(i)//c是每個字元
* 1) 初始化
* StringBuilder buf = new StringBuilder();
* int count = 0;代表相同的字元個數
* char ch = str.charAt(0);代表正在統計的相同字元'a'
* 2) 從i=1開始迭代每個字元
* c = str.charAt(i);//c是每個當前字元
* 3) 檢查當前字元c與被統計ch是否一致
* 如果一致 count++
* 否則(不一致)
* 向緩沖區buf增加count+ch
* count=0,ch=c;
* 3)沒有下個字元就結束
* 4)還有字元串嗎?回到2)
*
* 2)實現還原演算法
* str = "4a2b4c2d1e3a";
* i
*/
public class Demo5 {
public static void main(String[] args) {
String s = comp("aaaawwwwe");
System.out.println(s);
// System.out.println(decomp(s));

}
public static String comp(String str){
int i = 1;
StringBuilder buf = new StringBuilder();
int count = 1;
char ch = str.charAt(0);
for(;;){
char c = i==str.length() ? '\10':str.charAt(i);
if(c==ch){
count++;
}else{
if(count == 1)
buf.append(ch);
else
buf.append(count).append(ch);
count=1;
ch = c;
}
i++;
if(i==str.length()+1){
break;
}
}
return buf.toString();

}
}

㈦ java 實現對字元串進行GSM(7bit)壓縮編碼後HEX 字元串輸出

JAVA有一個public String(byte bytes[], Charset charset)函數可以用指定位元組數組和編碼來構造字元串。一個public byte[] getBytes(Charset charset)函數把字元串按指定編碼來得到位元組數組。可以用這兩個函數來實現編碼轉換。
下面是一個簡單的例子,注意一下例子中的文字本身的編碼,最好在自己的環境中用gb2312重新輸入,不然可能是亂碼。當然轉換後輸出肯定有一個是亂碼,也肯能都是亂碼。根據你的編輯器的編碼格式有關。
public class EncodingTest{ public static void main(String[] args) { try { String gb = new String("國標2312".getBytes(),"gb2312"); System.out.println(gb); byte [] b = gb.getBytes("gb2312"); String ios = new String(b,"ISO-8859-1"); System.out.println(ios); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } }}

㈧ java 什麼演算法壓縮文件最小

有三種方式實現java壓縮:

1、jdk自帶的包java.util.zip.ZipOutputStream,不足之處,文件(夾)名稱帶中文時,出現亂碼問題,實現代碼如下:
/**
* 功能:把 sourceDir 目錄下的所有文件進行 zip 格式的壓縮,保存為指定 zip 文件
* @param sourceDir 如果是目錄,eg:D:\\MyEclipse\\first\\testFile,則壓縮目錄下所有文件;
* 如果是文件,eg:D:\\MyEclipse\\first\\testFile\\aa.zip,則只壓縮本文件
* @param zipFile 最後壓縮的文件路徑和名稱,eg:D:\\MyEclipse\\first\\testFile\\aa.zip
*/
public File doZip(String sourceDir, String zipFilePath) throws IOException {
File file = new File(sourceDir);
File zipFile = new File(zipFilePath);
ZipOutputStream zos = null;
try {
// 創建寫出流操作
OutputStream os = new FileOutputStream(zipFile);
BufferedOutputStream bos = new BufferedOutputStream(os);
zos = new ZipOutputStream(bos);

String basePath = null;

// 獲取目錄
if(file.isDirectory()) {
basePath = file.getPath();
}else {
basePath = file.getParent();
}

zipFile(file, basePath, zos);
}finally {
if(zos != null) {
zos.closeEntry();
zos.close();
}
}

return zipFile;
}
/**
* @param source 源文件
* @param basePath
* @param zos
*/
private void zipFile(File source, String basePath, ZipOutputStream zos)
throws IOException {
File[] files = null;
if (source.isDirectory()) {
files = source.listFiles();
} else {
files = new File[1];
files[0] = source;
}

InputStream is = null;
String pathName;
byte[] buf = new byte[1024];
int length = 0;
try{
for(File file : files) {
if(file.isDirectory()) {
pathName = file.getPath().substring(basePath.length() + 1) + "/";
zos.putNextEntry(new ZipEntry(pathName));
zipFile(file, basePath, zos);
}else {
pathName = file.getPath().substring(basePath.length() + 1);
is = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
zos.putNextEntry(new ZipEntry(pathName));
while ((length = bis.read(buf)) > 0) {
zos.write(buf, 0, length);
}
}
}
}finally {
if(is != null) {
is.close();
}
}
}

2、使用org.apache.tools.zip.ZipOutputStream,代碼如下,
package net.szh.zip;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

public class ZipCompressor {
static final int BUFFER = 8192;

private File zipFile;

public ZipCompressor(String pathName) {
zipFile = new File(pathName);
}

public void compress(String srcPathName) {
File file = new File(srcPathName);
if (!file.exists())
throw new RuntimeException(srcPathName + "不存在!");
try {
FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream,
new CRC32());
ZipOutputStream out = new ZipOutputStream(cos);
String basedir = "";
compress(file, out, basedir);
out.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}

private void compress(File file, ZipOutputStream out, String basedir) {
/* 判斷是目錄還是文件 */
if (file.isDirectory()) {
System.out.println("壓縮:" + basedir + file.getName());
this.compressDirectory(file, out, basedir);
} else {
System.out.println("壓縮:" + basedir + file.getName());
this.compressFile(file, out, basedir);
}
}

/** 壓縮一個目錄 */
private void compressDirectory(File dir, ZipOutputStream out, String basedir) {
if (!dir.exists())
return;

File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
/* 遞歸 */
compress(files[i], out, basedir + dir.getName() + "/");
}
}

/** 壓縮一個文件 */
private void compressFile(File file, ZipOutputStream out, String basedir) {
if (!file.exists()) {
return;
}
try {
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(file));
ZipEntry entry = new ZipEntry(basedir + file.getName());
out.putNextEntry(entry);
int count;
byte data[] = new byte[BUFFER];
while ((count = bis.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
bis.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

3、可以用ant中的org.apache.tools.ant.taskdefs.Zip來實現,更加簡單。
package net.szh.zip;

import java.io.File;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Zip;
import org.apache.tools.ant.types.FileSet;

public class ZipCompressorByAnt {

private File zipFile;

public ZipCompressorByAnt(String pathName) {
zipFile = new File(pathName);
}

public void compress(String srcPathName) {
File srcdir = new File(srcPathName);
if (!srcdir.exists())
throw new RuntimeException(srcPathName + "不存在!");

Project prj = new Project();
Zip zip = new Zip();
zip.setProject(prj);
zip.setDestFile(zipFile);
FileSet fileSet = new FileSet();
fileSet.setProject(prj);
fileSet.setDir(srcdir);
//fileSet.setIncludes("**/*.java"); 包括哪些文件或文件夾 eg:zip.setIncludes("*.java");
//fileSet.setExcludes(...); 排除哪些文件或文件夾
zip.addFileset(fileSet);

zip.execute();
}
}
測試一下
package net.szh.zip;

public class TestZip {
public static void main(String[] args) {
ZipCompressor zc = new ZipCompressor("E:\\szhzip.zip");
zc.compress("E:\\test");

ZipCompressorByAnt zca = new ZipCompressorByAnt("E:\\szhzipant.zip");
zca.compress("E:\\test");
}
}

㈨ 求救用java實現哈夫曼壓縮解壓演算法 小妹非常感謝

這是C的 僅供參考

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define MAX_SINGLECODE_LEN 10 //單個字元最大碼長
#define MAX_STRING_LEN 1000 //要編碼的字元串的最大長度
#define MAX_CODESTRING_LEN 10000 //產生的二進制碼的最大長度
#define MAX_WORDS 1000 //要編碼的字元串中字元種數最大值
#define END_TREE 30000 //樹部分存儲的結束符
#define PATH_LEN 50 //路徑串最大長度

/*****哈夫曼樹結構定義*****/
typedef struct Huffmantree
{
char ch; //字元部分
int weight; //結點權值
int mark; //標記是否加入樹中
struct Huffmantree *parent,*lchild,*rchild,*next;
}HTNode,*LinkTree;

/*****編碼字典結構定義*****/
typedef struct
{
char ch; //字元部分
char code[MAX_SINGLECODE_LEN]; //編碼部分
}CodeDictionary;

/*********函數聲明*********/
LinkTree setWeight(char *);
LinkTree sortNode(LinkTree);
LinkTree createHTree(LinkTree);
void codeHTree(LinkTree,CodeDictionary *);
void decodeHTree(LinkTree,char *,char *);
void deleteNode(LinkTree);
void compressString(char *s,CodeDictionary *,char *);
void readFile(char *);
void writeFile(char *);
void readCode(LinkTree,char *);
void writeCode(LinkTree,char *);
void menu();

/**
*主函數
*輸入:空
*返回:空
*/
void main(void)
{
char choice; //菜單選擇變數
char string[MAX_STRING_LEN]; //保存從文件中讀取的內容
LinkTree temp; //保存賦了權值的表
LinkTree ht; //保存排序後的表
LinkTree ht,temp; //表備份
LinkTree htree; //保存哈夫曼樹
LinkTree ptr=NULL;
CodeDictionary codedictionary[MAX_WORDS];//編碼字典
char codestring[MAX_CODESTRING_LEN]; //保存0-1形的代碼串
char codestring2[MAX_CODESTRING_LEN];//保存0-1形的代碼串
LinkTree ht2; //保存讀取的樹
LinkTree htree2; //保存排序後的表
char filestring[MAX_STRING_LEN]; //解碼後要寫入文件中的內容

if((ht2=(LinkTree)malloc(sizeof(HTNode)))==NULL)//創建鏈表的頭結點
{
printf("內存不足!");
getch();
exit(0);
}
ht2->next=NULL;

while(1)
{
menu(); //調入主菜單
choice=getch(); //讀入用戶選項
switch(choice) //判斷用戶選擇
{
case 'c':
case 'C':
printf("\n您選擇了壓縮文件模式:\n\n");
readFile(string); //讀取要編碼的文件(字元串)
temp=setWeight(string); //得到有權值的表
temp=setWeight(string);
ht=sortNode(temp); //按權值排序後的表
ht=sortNode(temp); //用於記錄解碼樹
htree=createHTree(ht); //得到哈夫曼樹
codeHTree(htree,codedictionary);//哈夫曼編碼
compressString(string,codedictionary,codestring);//壓縮為0-1碼
writeCode(ht,codestring); //將解碼樹和0-1碼保存
deleteNode(htree); //釋放空間*/
break;
case 'u':
case 'U':
printf("您選擇了解壓縮文件模式:\n\n");
readCode(ht2,codestring2); //讀取要解碼的0-1碼
htree2=createHTree(ht2); //得到哈夫曼樹
codeHTree(htree2,codedictionary);//哈夫曼編碼
decodeHTree(htree2,codestring2,filestring); //解碼
writeFile(filestring); //將解碼文件保存
deleteNode(htree2); //釋放空間
break;
case 'e':
case 'E':
exit(0); //退出程序
}
}
}/**
*整理輸入的字元串,求出每個字元在數組中出現的次數,作為權值
*輸入:(字元型指針)字元串的地址
*返回:(哈夫曼結點指針)含權鏈表的首地址
*/
LinkTree setWeight(char *string)
{
int i=0; //文件字元串下標
LinkTree tree; //頭指針
LinkTree ptr,beforeptr; //創建指針與其前驅
HTNode *node;

if((tree=(LinkTree)malloc(sizeof(HTNode)))==NULL)//創建鏈表的頭結點
return NULL;
tree->next=NULL;

for(i=0;string[i]!='\0';i++)
{
ptr=tree;
beforeptr=tree;

if((node=(HTNode *)malloc(sizeof(HTNode)))==NULL)
return NULL;
node->next=NULL;
node->parent=NULL;
node->lchild=NULL;
node->rchild=NULL;
node->mark=0;
node->ch=string[i];
node->weight=1;

if(tree->next==NULL) //如果是第一個非頭結點
tree->next=node;
else
{
ptr=tree->next;
while(ptr&&ptr->ch!=node->ch) //查找相同字元
{
ptr=ptr->next;
beforeptr=beforeptr->next;
}
if(ptr&&ptr->ch==node->ch) //如果鏈表中某結點的字元與新結點的字元相同
{
ptr->weight++; //將該結點的權加一
free(node);
}
else //將新結點插入鏈表後
{
node->next=beforeptr->next;
beforeptr->next=node;
}
}
}
return tree; //返回頭指針
}

/**
*將整理完的字元串(帶權鏈表)按出現次數從小到大的順序排列
*輸入:(哈夫曼結點指針)要排序的表頭地址
*返回:(哈夫曼結點指針)排序後的表頭地址
*/
LinkTree sortNode(LinkTree tree)
{
LinkTree head; //頭指針
LinkTree ph,beforeph; //創建指針及其前驅
LinkTree pt;

if((head=(LinkTree)malloc(sizeof(HTNode)))==NULL)//創建新鏈表的頭結點
return NULL;
head->next=NULL;

ph=head;
beforeph=head;

while(tree->next)
{
pt=tree->next; //取被*作鏈表的頭結點
tree->next=pt->next;
pt->next=NULL;

ph=head->next;
beforeph=head;

if(head->next==NULL)
head->next=pt; //創建當前*作鏈表頭結點
else
{
while(ph&&ph->weight<pt->weight) //將被*作結點插入相應位置
{
ph=ph->next;
beforeph=beforeph->next;
}
pt->next=beforeph->next;
beforeph->next=pt;
}
}
free(tree);
return head; //返回排序後的頭指針
}

/**
*用排完序的字元串建立哈夫曼樹
*輸入:(哈夫曼結點指針)要建立哈夫曼樹的地址
*返回:(哈夫曼結點指針)建立後的哈夫曼樹地址
*/
LinkTree createHTree(LinkTree tree)
{
LinkTree p,q,beforep;
HTNode *newnode;

for(p=tree->next,q=p->next;p!=NULL&&q!=NULL;p=tree->next,q=p->next)
//p、q初值為頭結點後的兩個結點,即最小權結點
{
tree->next=q->next;
q->next=NULL;
p->next=NULL;

if((newnode=(HTNode *)malloc(sizeof(HTNode)))==NULL)
//申請新結點作為哈夫曼樹的中間結點
return NULL;
newnode->next=NULL;
newnode->mark=0;

newnode->lchild=p; //取鏈表頭結點後的兩個結點作為新結點的左、右孩子
newnode->rchild=q;
p->parent=newnode;
q->parent=newnode;
newnode->weight=p->weight+q->weight; //權值相加

p=tree->next;
beforep=tree;

if(p!=NULL&&p->weight>=newnode->weight)
{
newnode->next=beforep->next; //將新結點插入原鏈表的相應位置
beforep->next=newnode;
}
else
{
while(p!=NULL&&p->weight<newnode->weight)
{
p=p->next;
beforep=beforep->next;
}
newnode->next=beforep->next;
beforep->next=newnode;
}
}
return (tree->next);
}

/**
*對哈夫曼樹進行編碼
*輸入:(哈夫曼結點指針)要編碼的哈夫曼樹地址
* (編碼字典類型指針)存放字典的首地址
*返回:空
*/
void codeHTree(LinkTree tree,CodeDictionary *codedictionary)
{
int index=0,k=0;
char code[MAX_SINGLECODE_LEN]; //用於統計每個字元的哈夫曼編碼
LinkTree ptr=tree; //從樹的根結點開始

if(ptr==NULL)
{
printf("要壓縮的文件是空的!\n");
exit(0);
}
else
{
while(ptr->lchild&&ptr->rchild&&ptr->mark==0)
{
while(ptr->lchild&&ptr->lchild->mark==0)
{
code[index++]='0'; //左支路編碼為0
ptr=ptr->lchild;
if(!ptr->lchild&&!ptr->rchild) //如果沒有左右孩子,即葉子結點
{
ptr->mark=1; //作標記,表明該字元已被編碼
code[index]='\0'; //編碼0-1字元串結束
codedictionary[k].ch=ptr->ch;//給字典賦字元值
for(index=0;code[index]!='\0';index++)
codedictionary[k].code[index]=code[index];//給字典賦碼值
codedictionary[k].code[index]='\0';
k++;
ptr=tree; //指針復位
index=0;
}
}
if(ptr->rchild&&ptr->rchild->mark==0)
{
ptr=ptr->rchild;
code[index++]='1'; //右支路編碼為1
}
if(!ptr->lchild&&!ptr->rchild) //如果沒有左右孩子,即葉子結點
{
ptr->mark=1;
code[index++]='\0';
codedictionary[k].ch=ptr->ch; //給字典賦字元值
for(index=0;code[index]!='\0';index++)
codedictionary[k].code[index]=code[index];//給字典賦碼值
codedictionary[k].code[index]='\0';
k++;
ptr=tree;
index=0;
}
if(ptr->lchild->mark==1&&ptr->rchild->mark==1)//如果左右孩子都已標記
{
ptr->mark=1;
ptr=tree;
index=0;
}
}
}
printf("\n");
}

/**
*解碼,即將0-1碼轉化為字元串
*輸入:(哈夫曼結點指針)編碼樹的地址
* (字元型指針)要解碼的0-1字元串地址
* (字元型指針)解碼後的字元串地址
*返回:空
*/
void decodeHTree(LinkTree tree,char *code,char *filestring)
{
int i=0,j=0,k=0;
char *char0_1;
LinkTree ptr=tree;
char0_1=(char *)malloc(MAX_SINGLECODE_LEN); //此數組用於統計輸入的0-1序列

printf("預覽解壓後的字元:\n");
for(j=0,ptr=tree;code[i]!='\0'&&ptr->lchild&&ptr->rchild;j=0,ptr=tree)
{
for(j=0;code[i]!='\0'&&ptr->lchild&&ptr->rchild;j++,i++)
{
if(code[i]=='0')
{
ptr=ptr->lchild;
char0_1[j]='0';
}
if(code[i]=='1')
{
ptr=ptr->rchild;
char0_1[j]='1';
}
}
if(!ptr->lchild&&!ptr->rchild)
{
printf("%c",ptr->ch); //顯示解壓後的字元
filestring[k++]=ptr->ch; //將字元逐一保存到字元串里
}
if(code[i]=='\0'&&ptr->lchild&&ptr->rchild)
{
char0_1[j]='\0';
printf("\n沒有與最後的幾個0-1序列:%s相匹配的字元!\n",char0_1);
return;
}
}
printf("\n\n");
filestring[k]='\0';
free(char0_1);
}

/**
*釋放哈夫曼樹所佔用的空間
*輸入:(哈夫曼結點指針)要釋放的結點地址
*返回:空
*/
void deleteNode(LinkTree tree)
{
LinkTree ptr=tree;
if(ptr)
{
deleteNode(ptr->lchild);
deleteNode(ptr->rchild);
free(ptr);
}
}

/**
*將整個字元串轉化為0-1的字元串
*輸入:(字元型指針)待轉化的字元串首地址
* (編碼字典類型指針)字典首地址
* (字元型指針)接收0-1碼串的首地址
*返回:空
*/
void compressString(char *string,CodeDictionary *codedictionary,char *codestring)
{
int i=0,j=0,k=0,m;

while(string[i]) //整個文件字元串沒結束時
{
while(string[i]!=codedictionary[j].ch&&j<MAX_WORDS)
//找與對應字元相同的字元
j++;
if(string[i]==codedictionary[j].ch) //如果找到與對應字元相同的字元
for(m=0;codedictionary[j].code[m];m++,k++)
codestring[k]=codedictionary[j].code[m];
j=0; //字典復位
i++;
}
codestring[k]='\0';
}

/**
*把指定文件讀到字元串中
*輸入:(字元型指針)待接收文件的字元串地址
*返回:空
*/
void readFile(char *string)
{
FILE *fp;
int i;
char ch; //記錄讀入的字元
char path[PATH_LEN]; //文本文件的讀路徑

printf("請輸入要壓縮的文本文件地址:(無需擴展名)");
gets(path);
if((fp=fopen(strcat(path,".txt"),"r"))==NULL)
{
printf("\n路徑不正確!\n");
getch();
return;
}

ch=fgetc(fp);
for(i=0;ch!=EOF;i++)
{
string[i]=ch;
ch=fgetc(fp);
}
string[i]='\0';
fclose(fp);
}

/**
*保存編碼後的解碼樹和字元串
*輸入:(哈夫曼結點指針)解碼樹的地址
* (字元型指針)要保存的0-1碼串首地址
*返回:空
*/
void writeCode(LinkTree tree,char *string)
{
FILE *fp;
int i;
int weight; //記錄寫入的權值
char ch; //記錄寫入的字元
LinkTree p;
char path[PATH_LEN]; //0-1碼文件的寫路徑

printf("請輸入壓縮後的保存路徑及文件名:(無需擴展名)");
gets(path);
if((fp=fopen(strcat(path,".yxy"),"w+"))==NULL)
{
printf("\n文件路徑出錯!\n");
getch();
return;
}
p=tree->next;

/*解碼樹部分寫入文件前部分*/
do
{
ch=p->ch;
weight=p->weight;
fprintf(fp,"%c%d",ch,weight);
p=p->next;
}while(p);
fprintf(fp,"%c%d",'^',END_TREE);

fseek(fp,sizeof(char),1); //空出區分位

/*0-1碼寫入文件後部分*/
for(i=0;string[i];i++)
{
ch=string[i];
fputc(ch,fp);
}
printf("\n壓縮成功!\n");
getch();
fclose(fp);
}

/**
*讀取編碼後的0-1字元串
*輸入:(哈夫曼結點指針)解碼樹的地址
* (字元型指針)要接收的0-1碼串首地址
*返回:空
*/
void readCode(LinkTree tree,char *string)
{

FILE *fp;
int i;
int weight; //記錄讀入的權值
char ch; //記錄讀入的字元
LinkTree ptr,beforeptr;
char path[PATH_LEN]; //0-1碼文件的讀路徑

printf("請輸入要解壓的文件路徑及文件名:(無需擴展名)");
gets(path);
if((fp=fopen(strcat(path,".yxy"),"r"))==NULL)
{
printf("\n文件路徑出錯!\n");
getch();
return;
}
beforeptr=tree;

/*從文件前部分讀出解碼樹*/
fscanf(fp,"%c%d",&ch,&weight);
while(weight!=END_TREE)
{
if((ptr=(LinkTree)malloc(sizeof(HTNode)))==NULL)
{
printf("內存不足!");
getch();
exit(1); //錯誤出口
}
ptr->ch=ch;
ptr->weight=weight;
ptr->lchild=NULL;
ptr->rchild=NULL;
ptr->parent=NULL;
ptr->mark=0;
beforeptr->next=ptr;
beforeptr=ptr;
fscanf(fp,"%c%d",&ch,&weight);
}
beforeptr->next=NULL;

fseek(fp,sizeof(char),1); //文件指針定位

/*從文件後部分讀出0-1碼*/
ch=fgetc(fp);
for(i=0;ch!=EOF;i++)
{
string[i]=ch;
ch=fgetc(fp);
}
string[i]='\0';
fclose(fp);
}

/**
*保存解碼後的文件
*輸入:(字元型指針)解碼後的字元串地址
*返回:空
*/
void writeFile(char *string)
{
FILE *fp;
char ch; //記錄寫入的字元
int i;
char path[PATH_LEN]; //文本文件的寫路徑

printf("請輸入解壓後的保存路徑及文件名:(無需擴展名)");
gets(path);
if((fp=fopen(strcat(path,".txt"),"w+"))==NULL)
{
printf("\n文件路徑出錯!\n");
getch();
return;
}

for(i=0;string[i];i++)
{
ch=string[i];
fputc(ch,fp);
}
printf("\n解壓成功!\n");
getch();
fclose(fp);
}

/**
*顯示主菜單
*輸入:空
*返回:空
*/
void menu()
{
printf("\n\n\n\n\n\n");
printf("\t\t -----** 歡迎使用WINYXY壓縮工具 **-----");
printf("\n\n\n");
printf("\t\t\t\t<c> 壓 縮\n\n");
printf("\t\t\t\t<u> 解 壓\n\n");
printf("\t\t\t\t<e> 退 出\n\n");
}

㈩ Java 字元串壓縮與解壓

給你提供個思想
首先你這不是物理上的壓縮,也就是說它是一個邏輯上的我們認同上的壓縮。
你需要寫一個演算法來對你所要處理的數據進行統計,然後按照演算法來改變結果。
最後達到一個後台的虛擬壓縮(實際上不是壓縮,只是演算法)。

閱讀全文

與java字元串壓縮演算法相關的資料

熱點內容
肯亞程序員 瀏覽:638
新科源碼 瀏覽:659
如何判斷伺服器有沒有帶寬 瀏覽:41
天正建築批量刪除命令 瀏覽:94
cad最下面的一排命令都什麼意思 瀏覽:456
pythonimportcpp 瀏覽:850
W10的系統怎麼給U盤加密 瀏覽:370
華為手機代碼編程教學入門 瀏覽:762
和彩雲沒會員怎樣解壓 瀏覽:634
androidimageview保存 瀏覽:387
新買店鋪什麼伺服器 瀏覽:883
文件夾能直接刻錄嗎 瀏覽:493
androidxmpp刪除好友 瀏覽:969
javac哪個前景好 瀏覽:428
中華英才網app為什麼不能搜索了 瀏覽:660
伺服器域名是什麼意思 瀏覽:52
Linux導出mysql命令 瀏覽:159
無詐建鄴是什麼app 瀏覽:228
python中的雙色球 瀏覽:168
python解釋器里如何換行 瀏覽:413