编译代码时出现此错误:" cars2.pp(3,8)致命:语法错误," ="预期,但":"发现"
这是我的代码:
program vehInfo;
type
wheels: array [1 .. 6] of integer;
purchaseYear: array [1919 .. 2051] of integer;
style = (sports, SUV, minivan, motorcycle, sedan, exotic);
pwrSrc = (electric, hybrid, gas, diesel);
vehicle = record
wheel : wheels;
buyDate : purchaseYear;
styles : style;
source : pwrSrc;
end;
var
myVehicle: vehicle;
listOfCars: file of vehicle;
begin
assign(listOfCars, 'hwkcarsfile.txt');
reset(listOfCars);
read(listOfCars, myVehicle);
writeln('wheel type: ' , myVehicle.wheel);
writeln('year purchased: ' , myVehicle.buyDate);
writeln('style: ' , myVehicle.styles);
writeln('power source: ' , myVehicle.source)
close(listOfCars);
end.
我是Pascal的新手,任何帮助都将不胜感激,谢谢。
答案 0 :(得分:1)
非常简单:type
使用=
,而变量声明使用:
。
所以:
type
wheels = 1..6; // not an array, but a subrange type!
purchaseYear = 1919..2051; // not an array, but a subrange type!
style = (sports, SUV, minivan, motorcycle, sedan, exotic);
pwrSrc = (electric, hybrid, gas, diesel);
vehicle = record
wheel: wheels; { a field of a record is a variable }
buyDate: purchaseYear;
styles: style;
source: pwrSrc;
end;
...
var
myVehicle: vehicle;
listOfCars: file of vehicle;
子范围类型是序数类型(在这种情况下,两者都是整数),但在给定范围内。超出范围的任何值都是非法的。您不希望拥有数字数组,只需要购买车辆的车轮数量和年份(也是数字)。你不需要133个不同的日期,对吗?