将算法从pascal转换为c#

时间:2017-04-03 08:32:12

标签: c# pascal turbo-pascal

我正在尝试将这个东西(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();
        }

    }
}

但是,不幸的是我的代码无休止地循环,我得到那样的smth: enter image description here 这个算法在pascal中运行良好,所以我认为我已经错误地翻译了它。请帮我理解我的错误在哪里。提前谢谢。

2 个答案:

答案 0 :(得分: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,所以加上m ++;在i ++之后;

答案 1 :(得分:2)

在pascal代码中,你错过了终止&#34;我没有别的事可做&#34;部分

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;
        }