📄 main.c
字号:
#define FAIL 0
#define OKAY 1
#include "stdio.h"
#include "stdlib.h"
#include "arith.h"
int main(void)
{
char *inputFile = "test.txt";
FILE *fpInput;
char *outputFile = "testOutput.txt";
FILE *fpOutput;
char *codeFile = "zoley.tmp";
int i;
char curBuffer;
short int totals[28] = {0};
double eachCharNum[27] = {0};
unsigned short int eachCharProb[27] = {0};
double totalCharNum;
BIT_FILE *codeBitFile;
BIT_FILE *decodeBitFile;
int index;
SYMBOL cSymbol;
fpInput = fopen(inputFile, "rb");
if (fpInput == NULL)
{
printf ("%s file not exists or can't be opened, please check it.\n", inputFile);
return FAIL;
}
while ((curBuffer = fgetc(fpInput)) != EOF)
{
switch(curBuffer)
{
case ' ' :
eachCharNum[0]++;
totalCharNum++;
break;
case 'A' :
eachCharNum[1]++;
totalCharNum++;
break;
case 'B' :
eachCharNum[2]++;
totalCharNum++;
break;
case 'C' :
eachCharNum[3]++;
totalCharNum++;
break;
case 'D' :
eachCharNum[4]++;
totalCharNum++;
break;
case 'E' :
eachCharNum[5]++;
totalCharNum++;
break;
case 'F' :
eachCharNum[6]++;
totalCharNum++;
break;
case 'G' :
eachCharNum[7]++;
totalCharNum++;
break;
case 'H' :
eachCharNum[8]++;
totalCharNum++;
break;
case 'I' :
eachCharNum[9]++;
totalCharNum++;
break;
case 'J' :
eachCharNum[10]++;
totalCharNum++;
break;
case 'K' :
eachCharNum[11]++;
totalCharNum++;
break;
case 'L' :
eachCharNum[12]++;
totalCharNum++;
break;
case 'M' :
eachCharNum[13]++;
totalCharNum++;
break;
case 'N' :
eachCharNum[14]++;
totalCharNum++;
break;
case 'O' :
eachCharNum[15]++;
totalCharNum++;
break;
case 'P' :
eachCharNum[16]++;
totalCharNum++;
break;
case 'Q' :
eachCharNum[17]++;
totalCharNum++;
break;
case 'R' :
eachCharNum[18]++;
totalCharNum++;
break;
case 'S' :
eachCharNum[19]++;
totalCharNum++;
break;
case 'T' :
eachCharNum[20]++;
totalCharNum++;
break;
case 'U' :
eachCharNum[21]++;
totalCharNum++;
break;
case 'V' :
eachCharNum[22]++;
totalCharNum++;
break;
case 'W' :
eachCharNum[23]++;
totalCharNum++;
break;
case 'X' :
eachCharNum[24]++;
totalCharNum++;
break;
case 'Y' :
eachCharNum[25]++;
totalCharNum++;
break;
case 'Z' :
eachCharNum[26]++;
totalCharNum++;
break;
default:
printf ("There are some errors ,please check.\n");
break;
}
}
for (i=0;i<27;i++)
{
eachCharProb[i] = (int) eachCharNum[i]/totalCharNum*10000;
}
rewind(fpInput);
codeBitFile = OpenOutputBitFile(codeFile);
initialize_arithmetic_encoder();
while ((curBuffer = fgetc(fpInput)) != EOF)
{
switch(curBuffer)
{
case ' ':
arith_encode(codeBitFile, &eachCharProb[0], 27, 0);
break;
case 'A':
arith_encode(codeBitFile, &eachCharProb[0], 27, 1);
break;
case 'B':
arith_encode(codeBitFile, &eachCharProb[0], 27, 2);
break;
case 'C':
arith_encode(codeBitFile, &eachCharProb[0], 27, 3);
break;
case 'D':
arith_encode(codeBitFile, &eachCharProb[0], 27, 4);
break;
case 'E':
arith_encode(codeBitFile, &eachCharProb[0], 27, 5);
break;
case 'F':
arith_encode(codeBitFile, &eachCharProb[0], 27, 6);
break;
case 'G':
arith_encode(codeBitFile, &eachCharProb[0], 27, 7);
break;
case 'H':
arith_encode(codeBitFile, &eachCharProb[0], 27, 8);
break;
case 'I':
arith_encode(codeBitFile, &eachCharProb[0], 27, 9);
break;
case 'J':
arith_encode(codeBitFile, &eachCharProb[0], 27, 10);
break;
case 'K':
arith_encode(codeBitFile, &eachCharProb[0], 27, 11);
break;
case 'L':
arith_encode(codeBitFile, &eachCharProb[0], 27, 12);
break;
case 'M':
arith_encode(codeBitFile, &eachCharProb[0], 27, 13);
break;
case 'N':
arith_encode(codeBitFile, &eachCharProb[0], 27, 14);
break;
case 'O':
arith_encode(codeBitFile, &eachCharProb[0], 27, 15);
break;
case 'P':
arith_encode(codeBitFile, &eachCharProb[0], 27, 16);
break;
case 'Q':
arith_encode(codeBitFile, &eachCharProb[0], 27, 17);
break;
case 'R':
arith_encode(codeBitFile, &eachCharProb[0], 27, 18);
break;
case 'S':
arith_encode(codeBitFile, &eachCharProb[0], 27, 19);
break;
case 'T':
arith_encode(codeBitFile, &eachCharProb[0], 27, 20);
break;
case 'U':
arith_encode(codeBitFile, &eachCharProb[0], 27, 21);
break;
case 'V':
arith_encode(codeBitFile, &eachCharProb[0], 27, 22);
break;
case 'W':
arith_encode(codeBitFile, &eachCharProb[0], 27, 23);
break;
case 'X':
arith_encode(codeBitFile, &eachCharProb[0], 27, 24);
break;
case 'Y':
arith_encode(codeBitFile, &eachCharProb[0], 27, 25);
break;
case 'Z':
arith_encode(codeBitFile, &eachCharProb[0], 27, 26);
break;
default:
printf ("There are some errors ,please check.\n");
break;
}
}
flush_arithmetic_encoder(codeBitFile);
CloseOutputBitFile(codeBitFile);
fclose(fpInput);
printf("Compressed successful,saved as:%s.\n",codeFile);
printf("Press any key to decompress %s.\n",codeFile);
getch();
decodeBitFile = OpenInputBitFile(codeFile);
initialize_arithmetic_decoder(decodeBitFile);
fpOutput = fopen(outputFile, "wb");
if(fpOutput == NULL)
{
printf ("%s file not exists or can't be opened, please check it.\n", outputFile);
return FAIL;
}
for(i=0;i<totalCharNum;i++)
{
index = arith_decode(decodeBitFile,&cSymbol,eachCharProb,27, totals);
switch (index)
{
case 0:
fputc(' ',fpOutput);
break;
case 1:
fputc('A',fpOutput);
break;
case 2:
fputc('B',fpOutput);
break;
case 3:
fputc('C',fpOutput);
break;
case 4:
fputc('D',fpOutput);
break;
case 5:
fputc('E',fpOutput);
break;
case 6:
fputc('F',fpOutput);
break;
case 7:
fputc('G',fpOutput);
break;
case 8:
fputc('H',fpOutput);
break;
case 9:
fputc('I',fpOutput);
break;
case 10:
fputc('J',fpOutput);
break;
case 11:
fputc('K',fpOutput);
break;
case 12:
fputc('L',fpOutput);
break;
case 13:
fputc('M',fpOutput);
break;
case 14:
fputc('N',fpOutput);
break;
case 15:
fputc('O',fpOutput);
break;
case 16:
fputc('P',fpOutput);
break;
case 17:
fputc('Q',fpOutput);
break;
case 18:
fputc('R',fpOutput);
break;
case 19:
fputc('S',fpOutput);
break;
case 20:
fputc('T',fpOutput);
break;
case 21:
fputc('U',fpOutput);
break;
case 22:
fputc('V',fpOutput);
break;
case 23:
fputc('W',fpOutput);
break;
case 24:
fputc('X',fpOutput);
break;
case 25:
fputc('Y',fpOutput);
break;
case 26:
fputc('Z',fpOutput);
break;
default:
break;
}
}
flush_arithmetic_encoder(codeBitFile);
CloseInputBitFile(decodeBitFile);
fclose(fpOutput);
printf("Decompressed successful,saved as:%s.\n",outputFile);
printf("Press any key to exit!\n");
getch();
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -