如何在Ada中编写人口增长表?

问题描述

**我希望我的代码显示针对人口的下表,但似乎我的代码不是 甚至接近这个。那么如何正确编写代码?我最大的问题是使用基于范围的 for循环。该表显示了A和B的人口及其人口增长(百分比), 要求用户输入一些数据,当总体b绕过总体a时,循环应 停。示例代码:**

Enter start year: 1976
Enter country A's population (in million): 200
Enter country A's population increase (in%): 10.0
Enter country B population (in million): 100
Enter country B population increase (in%): 50.0
Year Increase A  Population A  Increase B  Population B
1976  ----        200.00        ------       100.00
1977 20.00        220.00        50.00        150.00
1978 22.00        242.00        75.00        225.00
1979 24.20        266.20        112.50       337.50
In 1979,country B bypassed country A in population.

我的代码:

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
x,Population_A,Population_B:Float;
Growth_A,Growth_B,Growth_1,Growth_2: Float;
begin
Put("Write in the year: "); Get(x);
Put("Population_A: "); Get(Population_A);
Put("Population_B: "); Get(Population_B);
Put("Population grow for A: "); Get(Growth_A);
Put("Population growth for B: "); Get(Growth_B); New_Line;

Put("Year   Growth A   Population A   Growth B    Population B ");
for I in 1900..2018 loop
  Put(Integer(I)); New_Line;
  while Population_B < Population_A loop
     Growth_1 := Growth_A +1.0;
     Growth_2 := Growth_B +1.0;
     Put(Growth_1,13,2,0); New_Line;
     Put(Population_A + (Population_A * Growth_A/100.0),15,0); New_Line;
     Put(Growth_2,17,0); New_Line;
     Put(Population_B + (Population_B * Growth_B/100.0),19,0);
     end loop;
end loop;
end Population;

解决方法

您的问题确实是在使用基于范围的for循环。还有其他形式。

我可能会选择

--  read in the data
--  check that there is actually a solution
--  (B’s population not already greater than A’s)

Year := -- value read from data,e.g. 1976
loop
   Year := Year + 1;
   Population_Of_A := -- calculate it
   Population_Of_B := -- calculate it
   exit when Population_Of_B > Population_Of_A;
end loop;
,

您需要对要解决的问题有更完整的了解。

该问题假设每个国家的人口增长率恒定。尽管此假设简化了问题,但远非现实。

A国或B国都可以从较小的初始人口开始。如果从人口较少的国家开始,人口增长率也最低,那么人口较小的国家将永远不会超过人口大的国家。您的计划将如何处理这两个国家最初可能会有更多人口的可能性?

以下程序处理其中一些问题,但并非全部。通过研究以下程序,也许您可​​以了解如何解决所有问题。

with Ada.Text_Io; use Ada.Text_IO;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

procedure Main is
   Year : Positive;

   type Country (Id : Character) is record
      Population : Float;
      Rate       : Float;
      Increase   : Float;
   end record;

   type Country_Access is access all Country;

   Country_A : aliased Country('A');
   Country_B : Aliased Country('B');
   Smaller : Country_Access;
   Larger  : Country_Access;

   procedure Display(Yr : Positive; Cnt_A : Country; Cnt_B : Country) is
   begin
      Put(Item => Yr,Width => 4);
      Put(Item => Cnt_A.Increase,Aft => 2,Fore => 8,Exp => 0);
      Put(Item => Cnt_A.Population,Exp => 0);
      Put(Item => Cnt_B.Increase,Exp => 0);
      Put(Item => Cnt_B.Population,Exp => 0);
      New_Line;
   end Display;

begin
   Put("Enter start year: ");
   Get(Year);
   Put("Enter country A's population (in million): ");
   Get(Country_A.Population);
   Put("Enter country A's population increase (in %): ");
   get(Country_A.Rate);
   Country_A.Rate := (Country_A.Rate / 100.0);
   Put("Enter country B's population (in million): ");
   Get(Country_B.Population);
   Put("Enter country B's population increase (in %): ");
   Get(Country_B.Rate);
   Country_B.Rate := (Country_B.Rate / 100.0);
   if Country_A.Population < Country_B.Population then
      Smaller := Country_A'Access;
      Larger := Country_B'Access;
   else
      Smaller := Country_B'Access;
      Larger := Country_A'Access;
   end if;

   Put_Line("Year Increase A Population A Increase B Population B");
   Put(Item => Year,Width => 4);
      Put(" ----------");
      Put(Item => Country_A.Population,Exp => 0);
      Put(" ----------");
      Put(Item => Country_B.Population,Exp => 0);
   New_Line;
   while Smaller.Population <= Larger.Population loop
      Year := Year + 1;
      Country_A.Increase := Country_A.Population * Country_A.Rate;
      Country_A.Population := Country_A.Population + Country_A.Increase;
      Country_B.Increase := Country_B.Population * Country_B.Rate;
      Country_B.Population := Country_B.Population + Country_B.Increase;
      Display(Yr    => Year,Cnt_A => Country_A,Cnt_B => Country_B);
   end loop;
   Put_Line("In" & Year'Image & " country " & Smaller.Id &
              " bypassed country " &
              Larger.Id & " in population.");
end Main;

编辑: 以下是上述程序的不使用访问类型的版本。

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

procedure No_Access is
   Year : Positive;

   type Country is record
      Population : Float;
      Rate       : Float;
      Increase   : Float;
   end record;

   Country_A : Country;
   Country_B : Country;

   procedure Increase
     (Yr : in out Positive; A : in out Country; B : in out Country)
   is
   begin
      Yr           := Yr + 1;
      A.Increase   := A.Population * A.Rate;
      A.Population := A.Population + A.Increase;
      B.Increase   := B.Population * B.Rate;
      B.Population := B.Population + B.Increase;

      Put (Item => Yr,Width => 4);
      Put (Item => A.Increase,Exp => 0);
      Put (Item => A.Population,Exp => 0);
      Put (Item => B.Increase,Exp => 0);
      Put (Item => B.Population,Exp => 0);
      New_Line;
   end Increase;

begin
   Put ("Enter start year: ");
   Get (Year);
   Put ("Enter country A's population (in million): ");
   Get (Country_A.Population);
   Put ("Enter country A's population increase (in %): ");
   Get (Country_A.Rate);
   Country_A.Rate := (Country_A.Rate / 100.0);
   Put ("Enter country B's population (in million): ");
   Get (Country_B.Population);
   Put ("Enter country B's population increase (in %): ");
   Get (Country_B.Rate);
   Country_B.Rate := (Country_B.Rate / 100.0);
   Put_Line ("Year Increase A Population A Increase B Population B");
   Put (Item => Year,Width => 4);
   Put (" ----------");
   Put (Item => Country_A.Population,Exp => 0);
   Put (" ----------");
   Put (Item => Country_B.Population,Exp => 0);
   New_Line;
   if Country_A.Population < Country_B.Population then
      while Country_A.Population <= Country_B.Population loop
         Increase (Year,Country_A,Country_B);
      end loop;
      Put_Line
        ("In" & Year'Image & " country A bypassed country B in population.");
   else
      while Country_B.Population <= Country_A.Population loop
         Increase (Year,Country_B);
      end loop;
      Put_Line
        ("In" & Year'Image & " country B bypassed country A in population.");
   end if;

end No_Access;

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...