卡在Ada程序上-卡在输入上 [英] Stuck on an Ada Program - Stuck on Input

查看:87
本文介绍了卡在Ada程序上-卡在输入上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我手上有一个非常简单的Ada项目。任务是收集选民的选票,并将其与每个候选人的得分进行比较,并确定最适合选民的候选人。

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

到目前为止,我有一个程序将获得每位候选人的票数,并将其与选民的票数进行比较。我知道我之前在Java中需要做的事情,但是我不确定如何在Ada中进行输入。

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.

预先感谢。

推荐答案

您可以按顺序使用流读取以下数据:
Integer'Read(STREAM_HANDLE,VARIABLE)

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

另一个选择是读取通过Get获取数组中每个元素的值,如果需要调整处理格式更改的过程,我建议使用一个辅助函数:

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;

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

这篇关于卡在Ada程序上-卡在输入上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆