Question

Hello I want to use Shannon-Fano algorithm in Delphi. My tables for string that "ABCDE" like this: This method.

enter image description here

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

So my string will look like 011011001000 but I couldn't come back. How can I return to my first original string? Is there any example code for Delphi-Pascal?

If I find any examples, I will understand what I need to do. But I couldn't find any examples.

Thank you..

My codes:

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.

Finally z equal to 011011001000. But how can I resolve this last string?

Was it helpful?

Solution

Like LU RD said the tomes of delphi covers this algorithm.
You'll have to buy the book, here's a link: http://www.lulu.com/shop/julian-bucknall/the-tomes-of-delphi-algorithms-and-data-structures/paperback/product-488272.html;jsessionid=5CCED10CCFDCB82897E853208BA6460A

However the sourcecode that accompanies the book is freely available; you can download the sourcecode here: http://www.boyet.com/Code/ToDADS_source.zip

Shannon-Fano is not implemented, but the closely related huffman code is see: TDHuffmn.pas.

Here's an implementation of Shannon-Fano in C: http://cppgm.blogspot.com/2008/01/shano-fano-code.html

You simple translate the code from C into Delphi.

Starting with:

{$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 

and continuing from there.
It's a pretty straightforward translation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top