• 大小: 4KB
    文件类型: .c
    金币: 1
    下载: 0 次
    发布日期: 2021-06-02
  • 语言: 其他
  • 标签: 算术编码  

资源简介

算术编码程序要求可以输入信源符号,可以自定义信号流,输出编码并解码。

资源截图

代码片段和文件信息

#include 
#include 
#include 

/* return the left boundary of a interval */
float interval(char cfloat *pp)
{
int in = c - ‘a‘;
float interval = 0;
if (in == 0)
{
return 0;
}
else
{
for (int i = 1; i <= in; i++)
{
interval += pp[i-1];
}
return interval;
}
}
/* function ends */

/* return the signal‘s probability */
float probability(char c float *pp)
{
int in = c - ‘a‘;
return pp[in];
}
/* function ends */

/* the coding function */
void init()
{
int flag = 1;
int n = 0;       //the number of signal
float prob = 0;  //each signal‘s probability
char ch = ““;      //a character to receive space
float *p = malloc(1 * sizeof(float));  //alloc room for the new pointer

/* receive signal‘s probabilities */
printf(“input the signal‘s probabilities(with spaces between numbers and an enter for end):\n“);
while (flag==1)
{
scanf_s(“%f%c“ &prob &ch); 
n++;
p = realloc(p n * sizeof(float));  //alloc a float size for a new signal
if (!p)
{
printf(“sorry realloc fails\n“);
return;
}
*(p + n - 1) = prob;
if (ch == ‘\n‘)
{
printf(“there are %d numbers\n“ n);
flag = 0;
break;
}
}
/* receiving ends */
/* check if the values are legal */
float allprob = 0;
for (int i = 0; i < n; i++)
{
allprob += p[i];
}
if (allprob != 1)  // if illegal shut down the program
{
printf(“sorry the input is illegal\n“);
return;
}
/* check ends */

/* initial signals */
flag = 1;
ch = ““;
char *s = malloc(1 * sizeof(char));
char sig = ““;   
int signum = 0;  // signum is the number of signals to be coded
/* initialing ends */

fflush(stdin);
/* input the signals */
printf(“input the signal stream to be coded(with an enter for end):\n“);
while (flag==1)
{
scanf_s(“%c“ &sig);
//printf(“ %c “ sig);
if (sig == ‘\n‘)
{
flag = 0;
break;
}
signum++;
s = realloc(s signum * sizeof(char));
if (!s)
{
printf(“sorry realloc fails\n“);
return;
}
*(s + signum - 1) = sig;

评论

共有 条评论