打印 Ada 中的字符

发布于 2024-12-09 17:48:01 字数 346 浏览 0 评论 0原文

我已经声明了这些:

  subtype Num_Char          is Natural 
     range 1 .. Definitions.Page_Width + 1;
  subtype Number_Of_Rows    is Definitions.Number_Of_Rows;
  type Chars                is array (Number_Of_Rows, Num_Char) of Character; 
  The_Chars           : Chars;

使用 Ada.Text_IO.Put_Line() 将其打印到屏幕的最佳方法是什么?

I have these declared:

  subtype Num_Char          is Natural 
     range 1 .. Definitions.Page_Width + 1;
  subtype Number_Of_Rows    is Definitions.Number_Of_Rows;
  type Chars                is array (Number_Of_Rows, Num_Char) of Character; 
  The_Chars           : Chars;

What is the best way to print this out to the screen using Ada.Text_IO.Put_Line()?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无所谓啦 2024-12-16 17:48:02

假设您想要使用 Ada.Text_IO 而不仅仅是 Put_Line,并且假设 Number_Of_Rows 是一个像 这样的整数范围Num_Char,那就是

for R in The_Chars'Range (1) loop
   for C in The_Chars'Range (2) loop
      Ada.Text_IO.Put (The_Chars (R, C));
   end loop;
   Ada.Text_IO.New_Line;
end loop;

Assuming you want to use Ada.Text_IO and not just Put_Line specifically, and assuming that Number_Of_Rows is meant to be an integer range like Num_Char, that would be

for R in The_Chars'Range (1) loop
   for C in The_Chars'Range (2) loop
      Ada.Text_IO.Put (The_Chars (R, C));
   end loop;
   Ada.Text_IO.New_Line;
end loop;
反差帅 2024-12-16 17:48:02

Ada 中的很多问题实际上都可以追溯到你最初选择的类型。因此,就我个人而言,我建议稍微重写一下,以使您的生活更轻松:

subtype Row is String (1..Definitions.Tote_Page_Width + 1);
type Chars is array (Definitions.Number_Of_Rows) of Row;

现在您可以用以下内容编写此内容:

for I in The_Chars'range loop
    Ada.Text_IO.Put_Line (The_Chars (I));
end loop;

但是,这里仍然存在一个大问题。 Put_Line 将打印出每行中的所有字符。 Ada 字符串不是以 null 结尾的,因此如果某些行的末尾有未使用的数据,这些数据也会被打印出来。

有很多方法可以处理这个问题,但它们与处理 C 字符串的技术有很大不同。如果您尝试像处理 C 字符串一样处理 Ada 字符串,您会把自己逼疯的。

出于这个原因,我非常希望看到您的代码实际上填充The_Char数据(及其背后的基本原理)。

A lot of problems in Ada actually go back to your initial choice of types. So personally, I'd suggest a slight rewrite to make your life easier:

subtype Row is String (1..Definitions.Tote_Page_Width + 1);
type Chars is array (Definitions.Number_Of_Rows) of Row;

Now you could write this out with the following:

for I in The_Chars'range loop
    Ada.Text_IO.Put_Line (The_Chars (I));
end loop;

However, there is still a big problem here. Put_Line will print out all the characters in each row. Ada strings are not null-terminated, so if there unused data at the end of some of your lines, that will get printed too.

There are lots of ways to deal with this, but they are very different techniques than would be used to handle C strings. If you try to handle Ada strings like you'd handle C strings, you will drive yourself nuts.

For this reason, I would dearly like to see your code that actually fills The_Char with data (and the rationale behind it).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文