Pascal 把一个函数变成一个过程

Pascal Change a function into a procedure

我正在尝试将以下程序中的函数更改为过程。该程序应该读取 5 个不同的整数,然后将它们从小到大排序。带有函数的版本工作正常,但是带有过程的版本不对数字进行排序。它只打印我输入的数字。例如,当我在控制台中输入 4, 5, 7, 3, 1 时,它会打印 4, 5, 7, 3, 1 而不是所需的 1, 3, 4, 5、7。

所以问题是:如何让过程版本以与函数版本相同的方式工作?

我很确定这里有些东西我不明白,但我似乎找不到...非常感谢任何帮助!

这是使用函数的版本:

 program FeldSortFunction(input, output);   { sorts a field of integers}

  FELDGROESSE = 5;

type   tIndex = 1..FELDGROESSE;   tFeld = array [tIndex] of integer;

var   EingabeFeld : tFeld;   MinPos,
    i : tIndex;   Tausch : integer;

function FeldMinimumPos (Feld : tFeld; von, bis:tIndex): tIndex; { finds the Position of the minimum inside the field between von and bis, 1 <= von <= bis <= FELDGROESSE }

   var    MinimumPos,    j:tIndex;

   begin
    MinimumPos := von;
    for j:= von + 1 to bis do
      if Feld[j] < Feld[MinimumPos] then
        MinimumPos := j;
    FeldMinimumPos := MinimumPos
    end; { FeldMinimumPos }

begin   { Read the field }   writeln ('Geben Sie ', FELDGROESSE, ' Werte ein:');   for i := 1 to FELDGROESSE do
    readln (EingabeFeld[i]);

  { sort the integers }

  for i := 1 to FELDGROESSE - 1 do   begin
    MinPos := FeldMinimumPos (EingabeFeld, i, FELDGROESSE);
    {The minimum has been found, now we need to exchange this value with       the element on position i}
    Tausch := EingabeFeld[MinPos];
    EingabeFeld[MinPos] := EingabeFeld[i];
    Eingabefeld[i] := Tausch;   end;

  { print the sorted field }   for i := 1 to FELDGROESSE do
      write (EingabeFeld[i]:6);   writeln;

  readln; end.     { FeldSort }

这是使用程序的版本:

program FeldSortProcedure(input, output);
{ sorts a field of integers and defines the value of the minimum}


const
  FELDGROESSE = 5;

type
  tIndex = 1..FELDGROESSE;
  tFeld = array [tIndex] of integer;

var
  SortierFeld : tFeld;
  MinPos,MinimumPos,
    i : tIndex;
  MinimumWert : integer;

procedure FeldMinimumPosUndWert (Feld : tFeld; von, bis:tIndex; MinPos:tIndex; MinWert : integer);
{ finds  Position and value of the Minimums inside Feld between von and bis }

   var
   ind:tIndex;

begin
    MinPos := von;
    MinWert := Feld[von];
    for ind := von + 1 to bis do
    begin
      if Feld[ind] < Feld[MinPos] then
      begin
        MinPos := ind;
        MinWert := Feld[ind]
      end;
    end;

end; { FeldMinPosUndWert }

begin
  { Read the field }
  writeln ('Please key in ', FELDGROESSE, ' numbers:');
  for i := 1 to FELDGROESSE do
    readln (SortierFeld[i]);

  { sort the field }
  FeldMinimumPosUndWert (SortierFeld, i, FELDGROESSE, MinimumPos, MinimumWert);

  { prints the sorted field }
  for i := 1 to FELDGROESSE do
      write (SortierFeld[i]:6);
  writeln;

  readln;
end.     { FeldSort }

显然你没有得到任何结果,所以我会给出一个提示:

开启你的功能

function FeldMinimumPos (Feld : tFeld; von, bis:tIndex): tIndex;

进入程序:

procedure FeldMinimumPos(feld: TFeld; von, bis: TIndex; var pos: TIndex);
{ finds the position of the minimum inside the field between von and bis,
  1 <= von <= bis <= FELDGROESSE }
var
  j: TIndex;
begin
  pos := von;
  for j := von + 1 to bis do
    if feld[j] < feld[pos] then
      pos := j;
end; { FeldMinimumPos }

并调用它:

FeldMimimumPos(EingabeFeld, i, FELDGROESSE, MinPos);

你应该没事。


但有几点说明:

  • 您的排序方式不够理想,即使对于只有 5 个元素的字段也是如此。了解选择排序甚至冒泡排序(只有 google 它们)。
  • 正确格式化您的代码。很难读。将注释放在自己的行上或 代码之后。将每个 end 放在自己的行上并正确对齐,不要将其放在上一个命令之后。对齐您的变量声明,并在它们自己的行上开始循环。等等...
  • 您在 FELDGROESSE 声明上方忘记了 const。您 posted 的代码无法编译。始终 post 实际代码,从您的编辑器中复制并粘贴(逐字)到您的问题(或答案)的编辑框中。不要 post 从记忆中编写代码,也不要通过在屏幕或 sheet 纸上输入代码。