调用其他函数和过程的函数和过程

Functions and procedures that call other functions and procedures

我需要:

  1. 编写一个名为 ReadCar() 的函数:Car;从 Car 记录中每个字段的终端值和 returns 完整记录中读取。
  2. 编写一个名为 WriteCar(c: Car) 的过程;它获取汽车记录并将每个字段写入终端,其中包含字段描述和字段值。
  3. 编写一个名为 ReadAllCars(count: Integer) 的函数:Cars;调用您的 ReadCar() 函数计数次数并将每辆汽车存储在 Cars 中。
  4. 编写一个名为 WriteAllCars(carArray: Cars) 的过程;为 carArray 中的每辆车调用 WriteCar() 过程。

到目前为止,我认为我已正确完成第 1 步和第 2 步,但我不确定如何执行第 3 步和第 4 步。我应该如何开始这些步骤?到这个程序结束时,我应该能够输入 3 辆车的数据并正确打印数据。

program carDetails;
uses TerminalUserInput;

type Cars = Array of Car;
Car = record
    ID : integer;
    Manufacturer : string;
    Model : string;
    Registration : integer;
end;

function ReadCar(): Car;
begin
    WriteLn(promt);
    ReadCar.ID := readInteger('Please enter the Car ID ');
    ReadCar.Manufacturer := readString('Please enter the manufacturer of car '+ ReadCar.ID);
    ReadCar.Model := readString('Please enter the model of car '+ ReadCar.ID);
    ReadCar.Registration := readInteger('Please enter the registration number for car '+ ReadCar.ID);
end;

procedure WriteCar(c: Car);
begin
    WriteLn('ID - ', c.ID);
    WriteLn('Manufacturer - ', c.Manufacturer);
    WriteLn('Model - ', c.Model);
    WriteLn('Registration - ', c.Registration);
end;

function ReadAllCars(count: integer): Cars;
begin

end;

procedure WriteAllCars(carArray: Cars);
begin 

end;

procedure Main();
    var cars: Array of Car;
        index: Integer;
begin
cars := ReadAllCars(3);
WriteAllCars(cars);
end;

begin
    Main();
end.

我不会为你做你的课程(来自斯威本大学?),但这里有几点。

您需要在 Cars 数组之前声明您的 Car 记录。

  type //Cars = Array of Car;
  Car = record
      ID : integer;
      Manufacturer : string;
      Model : string;
      Registration : integer;
  end;

Cars = Array of Car;

在 ReadCar 中,您的 Prompt 变量未声明(且拼写错误)。应该是

function ReadCar(const Prompt : String): Car;
begin
//    WriteLn(promt);
    WriteLn(Prompt);

另外,在 ReadCar 中,您需要先将 Car.ID 转换为字符串,然后才能在对 readString 的调用中使用它,如下所示:

ReadCar.Manufacturer := readString('Please enter the manufacturer of car ' + IntToStr(ReadCar.ID));
ReadCar.Model := readString('Please enter the model of car ' + IntToStr(ReadCar.ID));
ReadCar.Registration := readInteger('Please enter the registration number for car ' + IntToStr(ReadCar.ID));

在ReadCars中,需要设置其返回的数组长度:

function ReadAllCars(count: integer): Cars;
begin
  SetLength(Result, Count);
end;

说完这些,writeCars其实很简单。你只需要

procedure WriteAllCars(carArray: Cars);
var
    i : Integer;
begin
   for i:= Low(carArray) to High(carArray) do
     WriteCar(carArray[i]);
end;

使用 Low() 和 High() 函数回避了必须知道数组的下限和上限的问题,该数组以您的 Cars 的方式声明(即记录数组)。实际上它们是从零开始的,而不是从1开始的。

出于某种原因,SO 的代码格式化程序没有使用此答案中的代码执行其正常操作,稍后我会尝试整理它。