1

私は非常に単純な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;

ご覧のとおり、数字を取得して配列に入れる方法がわかりません。何か提案や私が物事に取り組むべき別の方法があれば、私はすべての耳です。

前もって感謝します。

4

2 に答える 2

3

次のデータを読み取るためにストリームを使用できます。 Integer'Read( STREAM_HANDLE, VARIABLE )

もう1つのオプションは、配列の各要素の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;
于 2012-10-16T05:10:47.010 に答える
1

行数はファイルに指定されているため、制約付き配列は可能なすべての要素を保持できる大きさである必要があります。代わりに、制約のない配列を宣言できます。

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;

後で実際の数がわかれば、配列に実際に必要なスペースのみを割り当てることができます。この宣言は、ネストされた scopeCandidatesのスタックに置きます。

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

または、ヒープにスペースを割り当てることができます。

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;

補遺: このアプローチは、候補名が連続した s であることを前提としていCharacterます。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;
于 2012-10-16T21:50:59.133 に答える