【问题标题】:How to loop through multiple data sets from standard input如何从标准输入循环多个数据集
【发布时间】:2015-02-12 22:18:41
【问题描述】:

我正在读取标准输入(一个文本文件并使用这样排列的数据进行计算:

 2 --This states the amount of following sets of info
 150 -- this is the first set of data
 250 -- this is the second set of data
 0 -- this is supposed to tell my program that this is the end of the two sets but
      keep looping because there might be multiple sets in here, seperated by "0"'s. 

我的 ADA 计划的基本大纲:

procedure test is

begin 

  while not end_of_file loop
  ......//my whole program executes


  end loop;
end test; 

我想知道如何告诉我的程序继续循环,直到没有要读取的内容,但要记住零是分开数据集的,如果每个“0”之后还有更多数据,则继续循环。

【问题讨论】:

  • 如果没有更多的输入,0 的位置会出现什么?
  • 最后一组数据后不用加“0”
  • 请向我们展示您是如何读取单个数据集的(带有完整的、可编译的示例)。

标签: ada ada2012


【解决方案1】:

我认为这个程序可以满足您的要求,无需过早退出循环:

with Ada.Integer_Text_Io; use Ada.Integer_Text_Io;
with Ada.Text_Io; use Ada.Text_Io;

procedure Reading_Data is
begin
   while not End_Of_File loop
      declare
         Number_Of_Sets : Natural;
      begin
         Get (Number_Of_Sets);
         if Number_Of_Sets > 0 then
            declare
               Sum : Integer := 0;
            begin
               for J in 1 .. Number_Of_Sets loop
                  declare
                     Tmp : Integer;
                  begin
                     Get (Tmp);
                     Sum := Sum + Tmp;
                  end;
               end loop;
               Put ("sum of");
               Put (Number_Of_Sets);
               Put (" elements is ");
               Put (Sum);
               New_Line;
            end;
         end if;
      end;
   end loop;
end Reading_Data;

但是,它不需要集合之间的0 分隔符; 0 只是表示“这是一个没有元素的集合,忽略它”。

现在,如果这是您需要检查数据一致性的问题的简化示例(即,如果您承诺 2 个元素,那么在读取 2 个元素后,您要么位于文件末尾,要么存在 0)此解决方案是不对的。 (你可能会认为我用declare 块来最小化变量的范围已经过火了......)

有输入:

1
10
0
2
20 30
3 40 50 60

程序给出输出:

sum of          1 elements is          10
sum of          2 elements is          50
sum of          3 elements is         150

【讨论】:

    【解决方案2】:

    使用标签编写循环:

    Until_Loop :
       While not end_of_file loop
    
          X := X + 1;
          ......//my whole program executes;
    
          exit Until_Loop when X > 5;//change criteria to something 
                                     //relating to no more files 
                                     //(whatever that will be)
       end loop Until_Loop;  
    

    编辑 - 关于 cmets 中嵌套循环的问题:

    例如:from here

    Named_Loop:
       for Height in TWO..FOUR loop
          for Width in THREE..5 loop
             if Height * Width = 12 then
                exit Named_Loop;
             end if;
             Put("Now we are in the nested loop and area is");
             Put(Height*Width, 5);
             New_Line;
          end loop;
       end loop Named_Loop;
    

    【讨论】:

    • 我忘记了直到 - 循环。但问题是我的程序停止运行的标准是它到达文件末尾时。但是现在我给它提供了不止一组数据,它只执行第一个数据而忽略第二个数据。我想以某种方式告诉它,集合之间的“0”表示继续前进。
    • Until_Loop 只是一个标签,类似于 C 中的 goto 语句。这里的 goto 是隐含的。 exit 语句成为离开的唯一方法。在0 上提供显示它如何评估和分支的代码部分,然后也许我可以提出一些建议。
    • 由于我的代码体中已经有一个不相关的直到循环,它给了我一个编译错误,说这两个直到循环相互混淆。你不能在 ADA 中嵌套直到循环吗?
    • 是的,您可以使用唯一的命名标签(再次)。例如,参见编辑。
    • 好吧,他们解决了,但我仍然不知道在“exit when:”中的 until_loop 中放入什么子句,以便我的程序知道如果有一个“0”还有另一个集合" 在数据集的末尾。
    猜你喜欢
    • 1970-01-01
    • 2017-02-05
    • 1970-01-01
    • 2020-07-04
    • 1970-01-01
    • 1970-01-01
    • 2020-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多