Delphi 7的Shannon-Fano算法示例?

时间:2014-05-02 22:01:34

标签: algorithm delphi delphi-7

您好我想在Delphi中使用Shannon-Fano算法。我的表格为#34; ABCDE"像这样: 这种方法。

enter image description here

C : 11
B : 10
A : 01
D : 001
E : 000

所以我的字符串看起来像011011001000,但我无法回来。我怎样才能回到我的第一个原始字符串?是否有Delphi-Pascal的示例代码?

如果我找到任何例子,我会明白我需要做什么。但我找不到任何例子。

谢谢..

我的代码:

button1.onclick.etc..
var
    s,z:string;
    i:integer;
begin
    s:='ABCDE';
    for i:=1 to Length(s) do
        if s[i]='A' then
            z:=z+'01' 
        else if s[i]='B' then
            z:=z+'10' 
        else if s[i]='C' then
            z:=z+'11' 
        else if s[i]='D' then
            z:=z+'001' 
        else if s[i]='E' then
            z:=z+'000';
    end;
    Showmessage(z);
end;
end.

最后z等于011011001000。但是我如何解决这最后一个字符串?

1 个答案:

答案 0 :(得分:2)

LU RD 一样,tomes of delphi表示此算法 你必须购买这本书,这里有一个链接:http://www.lulu.com/shop/julian-bucknall/the-tomes-of-delphi-algorithms-and-data-structures/paperback/product-488272.html;jsessionid=5CCED10CCFDCB82897E853208BA6460A

然而,本书附带的源代码是免费提供的;你可以在这里下载源代码:http://www.boyet.com/Code/ToDADS_source.zip

Shannon-Fano没有实现,但密切相关的霍夫曼代码见:TDHuffmn.pas。

以下是C中Shannon-Fano的实现: http://cppgm.blogspot.com/2008/01/shano-fano-code.html

您可以简单地将代码从C转换为Delphi。

从:

开始
{$APPTYPE CONSOLE}

{$R *.res}

uses SysUtils;

type
  node = record
    sym: array[0..9] of char
    pro: real;
    arr: array[0..19] of integer;
    top: integer;
  end;

var
  s: array[0..19] of node;

procedure prints(l,h: integer; s: array of node);
var
  i: integer;
  output: string;
begin
  for i:= l to h do begin
    output:= format('\n%s\t%f',s[i].sym,s[i].pro);
    writeln(output);
  end; {for i}
end;

procedure shannon(l,h: integer; s: array of node);
var
  pack1,pack2, diff1, diff2: real;
  i,d,k,j: integer;
begin
 pack1=0; pack2=0; diff1=0; diff2=0;

if (((l+1)=h) or (l=h) or (l>h)) then begin
  if ((l=h) or (l>h)) then 

并从那里继续。
这是一个非常简单的翻译。