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 )

另一种选择是通过 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;

稍后,当您知道实际计数时,您可以只分配数组实际需要的空间。此声明在嵌套范围内Candidates放入堆栈:

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;

附录:这种方法假设候选名称是连续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 回答