📄 code.c
字号:
#include "ECBTree.h"
#include "MyAssert.h"
#include "Ulti.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LENGTH 128
#define DEBUG 1
#define REARPOS 80
char dotTxt[]=".txt";
char dotRer[]=".rer";
int getBinLen(unsigned long inData);
void main(int argc,char *argv[])
{
//core data.
long wList[LENGTH];
unsigned long haffCodeList[LENGTH];
int haffCodeLen[LENGTH];
HaffCode haffList[LENGTH];
PHtTree myHtTree;
//file data.
char inputFileName[LENGTH],outputFileName[LENGTH];
FILE*inputFile,*outputFile,*keyFile;
int fileNameLen;
//binary opeartion data.
char inData,outputData;
unsigned long curCode,tmpBinData;
int curLen,realLen,curIndex;
int i;
int count;
unsigned long rearCode;/*rear data consult*/
int rearCodeLen;
//open the files.
if (argc<=1)
{
printf("please enter your file address.\n");
return;
}
else
{
//make up the files' name.
strcpy(inputFileName,argv[1]);
strcpy(outputFileName,argv[1]);
fileNameLen=strlen(argv[1]);
//only could zip the file appendix as .***,*** have to be 3 char length.
outputFileName[fileNameLen-4]='\0';
strcat(outputFileName,dotRer);
//strcat(inputFileName,dotTxt);
inputFileName[fileNameLen]='\0';
outputFileName[fileNameLen+4]='\0';
/*inputFile open.*/
if((inputFile=fopen(inputFileName,"rb"))==NULL)
{
printf("file path not found\n");
return;
}
if (DEBUG)
printf("input file open success\n");
/*outpout file open*/
assertF((outputFile=fopen(outputFileName,"wb"))!=NULL,"output file error");
if (DEBUG)
printf("output file open success\n");
}
if((keyFile=fopen("KEY.txt","rb"))==NULL)
{
printf(">--keyFile not founded--<\n");
return;
}
for(i=0;i<LENGTH-1;i++)
fscanf(keyFile,"%d,",&wList[i]);
fscanf(keyFile,"%d;",&wList[i]);
myHtTree=haffmanAlgorithm(LENGTH,wList);
//preHtOrder(myHtTree,myHtTree->rootIndex);
//step2:
for(i=0;i<LENGTH;i++)
haffList[i].asciiCode=(char)i;
preHaffListMake(myHtTree,myHtTree->rootIndex,0x000000,0,haffList);
fprintf(stdout,"haffCode List:\r\n");
for(i=0;i<LENGTH-1;i++)
fprintf(stdout,"%d,",haffList[i].haffCode);
fprintf(stdout,"%d\r\n",haffList[i].haffCode);
fprintf(stdout,"haffCode List Len:\r\n");
for(i=0;i<LENGTH-1;i++)
fprintf(stdout,"%d,",haffList[i].haffCodeLen);
fprintf(stdout,"%d\r\n",haffList[i].haffCodeLen);
if(DEBUG)
printf("\ntest start.\n");
//starting setting.
curIndex=curLen=0;
rearCode=haffList[REARPOS].haffCode;
rearCodeLen=haffList[REARPOS].haffCodeLen;
while(!feof(inputFile))
{
count=0;
outputData=0x01;
while(count<8)
{
/*----------------------------*/
if(curIndex==curLen)
{
//1.get data.
if(feof(inputFile))
break;
inData=fgetc(inputFile);
if(inData==-1&&feof(inputFile))
{
if(count==0)
outputData=-1;
else/*the rear output adjust*/
{
for(i=0;i<8-count;i++)
{
outputData<<=1;
outputData|=((rearCode>>(rearCodeLen-1-i))&0x01);
}
}
/*
the consult below will make error happen!
outputData<<=(8-count);
*/
break;
}
//2.search table ->Should be a ascii file.
curCode=haffList[inData].haffCode;
curLen=haffList[inData].haffCodeLen;
realLen=getBinLen(curCode);
i=curLen-realLen;
curIndex=0;
}
if(i>0)
{
outputData<<=1;
//no need to fill bit data.
i--;
}
else
{
tmpBinData=(curCode>>(curLen-curIndex-1))&0x01;
outputData<<=1;
outputData|=(char)tmpBinData;
}
/*-----------------------------------*/
curIndex++;
count++;
}
fputc(outputData,outputFile);
}
if(DEBUG)
printf("\ntest ends.\n");
//step3:end of code.
fclose(inputFile);
fclose(outputFile);
getchar();
return;
}
int getBinLen(unsigned long inData)
{
int i=0;
if( inData==0)
return 1;
else
while(inData!=0)
{
inData/=2;
i++;
}
return i;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -