我能够创建一个名为speeding.dat的文件来记录警察速度摄像头注册的罚款信息。有日期,车辆号码,速度和罚款等信息。但是我不知道如何通过提示用户输入车辆登记号并显示该记录中的所有数据来开发可以查询文件的子程序。
这是我到目前为止所拥有的......
unit Main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm4 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
procedure FormCreate(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form4: TForm4;
type
Tfine = Record
name : string[15];
money : String[15];
date : String[15];
speed : String[15];
registration : string[15];
end;
implementation
{$R *.dfm}
procedure TForm4.FormCreate(Sender: TObject);
var
myFile : File of Tfine;
policeCamera : Tfine;
begin
AssignFile(myFile, 'speeding.dat');
ReWrite(myFile);
policeCamera.name := 'aaa';
policeCamera.money := '299.99';
policeCamera.registration := '123SPY';
policeCamera.speed := '140';
policeCamera.Date := '12/01/2001';
Write(myFile,policeCamera);
policeCamera.name := 'bbb';
policeCamera.money := '299.99';
policeCamera.registration := 'HELLA';
policeCamera.speed := '135';
policeCamera.Date := '12/01/2002';
Write(myFile,policeCamera);
policeCamera.name := 'ccc';
policeCamera.money := '299.97';
policeCamera.registration := 'HELLI';
policeCamera.speed := '145';
policeCamera.Date := '12/01/2003';
Write(myFile,policeCamera);
policeCamera.name := 'ddd';
policeCamera.money := '299.96';
policeCamera.registration := 'HELLL';
policeCamera.speed := '200';
policeCamera.Date := '12/01/2004';
Write(myFile,policeCamera);
policeCamera.name := 'eee';
policeCamera.money := '399.95';
policeCamera.registration := 'HELLP';
policeCamera.speed := '310';
policeCamera.Date := '12/01/2005';
Write(myFile,policeCamera);
policeCamera.name := 'fff';
policeCamera.money := '199.94';
policeCamera.registration := 'HELLT';
policeCamera.speed := '70';
policeCamera.Date := '12/01/2006';
Write(myFile,policeCamera);
CloseFile(myFile);
end;
end.
答案 0 :(得分:2)
您想通过车辆登记号码搜索records
并显示它,请尝试:
Var List : TStringGrid; I : Integer;
myFile : File of Tfine;
policeCamera : Tfine;
Search : String[15];
begin
List := FindComponent('List') as TStringGrid;
List.Free;
Search := InputBox('Type the registration number:' , 'Search' , '');
List := TStringGrid.Create(Self);
With List do
begin
Align := alLeft;
Name := 'List';
Width := 120*5+20;
FixedCols :=0;
FixedRows := 1;
DefaultColWidth := 120;
ColCount := 5;
Parent := Self;
end;
List.Cells [0,0] := 'Name';
List.Cells [1,0] := 'Money';
List.Cells [2,0] := 'Registration';
List.Cells [3,0] := 'Speed';
List.Cells [4,0] := 'Date';
i := 1;
AssignFile(myFile , 'speeding.dat');
Reset(myFile);
While FilePos(myFile) <> FileSize(myFile) do
begin
Read(myFile,policeCamera);
if policeCamera.registration = Search then
begin
List.Cells[0,i] := policeCamera.name;
List.Cells[1,i] := policeCamera.money;
List.Cells[2,i] := policeCamera.date;
List.Cells[3,i] := policeCamera.speed;
List.Cells[4,i] := policeCamera.registration;
Inc(i);
end;
end;
CloseFile(myFile);
end;