输入:输入空格及26小写英文字母出现的频次,然后输入一小段文字。 输出: 对整段文字 输出=“:整段文字字符数回车”; WPL“=:整段文字的WPL值回车”; 平均比特数“=:整段文字的平均每个字符的比特数回车”; 对小段文字 编码“=:二进制编码回车”; 译码“=:对上述编码进行译码回车”;c语言
时间: 2023-08-10 12:03:43 浏览: 36
以下是C语言的实现代码,可以帮助您完成该任务:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_FREQ 26
#define MAX_TEXT 1000
// 获取输入的频次统计信息
void get_char_freq(int char_freq[MAX_FREQ]) {
char freq[MAX_FREQ * 2];
int i, j;
fgets(freq, MAX_FREQ * 2, stdin);
for (i = 0, j = 0; freq[j] != '\n'; i += 1, j += 2) {
char_freq[i] = atoi(&freq[j + 1]);
}
}
// 获取输入的文本信息
void get_text(char text[MAX_TEXT]) {
fgets(text, MAX_TEXT, stdin);
text[strcspn(text, "\n")] = '\0'; // 去除读入的换行符
}
// 计算文本中字符数
int get_char_count(char *text) {
return strlen(text);
}
// 计算文本的WPL值
float get_wpl(int char_freq[MAX_FREQ]) {
int i, total_bits = 0, char_bits = 0;
float wpl = 0.0;
for (i = 0; i < MAX_FREQ; i += 1) {
total_bits += char_freq[i];
}
for (i = 0; i < MAX_FREQ; i += 1) {
if (char_freq[i] > 0) {
char_bits = (int) (char_freq[i] * (float) (log2(char_freq[i])));
wpl += (float) char_bits / total_bits;
}
}
return wpl;
}
// 计算每个字符的平均比特数
float get_avg_bits(int char_freq[MAX_FREQ], int char_count) {
int i, total_bits = 0, char_bits = 0;
float avg_bits = 0.0;
for (i = 0; i < MAX_FREQ; i += 1) {
total_bits += char_freq[i];
}
for (i = 0; i < MAX_FREQ; i += 1) {
if (char_freq[i] > 0) {
char_bits = (int) (char_freq[i] * (float) (log2(char_freq[i])));
avg_bits += (float) char_bits / char_count;
}
}
return avg_bits;
}
// 对小段文字进行编码
void encode_text(char *text, int char_freq[MAX_FREQ], char *encoded_text) {
int i, j, max_freq = 0, freq_size = 0;
char freq_str[MAX_FREQ][9];
for (i = 0; i < MAX_FREQ; i += 1) {
if (char_freq[i] > max_freq) {
max_freq = char_freq[i];
}
}
sprintf(freq_str[0], "%d", max_freq);
freq_size = strlen(freq_str[0]);
for (i = 1; i < MAX_FREQ; i += 1) {
if (char_freq[i] > 0) {
sprintf(freq_str[i], "%0*d", freq_size, char_freq[i]);
} else {
freq_str[i][0] = '\0';
}
}
j = 0;
for (i = 0; text[i] != '\0'; i += 1) {
strcat(encoded_text, freq_str[text[i] - 'a']);
j += strlen(freq_str[text[i] - 'a']);
}
}
// 对编码进行译码
void decode_text(char *encoded_text, int char_freq[MAX_FREQ], char *decoded_text) {
int i, j, k, max_freq = 0, freq_size = 0, freq_count[MAX_FREQ] = {0};
char freq_str[MAX_FREQ][9], freq_char[MAX_FREQ];
for (i = 0; i < MAX_FREQ; i += 1) {
if (char_freq[i] > max_freq) {
max_freq = char_freq[i];
}
}
sprintf(freq_str[0], "%d", max_freq);
freq_size = strlen(freq_str[0]);
for (i = 1; i < MAX_FREQ; i += 1) {
if (char_freq[i] > 0) {
sprintf(freq_str[i], "%0*d", freq_size, char_freq[i]);
freq_char[i] = 'a' + i;
} else {
freq_str[i][0] = '\0';
freq_char[i] = '\0';
}
}
j = 0;
while (encoded_text[j] != '\0') {
for (i = 0; i < MAX_FREQ; i += 1) {
if (strncmp(&encoded_text[j], freq_str[i], freq_size) == 0) {
decoded_text[k++] = freq_char[i];
freq_count[i] += 1;
j += freq_size;
break;
}
}
}
}
int main() {
int char_freq[MAX_FREQ] = {0};
char text[MAX_TEXT] = {'\0'}, encoded_text[MAX_TEXT * 9] = {'\0'}, decoded_text[MAX_TEXT] = {'\0'};
int char_count;
float wpl, avg_bits;
get_char_freq(char_freq);
get_text(text);
char_count = get_char_count(text);
wpl = get_wpl(char_freq);
avg_bits = get_avg_bits(char_freq, char_count);
encode_text(text, char_freq, encoded_text);
decode_text(encoded_text, char_freq, decoded_text);
printf(":%d\n", char_count);
printf("WPL=%.2f\n", wpl);
printf("平均比特数=%.2f\n", avg_bits);
printf("编码=%s\n", encoded_text);
printf("译码=%s\n", decoded_text);
return 0;
}
```
您可以将上述代码保存为.c文件并在C语言环境中编译运行,输入频次和文本信息即可输出对应的结果。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)