2014-03-03 34 views
1

我創建一個程序,顯示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. 

這是我的代碼到目前爲止。爲了搜索書名或作者,我需要做些什麼?

+1

這是功課,還是可以使用數據庫? –

+0

我可以使用數據庫,但我正在使用控制檯應用程序 – user3371799

+2

您可以使用任何類型的應用程序中的數據庫。你絕對應該。你正在重新發明輪子。你的車輪不太可能是完美的圓形。 –

回答

1

如果我的所有使用相同的基本結構記錄列表,我希望能夠通過他們搜索和查找匹配一定的過濾模式記錄,我會做的第一件事就是把它變成一個SQL數據庫而不是file of [record type]。然後我可以簡單地運行查詢:select * from BOOKS where TITLE = :title並讓DBMS處理細節。

如果你不能這樣做,有兩種基本的方法來運行你自己的過濾:通過線性搜索和按鍵查找。

它們都以相同的方式啓動:將整個文件讀入內存,讀入TBook數組。你下一步做什麼取決於你的策略。

線性搜索非常簡單:

function FindBookIndex(const searchTitle: string): integer; 
begin 
    for i := 0 to high(bookArray) do 
     if bookArray[i].Title = searchTitle then 
     exit(i); 
    result := -1; //not found 
end; 

索引的搜索需要多一點工作的前期,但是是快了很多實際運行搜索。你設置一個TDictionary映射字符串(標題)爲整數(數組索引)。然後運行查找,在字典上調用TryGetValue,如果沒有結果,則返回-1。請注意,這隻適用於完全匹配;如果您想進行部分匹配(例如,標題中包含單詞「Wind」的書籍),則您需要線性搜索或更復雜的索引方案。

但是,真的最簡單的方法是將其放入數據庫中。