<?xml version='1.0' encoding="utf-8"?>
      <rss version='2.0'>
      <channel>
      <title>Форум на Исходниках.RU</title>
      <link>https://forum.sources.ru</link>
      <description>Форум на Исходниках.RU</description>
      <generator>Форум на Исходниках.RU</generator>
  	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=9057&amp;view=findpost&amp;p=89096</guid>
        <pubDate>Sat, 01 Nov 2003 11:37:42 +0000</pubDate>
        <title>Алгоритм и код Хаффмана. $$$!</title>
        <link>https://forum.sources.ru/index.php?showtopic=9057&amp;view=findpost&amp;p=89096</link>
        <description><![CDATA[GrAnd: В дальнейшем разобраться труда не составит..]]></description>
        <author>GrAnd</author>
        <category>Алгоритмы</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=9057&amp;view=findpost&amp;p=89095</guid>
        <pubDate>Sat, 01 Nov 2003 11:36:41 +0000</pubDate>
        <title>Алгоритм и код Хаффмана. $$$!</title>
        <link>https://forum.sources.ru/index.php?showtopic=9057&amp;view=findpost&amp;p=89095</link>
        <description><![CDATA[GrAnd: <div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">&#60;br&#62;{$A+,B-,D+,E+,F-,G-,I-,L+,N-,O-,R+,S+,V+,X-}&#60;br&#62;{$M 16384,0,655360}&#60;br&#62;{******************************************************}&#60;br&#62;{* &nbsp; &nbsp; &nbsp; &nbsp; Алгоритм уплотнения данных по методу &nbsp; &nbsp; &nbsp; *}&#60;br&#62;{* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Хафмана. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *}&#60;br&#62;{******************************************************}&#60;br&#62;Program Hafman;&#60;br&#62;Uses Crt,Dos,Printer;&#60;br&#62;Type &nbsp; &nbsp;PCodElement = ^CodElement;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp;CodElement = record&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NewLeft,NewRight,&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;P0, P1 : PCodElement; &nbsp; {элемент входящий одновременно}&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;LengthBiteChain : byte; { в массив , очередь и дерево }&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;BiteChain : word;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CounterEnter : word;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Key : boolean;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Index : byte;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp;TCodeTable = array [0..255] of PCodElement;&#60;br&#62;Var &nbsp; &nbsp; CurPoint,HelpPoint,&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp;LeftRange,RightRange : PCodElement;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp;CodeTable : TCodeTable;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp;Root : PCodElement;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp;InputF, OutputF, InterF : file;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp;TimeUnPakFile : longint;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp;AttrUnPakFile : word;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp;NumRead, NumWritten: Word;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp;InBuf &nbsp;: array[0..10239] of byte;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp;OutBuf : array[0..10239] of byte;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp;BiteChain : word;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp;CRC,&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp;CounterBite : byte;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp;OutCounter : word;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp;InCounter : word;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp;OutWord : word;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp;St : string;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp;LengthOutFile, LengthArcFile : longint;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp;Create : boolean;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp;NormalWork : boolean;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp;ErrorByte : byte;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp;DeleteFile : boolean;&#60;br&#62;{-------------------------------------------------}&#60;br&#62;procedure ErrorMessage;&#60;br&#62;{ --- вывод сообщения об ошибке --- }&#60;br&#62;begin&#60;br&#62; If ErrorByte &#60;&#62; 0 then&#60;br&#62; &nbsp;begin&#60;br&#62; &nbsp; Case ErrorByte of&#60;br&#62; &nbsp; &nbsp;2 : Writeln(&#39;File not found ...&#39;);&#60;br&#62; &nbsp; &nbsp;3 : Writeln(&#39;Path not found ...&#39;);&#60;br&#62; &nbsp; &nbsp;5 : Writeln(&#39;Access denied ...&#39;);&#60;br&#62; &nbsp; &nbsp;6 : Writeln(&#39;Invalid handle ...&#39;);&#60;br&#62; &nbsp; &nbsp;8 : Writeln(&#39;Not enough memory ...&#39;);&#60;br&#62; &nbsp; 10 : Writeln(&#39;Invalid environment ...&#39;);&#60;br&#62;.&#60;br&#62; &nbsp; 11 : Writeln(&#39;Invalid format ...&#39;);&#60;br&#62; &nbsp; 18 : Writeln(&#39;No more files ...&#39;);&#60;br&#62; &nbsp; else Writeln(&#39;Error #&#39;,ErrorByte,&#39; ...&#39;);&#60;br&#62; &nbsp; end;&#60;br&#62; &nbsp; NormalWork:=False;&#60;br&#62; &nbsp; ErrorByte:=0;&#60;br&#62; &nbsp;end;&#60;br&#62;end;&#60;br&#62;procedure ResetFile;&#60;br&#62;{ --- открытие файла для архивации --- }&#60;br&#62;Var St : string;&#60;br&#62;begin&#60;br&#62; &nbsp;Assign(InputF, ParamStr(3));&#60;br&#62; &nbsp;Reset(InputF, 1);&#60;br&#62; &nbsp;ErrorByte:=IOResult;&#60;br&#62; &nbsp;ErrorMessage;&#60;br&#62; &nbsp;If NormalWork then Writeln(&#39;Pak file : &#39;,ParamStr(3),&#39;...&#39;);&#60;br&#62;end;&#60;br&#62;procedure ResetArchiv;&#60;br&#62;{ --- открытие файла архива, или его создание --- }&#60;br&#62;begin&#60;br&#62; &nbsp;St:=ParamStr(2);&#60;br&#62; &nbsp;If Pos(&#39;.&#39;,St)&#60;&#62;0 then Delete(St,Pos(&#39;.&#39;,St),4);&#60;br&#62; &nbsp;St:=St+&#39;.vsg&#39;;&#60;br&#62; &nbsp;Assign(OutputF, St);&#60;br&#62; &nbsp;Reset(OutPutF,1);&#60;br&#62; &nbsp;Create:=False;&#60;br&#62; &nbsp;If IOResult=2 then&#60;br&#62; &nbsp; begin&#60;br&#62; &nbsp; &nbsp;Rewrite(OutputF, 1);&#60;br&#62; &nbsp; &nbsp;Create:=True;&#60;br&#62; &nbsp; end;&#60;br&#62; &nbsp;If NormalWork then&#60;br&#62; &nbsp; If Create then Writeln(&#39;Create archiv : &#39;,St,&#39;...&#39;)&#60;br&#62; &nbsp; &nbsp;else Writeln(&#39;Open archiv : &#39;,St,&#39;...&#39;)&#60;br&#62;end;&#60;br&#62;procedure SearchNameInArchiv;&#60;br&#62;{ --- в дальнейшем - поиск имени файла в архиве --- }&#60;br&#62;begin&#60;br&#62; Seek(OutputF,FileSize(OutputF));&#60;br&#62; ErrorByte:=IOResult;&#60;br&#62; ErrorMessage;&#60;br&#62;end;&#60;br&#62;procedure DisposeCodeTable;&#60;br&#62;{ --- уничтожение кодовой таблицы и очереди --- }&#60;br&#62;Var I : byte;&#60;br&#62;begin&#60;br&#62; For I:=0 to 255 do Dispose(CodeTable[I]);&#60;br&#62;end;&#60;br&#62;procedure ClosePakFile;&#60;br&#62;{ --- закрытие архивируемого файла --- }&#60;br&#62;Var I : byte;&#60;br&#62;begin&#60;br&#62; If DeleteFile then Erase(InputF);&#60;br&#62;.&#60;br&#62; Close(InputF);&#60;br&#62;end;&#60;br&#62;procedure CloseArchiv;&#60;br&#62;{ --- закрытие архивного файла --- }&#60;br&#62;begin&#60;br&#62; If FileSize(OutputF)=0 then Erase(OutputF);&#60;br&#62; Close(OutputF);&#60;br&#62;end;&#60;br&#62;procedure InitCodeTable;&#60;br&#62;{ --- инициализация таблицы кодировки --- }&#60;br&#62;Var I : byte;&#60;br&#62;begin&#60;br&#62; For I:=0 to 255 do&#60;br&#62; &nbsp;begin&#60;br&#62; &nbsp; &nbsp;New(CurPoint);&#60;br&#62; &nbsp; &nbsp;CodeTable[I]:=CurPoint;&#60;br&#62; &nbsp; &nbsp;With CodeTable[I]^ do&#60;br&#62; &nbsp; &nbsp; begin&#60;br&#62; &nbsp; &nbsp; &nbsp;P0:=Nil;&#60;br&#62; &nbsp; &nbsp; &nbsp;P1:=Nil;&#60;br&#62; &nbsp; &nbsp; &nbsp;LengthBiteChain:=0;&#60;br&#62; &nbsp; &nbsp; &nbsp;BiteChain:=0;&#60;br&#62; &nbsp; &nbsp; &nbsp;CounterEnter:=1;&#60;br&#62; &nbsp; &nbsp; &nbsp;Key:=True;&#60;br&#62; &nbsp; &nbsp; &nbsp;Index:=I;&#60;br&#62; &nbsp; &nbsp; end;&#60;br&#62; &nbsp;end;&#60;br&#62; For I:=0 to 255 do&#60;br&#62; &nbsp;begin&#60;br&#62; &nbsp; If I&#62;0 then CodeTable[I-1]^.NewRight:=CodeTable[I];&#60;br&#62; &nbsp; If I&#60;255 then CodeTable[I+1]^.NewLeft:=CodeTable[I];&#60;br&#62; &nbsp;end;&#60;br&#62; LeftRange:=CodeTable[0];&#60;br&#62; RightRange:=CodeTable[255];&#60;br&#62; CodeTable[0]^.NewLeft:=Nil;&#60;br&#62; CodeTable[255]^.NewRight:=Nil;&#60;br&#62;end;&#60;br&#62;procedure SortQueueByte;&#60;br&#62;{ --- пузырьковая сортировка по возрастанию --- }&#60;br&#62;Var Pr1,Pr2 : PCodElement;&#60;br&#62;begin&#60;br&#62; CurPoint:=LeftRange;&#60;br&#62; While CurPoint &#60;&#62; RightRange do&#60;br&#62; &nbsp;begin&#60;br&#62; &nbsp; If CurPoint^.CounterEnter &#62; CurPoint^.NewRight^.CounterEnter then&#60;br&#62; &nbsp; &nbsp;begin&#60;br&#62; &nbsp; &nbsp; HelpPoint:=CurPoint^.NewRight;&#60;br&#62; &nbsp; &nbsp; HelpPoint^.NewLeft:=CurPoint^.NewLeft;&#60;br&#62; &nbsp; &nbsp; CurPoint^.NewLeft:=HelpPoint;&#60;br&#62; &nbsp; &nbsp; If HelpPoint^.NewRight&#60;&#62;Nil then HelpPoint^.NewRight^.NewLeft:=CurPoint;&#60;br&#62; &nbsp; &nbsp; CurPoint^.NewRight:=HelpPoint^.NewRight;&#60;br&#62; &nbsp; &nbsp; HelpPoint^.NewRight:=CurPoint;&#60;br&#62; &nbsp; &nbsp; If HelpPoint^.NewLeft&#60;&#62;Nil then HelpPoint^.NewLeft^.NewRight:=HelpPoint;&#60;br&#62; &nbsp; &nbsp; If CurPoint=LeftRange then LeftRange:=HelpPoint;&#60;br&#62; &nbsp; &nbsp; If HelpPoint=RightRange then RightRange:=CurPoint;&#60;br&#62; &nbsp; &nbsp; CurPoint:=CurPoint^.NewLeft;&#60;br&#62;.&#60;br&#62; &nbsp; &nbsp; If CurPoint = LeftRange then CurPoint:=CurPoint^.NewRight&#60;br&#62; &nbsp; &nbsp; &nbsp;else CurPoint:=CurPoint^.NewLeft;&#60;br&#62; &nbsp; &nbsp;end&#60;br&#62; &nbsp; &nbsp;else CurPoint:=CurPoint^.NewRight;&#60;br&#62; &nbsp;end;&#60;br&#62;end;&#60;br&#62;procedure CounterNumberEnter;&#60;br&#62;{ --- подсчет частот вхождений байтов в блоке --- }&#60;br&#62;Var C : word;&#60;br&#62;begin&#60;br&#62; For C:=0 to NumRead-1 do&#60;br&#62; &nbsp;Inc(CodeTable[(InBuf[C])]^.CounterEnter);&#60;br&#62;end;&#60;br&#62;function SearchOpenCode : boolean;&#60;br&#62;{ --- поиск в очереди пары открытых по Key минимальных значений --- }&#60;br&#62;begin&#60;br&#62; CurPoint:=LeftRange;&#60;br&#62; HelpPoint:=LeftRange;&#60;br&#62; HelpPoint:=HelpPoint^.NewRight;&#60;br&#62; While not CurPoint^.Key do&#60;br&#62; &nbsp;CurPoint:=CurPoint^.NewRight;&#60;br&#62; While (not (HelpPoint=RightRange)) and (not HelpPoint^.Key) do&#60;br&#62; &nbsp;begin&#60;br&#62; &nbsp; HelpPoint:=HelpPoint^.NewRight;&#60;br&#62; &nbsp; If (HelpPoint=CurPoint) and (HelpPoint&#60;&#62;RightRange) then&#60;br&#62; &nbsp; &nbsp;HelpPoint:=HelpPoint^.NewRight;&#60;br&#62; &nbsp;end;&#60;br&#62; If HelpPoint=CurPoint then SearchOpenCode:=False else SearchOpenCode:=True;&#60;br&#62;end;&#60;br&#62;procedure CreateTree;&#60;br&#62;{ --- создание дерева частот вхождения --- }&#60;br&#62;begin&#60;br&#62; While SearchOpenCode do&#60;br&#62; &nbsp;begin&#60;br&#62; &nbsp; New(Root);&#60;br&#62; &nbsp; With Root^ do&#60;br&#62; &nbsp; &nbsp;begin&#60;br&#62; &nbsp; &nbsp; P0:=CurPoint;&#60;br&#62; &nbsp; &nbsp; P1:=HelpPoint;&#60;br&#62; &nbsp; &nbsp; LengthBiteChain:=0;&#60;br&#62; &nbsp; &nbsp; BiteChain:=0;&#60;br&#62; &nbsp; &nbsp; CounterEnter:=P0^.CounterEnter + P1^.CounterEnter;&#60;br&#62; &nbsp; &nbsp; Key:=True;&#60;br&#62; &nbsp; &nbsp; P0^.Key:=False;&#60;br&#62; &nbsp; &nbsp; P1^.Key:=False;&#60;br&#62; &nbsp; &nbsp;end;&#60;br&#62; &nbsp; HelpPoint:=LeftRange;&#60;br&#62; &nbsp; While (HelpPoint^.CounterEnter &#60; Root^.CounterEnter) and&#60;br&#62; &nbsp; &nbsp;(HelpPoint&#60;&#62;Nil) do HelpPoint:=HelpPoint^.NewRight;&#60;br&#62; &nbsp; If HelpPoint=Nil then { добавление в конец }&#60;br&#62; &nbsp; &nbsp;begin&#60;br&#62; &nbsp; &nbsp; Root^.NewLeft:=RightRange;&#60;br&#62; &nbsp; &nbsp; RightRange^.NewRight:=Root;&#60;br&#62; &nbsp; &nbsp; Root^.NewRight:=Nil;&#60;br&#62; &nbsp; &nbsp; RightRange:=Root;&#60;br&#62; &nbsp; &nbsp;end&#60;br&#62;.&#60;br&#62; &nbsp; else&#60;br&#62; &nbsp; &nbsp;begin { вставка перед HelpPoint }&#60;br&#62; &nbsp; &nbsp; Root^.NewLeft:=HelpPoint^.NewLeft;&#60;br&#62; &nbsp; &nbsp; HelpPoint^.NewLeft:=Root;&#60;br&#62; &nbsp; &nbsp; Root^.NewRight:=HelpPoint;&#60;br&#62; &nbsp; &nbsp; If Root^.NewLeft&#60;&#62;Nil then Root^.NewLeft^.NewRight:=Root;&#60;br&#62; &nbsp; &nbsp;end;&#60;br&#62; &nbsp;end;&#60;br&#62;end;&#60;br&#62;procedure ViewTree( P : PCodElement );&#60;br&#62;{ --- просмотр дерева частот и присваивание кодировочных цепей листьям --- }&#60;br&#62;Var Mask,I : word;&#60;br&#62;begin&#60;br&#62; Inc(CounterBite);&#60;br&#62; If P^.P0&#60;&#62;Nil then ViewTree( P^.P0 );&#60;br&#62; If P^.P1&#60;&#62;Nil then&#60;br&#62; &nbsp;begin&#60;br&#62; &nbsp; Mask:=(1 SHL (16-CounterBite));&#60;br&#62; &nbsp; BiteChain:=BiteChain OR Mask;&#60;br&#62; &nbsp; ViewTree( P^.P1 );&#60;br&#62; &nbsp; Mask:=(1 SHL (16-CounterBite));&#60;br&#62; &nbsp; BiteChain:=BiteChain XOR Mask;&#60;br&#62; &nbsp;end;&#60;br&#62; If (P^.P0=Nil) and (P^.P1=Nil) then&#60;br&#62; &nbsp;begin&#60;br&#62; &nbsp; P^.BiteChain:=BiteChain;&#60;br&#62; &nbsp; P^.LengthBiteChain:=CounterBite-1;&#60;br&#62; &nbsp;end;&#60;br&#62; Dec(CounterBite);&#60;br&#62;end;&#60;br&#62;procedure CreateCompressCode;&#60;br&#62;{ --- обнуление переменных и запуск просмотра дерева с вершины --- }&#60;br&#62;begin&#60;br&#62; BiteChain:=0;&#60;br&#62; CounterBite:=0;&#60;br&#62; Root^.Key:=False;&#60;br&#62; ViewTree(Root);&#60;br&#62;end;&#60;br&#62;procedure DeleteTree;&#60;br&#62;{ --- удаление дерева --- }&#60;br&#62;Var P : PCodElement;&#60;br&#62;begin&#60;br&#62; CurPoint:=LeftRange;&#60;br&#62; While CurPoint&#60;&#62;Nil do&#60;br&#62; &nbsp;begin&#60;br&#62; &nbsp; If (CurPoint^.P0&#60;&#62;Nil) and (CurPoint^.P1&#60;&#62;Nil) then&#60;br&#62; &nbsp; &nbsp;begin&#60;br&#62; &nbsp; &nbsp; If CurPoint^.NewLeft &#60;&#62; Nil then&#60;br&#62; &nbsp; &nbsp; &nbsp;CurPoint^.NewLeft^.NewRight:=CurPoint^.NewRight;&#60;br&#62; &nbsp; &nbsp; If CurPoint^.NewRight &#60;&#62; Nil then&#60;br&#62; &nbsp; &nbsp; &nbsp;CurPoint^.NewRight^.NewLeft:=CurPoint^.NewLeft;&#60;br&#62; &nbsp; &nbsp; If CurPoint=LeftRange then LeftRange:=CurPoint^.NewRight;&#60;br&#62; &nbsp; &nbsp; If CurPoint=RightRange then RightRange:=CurPoint^.NewLeft;&#60;br&#62; &nbsp; &nbsp; P:=CurPoint;&#60;br&#62; &nbsp; &nbsp; CurPoint:=P^.NewRight;&#60;br&#62; &nbsp; &nbsp; Dispose(P);&#60;br&#62; &nbsp; &nbsp;end&#60;br&#62;.&#60;br&#62; &nbsp; else CurPoint:=CurPoint^.NewRight;&#60;br&#62; &nbsp;end;&#60;br&#62;end;&#60;br&#62;procedure SaveBufHeader;&#60;br&#62;{ --- запись в буфер заголовка архива --- }&#60;br&#62;Type&#60;br&#62; &nbsp; &nbsp; &nbsp;ByteField = array[0..6] of byte;&#60;br&#62;Const&#60;br&#62; &nbsp; &nbsp; &nbsp;Header : ByteField = ( , , , {text}, {text}, {text}, {text} );&#60;br&#62;begin&#60;br&#62; If Create then&#60;br&#62; &nbsp;begin&#60;br&#62; &nbsp; Move(Header,OutBuf[0],7);&#60;br&#62; &nbsp; OutCounter:=7;&#60;br&#62; &nbsp;end&#60;br&#62; else&#60;br&#62; &nbsp;begin&#60;br&#62; &nbsp; Move(Header[3],OutBuf[0],4);&#60;br&#62; &nbsp; OutCounter:=4;&#60;br&#62; &nbsp;end;&#60;br&#62;end;&#60;br&#62;procedure SaveBufFATInfo;&#60;br&#62;{ --- запись в буфер всей информации по файлу --- }&#60;br&#62;Var I : byte;&#60;br&#62; &nbsp; &nbsp;St : PathStr;&#60;br&#62; &nbsp; &nbsp;R : SearchRec;&#60;br&#62;begin&#60;br&#62; St:=ParamStr(3);&#60;br&#62; For I:=0 to Length(St)+1 do&#60;br&#62; &nbsp;begin&#60;br&#62; &nbsp; OutBuf[OutCounter]:=byte(Ord(St[I]));&#60;br&#62; &nbsp; Inc(OutCounter);&#60;br&#62; &nbsp;end;&#60;br&#62; FindFirst(St,{text},R);&#60;br&#62; Dec(OutCounter);&#60;br&#62; Move(R.Time,OutBuf[OutCounter],4);&#60;br&#62; OutCounter:=OutCounter+4;&#60;br&#62; OutBuf[OutCounter]:=R.Attr;&#60;br&#62; Move(R.Size,OutBuf[OutCounter+1],4);&#60;br&#62; OutCounter:=OutCounter+5;&#60;br&#62;end;&#60;br&#62;procedure SaveBufCodeArray;&#60;br&#62;{ --- сохранить массив частот вхождений в архивном файле --- }&#60;br&#62;Var I : byte;&#60;br&#62;begin&#60;br&#62; For I:=0 to 255 do&#60;br&#62; &nbsp;begin&#60;br&#62; &nbsp; OutBuf[OutCounter]:=Hi(CodeTable[I]^.CounterEnter);&#60;br&#62; &nbsp; Inc(OutCounter);&#60;br&#62; &nbsp; OutBuf[OutCounter]:=Lo(CodeTable[I]^.CounterEnter);&#60;br&#62; &nbsp; Inc(OutCounter);&#60;br&#62; &nbsp;end;&#60;br&#62;end;&#60;br&#62;.&#60;br&#62;procedure CreateCodeArchiv;&#60;br&#62;{ --- создание кода сжатия --- }&#60;br&#62;begin&#60;br&#62; InitCodeTable; &nbsp; &nbsp; &nbsp;{ инициализация кодовой таблицы &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&#60;br&#62; CounterNumberEnter; { подсчет числа вхождений байт в блок &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&#60;br&#62; SortQueueByte; &nbsp; &nbsp; &nbsp;{ cортировка по возрастанию числа вхождений &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&#60;br&#62; SaveBufHeader; &nbsp; &nbsp; &nbsp;{ сохранить заголовок архива в буфере &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&#60;br&#62; SaveBufFATInfo; &nbsp; &nbsp; { сохраняется FAT информация по файлу &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&#60;br&#62; SaveBufCodeArray; &nbsp; { сохранить массив частот вхождений в архивном файле }&#60;br&#62; CreateTree; &nbsp; &nbsp; &nbsp; &nbsp; { создание дерева частот &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&#60;br&#62; CreateCompressCode; { cоздание кода сжатия &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&#60;br&#62; DeleteTree; &nbsp; &nbsp; &nbsp; &nbsp; { удаление дерева частот &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&#60;br&#62;end;&#60;br&#62;procedure PakOneByte;&#60;br&#62;{ --- сжатие и пересылка в выходной буфер одного байта --- }&#60;br&#62;Var Mask : word;&#60;br&#62; &nbsp; &nbsp;Tail : boolean;&#60;br&#62;begin&#60;br&#62; CRC:=CRC XOR InBuf[InCounter];&#60;br&#62; Mask:=CodeTable[InBuf[InCounter]]^.BiteChain SHR CounterBite;&#60;br&#62; OutWord:=OutWord OR Mask;&#60;br&#62; CounterBite:=CounterBite+CodeTable[InBuf[InCounter]]^.LengthBiteChain;&#60;br&#62; If CounterBite&#62;15 then Tail:=True else Tail:=False;&#60;br&#62; While CounterBite&#62;7 do&#60;br&#62; &nbsp;begin&#60;br&#62; &nbsp; OutBuf[OutCounter]:=Hi(OutWord);&#60;br&#62; &nbsp; Inc(OutCounter);&#60;br&#62; &nbsp; If OutCounter=(SizeOf(OutBuf)-4) then&#60;br&#62; &nbsp; &nbsp;begin&#60;br&#62; &nbsp; &nbsp; BlockWrite(OutputF,OutBuf,OutCounter,NumWritten);&#60;br&#62; &nbsp; &nbsp; OutCounter:=0;&#60;br&#62; &nbsp; &nbsp;end;&#60;br&#62; &nbsp; CounterBite:=CounterBite-8;&#60;br&#62; &nbsp; If CounterBite&#60;&#62;0 then OutWord:=OutWord SHL 8 else OutWord:=0;&#60;br&#62; &nbsp;end;&#60;br&#62; If Tail then&#60;br&#62; &nbsp;begin&#60;br&#62; &nbsp; Mask:=CodeTable[InBuf[InCounter]]^.BiteChain SHL&#60;br&#62; &nbsp; (CodeTable[InBuf[InCounter]]^.LengthBiteChain-CounterBite);&#60;br&#62; &nbsp; OutWord:=OutWord OR Mask;&#60;br&#62; &nbsp;end;&#60;br&#62; Inc(InCounter);&#60;br&#62; If (InCounter=(SizeOf(InBuf))) or (InCounter=NumRead) then&#60;br&#62; &nbsp;begin&#60;br&#62; &nbsp; InCounter:=0;&#60;br&#62; &nbsp; BlockRead(InputF,InBuf,SizeOf(InBuf),NumRead);&#60;br&#62; &nbsp;end;&#60;br&#62;end;&#60;br&#62;procedure PakFile;&#60;br&#62;{ --- процедура непосредственного сжатия файла --- }&#60;br&#62;begin&#60;br&#62; ResetFile;&#60;br&#62; SearchNameInArchiv;&#60;br&#62; If NormalWork then&#60;br&#62; &nbsp;begin&#60;br&#62; &nbsp; BlockRead(InputF,InBuf,SizeOf(InBuf),NumRead);&#60;br&#62; &nbsp; OutWord:=0;&#60;br&#62;.&#60;br&#62; &nbsp; CounterBite:=0;&#60;br&#62; &nbsp; OutCounter:=0;&#60;br&#62; &nbsp; InCounter:=0;&#60;br&#62; &nbsp; CRC:=0;&#60;br&#62; &nbsp; CreateCodeArchiv;&#60;br&#62; &nbsp; While (NumRead&#60;&#62;0) do PakOneByte;&#60;br&#62; &nbsp; OutBuf[OutCounter]:=Hi(OutWord);&#60;br&#62; &nbsp; Inc(OutCounter);&#60;br&#62; &nbsp; OutBuf[OutCounter]:=CRC;&#60;br&#62; &nbsp; Inc(OutCounter);&#60;br&#62; &nbsp; BlockWrite(OutputF,OutBuf,OutCounter,NumWritten);&#60;br&#62; &nbsp; DisposeCodeTable;&#60;br&#62; &nbsp; ClosePakFile;&#60;br&#62; &nbsp;end;&#60;br&#62;end;&#60;br&#62;procedure ResetUnPakFiles;&#60;br&#62;{ --- открытие файла для распаковки --- }&#60;br&#62;begin&#60;br&#62; InCounter:=7;&#60;br&#62; St:=&#39;&#39;;&#60;br&#62; repeat&#60;br&#62; &nbsp;St[InCounter-7]:=Chr(InBuf[InCounter]);&#60;br&#62; &nbsp;Inc(InCounter);&#60;br&#62; until InCounter=InBuf[7]+8;&#60;br&#62; Assign(InterF,St);&#60;br&#62; Rewrite(InterF,1);&#60;br&#62; ErrorByte:=IOResult;&#60;br&#62; ErrorMessage;&#60;br&#62; If NormalWork then&#60;br&#62; &nbsp;begin&#60;br&#62; &nbsp; WriteLn(&#39;UnPak file : &#39;,St,&#39;...&#39;);&#60;br&#62; &nbsp; Move(InBuf[InCounter],TimeUnPakFile,4);&#60;br&#62; &nbsp; InCounter:=InCounter+4;&#60;br&#62; &nbsp; AttrUnPakFile:=InBuf[InCounter];&#60;br&#62; &nbsp; Inc(InCounter);&#60;br&#62; &nbsp; Move(InBuf[InCounter],LengthArcFile,4);&#60;br&#62; &nbsp; InCounter:=InCounter+4;&#60;br&#62; &nbsp;end;&#60;br&#62;end;&#60;br&#62;procedure CloseUnPakFile;&#60;br&#62;{ --- закрытие файла для распаковки --- }&#60;br&#62;begin&#60;br&#62; If not NormalWork then Erase(InterF)&#60;br&#62; &nbsp;else&#60;br&#62; &nbsp; begin&#60;br&#62; &nbsp; &nbsp;SetFAttr(InterF,AttrUnPakFile);&#60;br&#62; &nbsp; &nbsp;SetFTime(InterF,TimeUnPakFile);&#60;br&#62; &nbsp; end;&#60;br&#62; Close(InterF);&#60;br&#62;end;&#60;br&#62;procedure RestoryCodeTable;&#60;br&#62;{ --- воссоздание кодовой таблицы по архивному файлу --- }&#60;br&#62;Var I : byte;&#60;br&#62;begin&#60;br&#62; InitCodeTable;&#60;br&#62; For I:=0 to 255 do&#60;br&#62;.&#60;br&#62; &nbsp;begin&#60;br&#62; &nbsp; CodeTable[I]^.CounterEnter:=InBuf[InCounter];&#60;br&#62; &nbsp; CodeTable[I]^.CounterEnter:=CodeTable[I]^.CounterEnter SHL 8;&#60;br&#62; &nbsp; Inc(InCounter);&#60;br&#62; &nbsp; CodeTable[I]^.CounterEnter:=CodeTable[I]^.CounterEnter+InBuf[InCounter];&#60;br&#62; &nbsp; Inc(InCounter);&#60;br&#62; &nbsp;end;&#60;br&#62;end;&#60;br&#62;procedure UnPakByte( P : PCodElement );&#60;br&#62;{ --- распаковка одного байта --- }&#60;br&#62;Var Mask : word;&#60;br&#62;begin&#60;br&#62; If (P^.P0=Nil) and (P^.P1=Nil) then&#60;br&#62; &nbsp;begin&#60;br&#62; &nbsp; OutBuf[OutCounter]:=P^.Index;&#60;br&#62; &nbsp; Inc(OutCounter);&#60;br&#62; &nbsp; Inc(LengthOutFile);&#60;br&#62; &nbsp; If OutCounter = (SizeOf(OutBuf)-1) then&#60;br&#62; &nbsp; &nbsp;begin&#60;br&#62; &nbsp; &nbsp; BlockWrite(InterF,OutBuf,OutCounter,NumWritten);&#60;br&#62; &nbsp; &nbsp; OutCounter:=0;&#60;br&#62; &nbsp; &nbsp;end;&#60;br&#62; &nbsp;end&#60;br&#62; else&#60;br&#62; &nbsp;begin&#60;br&#62; &nbsp; Inc(CounterBite);&#60;br&#62; &nbsp; If CounterBite=9 then&#60;br&#62; &nbsp; &nbsp;begin&#60;br&#62; &nbsp; &nbsp; Inc(InCounter);&#60;br&#62; &nbsp; &nbsp; If InCounter = (SizeOf(InBuf)) then&#60;br&#62; &nbsp; &nbsp; &nbsp;begin&#60;br&#62; &nbsp; &nbsp; &nbsp; InCounter:=0;&#60;br&#62; &nbsp; &nbsp; &nbsp; BlockRead(OutputF,InBuf,SizeOf(InBuf),NumRead);&#60;br&#62; &nbsp; &nbsp; &nbsp;end;&#60;br&#62; &nbsp; &nbsp; CounterBite:=1;&#60;br&#62; &nbsp; &nbsp;end;&#60;br&#62; &nbsp; Mask:=InBuf[InCounter];&#60;br&#62; &nbsp; Mask:=Mask SHL (CounterBite-1);&#60;br&#62; &nbsp; Mask:=Mask OR $FF7F; { установка всех битов кроме старшего }&#60;br&#62; &nbsp; If Mask=$FFFF then UnPakByte(P^.P1)&#60;br&#62; &nbsp; &nbsp;else UnPakByte(P^.P0);&#60;br&#62; &nbsp;end;&#60;br&#62;end;&#60;br&#62;procedure UnPakFile;&#60;br&#62;{ --- распаковка одного файла --- }&#60;br&#62;begin&#60;br&#62; BlockRead(OutputF,InBuf,SizeOf(InBuf),NumRead);&#60;br&#62; ErrorByte:=IOResult;&#60;br&#62; ErrorMessage;&#60;br&#62; If NormalWork then ResetUnPakFiles;&#60;br&#62; If NormalWork then&#60;br&#62; &nbsp;begin&#60;br&#62; &nbsp; RestoryCodeTable;&#60;br&#62; &nbsp; SortQueueByte;&#60;br&#62; &nbsp; CreateTree; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { создание дерева частот }&#60;br&#62; &nbsp; CreateCompressCode;&#60;br&#62; &nbsp; CounterBite:=0;&#60;br&#62;.&#60;br&#62; &nbsp; OutCounter:=0;&#60;br&#62; &nbsp; LengthOutFile:=0;&#60;br&#62; &nbsp; While LengthOutFile&#60;LengthArcFile do&#60;br&#62; &nbsp; &nbsp;UnPakByte(Root);&#60;br&#62; &nbsp; BlockWrite(InterF,OutBuf,OutCounter,NumWritten);&#60;br&#62; &nbsp; DeleteTree;&#60;br&#62; &nbsp; DisposeCodeTable;&#60;br&#62; &nbsp;end;&#60;br&#62; CloseUnPakFile;&#60;br&#62;end;&#60;br&#62;{ ------------------------- main text ------------------------- }&#60;br&#62;begin&#60;br&#62; DeleteFile:=False;&#60;br&#62; NormalWork:=True;&#60;br&#62; ErrorByte:=0;&#60;br&#62; WriteLn;&#60;br&#62; WriteLn(&#39;ArcHaf version 1.0 &nbsp;(c) Copyright VVS Soft Group, 1992.&#39;);&#60;br&#62; ResetArchiv;&#60;br&#62; If NormalWork then&#60;br&#62; &nbsp;begin&#60;br&#62; &nbsp; St:=ParamStr(1);&#60;br&#62; &nbsp; Case St[1] of&#60;br&#62; &nbsp; &nbsp;&#39;a&#39;,&#39;A&#39; : PakFile;&#60;br&#62; &nbsp; &nbsp;&#39;m&#39;,&#39;M&#39; : begin&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DeleteFile:=True;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PakFile;&#60;br&#62; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;&#60;br&#62; &nbsp; &nbsp;&#39;e&#39;,&#39;E&#39; : UnPakFile;&#60;br&#62; &nbsp; &nbsp;else ;&#60;br&#62; &nbsp; end;&#60;br&#62; &nbsp;end;&#60;br&#62; CloseArchiv;&#60;br&#62;end.&#60;br&#62;</div></ol></div></div></div></div><script>preloadCodeButtons('1');</script>]]></description>
        <author>GrAnd</author>
        <category>Алгоритмы</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=9057&amp;view=findpost&amp;p=89094</guid>
        <pubDate>Sat, 01 Nov 2003 10:59:43 +0000</pubDate>
        <title>Алгоритм и код Хаффмана. $$$!</title>
        <link>https://forum.sources.ru/index.php?showtopic=9057&amp;view=findpost&amp;p=89094</link>
        <description><![CDATA[shved: Я вам очень благодарен за совет, и я понимаю что надо учицца, учицца и учицца,<br>но в данном случае мне этот вариант не подходит, т.к. еслия не сдам эту и еще кучу программ, учиться будет негде...<br>Поэтому, если кто может сделать, скажите ВАШУ цену.<br>Пожалуйста!]]></description>
        <author>shved</author>
        <category>Алгоритмы</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=9057&amp;view=findpost&amp;p=89093</guid>
        <pubDate>Sat, 01 Nov 2003 09:13:23 +0000</pubDate>
        <title>Алгоритм и код Хаффмана. $$$!</title>
        <link>https://forum.sources.ru/index.php?showtopic=9057&amp;view=findpost&amp;p=89093</link>
        <description><![CDATA[Tishaishii: http://www.yandex.ru/yandsearch?rpt=rad&amp;text=\%C0\%EB\%E3\%EE\%F0\%E8\%F2\%EC+\%E8+\%EA\%EE\%E4+\%D5\%E0\%F4\%F4\%EC\%E0\%ED\%E0<br>Вот описание с алгоритмом:<br>http://mindspring.narod.ru/alg/huffman.html]]></description>
        <author>Tishaishii</author>
        <category>Алгоритмы</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=9057&amp;view=findpost&amp;p=89091</guid>
        <pubDate>Sat, 01 Nov 2003 08:24:39 +0000</pubDate>
        <title>Алгоритм и код Хаффмана. $$$!</title>
        <link>https://forum.sources.ru/index.php?showtopic=9057&amp;view=findpost&amp;p=89091</link>
        <description><![CDATA[GrAnd: <div class='tag-quote'><span class='tag-quote-prefix'>Цитата</span> <span class='tag-quote__quote-info'>shved, 31.10.03, 23:17:59</span><div class='quote '>И какова ВАША цена?<br></div></div><br>НАША цена научить и помочь самому разбираться в поставленных задачах и находить решения  ;D 8-) ;)]]></description>
        <author>GrAnd</author>
        <category>Алгоритмы</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=9057&amp;view=findpost&amp;p=89090</guid>
        <pubDate>Fri, 31 Oct 2003 20:36:01 +0000</pubDate>
        <title>Алгоритм и код Хаффмана. $$$!</title>
        <link>https://forum.sources.ru/index.php?showtopic=9057&amp;view=findpost&amp;p=89090</link>
        <description><![CDATA[Visitor: Здесь, в основном, математики сидят. У них программы чисто умозрительные. Для абстрактных, возможно не существующих в реальном мире контуперов.<br><br>Так что, <a class='tag-url' href='http://algolist.manual.ru/compress/standard/huffman.php' target='_blank'>внимательнее читай ФАК</a> :)]]></description>
        <author>Visitor</author>
        <category>Алгоритмы</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=9057&amp;view=findpost&amp;p=89089</guid>
        <pubDate>Fri, 31 Oct 2003 20:17:59 +0000</pubDate>
        <title>Алгоритм и код Хаффмана. $$$!</title>
        <link>https://forum.sources.ru/index.php?showtopic=9057&amp;view=findpost&amp;p=89089</link>
        <description><![CDATA[shved: Нужна программа!<br>Текст задания:<br>&quot;Используя построения дерева Хаффмана получить код Хаффмана для произвольного текстового файла и при помощи кода сжать файл, а затем разархифировать. Оценить время работы&quot;<br>Может кто-нибудь, разбирающийся в этом, взяться?<br>И какова ВАША цена?<br>shved@msx.ru]]></description>
        <author>shved</author>
        <category>Алгоритмы</category>
      </item>
	
      </channel>
      </rss>
	