将算法从 Pascal 转换为 C#

Convert algorithm from pascal to c#

我正在尝试将这个东西(Shannon-Fano 算法)从 Pascal 转换为 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();
        }

    }
}

但是,不幸的是我的代码无休止地循环,我得到了这样的消息: 该算法在 Pascal 中运行良好,所以我认为我翻译不正确。请帮助我了解我的错误在哪里。提前致谢。

除了 BugFinder 的回答。

Pascal 有这个:

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,所以加m++;在 i++ 之后;

在 pascal 代码中你错过了终止的 "I have nothing else to do" 部分

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

您在 C# 代码中跳过了 exit ..,所以它一直在循环

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

你需要在后面加一个return Console.WriteLine 例如

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