存储和使用不同长度的字符串 (ADA) [英] Storing and using strings of varying length (ADA)

查看:42
本文介绍了存储和使用不同长度的字符串 (ADA)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在解决一个问题,我需要根据输入数字制作一组框,其中每个框都有一个唯一的名称.我已经设法创建了这些框,但我只能在所有这些框上插入一个名字,因为我的名字在名称收集过程中被覆盖了.

Im working on a problem where I need to make a set of boxes according to an input number where each and every box has a unique name. I've managed to create the boxes but I've only managed to insert one name on all of them as my names are overwritten in the name collecting procedure.

这是代码https://pastebin.com/FBMvvrn4

with Ada.Text_IO;       use Ada.Text_IO;
with Ada.Float_Text_IO;     use Ada.Float_Text_IO;
with Ada.Integer_Text_IO;   use Ada.Integer_Text_IO;

procedure exercise is

  N : Integer;
  Names : String(1..10);
  L : Integer;

  procedure Objectcatcha (N: out Integer) is

  begin

  Put("Enter amount of objects: ");
  Get(N);

  end Objectcatcha;

  procedure Namescatcha (Names: out string; L: out integer) is

  begin
 for I in 1..N loop
    Get_Line(Names, L);
 end loop;

  end Namescatcha;

  procedure SpaceBox(Names: in String; L: in Integer; N : in integer) is

 begin

 for I in 1..N loop
    Put("+-----------+     ");
 end loop;
 New_Line;

 for I in 1..N loop
    Put("! ");
    Put(Names(1..L));
    for J in (L+1)..10 loop
       Put(" ");
    end loop;
    Put("!");

    if I = N then
       Put("");
    else
       Put("<>---");
    end if;
 end loop;
 New_Line;

 for I in 1..N loop
    Put("+-----------+     ");
 end loop;

  end SpaceBox;

  begin

  Objectcatcha(N);

  Put("Enter the name of the objects: ");
  Namescatcha(Names, L);

  SpaceBox(Names,L, N);

  end exercise;

我一直在讨论这个问题,如果有人能帮我找到一种方法来单独命名每个框,我会很高兴.

I've been sitting around a lot with this and Id be very glad if someone could help me find a way to name each box individually.

提前致谢!

推荐答案

感谢 Zerte、Jere 和 Brian 提供的示例,非常感谢.不幸的是,我不能使用第三方软件包,因此排除了 Zertes 解决方案,至于 Jere 对不起,我只是一个简单的 codemonkey,对 ADA 知识非常浅薄,而您的示例对我来说太复杂了.即使我得到了确切的代码并且它有效,我仍然不会学习它,因为它与我学校教授的内容相差太大.就像没有输入/输出参数的程序一样,可以说是一个.也许我误解了它并没有那么糟糕,但乍一看,对于我的 ADA 水平来说似乎太复杂了.

Thanks Zerte, Jere and Brian for your examples, it's much appreciated. Unfortunately I can't use third party packages so that rules out Zertes solution and as for Jere Im sorry but Im just a simple codemonkey with very shallow ADA knowledge and your example is just too complicated for me. Even if I got the exact code and it worked I still wouldnt learn it because it differs too much from what my school is teaching out. Like the procedures not having in/out parameters lets say for one. Maybe I misunderstand and its not that bad but at first glance it seems too complex for my level of ADA.

布莱恩我认为会起作用,但它的作用是,因为它循环 Spacebox N 次,它创建 N^2 个盒子,并且在单独的行上,当我只需要一行上 N 个盒子时.有没有什么办法可以修补代码来解决这个问题,因为它看起来很有希望?

Brians I thought would work but what it does is, because it loops Spacebox N times, it creates N^2 amount of boxes, and on separate lines, when I only need N boxes on one line. Is there any way we could perhaps patch up the code to fix this because it seemed promising?

再次感谢您抽出宝贵时间!

Thanks again for your time all of you!

with Ada.Text_IO;       use Ada.Text_IO;
with Ada.Float_Text_IO;     use Ada.Float_Text_IO;
with Ada.Integer_Text_IO;   use Ada.Integer_Text_IO;
with Ada.Strings.Unbounded;     use Ada.Strings.Unbounded;
with Ada.Strings.Unbounded.Text_IO; use Ada.Strings.Unbounded.Text_IO;


procedure exercise is

   Y : Ada.Text_IO.Count;
   N : Integer; 
   Names : Unbounded_string;
   type Names_Type is array (Positive range <>) of Unbounded_String;

   procedure Objectcatcha (N : out Integer) is

   begin

      Put("Enter amount of objects: ");
      Get(N);
      Skip_Line;

   end Objectcatcha;



   procedure Namescatcha (Names: out Names_Type; N : in Integer) is

   begin
      for I in Names'Range loop
     Get_Line(Names(I));
      end loop;

   end Namescatcha;

   procedure SpaceBox (Names: in Names_Type; N : in Integer) is

   begin

      for I in 1..N loop
     Put("+-----------+     ");
      end loop;
      New_Line;

      for I in Names'Range loop
     Put("! ");
     Put(Names(I));

     Y := Ada.Text_IO.Count(I);
     Set_Col(13+18*(Y-1));

     Put("!");

     if I = N then
        Put("");
     else
        Put("<>---");
     end if;

      end loop;
      New_Line;

      for I in 1..N loop
     Put("+-----------+     ");
      end loop;

   end SpaceBox;

begin

   Objectcatcha(N);
   declare
      A : Names_Type (1..N);
   begin
      Put("Enter the name of the objects: ");
      Namescatcha(A, N);
      SpaceBox(A, N);
   end;

end exercise;

这段代码完全按照我想要的方式工作,所以我认为它终于解决了:耶!:)

This code works exactly the way I wanted it to, so I think it's finally solved: yay! :)

这篇关于存储和使用不同长度的字符串 (ADA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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