通过编写递归程序(Ada)绘制标尺

通过编写递归程序(Ada)绘制标尺,ada,Ada,通过使用递归,我需要编写一个程序,允许用户编写标尺的多少部分以及每个部分的长度。然后,基于这些信息,程序应该能够显示用户想要的标尺。在我的代码中,我成功地显示了标尺的大小。但是,我有一些困难,编写一个显示每个部分长度的递归。似乎我的第二个函数(函数计数)在某种程度上是不可见的,因为它不影响代码,不管它是否存在 with Ada.Text_IO; use Ada.Text_IO; with Ada.Integer_Text_IO; use

通过使用递归,我需要编写一个程序,允许用户编写标尺的多少部分以及每个部分的长度。然后,基于这些信息,程序应该能够显示用户想要的标尺。在我的代码中,我成功地显示了标尺的大小。但是,我有一些困难,编写一个显示每个部分长度的递归。似乎我的第二个函数(函数计数)在某种程度上是不可见的,因为它不影响代码,不管它是否存在

with Ada.Text_IO;                    use Ada.Text_IO;
with Ada.Integer_Text_IO;            use Ada.Integer_Text_IO;

 Procedure Ruler is
  section, length,Y: Positive;

  function Factorial(value, value1: Natural) return Natural is   --length of 
 begin                                                           --the ruler   
  Put("|");
  if value > 1 then
  return Factorial((value * value1)-1,1);
  end if; 
  end Factorial;

  function Count(value,value1: Natural) return natural is   --Length of each 
  Calc: Natural;                                            --section     
begin
  Calc:= (value*value1)/value;    
  Set_Col(Positive_Count(Calc));
  Put("|");
  return Count(1, calc+value1); 
  end Count;   
begin
 Put("The number of sections and the length of each section: ");
 Get(section); Get(length);
 Y:= Factorial(section, length);
 Y:= Count(section, length);
 end Ruler; 
这是一把尺子的图片,共有4节,每节长10厘米:
所以,我不确定为什么需要递归,但下面是一个例子。我必须承认这不是最简单的代码。递归只是代码的一小部分(请参见
Put\u Rec
函数)

标尺。广告

package Rulers is

   procedure Put_Ruler (Length, Minor_Tick, Major_Tick : Positive);
   --  Draws a ruler of a given length.

end Rulers;
统治者。亚洲开发银行

with Ada.Strings.Fixed;
with Ada.Text_IO;        
with Ada.Integer_Text_IO;

package body Rulers is
   
   type On_Step_Fcn is access procedure (Buffer : out String; 
                                         Count  : Natural);
      
   --------------------
   -- Put_Ruler_Part --
   --------------------
      
   procedure Put_Ruler_Part 
     (Step     : Integer;
      Last     : Integer;
      On_First : String;
      On_Step  : On_Step_Fcn;
      On_Last  : String)
   is      
      use Ada.Text_IO;
      
      procedure Put_Rec (Count : Natural) is
         Buffer : String (1 .. Step);
      begin
         if Count <= Last then         --  Check continuation predicate.
            On_Step (Buffer, Count);
            Put (Buffer);
            Put_Rec (Count + Step);    --  Recurse.
         end if;
      end Put_Rec;
     
   begin      
      Put (On_First);
      Put_Rec (Step);                  --  Initiate recursion.
      Put (On_Last);
      New_Line;
   end Put_Ruler_Part;      
      
   
   ---------------------
   --  Step functions --
   ---------------------

   --  Functions that will be called on each recursion. The size of the 
   --  buffer is dictated by the (step-size known by the) calling function.

   use Ada.Strings;
   use Ada.Strings.Fixed;
      
   procedure Image_Minor_Tick (Buffer : out String; Count : Natural) is
      pragma Unreferenced (Count);
   begin
      Move ("|", Buffer, Justify => Right);
   end Image_Minor_Tick;
   
      
   procedure Image_Major_Tick (Buffer : out String; Count : Natural) is
      pragma Unreferenced (Count);
   begin
      Move ("|", Buffer, Justify => Right);
   end Image_Major_Tick;
            
      
   procedure Image_Number (Buffer : out String; Count : Natural) is 
   begin
      Move (Count'Image, Buffer, Justify => Right, Drop => Left);
   end Image_Number;
   
   
   ---------------
   -- Put_Ruler --
   ---------------
   
   procedure Put_Ruler (Length, Minor_Tick, Major_Tick : Positive) is
   begin
      
      --  Minor scale.
      Put_Ruler_Part
        (Step     => Minor_Tick,
         Last     => Length,
         On_First => "|",
         On_Step  => Image_Minor_Tick'Access,
         On_Last  => "");        
        
      --  Major scale.
      Put_Ruler_Part
        (Step     => Major_Tick,
         Last     => Length,
         On_First => "|",
         On_Step  => Image_Major_Tick'Access,
         On_Last  => "");
      
      --  Numbers.
      Put_Ruler_Part
        (Step     => Major_Tick,
         Last     => Length,
         On_First => "0",
         On_Step  => Image_Number'Access,
         On_Last  => " cm");
      
   end Put_Ruler;

end Rulers;
with Ada.Text_IO; use Ada.Text_IO;
with Rulers;      use Rulers;

procedure Main is
begin

   Rulers.Put_Ruler
     (Length     => 40,
      Minor_Tick => 1,
      Major_Tick => 10);
   New_Line;

   Rulers.Put_Ruler
     (Length     => 40,
      Minor_Tick => 2,
      Major_Tick => 5);
   New_Line;

   Rulers.Put_Ruler
     (Length     => 40,
      Minor_Tick => 1,
      Major_Tick => 1);
   New_Line;

end Main;
输出

|||||||||||||||||||||||||||||||||||||||||
|         |         |         |         |
0        10        20        30        40 cm

| | | | | | | | | | | | | | | | | | | | |
|    |    |    |    |    |    |    |    |
0    5   10   15   20   25   30   35   40 cm

|||||||||||||||||||||||||||||||||||||||||
|||||||||||||||||||||||||||||||||||||||||
01234567890123456789012345678901234567890 cm

伯爵该怎么办?由于某种原因,您需要计算:=(值*value1)/value;这就是Calc=value1,画一个冒号并进行无限递归。您的计数没有指定显式的返回值,所以当您返回另一个“计数”时,这不应该给您任何信息吗?