C++实现lzw编码的简单方案

#include
#include
#include
#include
struct stringcode
{ char string[50];
int len;
};

int ISINDic(stringcode str,stringcode dict[50],int len2)//判断出字符串若在,并返回字典中的位置
{ for(int i=0;iif(dict[i].len==str.len&&strnicmp(dict[i].string,str.string,str.len)==0)
return i+1;
return 0;
}
//判断字符是否在字典中
int ISINDic(char str,stringcode dict[50],int len2)
{ for(int i=1;i<=len2;i++)
{ if(dict[i-1].len==1&&str==dict[i-1].string[0])
return 1;
}
return 0;
}
void main()
{ int choice,yima[50];
int i,j,k,len2=0,m;
stringcode zifudata,dict[50],profix;
for(i=0;i<50;i++){zifudata.string[i]=0;}
for(i=0;i<50;i++){dict[i].len =0;}
for(i=0;i<50;i++){yima[i]=0;}
cout<<"_____________________LZW算法__________________________"<cout<<"++++++++++++++++++++++++++++++++++++++++++++++++++++++"<cout<<"学号:0811100211———————姓名:樊军———————"<cout<<"++++++++++++++++++++++++++++++++++++++++++++++++++++++"<cout<<"请输入所需的功能1:编码 2:译码"<cin>>choice;
if (choice==1) //该段实现编码————————————————————————
{
cout<<"please input the stringstream"<cin>>zifudata.string;
zifudata.len=strlen(zifudata.string);
dict[0].string[0]=zifudata.string[0];
dict[0].len=1;
int len2=1;
for (i=1;i{ if (ISINDic(zifudata.string[i],dict,len2)==0)
{ dict[len2].len++;
dict[len2].string[0]=zifudata.string[i];
len2++;
}
}

//此处将不断循环以判断和添加字典中 //编码
profix.len=0;j=0;k=1;
while(j{ stringcode profixc; //2、保存Profix对应的词典码字(索引)到yima[50]。
char c; //3、将ProfixC放入词典,词典长度+1
c=zifudata.string[j];
profixc.len=profix.len+1;
for(m=0;mprofixc.string[profixc.len-1]=c;
if (ISINDic(profixc,dict,len2)!=0){profix.len++;profix.string[profix.len-1]=profixc.string[profixc.len-1];j++;}
else{
yima[k]=ISINDic(profix,dict,len2);k++;
dict[len2].len=profixc.len;
for(m=0;mlen2++;
profix.len=1;
profix.string[0]=zifudata.string[j];
if(j+1>=zifudata.len){yima[k]=ISINDic(profix,dict,le

n2);k++;}
j++;
}

}
cout<<"DICTIONARY+++++++++++++++++++++++++++++++++++++++"<for(j=0;jcout<<"第"<for(i=0;icout<cout<cout<<"TRANSLATE++++++++++++++++++++++++++++++++++++++="<for(j=1;j}
else //该段实现译码——————————————————————————————-
{ for(i=0;i<50;i++){zifudata.string[i]=0;}//初始化
for(i=0;i<50;i++){dict[i].len =0;}
for(i=0;i<50;i++){yima[i]=0;}
int len2=0;
int Codelength=0;
stringcode pw,cw;
pw.len=0;
cw.len=0;
zifudata.len=0;
if(choice==2)
{ cout<<"请输入字符根缀表(不需要符号隔开):";
char s[20];
cin>>s;
int s_length=strlen(s);
for (i=0;i{ dict[i].string[0]=s[i];
dict[i].len=1;
len2++;
} //压入可能的所有的单字
cout<<"请输入解码数字的个数:";
cin>>Codelength;
cout<<"请输入码字(数字之间用Enter隔开):"<for (i=0;icin>>yima[i];
int p=0;//以p控制码字的向后移动
int num=yima[0];
zifudata.string[0]=dict[num-1].string[0]; //
zifudata.len++;
cw.string[0]=zifudata.string[0]; //
cw.len=1;
pw=cw; //第一个字符输出
while (++p{ num=yima[p];
if (num<=len2) //字典中存在数字序号对应字符,即在词典中有
{ cw=dict[num-1]; //①保存码字对应的字典内容到字符串
for(int i=0;i{ zifudata.len++; //③将码字对应的字典内容+当前字符输入到字典
zifudata.string[zifudata.len-1]=dict[num-1].string[i];
}
stringcode pc;
pc.len=pw.len+1;
for (i=0;ipc.string[i]=pw.string[i];
pc.string[pc.len-1]=cw.string[0];
len2++;
dict[len2-1]=pc;
pw=cw;
}
else if (num==len2+1) //字典中没有
{ stringcode pc; //①将pc=pw+cw
pc.len=pw.len+1; //②输出到字符串
for (i=0;ipc.string[i]=pw.string[i];
pc.string[pc.len-1]=cw.string[0];
cw=pc;
for(int i=0;i{ zifudata.len++;
zifudata.string[zifudata.len-1]=cw.string[i];
}
pw=cw;
}
else cout<<"输入的数字序列有误,无法解码……"<

}

cout<<"字典为:"<

出字典
for (i=0;i{ cout<for(int j=0;jcout<cout<}
cout<
cout<<"LZW解压缩后字符串为:"<for (i=0;icout<cout<
}
else{cout<<"wrong input"<}
}

相关文档
最新文档