Stuck on an Ada Program - Stuck on Input

前端 未结 2 1937
日久生厌
日久生厌 2020-12-04 03:43

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 ca

相关标签:
2条回答
  • 2020-12-04 04:11

    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;
    
    0 讨论(0)
  • 2020-12-04 04:26

    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;
    
    0 讨论(0)
提交回复
热议问题