我需要:
到目前为止,我相信我已正确完成了第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.
答案 0 :(得分:0)
我不会为你做你的课程(来自Swinburne Uni?),但这里有几点。
您需要在Cars数组之前声明您的Car记录。
type //Cars = Array of Car;
Car = record
ID : integer;
Manufacturer : string;
Model : string;
Registration : integer;
end;
Cars = Array of Car;
在ReadCar中,您的Prompt变量是未声明的(并且是mispelt)。它应该是
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的代码格式化程序并没有用这个答案中的代码做正常的事情,我稍后会尝试整理它。