对字符做累加,如果遇到相同字符则加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 字符串压缩与解压
给你提供个思想
首先你这不是物理上的压缩,也就是说它是一个逻辑上的我们认同上的压缩。
你需要写一个算法来对你所要处理的数据进行统计,然后按照算法来改变结果。
最后达到一个后台的虚拟压缩(实际上不是压缩,只是算法)。