ADA中的输入口罩
-- Date: 11/06/2022
with Ada.Text_IO; Use Ada.Text_Io;
procedure Masques is
type XX is record
X1 : character range 'A'..'D';
X2 : character range 'E'..'H';
X3 : character range 'I'..'L';
end record;
begin
Get_Line (XX);
end Masques;
我正在尝试编写某种输入蒙版,以控制输入。 当然,以上示例不会编译,因为get_line无法接受记录。 当然,我们可以编写一个操作以将字符放在一起以创建字符串,通过get或get_immediate。
- 但这个想法是使用语言输入来控制输入&通过例外来捕获错误。
- 几年前,对于我的回忆,我记得有人这样做了,但我无法写。 感谢您的帮助。
-- Date: 11/06/2022
with Ada.Text_IO; Use Ada.Text_Io;
procedure Masques is
type XX is record
X1 : character range 'A'..'D';
X2 : character range 'E'..'H';
X3 : character range 'I'..'L';
end record;
begin
Get_Line (XX);
end Masques;
I'm trying to write some sort of input masks to control the inputs as we do in IT.
Of course the above example doesn't compile because Get_Line can't accept a record.
Of course we can write an operation to put the characters together to create a string, thru a get or get_immediate.
-But the idea would be to use the language typing to control the input & to trap the errors by an exception by example.
-Some years ago, to the best of my recollection, i remember someone did this, but I'm unable to write it..
Thanks for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面证明了一种可能性:
One possibility is demonstrated below:
根据您要使用XX的操作,有很多选择可以处理类似的方法。例如:
如果
last< 3
或正在读取的行是> 3个字符。通常,您将需要定义自己的
get_line
来执行您的要求:如果
line> line> line'length< 3
或line
的前3个字符中的任何一个都是无效的,最后丢弃了任何额外的字符。通常,在处理可能无效的输入时,最好用
get_line
函数输入整个行,然后将其解析。然后,检查line'length = 3
之类的东西是一个简单的事情,或使用自定义异常。There are a lot of options for dealing with something like this, depending on what you're going to do with an XX. For example:
This may not do what you want if
Last < 3
or the line being read is > 3 Characters.Generally, you will want to define your own
Get_Line
to enforce your requirements:This raises
Constraint_Error
ifLine'Length < 3
or any of the first 3 Characters ofLine
are invalid, and discards any extra Characters at the end.Often, when dealing with input that may be invalid it is a good idea to input an entire line with the
Get_Line
function and then parse it, as here. Then it's a simple matter to check things likeLine'Length = 3
or use a custom exception.