我創建一個程序,顯示10本書的標題,作者和ISBN的列表。該程序可以顯示記錄並對其進行排序,但我無法搜索到他。我不知道如何在代碼中實現FindArray標識符,並且我不知道如何讓程序搜索該書。記錄存儲在.dat文件中。任何幫助,將不勝感激。這裏的代碼:搜索德爾福數據文件中的記錄文件
uses SysUtils;
type TBook = Record
Number : integer;
Title : String[50];
Author : String[50];
ISBN : String[13];
end;
var Book : TBook;
CurrentFile : File Of TBook;
index : integer;
FindArray : array[1..10] of TBook;
Procedure Add_Book;
var Title, Author, ISBN : String;
index : integer;
begin
Assign (CurrentFile, 'C:\Books.dat');
reset (CurrentFile);
Seek (CurrentFile, filesize(CurrentFile));
Book.Number := (filepos(CurrentFile)+1);
writeln ('Type in book title');
readln (Title);
For index := 1 to Length(Title) do
Title[index] := Upcase(Title[index]);
Book.Title := Title;
writeln ('Type in author');
readln (Author);
For index := 1 to Length(Author) do
Author[index] := Upcase(Author[index]);
Book.Author := Author;
writeln ('Type in ISBN');
readln (ISBN);
For index := 1 to Length(ISBN) do
ISBN[index] := Upcase(ISBN[index]);
Book.ISBN := ISBN;
write (CurrentFile, Book);
Close (CurrentFile);
end;
Procedure Show_All;
begin
Assign (CurrentFile, 'C:\Books.dat');
reset (CurrentFile);
while filepos(CurrentFile) <> filesize(CurrentFile) do
begin
read (CurrentFile, Book);
writeln ('File: ', Book.Number);
writeln ('Title: ', Book.Title);
writeln ('Author: ', Book.Author);
writeln ('ISBN: ', Book.ISBN);
writeln;
end;
writeln;
write ('END OF FILE!');
readln;
Close (CurrentFile);
end;
Procedure Delete_All;
begin
Assign (CurrentFile, 'C:\Books.dat');
reset (CurrentFile);
seek (CurrentFile,0);
Truncate (CurrentFile);
Close (CurrentFile);
end;
Procedure Menu;
var option :integer;
begin
writeln ('Press appropriate number');
writeln;
writeln ('1. Search book');
writeln ('2. Quit');
readln (option);
CASE option of
1: show_all;
2: delete_all
else;menu
end; {End Case}
end;
begin
Assign (CurrentFile, 'C:\Books.dat');
reset (CurrentFile);
index := 0;
while not eof(CurrentFile) do
begin
index := index+1;
read (CurrentFile, Book);
end;
index := Book.Number;
repeat
menu
until eof(CurrentFile);
close (CurrentFile)
end.
這是我的代碼到目前爲止。爲了搜索書名或作者,我需要做些什麼?
這是功課,還是可以使用數據庫? –
我可以使用數據庫,但我正在使用控制檯應用程序 – user3371799
您可以使用任何類型的應用程序中的數據庫。你絕對應該。你正在重新發明輪子。你的車輪不太可能是完美的圓形。 –