在这段代码中,我试图编写一个程序,根据用户的输入(索引、大小)打印出斐波那契数列。然后,程序应该打印出 Index..Size 之间的所有斐波那契数。我在编写一个计算并打印出斐波那契数列的递归时遇到了麻烦。有什么建议?
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Text_IO, Ada.Unchecked_Deallocation;
procedure Fibonacci is
type Arr is array (Positive range <>) of Integer;
type Array_Access is access Arr;
Size, Index : Positive;
Variable : Array_Access;
procedure Free is new Ada.Unchecked_Deallocation (Arr, Array_Access);
procedure Recursion (Item : Arr) is --Recursion
begin
Put_Line
(Item (Item'First)'Image); --Prints out the numbers
Recursion
(Item
(Item'First + Item'First + 1 ..
Item'Last)); --Calculating the Fibonacci numbers
end Recursion;
begin
Put ("Welcome to the …Run Code Online (Sandbox Code Playgroud) 我正在学习 Ada 编程语言,我可以承认它是强类型的。在这段代码中,我试图让用户输入 4 个整数,然后使用递归打印出这些数字。但是,我在为它编写递归子程序时遇到了麻烦,想知道我是否可以获得有关如何正确编写它的任何建议?
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
procedure Dugga is
Y: Natural;
type arr is
array (1..4) of Natural;
A:arr;
function Recurs(Item: in Natural) return Natural is
begin
if Item >= 1 then
return Get(Item ); --Error: Context requires function call, found procedure name
end if;
end Recurs;
begin
Y:=Recurs(arr); --Error: expected type "Standard.Integer. Found type: arr
end Dugga;
Run Code Online (Sandbox Code Playgroud)
警告:Program_error 可能会在运行时引发
“return”语句丢失,此语句之后
错误:在调用 get 时缺少参数“Item”的参数
我真的不明白 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;
Run Code Online (Sandbox Code Playgroud) ada ×3