gpt4 book ai didi

probability - 有没有更好的方法来编程这个,

转载 作者:行者123 更新时间:2023-12-01 23:12:15 26 4
gpt4 key购买 nike

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;
with Ada.Numerics.Float_Random;
with Ada.Numerics.Discrete_Random;

procedure Probability is

subtype My_Characters is
Character range 'a' .. 'z';

package My_Random_Character_Package is
new Ada.Numerics.Discrete_Random(My_Characters);

use My_Random_Character_Package;

Gen_1 : My_Random_Character_Package.Generator;




procedure My_Character_Program(First_Character, Second_Character : in Character) is

begin

Put(First_Character);
Put(" ");
Put(Second_Character);
New_Line;
Put("All characters between ");
Put(First_Character);
Put(" and ");
Put(Second_Character);
Put(":");
for C in Character range First_Character .. Second_Character loop
Put(" ");
Put(A);
end loop;

end My_Character_Program;

procedure Part_2 is

First_Character, Second_Character : Character;

begin

Put_Line("PART 2:");
Put_Line("Two random characters from a to z will now be generated.");
Put("Random characters: ");
First_Character := Random(Gen_1);
Second_Character := Random(Gen_1);

if First_Character > Second_Character then
My_Character_Program(Second_Character, First_Character);
else
My_Character_Program(First_Character, Second_Character);
end if;

end Part_2;



begin

-- Reset(Gen_1);
Part_2;
New_Line(2);


end Probability;

以下程序将在 A 和 Z 之间随机选择两个字母,然后将它们之间的所有字母打出来。因此,例如,如果它随机生成字母

d 和 g

它会输出

d e f g

但是我需要你的帮助。即使我的程序可以运行,我也不认为它执行得那么好,尤其是第 2 部分。有没有办法可以将我的“第 2 部分程序”的一部分放入“我的角色程序程序”中?如您所见,我交换了第一个和第二个字符。我不能用其他方式做到这一点吗?

我很感激我能得到的任何帮助。

最佳答案

看看你的“工具箱”——Ada 以 subtypeSucc/Pred 属性的形式提供了你想要的,您已经使用过。

procedure My_Character_Program(First, Second : in Character) is
Subtype Middle is Character range
Character'Succ(First)..Character'Pred(Second);
begin
Put_Line( First & ' ' & Second );

Put("All characters between " & First & " and " & Second & ':');
for C in Middle loop
Put(' ' & C);
end loop;
Put_Line( "." );
end My_Character_Program;

关于第 2 部分,请允许我建议一个更好的方法是使用内联声明 block 和属性/重命名。

--...
First_Character := Random(Gen_1);
Second_Character := Random(Gen_1);

Declare
First : Character renames Character'Min(First_Character,Second_Character);
Second : Character renames Character'Max(First_Character,Second_Character);
Begin
My_Character_Program(First, Second);
End;

关于probability - 有没有更好的方法来编程这个,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69678807/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com