2016-11-25 25 views
1

我想知道你們中的任何人能否爲我回答一個簡短的問題。我目前正在處理記錄,並且在我的程序中我需要它來了解我要導入的文件的行包含哪些內容。我的問題在於我不知道如何將這條線「拆分」爲實際變量。例如,該線路是從文件中獲得變量的變量

22134.09 Kia Bernice 

如何讓程序知道,第一部分,22134.09是可變的價格,起亞是可變的公司和伯尼斯是變量模型,然後將它們都整理到一個記錄?

type PriceCompModel is record 
price : Float range 1.0..99999.99; 
company : String (1..CompNameLength); 
Model : String (1..ModelNameLength); 

感謝。

編輯代碼

with Ada.Text_IO; use Ada.Text_IO; 
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; 
with Ada.Float_Text_IO; use Ada.Float_Text_IO; 

procedure Testexercise is 

    type Inventory is Record 

     CarPrice : Float := 0.0; 
     CarType : String (1..40); 
     -- CarType will include both the model and company 
    end record; 

    InventoryItem : Inventory; 
    ImportedFile : File_Type; 
    FileName  : String := "Cars.txt"; 
    WordsFromFile : String(1..40); 
    LengthofWords : Integer ; 
    PriceofCar  : Float := 0.0; 
    LoopCount  : Integer := 1; 

    type Cars is array (1..12) of Inventory; 

    begin 

     Open(File => ImportedFile, Mode => In_File, Name => FileName); 

     --for I in 1..12 loop 

     while LoopCount /= 12 loop 

     Get(File => ImportedFile, Item => InventoryItem.CarPrice); 

     Get_Line(File => ImportedFile, Item => WordsFromFile, Last=> LengthofWords);  

     Put (Integer(InventoryItem.CarPrice),1); 

     Put (WordsFromFile(1..LengthofWords)); 

     New_Line; 

     LoopCount := LoopCount + 1; 

     InventoryItem.CarType := WordsFromFile;  


     end loop; 
    close(ImportedFile); 



    end Testexercise; 

所以我嘗試循環

for I in 1..12 loop 
    Cars := Inventory; 
    end loop; 

內這樣做就這樣結束了,我的工作後,我設置

汽車:汽車;

for I in 1..12 loop 
Car(I) := Inventory; 
end loop; 
+0

該文件是否總是包含12個庫存記錄?如果文件包含少於12條記錄,則當您嘗試讀取超出文件末尾時,將會遇到異常。如果文件包含多於12條記錄,則將無法處理額外的記錄。 –

+0

是的,我只是爲了測試目的而在這個特定時刻做了這個。如果我願意,我可以隨時調整範圍。 – Dibs

+0

關於數組索引的研究。數組中的每個元素都通過索引值訪問數組。在上例中,Cars數組索引值從1開始並以12結尾。第一個Inventory值應該分配給Cars(1)。最後的庫存值應該分配給汽車(12)。 –

回答

4

定義記錄以包含您的信息時,需要考慮很多因素。

創建float的命名子類型或命名浮點類型以便I/O例程可以檢查價格組件的輸入值將很有用。

字符串類型的字段必須限制爲預定義的大小。這意味着所有「公司」字符串必須具有相同的大小,並且所有「模型」字符串的大小必須相同,儘管模型字符串可能與公司字符串的長度不同。如果公司和/或模型的名稱可能有所不同,則應考慮使用有界字符串(Ada語言參考手冊第A.4.4節)或無界字符串(Ada語言參考手冊第A.4.5節)。

如果輸入文件的字符串大小固定,可以使用Ada.Text_IO.Text_Streams(Ada語言參考手冊A.12.2節)讀取記錄的每個字段。如果字符串的大小可能不同,那麼您需要使用Ada.Text_IO手動讀取和解析每個字段。

-- Read record data from a file 
with Ada.Text_Io; use Ada.Text_IO; 
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; 
with Ada.Strings.Fixed; use Ada.Strings.Fixed; 

procedure read_record is 
    type Prices is delta 0.01 digits 7 range 0.0..99999.99; 
    type Auto_Inventory is record 
     Price : Prices := 0.0; 
     Company : Unbounded_String := Null_Unbounded_String; 
     Model : Unbounded_String := Null_Unbounded_String; 
    end record; 
    package AI_IO is new Ada.Text_IO.Decimal_IO(Prices); 
    use AI_IO; 

    Inventory_Item : Auto_Inventory; 
    The_File : File_Type; 
    File_Name : String := "inventory.txt"; 
    Inpt_Str : String(1..1024); 
    Length : Natural; 
    Start, Finis : Positive; 
begin 
    Open(File => The_File, 
     Mode => In_File, 
     Name => File_Name); 
    Get(File => The_File, 
     Item => Inventory_Item.Price); 
    Get_Line(File => The_File, 
      Item => Inpt_Str, 
      Last => Length); 
    Close(The_File); 
    Start := Index_Non_Blank(Source => Inpt_Str(1..Length)); 
    Finis := Start; 
    while Finis < Length and then Inpt_Str(Finis) /= ' ' loop 
     Finis := Finis + 1; 
    end loop; 
    Inventory_Item.Company := To_Unbounded_String(Inpt_Str(Start..Finis)); 
    Start := Index_Non_Blank(Inpt_Str(Finis + 1..Length)); 
    Inventory_Item.Model := To_Unbounded_String(Inpt_Str(Start..Length)); 
    Put_Line("Price: " & Prices'Image(Inventory_Item.Price)); 
    Put_Line("Company: " & To_String(Inventory_Item.Company)); 
    Put_Line("Model: " & To_String(Inventory_Item.Model)); 
end read_record; 

如果你想讀取包含你需要收集某種容器的信息多記錄的文件。以下示例使用通用包Ada.Containers.Vectors中的Vector。

-- Auto Inventory Package specification 
with Ada.Text_IO; use Ada.Text_IO; 
with Ada.Containers.Vectors; 
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; 

package Auto_Inventory is 
    type Prices is delta 0.01 digits 7 range 0.0..99999.99; 
    type Automobile is tagged private; 
    procedure Print(Item : Automobile); 
    function Set return Automobile; 
    function Get_Price(Item : Automobile) return Prices; 
    function Get_Company(Item : Automobile) return String; 
    function Get_Model(Item : Automobile) return String; 

    type Inventory is tagged private; 
    procedure Read(Item : out Inventory; File : File_Type) with 
    Pre => Mode(File) = In_File; 

    procedure Write(Item : in Inventory; File : File_type) with 
    Pre => Mode(File) = Out_File; 

    procedure Print(Item : Inventory); 
private 
    type Automobile is tagged record 
     Price : Prices := 0.0; 
     Company : Unbounded_String := Null_Unbounded_String; 
     Model : Unbounded_String := Null_Unbounded_String; 
    end record; 

    package Auto_Vect is new 
    Ada.Containers.Vectors(Index_Type => Positive, 
          Element_Type => Automobile); 
    use Auto_Vect; 
    type Inventory is tagged record 
     List : Vector; 
    end record; 
end Auto_Inventory; 

主體此包是:

with Ada.Strings.Fixed; use Ada.Strings.Fixed; 

package body Auto_Inventory is 
    package Prices_IO is new Ada.Text_IO.Decimal_IO(Prices); 
    use Prices_IO; 
    ----------- 
    -- Print -- 
    ----------- 

    procedure Print (Item : Automobile) is 
     use Prices_Io; 
    begin 
     Put_Line("Price : " & Prices'Image(Item.Price)); 
     Put_Line("Company: " & To_string(Item.Company)); 
     Put_Line("Model : " & To_String(Item.Model)); 
     New_Line; 
    end Print; 

    --------- 
    -- Set -- 
    --------- 

    function Set return Automobile is 
     Temp  : Automobile; 
     Inpt_Str : String(1..1024); 
     Length : Natural; 
    begin 
     Put("Enter the automobile price: "); 
     Get(Item => Temp.Price); 
     Put("Enter the automobile company: "); 
     Get_Line(Item => Inpt_Str, Last => Length); 
     Temp.Company := To_Unbounded_String(Inpt_Str(1..Length)); 
     Put("Enter the automobile model: "); 
     Get_Line(Item => Inpt_Str, Last => Length); 
     Temp.Model := To_Unbounded_String(Inpt_Str(1..Length)); 
     return Temp; 
    end Set; 

    --------------- 
    -- Get_Price -- 
    --------------- 

    function Get_Price (Item : Automobile) return Prices is 
    begin 
     return Item.Price; 
    end Get_Price; 

    ----------------- 
    -- Get_Company -- 
    ----------------- 

    function Get_Company (Item : Automobile) return String is 
    begin 
     return To_String(Item.Company); 
    end Get_Company; 

    --------------- 
    -- Get_Model -- 
    --------------- 

    function Get_Model (Item : Automobile) return String is 
    begin 
     return To_String(Item.Model); 
    end Get_Model; 

    ---------- 
    -- Read -- 
    ---------- 

    procedure Read (Item : out Inventory; 
        File : File_Type) is 
     Temp : Inventory; 
     Auto : Automobile; 
     Inpt_Str : String(1..1024); 
     Length : Natural; 
     Start, Finis : Positive; 
    begin 
     while not End_Of_File(File) loop 
     Get(File => File, Item => Auto.Price); 
     Get_Line(File => File, Item => Inpt_str, Last => Length); 
     Start := Index_Non_Blank(Inpt_Str(1..Length)); 
     Finis := Start; 
     while Finis < Length and then Inpt_Str(Finis) /= ' ' loop 
      Finis := Finis + 1; 
     end loop; 
     Auto.Company := To_Unbounded_String(Inpt_Str(Start..Finis - 1)); 
     Start := Index_Non_Blank(Inpt_Str(Finis..Length)); 
     Auto.Model := To_Unbounded_String(Inpt_Str(Start..Length)); 
     Temp.List.Append(Auto); 
     end loop; 
     Item := Temp; 
    end Read; 

    ----------- 
    -- Write -- 
    ----------- 

    procedure Write (Item : in Inventory; 
        File : File_type) is 

    begin 
     for Element of Item.List loop 
     Put(File => File, Item => Prices'Image(Element.Price) & 
       " " & To_String(Element.Company) & " " & 
       To_String(Element.Model)); 
     New_Line; 
     end loop; 

    end Write; 

    ----------- 
    -- Print -- 
    ----------- 

    procedure Print (Item : Inventory) is 
    begin 
     for Element of Item.List loop 
     Element.Print; 
     end loop; 
    end Print; 

end Auto_Inventory; 

的主過程的示例鍛鍊這個包:

------------------------------------------------------------------ 
-- Read a file of many records -- 
------------------------------------------------------------------ 
with Auto_Inventory; use Auto_Inventory; 
with Ada.Text_IO; use Ada.Text_IO; 

procedure read_file is 
    The_Inventory : Inventory; 
    The_File : File_Type; 
    File_Name : String := "inventory.txt"; 
begin 
    Open(File => The_File, 
     Mode => In_File, 
     Name => File_Name); 
    The_Inventory.Read(The_File); 
    Close(The_File); 
    The_Inventory.Print; 
end read_file; 

這個程序的一個例子的輸入文件是:

22134.09 Kia Bernice 
12201.15 Nissan Versa 
22349.99 Chevrolet Cruse 
+0

我看到了,如果我需要讀取多行信息,然後再訪問它們,那麼我可以簡單地循環整個事件並在每次迭代結束時增加一個計數器?我剛開始研究代碼,在文件輸入,輸出和閱讀方面,我幾乎都是新手。非常感謝! – Dibs

+0

您可以選擇一組記錄來存儲輸入,也可以將它們存儲在矢量或鏈接列表中。數組的使用很簡單,但在開始讀取元素之前,您需要至少分配數組中的元素。在從文件中讀取記錄的過程中,可根據需要增加向量和鏈接列表。通用向量和鏈接列表可在Ada.Containers包中找到。請參閱「Ada語言參考手冊」A.18.11節和A.18.12節。 –

+0

我編輯了我的答案以顯示處理多行文件的示例。 –

-3

它不是c爲我學習你使用的是什麼語言。然而,這個概念是要單獨處理文件中的每一行,然後用一個函數來處理它,這個函數根據你使用的語言執行token-zing或splitting,並將每個token保存在一個變量中你如何使用將保存令牌

比如函數:

在java中有一類

​​

DELIM你的情況是空間,所以使用的格式

StringTokenizer st = new StringTokenizer("this is a test"); 

String line = reader.readLine(); 

    String[] tokens = line.split("\\s"); 

注意,你需要保存你在一個字符串讀取的行,所以你可以在Java中使用這些功能,然後從陣列

String price = tokens[1] and so on 

其他語言訪問每個令牌,請發現以下資源:

在C https://www.tutorialspoint.com/c_standard_library/c_function_strtok.htm

在pyhton https://www.tutorialspoint.com/python/string_split.htm

+3

這個問題顯然被標記爲關於Ada編程語言。 Java,C或Python如何相關? –

+0

我很抱歉,我沒有注意到標籤。 – learn