📄 sscoding.cpp
字号:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
//----------Initialization.
begin: FILE *f_str;
char *f_path;
int array_length,el_number=1;
int tmp_length2=1,ch_tmp_flag=1,ch_counter,order_num;
char str_array[20],str_element[20],Dec_str_array[20],ch_temp_str,ch_temp_el,ch_exit;
char *str_exit={"System is trying to Quit...Byebye!"};
double ch_rates[20][2],ch_area[20][2],Code_area[1][2],Decode_area[1][2];
double P_acum_rate=0,P_temp=0,p_get_rate=1,p_temp=1,C_codes=0,C_temp=0,Area_length=1,Area_temp=1,Dec_code;
printf("Please input the file PATH:");
gets(f_path);
f_str=fopen(f_path,"r");
fgets(str_array,20,f_str);
array_length=strlen(str_array);
//----------Input error?
if(array_length>20)
{printf("The file string OVERFLOW!\n");goto exit;}
if(f_str==NULL)
{printf("The file is not exist or is empty!\n");goto again;}
else if(array_length>0&&array_length<=20)
{
printf("The CODING string of the file is:\n");
printf("%s\n",str_array);
printf("The total character number of the string is: %d.\n",array_length);
printf("Character--Amount----p(x)------P(x)---------Area---------\n");
//----------Get single-characters.
str_element[0]=str_array[0];
for(int i=0;i<array_length;i++)
{
ch_temp_str=str_array[i];
for(int i1=0;i1<el_number;i1++)
{
if(ch_temp_str==str_element[i1])
{ch_tmp_flag=0;break;}
else {ch_tmp_flag=1;}
}
if(ch_tmp_flag==1)
{
str_element[el_number]=ch_temp_str;
tmp_length2=tmp_length2+1;
el_number=tmp_length2;
}
}
//----------Get the rates and the areas.
ch_rates[0][1]=0;
for(i=0;i<el_number;i++)
{
ch_counter=0;
ch_temp_el=str_element[i];
for(int j=0;j<array_length;j++)
{
ch_temp_str=str_array[j];
if(ch_temp_str==ch_temp_el)
{ch_counter=ch_counter+1;}
}
ch_rates[i][0]=(double)ch_counter/(double)array_length;
if(i<el_number-1)
{ch_rates[i+1][1]=ch_rates[i][1]+ch_rates[i][0];}
ch_area[i][0]=ch_rates[i][1];
ch_area[i][1]=ch_rates[i][1]+ch_rates[i][0];
printf(" %c ",ch_temp_el);
printf("%d ",ch_counter);
printf("%lf %lf ",ch_rates[i][0],ch_rates[i][1]);
printf("[%lf,%lf)\n",ch_area[i][0],ch_area[i][1]);
}
//----------Get the coding area.
for(i=0;i<array_length;i++)
{
ch_temp_str=str_array[i];
for(int j=0;j<el_number;j++)
{
if(ch_temp_str==str_element[j])
{order_num=j; break;}
}
P_acum_rate=ch_rates[order_num][1];
p_get_rate=ch_rates[order_num][0];
C_codes=C_codes+P_acum_rate*Area_length;
Area_length=p_get_rate*Area_length;
}
Code_area[0][0]=C_codes;
Code_area[0][1]=C_codes+Area_length;
printf("The last Coding/Decoding-Area is: [%g,%g)\n",Code_area[0][0],Code_area[0][1]);
//----------Decoding start here.
Dec_code=(Code_area[0][0]+Code_area[0][1])/2;
P_acum_rate=0,p_get_rate=1,C_codes=0,Area_length=1;
//----------Get the first character and its rate information.
for(i=0;i<el_number;i++)
{
if(Dec_code>=ch_area[i][0]&&Dec_code<ch_area[i][1])
{
Dec_str_array[0]=str_element[i];
Decode_area[0][0]=ch_area[i][0];
Decode_area[0][1]=ch_area[i][1];
P_acum_rate=ch_rates[i][1];
p_get_rate=ch_rates[i][0];
C_codes=C_codes+P_acum_rate*Area_length;
Area_length=p_get_rate*Area_length;
break;
}
}
//----------Resume the string with computing and comparing circularly.
for(i=1;i<array_length;i++)
{
P_temp=P_acum_rate;
p_temp=p_get_rate;
C_temp=C_codes;
Area_temp=Area_length;
ch_temp_str=str_array[i];
for(int j=0;j<el_number;j++)
{
if(ch_temp_str==str_element[j])
{
P_temp=ch_rates[j][1];
p_temp=ch_rates[j][0];
C_temp=C_temp+P_temp*Area_temp;
Area_temp=p_temp*Area_temp;
if(Dec_code>=C_temp&&Dec_code<C_temp+Area_temp)
{
P_acum_rate=P_temp;
p_get_rate=p_temp;
C_codes=C_temp;
Area_length=Area_temp;
Dec_str_array[i]=str_element[j];
}
break;
}
}
}
printf("The DECODED string is as follow:\n");
for(i=0;i<array_length;i++)
{printf("%c",Dec_str_array[i]);}
printf("\n");
}
again: printf("\nTry again?(Enter to Continue;Any key for Quit.)\n");
ch_exit=getch();
if(ch_exit==13)
{
//free valiables here
printf("\n");
goto begin;
}
exit: for(long i=0;i<7000000;i++)
{if(i%200000==0)printf("%c",str_exit[i/200000]);}
for(i=0;i<100000000;i++){;}
printf("\n\n");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -