2012-10-16 410 views
1

我的手上有一個非常簡單的Ada項目。其任務是收集「選民」選票並將其與每個「候選人」分數進行比較,並確定哪個候選人與選民最匹配。卡住Ada計劃 - 卡住輸入

輸入看起來是這樣的,其次是被趕出輸出:

Input: 
0 0 0 1 1 1 -1 -1 -1 1 
7 
A 
1 1 1 1 1 1 1 1 1 1 
B 
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 
C 
1 -1 1 -1 1 -1 1 -1 1 -1 
D 
1 0 1 0 1 0 1 0 1 0 
E 
0 -1 0 -1 0 -1 0 -1 0 -1 
F 
1 1 1 1 0 0 0 0 0 0 
G 
0 0 0 1 -1 0 0 -1 1 1 

Output: 

A 
F 
G 

到目前爲止,我將採取每個候選人的選票,並比較他們的投票程序選民。我知道我需要做什麼,因爲我之前用Java做過,但我不確定我應該如何在Ada中進行輸入。這是我到目前爲止。

with Ada.Text_IO, Ada.Integer_Text_IO; 
use Ada.Text_IO, Ada.Integer_Text_IO; 

procedure Candidates is 
-- take an array of candidates and determine which candidate best matches 
-- the user's votes, then return those candidates 
    Number_Of_Candidates: Integer; 
subtype VoterArray_Index is Integer range 1..10; 
subtype CandidatesArray_Index is Integer range 1..Number_Of_Candidates; 
type VoterArray is array(VoterArray_Index) of Integer; 
type CandidatesArray is array(Character range 'A' .. 'Z') of array; 
type Best_CandidatesArray is array(CandidatesArray_Index) of array; 

Voter: VoterArray; 
Candidates: CandidatesArray; 
Best_Candidates: Best_CandidatesArray; 

function Get_Input() is 
-- get the input and put it into the correct arrays 
    Current_Line : string; 

    begin 
     Get(Current_Line); 

function Get_Best_Score(CandidatesArray: in out CandidatesArray) is 
-- go through the arrays and find the best score 
    SameAnswers: Integer; 
    DifferentAnswers: Integer; 
    BestScore: Integer; 
    subtype CandidateArray is array(VoterArray_Index) of Integer; 
    Candidate: CandidateArray; 

    begin 
     for I in CandidatesArray_Index loop 
      Candidate := Candidates(I); 
      for J in VoterArray_Index loop 
       if Candidate(J) /= 0 and Voter(J) /= 0 then 
        if Candidate(J) /= Voter(J) then 
            DifferentAnswers     :=     DifferentAnswers + 1 
        else 
         SameAnswers := SameAnswers + 1 
        end if; 
       end if; 
      end loop; 
      if SameAnswers - DifferentAnswers >= BestScore then 
       Best_Candidates(I) := Candidate; 
      end if; 
      SameAnswers := 0; 
      DifferentAnswers := 0; 
     end loop; 
    end Get_Best_Score; 

正如你所看到的,我不知道如何把這些數字放到一個數組中。如果你有任何建議或不同的方式,我應該去做的事情,我都聽過。

在此先感謝。

+0

你需要從文件中讀取輸入?行數似乎是內在的;是__ priori_中已知行的條目數? – trashgod

+0

輸入將從文件讀取,是的。它將以我在我的帖子中獲得的格式給出。我也會知道行數。 – user1704677

回答

3

你可以使用流,以便讀取數據: Integer'Read(STREAM_HANDLE, VARIABLE)

另一種選擇是通過GET讀取值的陣列中的每個元素,我建議的情況下,一個輔助功能,你需要調整程序,用於處理格式的變化:

Function Get_Vote (File : File_Type) Return Integer is 
    begin 
     Return Result : Integer do 
      Integer_IO.Get(
       File => File, 
       Item => Result 
       ); 
     End return; 
    end Read_Votes; 

For Index in VOTE_ARRAY'range loop 
    VOTE_ARRAY(Index) := Get_Vote(INPUT_FILE); 
End loop; 
+0

謝謝。這幫了我很多。 – user1704677

1

由於行的數量在第給出e文件,constrained array必須足夠大以容納所有可能的元素。相反,你可以聲明unconstrained array

subtype Voter_Index is Positive range 1 .. 10; 
type Voter_Array is array(Voter_Index) of Integer; 
type Candidate_Array is array(Character range <>) of Voter_Array; 

後來,當你知道實際計數,你只能分配實際需要的陣列空間。此聲明在一個nested scope放堆棧上Candidates

Number_Of_Candidates := ...; 
declare 
    Candidates : Candidate_Array(
     'A' .. Character'Val(Character'Pos('A') + Number_Of_Candidates)); 
begin 
    ... 
end; 

或者,可以allocate space on the heap

type Candidate_Array_Ptr is access Candidate_Array; 
Candidates: Candidate_Array_Ptr; 

begin 
    Number_Of_Candidates := ...; 
    Candidates := new Candidate_Array(
    'A' .. Character'Val(Character'Pos('A') + Number_Of_Candidates)); 
end; 

在任一情況下,可以訪問根據需要的數組元素:

for i in Candidates'Range loop 
    for j in Voter_Array'Range loop 
     Ada.Integer_Text_IO.put(Candidates(i)(j), 5); 
    end loop; 
    Ada.Text_IO.New_Line; 
end loop; 

附錄:此方法假定候選名稱是連續的Character s。作爲替代方案,考慮Candidate_Record,其中每個Name從文件中讀取一個數組:

type Candidate_Record is 
    record 
     Name : Character; 
     Votes : Voter_Array; 
    end record; 
type Candidate_Array is array(Positive range <>) of Candidate_Record; 

Candidates : Candidate_Array(1 .. Number_Of_Candidates); 

for i in Candidates'Range loop 
    Ada.Text_IO.Put(Candidates(i).Name & ':'); 
    for j in Voter_Array'Range loop 
     Ada.Integer_Text_IO.Put(Candidates(i).Votes(j), 5); 
    end loop; 
    Ada.Text_IO.New_Line; 
end loop;