假设我们有一个类型:
type ABC is (A, B, C);
type BAC is (B, A, C);
type CBA is (C, B, A);
有没有办法用参数 Order 声明通用包:
generic
   type Order is ...
package Pkg
    
   type Value_Type is ...
    
   type Value_Type_Array is array (Order) of Value_Type;
    
end Pkg;
实例化后我可以有 3 种不同的类型:
AA : Package_ABC.Value_Type_Array;
BB : Package_BAC.Value_Type_Array;
CC : Package_CBA.Value_Type_Array;
更新:我忘了说我想在程序中使用这些类型的值。如果我们说 X、Y 和 Z 是 Value_Type 类型的变量:
begin
   AA (A) := X;
   AA (B) := Y;
   AA (C) := Z;
   ...
   BB (B) := X;
   BB …各位编码员..
我只是一个新学习者,尝试在特定的设计环境中练习一些编程范例。最近我在Ada语言过程编程中学到了很多新的小东西,并且有很好的学习经验。但是,我自己找不到答案,为什么 Ada;在子程序声明中使用分号来分隔参数。
-- subprogram declaration - - - - - - - - -
procedure Initialize_Some_Things
   ( Result : out Integer
   ; Asset : in Integer
   ; Option: in Integer ) is
begin
   null;
end Initialize_Some_Things;
-- invoking subprogram - - - - - - - - -
Instance : Integer := 0;
Initialize_Some_Things (Instance, 9, 5);
对于来自任何其他编程语言的人来说,在子程序声明中使用分号来分隔参数显然很奇怪。更奇怪的是,当子程序的调用需要使用逗号,而不是;声明中的分号时。我搜索了相关问题一段时间,但在谷歌上找不到任何结果。无论如何,是否可以提交请求以对语言的标准规范进行这种微妙的更改?
我在 Google 上没有找到相关问题。我希望我能在这里找到。
我正在查看一些遗留的 Ada 代码。
type My_Test_Type is (<>);
type My_Table_Type is array(My_Test_Type) of Integer;
当我尝试在 GNAT 社区 IDE 中使用此代码时,出现以下错误:
ads:9:26: error: identifier expected
这是它指向的行:
type My_Test_Type is (<>);
当我在旧版 IDE 中构建代码时,它可以正常编译并运行。
有什么想法吗?
我仅在运行 x86_64-w64-mingw32 上托管的 GNAT Studio Community 2021 (20210423) 的本地 PC 上尝试过此操作。
遗留系统是 Power PC 的交叉编译器。它很旧了。
好的,这是更多背景信息。
我按照上面的代码创建了一个包,我正在 GNAT Studio 社区添加中构建和运行该包。
这是 my_generic.ads
package My_Generic is
 generic
   type My_Test_Type is (<>);
   type My_Table_Type is array(My_Test_Type) of Integer;
   --
   My_Table : My_Table_Type;
   procedure InitMyTable(My_Table : My_Table_Type);   
end My_Generic;
这是 my_generic.adb
with …为什么伏特、安培和欧姆兼容?
with ada.text_io; use ada.text_io;
                                                                    
procedure main is                                                   
    type Volts is delta 1.0 / 2.0 ** 12 range -45_000.0 .. 45_000.0;
    type Amps is delta 1.0 / 2.0 ** 16 range -1_000.0 .. 1_000.0;   
    type Ohms is delta 0.125 range 0.0 .. 1.0E8;                    
                                                                    
    V : Volts := 1.0;                                               
    A : Amps := 1.0;                                                
    R1 : Ohms := 1.0;                                               
    R2 : Ohms := 1.0;                                               
                                                                   
begin                                             
                     
    v := A * (R1 + R2);
                       
    put_line(V'Img);   
                       
end main;  
如果类型被定义为new Float我在编译期间收到以下异常:
main.adb:22:12: error: …正如问题所说,我想在Ada中创建一个动态分配的数组.像C++这样的东西std::vector,我不希望将数组的长度存储在一个单独的变量中,就像在这里完成一样.由于Ada支持泛型,是否可以std::vector在Ada中创建类似的功能?
我需要创建一些处理任意大小矩阵的函数.我熟悉这里使用的declare语法,但这是为了大学任务,我的教授告诉我,使用'declare'有一些矫枉过正.我在网上找不到任何相关内容,有什么帮助吗?
基本上我想通过键盘获得矩阵大小,然后使用生成的矩阵,我坚持declare 
目前我有:
type myMatrix is array (Natural range <>, Natural range <>) of Integer; 
type myVector is array (Natural range <>) of Integer;       
我用它作为:
procedure Lab1 is
 begin
 declare A, B: myVector(1..5):=
 (3, 14, 15, 92, 6);
它不允许在运行时指定大小,并且:
  declare 
    int1:Integer:=generate_random_number(50)+2; 
    int3:Integer:=generate_random_number(50)+2; -- +2 so we don't get length/size 0 or 1
    X, Y:myVector(1..int1):=(others=>generate_random_number(20));
    MT:myMatrix(1..int1, 1..int3):=(others =>(others=>generate_random_number(10))); -- 'others' used for all the unmentioned elements, http://rosettacode.org/wiki/Array_Initialization
    MS:myMatrix(1..int3, 1..int1):=(others =>(others=>generate_random_number(10))); 
    F3_result:myMatrix(1..int1, 1..int1);  
  begin 
    F3_result:=F3(X, Y, MT, …我正在尝试返回一个全局变量的String值,并希望稍后在一个过程中使用使用它的函数.
function get_name return String
is begin
Put_line("Your name?");
 Get(name); -- name is in "globals"
 put(name);
return name;
end get_name;
包文件=
package globals
is
name : String(1..20) ;
end globals; 
这里是函数中使用的"获取"=
       procedure Get (Item : out String);
现在,如果我在一个过程中使用fonction,它会编译但是=
在启动时,没有得到执行,程序"创建"一个"跳过"线!!?
那么,是否可以使用此过程获取函数?
你如何调用包含它的函数?
我有一段代码(见下文)从作为命令行参数给出的文件中读取数据。我想添加对能够从管道读取输入的支持。例如,当前版本将数据读取为main <file_name>,而它也应该可以做一些 line cmd1 | main。这是从文件中读取数据的来源:
procedure main is
    File : Ada.Text_IO.File_Type;
begin
   if Ada.Command_Line.Argument_Count /= 1 then
      return;
   else
      Ada.Text_IO.Open (
         File => File,
         Mode => In_File,
         Name => Ada.Command_Line.Argument (1));
      while (not Ada.Text_IO.End_Of_File (File)) loop
         -- Read line using Ada.Text_IO.Get_Line
         -- Process the line
      end loop;
      Ada.Text_IO.Close (File);
end main;
如果我理解正确的话,管道只是 Ada 中的一种非常规文件类型。但是我该如何处理呢?
我对Ada很陌生,而且我很难弄清楚如何使用这些结构.当他们被转换成Ada时,下面的C结构会是什么样子?
这些是我的结构:
struct dataT
{
    int m;
};
struct stack
{
    int top;
    struct dataT items[STACKSIZE];
} st;
如何在Ada中表达这种说法?
st.items[st.top].m
编译用Ada编程语言编写的代码时,会出现下一条消息:missing operand in deg:=degree();.  为什么以及如何解决此问题?
With Ada.Text_IO; Use Ada.Text_IO;
With Ada.Integer_Text_IO; Use Ada.Integer_Text_IO;
With Ada.Strings.Unbounded; Use Ada.Strings.Unbounded;
procedure polynomial is
    -- constructs a new polynomial term of the form a * x^b:
    function Polynomial(a,b : Integer) return Integer is
        type coef is array(Integer range <>)of Integer;
        type deg is new Integer;
        type c is new Integer;
    begin
        coef:=new int(b+1);
        coef(b):=a;
        deg:=degree(); --missing operand
        return a;
    end Polynomial;
我真的不明白 Ada 编程中的 for 循环,即使我可以在 C++ 中使用它。例如,在 Ada 中,您应该编写,for I in 0..7 loop但是如果您编写for I in 0..year loop,则会出现编译器错误,提示year不兼容。那么如果我想从特定年份循环到另一个特定年份,我应该如何在 Ada 中使用 for 循环?假设我希望我的代码看起来像这样。但是,这不会编译。
  with Ada.Text_IO;                    use Ada.Text_IO;
  with Ada.Integer_Text_IO;            use Ada.Integer_Text_IO;
  with Ada.Float_Text_IO;              use Ada.Float_Text_IO;
 procedure Population is
  Min, Max: Integer;
  type percent is range Min..Max;
begin
for I in percent'Range loop
    Put_Line("I is "& percent'Image(I));
 end loop;
 end Population;