Stuck on an Ada Program - Stuck on Input

2019-01-12 11:50发布

问题:

I have a pretty simple Ada project on my hands. The task is to take a collection of a "voter's" votes and compare it to each "candidate's" score and determine which candidate best matches the voter.

The input looks like this, followed by the output that should be put out:

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

So far, I have a procedure that will take the votes of each candidate and compare them to the votes of the voter. I know what I need to do as I have done it before in Java, but I am not sure how I should take the input in Ada. Here is what I have so far.

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;

As you can see, I'm not sure how to take the numbers and put them into an array. If you have any suggestions or a different way I should go about things, I'm all ears.

Thanks in advance.

回答1:

You could use streams in order to read the data in: Integer'Read( STREAM_HANDLE, VARIABLE )

Another option is reading the values via Get for each element of your array, I'd recommend a helper-function in case you need to tweak the procedure for handling format changes:

    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;

and

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


回答2:

Because the number of rows is given in the file, a constrained array has to be large enough to hold all possible elements. Instead, you can declare an 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;

Later, when you know the actual count, you can allocate only the space actually required for the array. This declaration puts Candidates on the stack in a nested scope:

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

Alternatively, you can 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;

In either case, you can access the array elements as required:

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;

Addendum: This approach assumes that candidate names are consecutive Characters. As an alternative, consider an array of Candidate_Record, where each Name is read from the file:

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;


标签: ada