打印 Ada 中的字符
我已经声明了这些:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您想要使用
Ada.Text_IO
而不仅仅是Put_Line
,并且假设Number_Of_Rows
是一个像这样的整数范围Num_Char
,那就是Assuming you want to use
Ada.Text_IO
and not justPut_Line
specifically, and assuming thatNumber_Of_Rows
is meant to be an integer range likeNum_Char
, that would beAda 中的很多问题实际上都可以追溯到你最初选择的类型。因此,就我个人而言,我建议稍微重写一下,以使您的生活更轻松:
现在您可以用以下内容编写此内容:
但是,这里仍然存在一个大问题。
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:
Now you could write this out with the following:
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).