A. 用c语言实现算术编码和解码
Turbo c 2.0编译通过
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#defineLENGTH100 /*字符串(编码前或编码后)的最大长度*/
/*编码*/
voidencode(char*strsource);
/*解码*/
voiddecode(char*strcode);
voidmain()
{裤和
橡帆charcode[LENGTH]="BILLGATES";
encode(code);
printf("\nencodedstringis:%s\n",code);
decode(code);
printf("\ndecodedstringis:%s\n",code);
getch();
}
voidencode(char*strsource){
char*p=strsource,tmp[LENGTH]={'\0'},buffer[3];
while(*p){
itoa(*p++,buffer,10);
strcat(tmp,"%");
strcat(tmp,buffer);
}
梁纯雹strcpy(strsource,tmp);
}
voiddecode(char*strcode){
inti=0;
char*p,*s=strcode,tmp[LENGTH]={'\0'};
char*cSplit="%";
p=strtok(s,cSplit);
while(p)
{
tmp[i++]=atoi(p);
p=strtok(NULL,cSplit);
}
strcpy(strcode,tmp);
}