Arrays 用折叠画书皮(Ada)

Arrays 用折叠画书皮(Ada),arrays,package,integer,ada,Arrays,Package,Integer,Ada,我正在做一项任务,类似于这个网站上已经解决的标志包问题 不过,这次我需要画一个书皮,上面有4个折叠,如下所示: Book with folds at the breaking points: 3, 10, 15, 17. Height = 2, Width = 20, Title (string(1..100), Author (string(1..50). <Title> <Author> |--|------|----|-|---| | | |

我正在做一项任务,类似于这个网站上已经解决的标志包问题

不过,这次我需要画一个书皮,上面有4个折叠,如下所示:

Book with folds at the breaking points: 3, 10, 15, 17.
Height = 2, Width = 20, 
Title (string(1..100),
Author (string(1..50).

<Title>
<Author>
|--|------|----|-|---| 
|  |      |    | |   |
|  |      |    | |   |   
|--|------|----|-|---|
我的包体文件

with ada.text_IO; use ada.text_IO;
with ada.integer_text_IO; use ada.integer_text_IO;

with Exercise_Bluebook; use Exercise_Bluebook;

package body Exercise_BlueBook is

  procedure Get (Item: out Book_Type;
         Title: in String;
         Author: in String;
         Width: in Integer;
         Height: in Integer;
         Break_Points: in Breaking_Array) is

  begin

     Item.Title_L := Integer'Min (Item.Title'Length, Title'Length);
     Item.Title (1..Item.Title_L) := Title(Title'First .. Item.Title_L);
     Item.Author_L := Integer'Min (Item.Author'Length, Author'Length);
     Item.Author (1..Item.Author_L) := Author (Author'First .. Item.Author_L);
     Item.Width := Width;
     Item.Height := Height;
     Item.Break_Points := Break_Points;

  end Get;

  procedure Put (Item: in Book_Type) is


  begin

     Put_Line(Item.Title(1..Item.Title_L));
     Put_Line(Item.Author(1..Item.Author_L));


     for H in 1..Item.Height loop
    Put("!");

    for I in 1..Item.Width loop
       Put("-");
       if I = Breaking_Array(1) then
          Put("!");
       elsif I = Breaking_Array(2) then
          Put("!");
       elsif I = Breaking_Array(3) then
          Put("!");
       elsif I = Breaking_Array(4) then
          Put("!");
       end if;
    end loop;
    Put_Line("!");
     end loop;

     end Put;

end Exercise_BlueBook;
我的主要程序

with ada.text_IO; use ada.text_IO;
with ada.integer_text_IO; use ada.integer_text_IO;

with Exercise_Bluebook; use Exercise_Bluebook;

procedure Exercise_BlueBook_Main is

   B : Book_Type;

begin

   Put("Enter the name of the book: ");
   declare
      Title : constant String := Get_Line; -- First Get_Line already taken
      Author: constant String := Get_Line; -- How do I solve this for "Author"?
      Width: Integer;
      Height: Integer;
      Break_Points : Exercise_BlueBook.Breaking_Array;
   begin
      Put("Enter the book's width: ");
      Get(Width);
      Put("Enter the book's height: ");
      Get(Height);
      Put("Enter the breaking points for the fold: ");
      Get(Break_Points); -- Won't read this as an array?

      Get(B, 
      Title => Title,
      Author => Author,
      Width => Width,
      Height => Height,
      Break_Points => Break_Points);
   end;

   New_Line;
   Put(B);


end Exercise_BlueBook_Main;
来自Ada.Integer\u Text\u IO包的Get子程序将只读取一个整数。您可以通过添加一个循环来扩展它:

main.adb

使用Ada.Text_IO;使用Ada.Text\u IO; 主要程序是 通用的 类型Num是范围; 类型Arr是Num的数组整数范围; 过程获取来源:字符串;项目:外部Arr; ----- -得到- ----- 过程获取来源:字符串;项目:出Arr is 包Num_IO是 新Ada.Text\u IO.Integer\u IO Num; 最后:整数:=From'First-1; 开始 对于项目范围循环中的Idx Num_IO.Get From Last+1。。From'Last,项目Idx,Last; 端环; 最终得到; 子类型Break_Points为整数范围1。。20; 类型Breaking_Array是Break_点的数组整数范围; 获取\断开\数组的过程是 新建获取断开点,断开数组; BA:正在断开_数组1。。4. 开始 获取数组获取线,BA; 新线; 放置结果:; BA回路的BP算法 放置BP的图像; 新线; 端环; 例外 当数据错误=> Put_行错误:无效数据。; 当End_错误=> 放线错误:元素不足。; 端干管; 输出太多元素,仅读取前4个

$ ./main
1 2 3 4 5 6

Result:
 1
 2
 3
 4
输出的元素太少

$ ./main
1 2 3
Error: Not enough elements.
输出非数字数据

$ ./main
1 2 x 4
Error: Invalid data.
元素的输出值超出范围

$ ./main
1 2 3 100
Error: Invalid data.
来自Ada.Integer\u Text\u IO包的Get子程序将只读取一个整数。您可以通过添加一个循环来扩展它:

main.adb

使用Ada.Text_IO;使用Ada.Text\u IO; 主要程序是 通用的 类型Num是范围; 类型Arr是Num的数组整数范围; 过程获取来源:字符串;项目:外部Arr; ----- -得到- ----- 过程获取来源:字符串;项目:出Arr is 包Num_IO是 新Ada.Text\u IO.Integer\u IO Num; 最后:整数:=From'First-1; 开始 对于项目范围循环中的Idx Num_IO.Get From Last+1。。From'Last,项目Idx,Last; 端环; 最终得到; 子类型Break_Points为整数范围1。。20; 类型Breaking_Array是Break_点的数组整数范围; 获取\断开\数组的过程是 新建获取断开点,断开数组; BA:正在断开_数组1。。4. 开始 获取数组获取线,BA; 新线; 放置结果:; BA回路的BP算法 放置BP的图像; 新线; 端环; 例外 当数据错误=> Put_行错误:无效数据。; 当End_错误=> 放线错误:元素不足。; 端干管; 输出太多元素,仅读取前4个

$ ./main
1 2 3 4 5 6

Result:
 1
 2
 3
 4
输出的元素太少

$ ./main
1 2 3
Error: Not enough elements.
输出非数字数据

$ ./main
1 2 x 4
Error: Invalid data.
元素的输出值超出范围

$ ./main
1 2 3 100
Error: Invalid data.

目前,主程序提示输入标题,然后为标题调用Get_行,然后为作者调用另一个Get_行。使用Gets读取以下数据;其工作方式是读取字符,跳过空格直到找到可能属于数字的部分,然后读取更多字符直到找到不属于数字的部分

我们一直建议采用这种形式的原因

   declare
      Title : constant String := Get_Line;
字符串是一个固定长度的字符数组;你怎么能提前知道要做多久?其工作方式是,实际需要的长度在运行时由Get_行返回的初始化值的长度确定

您可以通过更改以下提示来修复缺少对作者的提示:输入书名和作者姓名,在每个之后单击RET:

或者你可以把它们放在巢里:

   Put("Enter the name of the book: ");
   declare
      Title : constant String := Get_Line;
   begin
      Put("Enter the author's name: ");
      declare
         Author: constant String := Get_Line;
         Width: Integer;
         Height: Integer;
         Break_Points : Exercise_BlueBook.Breaking_Array;
      begin
         Put("Enter the book's width: ");
         Get(Width);
         Put("Enter the book's height: ");
         Get(Height);
         Put("Enter the breaking points for the fold: ");
现在我们来看看断点。您需要使用自己的Get读取每个元素:


目前,主程序提示输入标题,然后为标题调用Get_行,然后为作者调用另一个Get_行。使用Gets读取以下数据;其工作方式是读取字符,跳过空格直到找到可能属于数字的部分,然后读取更多字符直到找到不属于数字的部分

我们一直建议采用这种形式的原因

   declare
      Title : constant String := Get_Line;
字符串是一个固定长度的字符数组;你怎么能提前知道要做多久?其工作方式是,实际需要的长度在运行时由Get_行返回的初始化值的长度确定

您可以通过更改以下提示来修复缺少对作者的提示:输入书名和作者姓名,在每个之后单击RET:

或者你可以把它们放在巢里:

   Put("Enter the name of the book: ");
   declare
      Title : constant String := Get_Line;
   begin
      Put("Enter the author's name: ");
      declare
         Author: constant String := Get_Line;
         Width: Integer;
         Height: Integer;
         Break_Points : Exercise_BlueBook.Breaking_Array;
      begin
         Put("Enter the book's width: ");
         Get(Width);
         Put("Enter the book's height: ");
         Get(Height);
         Put("Enter the breaking points for the fold: ");
现在我们来看看断点。您需要使用自己的Get读取每个元素:


再次感谢你的勤奋工作,迪迪,不幸的是,这是一个头部或两个abo
让我真正理解它的实现。比如,1我如何将新的Int_数组链接到我的Breaking_数组,2在声明部分,假设在声明后立即输入整数,对吗?问题是我的Title get_line字符串已经占据了这个位置,为什么我还问我如何在声明部分添加另一个get_line函数,而不声明一个新的声明,我想这是不允许的,因为GetB的内聚需要,Title=>Title,Author=>Author;etc@GustavAgrell我添加了一个在您的案例中可能更容易使用的替代实现。再次感谢您的勤奋工作,DeeDee,不幸的是,就真正理解其实现而言,它比我高出一两个百分点。比如,1我如何将新的Int_数组链接到我的Breaking_数组,2在声明部分,假设在声明后立即输入整数,对吗?问题是我的Title get_line字符串已经占据了这个位置,为什么我还问我如何在声明部分添加另一个get_line函数,而不声明一个新的声明,我想这是不允许的,因为GetB的内聚需要,Title=>Title,Author=>Author;etc@GustavAgrell我添加了一个替代实现,它可能更容易在您的案例中使用。