📄 mnp5.cpp
字号:
// mxulie.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdlib.h"
#include "math.h"
#include "stdio.h"
main()
{
FILE *f1,*f2,*f3,*f4;
char fn1[128];
//char fn2[128];
char fn3[128];
printf("source file:\n");
scanf("%s",fn1);
//printf("out file:\n");
//scanf("%s",fn2);
printf("out file:\n");
scanf("%s",fn3);
if((f1=fopen(fn1,"r+b"))==NULL)
{
printf("the file can not open\n");
exit(0);
}
f2=fopen("candel.txt","w+b") ; //写中间文件
{
//printf("the file does not exist:\n");
//exit(0);
}
f3=fopen(fn3,"w+b") ; //写最后数据
{
//printf("the file does not exist:\n");
//exit(0);
}
unsigned char list[256][4];//一列记原值,一列记标记头,一列记主干,一列记频次
for(int i=0;i<256;i++)
{
list[i][0]=i; //写数字值
}
for(int k=0;k<8;k++)
{
int m=0;
double m1=pow(2,k)-1;
double m2=pow(2,k+1);
for(int i=0;i<256;i++)
{
if((m1<i&&i<m2)!=0)
{
list[i][1]=k; //写标记头
list[i][2]=m; //写标记主干
m=m+1;
}
}
}
list[0][1]=0;
list[0][2]=0;
list[1][2]=1;
list[255][2]=~0<<1;
for(i=0;i<256;i++)
{
list[i][3]=0;
}
unsigned char mout[4096];
unsigned char buffer=0; //存放前三个bit
unsigned char buffer1=0; //存放主干比特
unsigned char buffer2; //
int lenlong;
//int sign1=0; //是否写入的标志
int sign2=0; //取得位数
while(lenlong=fread(mout,1,4096,f1))
{
for(int m=0;m<4096;m++) //注意此处判断的次数
{
if((mout[0]==mout[1]&&mout[1]==255)==1)
break;
for(i=0;i<3;i++)
{
buffer=buffer<<1|mout[0]>>7;
for(int j=0;j<lenlong;j++)
{
mout[j]=mout[j]<<1|mout[j+1]>>7;
}
}
sign2=buffer;
if(buffer==0)
sign2=1;
//注意特殊值0,1,255
for(i=0;i<sign2;i++)
{
buffer1=buffer1<<1|mout[0]>>7;
for(int j=0;j<lenlong;j++)
{
mout[j]=mout[j]<<1|mout[j+1]>>7;
}
}
if(buffer1==127)
{
buffer1=buffer1<<1|mout[0]>>7;
for(int j=0;j<lenlong;j++)
{
mout[j]=mout[j]<<1|mout[j+1]>>7;
}
}
if(buffer1==255)
{
for(i=0;i<4;i++)
{
buffer1=buffer1<<1|mout[0]>>7;
for(int j=0;j<lenlong;j++)
{
mout[j]=mout[j]<<1|mout[j+1]>>7;
}
}
buffer=0;
buffer1=0;
continue;
}
for(i=0;i<256;i++)
{
if((list[i][1]!=buffer||list[i][2]!=buffer1)==1)
continue; //若不相等重新开始,不执行下面语句
//if(sign1==0)
{
buffer2=list[i][0];
fputc(buffer2,f2);
}
buffer=0;
buffer1=0;
//创建新表
//首先判断该数据是否是计数单元
unsigned char oncemore[4];
if(m<3) //前三次的情况
{
oncemore[m]=buffer2;
list[i][3]=list[i][3]+1; //频次加一
for(int j=i;j>0;j--)
{
if(list[j][3]>list[j-1][3])
{
int buffer3=list[j-1][0];
list[j-1][0]=list[j][0];
list[j][0]=buffer3;
int frequency=list[j-1][3];
list[j-1][3]=list[j][3];
list[j][3]=frequency;
}
}
}
else //m>=3,有漏洞
{
oncemore[3]=buffer2;
if((oncemore[0]==oncemore[1]&&oncemore[1]==oncemore[2])==1)//满足计数为三
{
list[i][3]=list[i][3]-1;
}
list[i][3]=list[i][3]+1; //频次加一
for(int j=i;j>0;j--)
{
if(list[j][3]>list[j-1][3])
{
int buffer3=list[j-1][0];
list[j-1][0]=list[j][0];
list[j][0]=buffer3;
int frequency=list[j-1][3];
list[j-1][3]=list[j][3];
list[j][3]=frequency;
}
}
oncemore[0]=oncemore[1];
oncemore[1]=oncemore[2];
oncemore[2]=oncemore[3];
}
}
}
}
fclose(f1);
fclose(f2);
//第二步
f4=fopen("candel.txt","r+b");
char* namedel="candel.txt";
buffer1=0;
unsigned char oncemore[4];
while(lenlong=fread(mout,1,4096,f4))
{
for(int m=0;m<4096;m++) //注意此处判断的次数
{
if((mout[m]==mout[m+1]&&mout[m]==204)==1)// if((mout[2]==mout[1]&&mout[1]==204)==1)
break;
buffer1=mout[m];
if(m<3)
{
oncemore[m]=buffer1;
fputc(buffer1,f3);
}
else
{
oncemore[3]=buffer1;
if((oncemore[0]==oncemore[1]&&oncemore[1]==oncemore[2])==1)//满足计数为三
{
int len=buffer1;
if(len!=0) //len>3
{
for(i=0;i<len-3;i++)
{
fputc(oncemore[0],f3);
}
}
}
else
{
fputc(buffer1,f3);
}
oncemore[0]=oncemore[1];
oncemore[1]=oncemore[2];
oncemore[2]=oncemore[3];
}
}
}
fclose(f4);
remove(namedel);
fclose(f3);
return(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -