8000 i go · home-coder/data-abstraction-001@e684276 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

8000
Appearance settings

Commit e684276

Browse files
author
oneface
committed
i go
1 parent 919276e commit e684276

File tree

4 files changed

+195
-3
lines changed

4 files changed

+195
-3
lines changed

004-use_aqueue.c

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/***************************************************************************
2+
> Filename : 004-use_aqueue.c
3+
> Author : oneface - one_face@sina.com
4+
> Company : 一尊还酹江月
5+
> Time : 2018-05-19
6+
> Description: to bits lhy
7+
8+
本实验对应006-hfcompress.c中的压缩算法中的一小段关键代码,与huffman本身无关,与队列 Asicc码 字符等知识有关
9+
10+
'a' -> "00110"
11+
'b' -> "110001"
12+
...
13+
14+
input a b , output 54 -> '6' ->(00110110) + '001'
15+
16+
Example:
17+
jiangxj@jiangxj-xlj:~/github/c-intensive$ ./a.out a b d e
18+
00110
19+
l=5
20+
110001
21+
l=11
22+
bits = 6
23+
11100011
24+
l=11
25+
bits = >
26+
01010101
27+
l=11
28+
bits = ~
29+
30+
- This program is free software; you can redistribute it and/or modify it
31+
- under the terms of the GNU General Public License as published by the
32+
- Free Software Foundation; either version 2 of the License, or (at your
33+
- option) any later version.
34+
**************************************************************************/
35+
#include <stdio.h>
36+
#include <stdlib.h>
37+
38+
#define MAX 32
39+
static char *hc[] = {
40+
"00110",
41+
"110001",
42+
"1001",
43+
"11100011",
44+
"01010101",
45+
"0011111",
46+
};
47+
48+
typedef struct {
49+
int front, rear;
50+
char elem[MAX];
51+
unsigned int length;
52+
}aqueue; //array;
53+
54+
static void creat_queue(aqueue **aque)
55+
{
56+
*aque = (aqueue *)malloc(sizeof(**aque));
57+
(*aque)->front = (*aque)->rear = 0;
58+
(*aque)->length = 0;
59+
}
60+
61+
static void en_queue(aqueue *aque, char elem)
62+
{
63+
if (aque->rear != 0 && aque->rear % MAX == aque->front) {
64+
printf("queue is full\n");
65+
return ;
66+
}
67+
68+
aque->elem[aque->rear] = elem;
69+
aque->rear++;
70+
aque->length++;
71+
}
72+
73+
static int de_queue(aqueue *aque, char *elem)
74+
{
75+
if (aque->front != 0 && aque->front % MAX == aque->rear) {
76+
printf("queue is null\n");
77+
return -1;
78+
}
79+
80+
*elem = aque->elem[aque->front];
81+
aque->front++;
82+
aque->length--;
83+
84+
return 0;
85+
}
86+
87+
int main(int argc, char **argv)
88+
{
89+
int i, j;
90+
char bit, bits = 0;
91+
int s = sizeof(hc) / sizeof(hc[0]);
92+
93+
if (argc < 2) {
94+
fprintf(stderr, "argc invalid\n");
95+
return -1;
96+
}
97+
98+
//一个好的通用队列如何设计
99+
aqueue *aque = NULL;
100+
creat_queue(&aque);
101+
102+
for (i = 1; i < argc; i++) {
103+
char *pc = hc[argv[i][0] - 'a'];
104+
printf("%s\n", pc);
105+
while (*pc) {
106+
en_queue(aque, *pc);
107+
pc++;
108+
}
109+
printf("l=%d\n", aque->length);
110+
while (aque->length > 8) {
111+
j = 8;
112+
while (j--) {
113+
de_queue(aque, &bit);
114+
//如何把字符'0' '1' '0' ...压入字符变量bit (8bit 类型)当中
115+
//printf("bit = %c\n", bit);
116+
bits = bits | ((bit - '0') << j);
117+
}
118+
printf("bits = %c\n", bits);
119+
//TODO fwrite file
120+
}
121+
}
122+
123+
return 0;
124+
}

006-hfcompress.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ HTNode *CreatHFM(FILE * fp, short *n, WeightType * FileLength)
201201
ht[i].parent = 0;
202202
ht[i].RChild = 0;
203203
}
204+
for (i = 0; i < *n; i++) {
205+
printf("%c:%ld\n", ht[i].ch, ht[i].weight);
206+
}
204207
/* ------------初始化完毕!对应算法步骤1--------- */
205208
for (i = *n; i < m; i++) //创建非叶子结点,建哈夫曼树
206209
{ //在ht[0]~ht[i-1]的范围内选择两个parent为0且weight最小的结点,其序号分别赋值给s1、s2返回
@@ -354,8 +357,11 @@ void Compress()
354357
free(ht); //getchar();printf("最长码串长度: %d\n",MaxCode);printf("ftell = %d\n",ftell(compressFile));
355358
codeNum = CodeToFile(compressFile, hc, LeafNum, Q, &finalLength); //把字符转成其二进制编码写入文件,返回压成多少个
356359

357-
rewind(compressFile); //使文件指针移到开始printf("ftelll = %d\n",ftell(compressFile));
360+
rewind(compressFile); //使文件指针移到开始
361+
printf("ftelll = %ld\n",ftell(compressFile));
358362
fseek(compressFile, sizeof(WeightType) + sizeof(MyType), SEEK_SET);
363+
printf("ftelll = %ld\n",ftell(compressFile));
364+
printf("leafnum:%d\n", LeafNum);
359365
fwrite(&LeafNum, sizeof(short), 1, compressFile); //写入叶子个数
360366
fwrite(&maxLen, sizeof(MyType), 1, compressFile); //最长码串长度
361367
fwrite(&minLen, sizeof(MyType), 1, compressFile); //最短码串长度

Readme.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
## 2018 2 9 17:36
22
>考虑要反复的磨练前面的线性表存储结构,重复训练,我的目的不是非要用这些数据结构,而是锻炼代码的优雅风格,如诗一样优雅
33
4-
# 重复 重复 重复 重复 重复练习的重要 !
4+
# &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; 重复
55

66
# 1.数据结构与音频算法的应用
77
- **将处理pcm数据的方式与数据结构结合**
@@ -20,7 +20,9 @@
2020
* **解码实现**
2121
* **模仿360文件压缩解压管理器的实现**
2222
* 哈夫曼综合应用,想到什么结合什么,比如结合 **链表** **队列** ****
23-
* **006-hfcompress.c** 这个代码 2 days 看完,然后重复练习
23+
* **006-hfcompress.c** 这个代码 2 days 看完,然后重复练习
24+
* **project-setenv-resolution/resolution.c** 这个代码也要完善后,重复练习,目的是文本处理
25+
* **文本处理** 还要去分析pcm里的那个去掉录音无数据的算法
2426
* [Jcompress: 一款基于huffman编码和最小堆的压缩、解压缩小程序](https://www.cnblogs.com/junhuster/p/junhuster.html) 这个程序实例写的比较经典,有1000行,需要一周重复练习
2527

2628
**客户需求:**
@@ -32,6 +34,7 @@
3234

3335
## **Q&A**
3436
> 文件的打开方式。这里打开文件一定要用二进制形式,"wb","rb".因为二进制打开和文本打开其实是有区别的。文本方式打开,会对‘\n’进行特殊处理,那如果这个字符本身就是'\n'.这就会出现问题,所以使用二进制打开,特点:不进行任何处理,是什么就是什么。
37+
> 哈夫曼编码的本质是找出海量数据的特征,如出现的次数,以此为权重,进行编码 ---这是我说的。
3538
3639
## 扩展阅读:
3740

x001.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/***************************************************************************
2+
> Filename : x001.c
3+
> Author : oneface - one_face@sina.com
4+
> Company : 一尊还酹江月
5+
> Time : 2018-06-01 16:03:16
6+
> Description:
7+
传入参数范围{0, 360}
8+
数组0-35
9+
结果举例:eg1:如果传入330, 则输出32 33 34数组中对应元素的值
10+
eg2:如果传入360, 输出35, 0, 1对应的值
11+
12+
- This program is free software; you can redistribute it and/or modify it
13+
- under the terms of the GNU General Public License as published by the
14+
- Free Software Foundation; either version 2 of the License, or (at your
15+
- option) any later version.
16+
**************************************************************************/
17+
#include <stdio.h>
18+
19+
static void print_voice(int w)
20+
{
21+
int i;
22+
23+
if (w < 0 || w > 360) {
24+
return ;
25+
}
26+
27+
w /= 10;
28+
w = w - 1;
29+
if (w == -1) {
30+
printf("%d ", w + 36);
31+
} else {
32+
printf("%d ", w);
33+
}
34+
for (i = ++w; i < w + 2; i++) {
35+
if (i == 36) {
36+
i = 0;
37+
printf("%d ", i);
38+
w = -2;
39+
} else
40+
printf("%d ", i);
41+
}
42+
printf("\n");
43+
}
44+
45+
int main()
46+
{
47+
int w[37];
48+
int i;
49+
50+
for (i = 0; i < 37; i++) {
51+
w[i] = i * 10;
52+
}
53+
54+
for (i = 0; i < 37; i++) {
55+
print_voice(w[i]);
56+
}
57+
58+
return 0;
59+
}

0 commit comments

Comments
 (0)
0