-
Notifications
You must be signed in to change notification settings - Fork 0
/
Huffman.pas
159 lines (149 loc) · 3.57 KB
/
Huffman.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
Program Huffman;
type
numbersA = array[1..256] of integer;
bynumbersA = array[0..255] of integer;
CharList = array[1..256] of string;
codes = array[1..256] of string;
var
Name1, Name2, Name3, obms, work: string;
f1, f2: file of byte;
bynumbers: bynumbersA;
numbers: numbersA;
symbols, dynsym: CharList;
f3: text;
CurrCh, Mask, CurrBy, zero, one: byte;
i, j, k, ka, obmi: integer;
code: codes;
fl:boolean;
begin
zero:=0;
one:=1;
writeln('Ââåäèòå íàçâàíèå ðàññìàòðèâàåìîãî ôàéëà');
readln(Name1);
writeln('Ââåäèòå æåëàåìîå íàçâàíèå íîâîãî ôàéëà');
readln(Name2);
Name3:=Concat('Ñëîâàðü ',Name2);
assignfile(f1,Name1);
assignfile(f2,Name2);
for i:=0 to 255 do
bynumbers[i]:=0;
reset(f1);
CurrCh:=1;
k:=0;
while not eof(f1) do
begin
read(f1, CurrCh);
Inc(bynumbers[CurrCh]);
if (bynumbers[CurrCh]=1) then
begin
Inc(k);
symbols[k]:=char(CurrCh);
dynsym[k]:=symbols[k];
numbers[k]:=1;
end
else begin
fl:=false;
j:=1;
while (j<=k) and (not fl) do
begin
if (char(CurrCh)=symbols[j]) then
begin
Inc(numbers[j]);
fl:=true;
end;
Inc(j);
end;
end;
end;
closefile(f1);
ka:=k;
if (ka=1) then code[1]:='0';
while (ka>1) do begin
for i:=1 to ka do
for j:=1 to (ka-1) do
if (numbers[j]>numbers[j+1]) then
begin
obms:=dynsym[j]; obmi:=numbers[j];
dynsym[j]:=dynsym[j+1]; numbers[j]:=numbers[j+1];
dynsym[j+1]:=obms; numbers[j+1]:=obmi;
end;
work:=dynsym[1];
for i:=1 to length(work) do
begin
j:=1;
fl:=false;
while (not fl) and (j<=k) do
begin
if (work[i]=symbols[j]) then
begin
code[j]:=code[j]+one;
fl:=true;
end;
Inc(j);
end;
end;
work:=dynsym[2];
for i:=1 to length(work) do
begin
j:=1;
fl:=false;
while (not fl) and (j<=k) do
begin
if (work[i]=symbols[j]) then
begin
code[j]:=code[j]+zero;
fl:=true;
end;
Inc(j);
end;
end;
numbers[1]:=numbers[1]+numbers[2];
dynsym[1]:=dynsym[1]+dynsym[2];
if (ka>2) then
for i:=2 to (ka-1) do
begin
dynsym[i]:=dynsym[i+1];
numbers[i]:=numbers[i+1];
end;
Dec(ka);
end;
assignfile(f3,Name3);
rewrite(f3);
for i:=1 to k do
writeln(f3,symbols[i],' - ',code[i]);
closefile(f3);
reset(f1);
rewrite(f2);
Mask:=$80;
CurrBy:=0;
while not eof(f1) do
begin
read(f1,CurrCh);
fl:=false;
i:=1;
while (i<=k) and (not fl) do
begin
if (symbols[i]=char(CurrCh)) then
begin
fl:=true;
for j:=1 to length(code[i]) do
begin
if (code[i][j]='1') then
CurrBy:=CurrBy or Mask;
Mask:=Mask shr 1;
if Mask=0 then
begin
write(f2,CurrBy);
Mask:=$80;
CurrBy:=0;
end;
end;
end;
Inc(i);
end;
end;
if Mask<>$80 then
write(f2,CurrBy);
closefile(f2);
closefile(f1);
end.