2017-04-03 69 views
0

我想这件事情(香农 - 法诺算法)从帕转换为C#:转换算法,C#

program ShennonFano; 
uses crt; 
const 
    a :array[1..6] of char = ('a','b','c','d','e','f'); 
    af:array[1..6] of integer = (10, 8, 6, 5, 4, 3); 


procedure SearchTree(branch:char; full_branch:string; start_pos:integer; end_pos:integer); 
var 
    dS:real; 
    i, m, S:integer; 
    c_branch:string; 
begin 

    if (a<>' ') then c_branch := full_branch + branch 
    else c_branch := ''; 


    if (start_pos = end_pos) then 
    begin 
    WriteLn(a[start_pos], ' = ', c_branch); 
    exit; 
    end; 


    dS := 0; 
    for i:=start_pos to end_pos do dS:= dS + af[i]; 
    dS := dS/2; 


    S := 0; 
    i := start_pos; 
    m := i; 
    while ((S+af[i]<dS) and (i<end_pos)) do 
    begin 
    S := S + af[i]; 
    inc(i); inc(m); 
    end; 


    SearchTree('1', c_branch, start_pos, m); 

    SearchTree('0', c_branch, m+1, end_pos); 

end; 

begin 
    WriteLn('Press <enter> to show'); 
    ReadLn; 
    ClrScr; 

    SearchTree(' ',' ', 1, 6); 
    ReadLn; 
end; 

这里是我的C#代码:

namespace Shannon_Fano_Alg 
{ 
    class Program 
    { 
     static char[] a = { 'a', 'b', 'c', 'd', 'e', 'f' }; 
     static int[] af = { 10, 8, 6, 5, 4, 3 }; 
     static bool first = true; 

     static void Search(char branch, string full_branch, int startPos, int endPos) 
     { 
      double dS; 
      int m, i, S; 
      string c_branch; 
      if (first) 
      { 
       c_branch = ""; 
       first = false; 
      } 
      else 
      { 
       c_branch = full_branch + branch; 
      } 

      if (startPos == endPos) 
      { 
       Console.WriteLine(a[startPos] + " = " + c_branch); 
      } 

      dS = 0; 
      for (i = startPos; i<endPos; i++) 
      { 
       dS += af[i]; 
      } 

      dS /= 2; 


      S = 0; 
      i = startPos; 
      m = i; 
      while (S + af[i] < dS && i<endPos) 
      { 
       S += af[i]; 
       i++; 
      } 

      Search('1', c_branch, startPos, m); 
      Search('0', c_branch, m + 1, endPos); 

     } 

     static void Main(string[] args) 
     { 
      Program.Search(' ', " ", 0, 5); 
      Console.ReadKey(); 
     } 

    } 
} 

但,不幸的是我的代码无休止地循环播放,而且我得到了如下的代码: enter image description here 这个算法在pascal中工作正常,所以我认为我把它翻译得不正确。请帮助我了解我的错误在哪里。提前致谢。

+2

你错过了真正重要的退出后返回...线的时候“ WriteLn(a [start_pos],'=',c_branch); 退出;“ – BugFinder

+0

@BugFinder,谢谢) – Burning

回答

2

除了BugFinder的回答。

帕斯卡尔有这样的:

while ((S+af[i]<dS) and (i<end_pos)) do 
begin 
    S := S + af[i]; 
    inc(i); inc(m); 
end; 

你的C#有这样的:

while (S + af[i] < dS && i<endPos) 
{ 
    S += af[i]; 
    i++; 
} 

你不增加男,所以加入M ++;在i ++之后;

+0

我不注意,非常感谢,伙计。 – Burning

2

在你错过了终止PASCAL代码“我没有什么别的事情可做”部分

if (start_pos = end_pos) then 
    begin 
    WriteLn(a[start_pos], ' = ', c_branch); 
    exit; 
    end; 

你跳过了退出。在你的C#代码,因此它只是不断循环

if (startPos == endPos) 
    { 
     Console.WriteLine(a[startPos] + " = " + c_branch); 
    } 

您需要添加Console.WriteLine如

 if (startPos == endPos) 
     { 
      Console.WriteLine(a[startPos] + " = " + c_branch); 
      return; 
     }