ADA中的输入口罩

发布于 2025-02-06 09:04:44 字数 532 浏览 1 评论 0原文

-- 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 技术交流群。

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

发布评论

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

评论(2

强者自强 2025-02-13 09:04:45

下面证明了一种可能性:

package input_masking is
   type Account_Number is private;
   function Get_Account return Account_Number;
   procedure Display (Account : Account_Number);
   Acct_Exception : Exception;
private
   subtype F1 is character range 'A' ..'D';
   subtype F2 is character range 'E' .. 'H';
   subtype F3 is character range 'I' .. 'L';
   subtype F4 is Integer range 1111 .. 9999;
   subtype F5 is character range 'S' .. 'U';
   type Account_Number is record
      Field_1 : F1;
      Field_2 : F2;
      Field_3 : F3;
      Field_4 : F4;
      Field_5 : F5;
   end record;
end input_masking;

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Exceptions; use Ada.Exceptions;

package body input_masking is

   -----------------
   -- Get_Account --
   -----------------

   function Get_Account return Account_Number is
      Result : Account_Number;
      Input : String(1..8);
   begin
      Input := Get_Line;
      begin
         Result.Field_1 := Input(1);
      exception
         when The_Error: Others =>
            Put_Line("Invalid value in Field_1");
            Put_Line(Exception_Message(The_Error));
            raise Acct_Exception;
      end;
       begin
         Result.Field_2 := Input(2);
      exception
         when The_Error: Others =>
            Put_Line("Invalid value in Field_2");
            Put_Line(Exception_Message(The_Error));
            raise Acct_Exception;
      end;
       begin
         Result.Field_3 := Input(3);
      exception
         when The_Error: Others =>
            Put_Line("Invalid value in Field_3");
            Put_Line(Exception_Message(The_Error));
            raise Acct_Exception;
      end;
       begin
         Result.Field_4 := F4'Value(Input(4..7));
      exception
         when The_Error: Others =>
            Put_Line("Invalid value in Field_4");
            Put_Line(Exception_Message(The_Error));
            raise Acct_Exception;
      end;
       begin
         Result.Field_5 := Input(8);
      exception
         when The_Error: Others =>
            Put_Line("Invalid value in Field_5");
            Put_Line(Exception_Message(The_Error));
            raise Acct_Exception;
      end;
      return result;
   end Get_Account;

   -------------
   -- Display --
   -------------

   procedure Display (Account : Account_Number) is
      Temp : string(1..3);
      Temp2 : string(1..5);
   begin
      Temp := Account.Field_1'Image;
      Put(Temp(2));
      Temp := Account.Field_2'Image;
      Put(Temp(2));
      Temp := Account.Field_3'Image;
      Put(Temp(2));
      temp2 := Account.Field_4'Image;
      Put(Temp2(2..5));
      Temp := Account.Field_5'Image;
      Put(Temp(2));
      New_Line;
   end Display;

end input_masking;


with Input_Masking; use Input_Masking;

procedure Main is
   X : Account_Number;
begin
   X := Get_Account;
   Display(X);
end Main;

One possibility is demonstrated below:

package input_masking is
   type Account_Number is private;
   function Get_Account return Account_Number;
   procedure Display (Account : Account_Number);
   Acct_Exception : Exception;
private
   subtype F1 is character range 'A' ..'D';
   subtype F2 is character range 'E' .. 'H';
   subtype F3 is character range 'I' .. 'L';
   subtype F4 is Integer range 1111 .. 9999;
   subtype F5 is character range 'S' .. 'U';
   type Account_Number is record
      Field_1 : F1;
      Field_2 : F2;
      Field_3 : F3;
      Field_4 : F4;
      Field_5 : F5;
   end record;
end input_masking;

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Exceptions; use Ada.Exceptions;

package body input_masking is

   -----------------
   -- Get_Account --
   -----------------

   function Get_Account return Account_Number is
      Result : Account_Number;
      Input : String(1..8);
   begin
      Input := Get_Line;
      begin
         Result.Field_1 := Input(1);
      exception
         when The_Error: Others =>
            Put_Line("Invalid value in Field_1");
            Put_Line(Exception_Message(The_Error));
            raise Acct_Exception;
      end;
       begin
         Result.Field_2 := Input(2);
      exception
         when The_Error: Others =>
            Put_Line("Invalid value in Field_2");
            Put_Line(Exception_Message(The_Error));
            raise Acct_Exception;
      end;
       begin
         Result.Field_3 := Input(3);
      exception
         when The_Error: Others =>
            Put_Line("Invalid value in Field_3");
            Put_Line(Exception_Message(The_Error));
            raise Acct_Exception;
      end;
       begin
         Result.Field_4 := F4'Value(Input(4..7));
      exception
         when The_Error: Others =>
            Put_Line("Invalid value in Field_4");
            Put_Line(Exception_Message(The_Error));
            raise Acct_Exception;
      end;
       begin
         Result.Field_5 := Input(8);
      exception
         when The_Error: Others =>
            Put_Line("Invalid value in Field_5");
            Put_Line(Exception_Message(The_Error));
            raise Acct_Exception;
      end;
      return result;
   end Get_Account;

   -------------
   -- Display --
   -------------

   procedure Display (Account : Account_Number) is
      Temp : string(1..3);
      Temp2 : string(1..5);
   begin
      Temp := Account.Field_1'Image;
      Put(Temp(2));
      Temp := Account.Field_2'Image;
      Put(Temp(2));
      Temp := Account.Field_3'Image;
      Put(Temp(2));
      temp2 := Account.Field_4'Image;
      Put(Temp2(2..5));
      Temp := Account.Field_5'Image;
      Put(Temp(2));
      New_Line;
   end Display;

end input_masking;


with Input_Masking; use Input_Masking;

procedure Main is
   X : Account_Number;
begin
   X := Get_Account;
   Display(X);
end Main;
深白境迁sunset 2025-02-13 09:04:45

根据您要使用XX的操作,有很多选择可以处理类似的方法。例如:

subtype XX is String (1 .. 3) with
   Dynamic_Predicate => XX (1) in 'A' .. 'D' and
                        XX (2) in 'E' .. 'H' and
                        XX (3) in 'I' .. 'L';

YY : XX;
Last : Natural;
...
Ada.Text_IO.Get_Line (Item => YY, Last => Last);

如果last< 3或正在读取的行是> 3个字符。

通常,您将需要定义自己的get_line来执行您的要求:

procedure Get_Line (Item : out XX) is
   Line : constant String := Ada.Text_IO.Get_Line;
begin -- Get_Line
   Item.X1 := Line (line'First);
   Item.X2 := Line (line'First + 1);
   Item.X3 := Line (line'First + 2);
end Get_Line;

如果line> line> line'length< 3line的前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:

subtype XX is String (1 .. 3) with
   Dynamic_Predicate => XX (1) in 'A' .. 'D' and
                        XX (2) in 'E' .. 'H' and
                        XX (3) in 'I' .. 'L';

YY : XX;
Last : Natural;
...
Ada.Text_IO.Get_Line (Item => YY, Last => Last);

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:

procedure Get_Line (Item : out XX) is
   Line : constant String := Ada.Text_IO.Get_Line;
begin -- Get_Line
   Item.X1 := Line (line'First);
   Item.X2 := Line (line'First + 1);
   Item.X3 := Line (line'First + 2);
end Get_Line;

This raises Constraint_Error if Line'Length < 3 or any of the first 3 Characters of Line 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 like Line'Length = 3 or use a custom exception.

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