简体   繁体   English

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

[英]How to write a population-growth table in Ada?

** I want my code to display the following table for the population but it seems that my code is not even close to this. ** 我希望我的代码为人口显示下表,但似乎我的代码甚至不接近于此。 So how can I make my code right?那么我怎样才能使我的代码正确呢? my biggest problem here is using a range-based for loop.我最大的问题是使用基于范围的 for 循环。 This table shows the population for A and B and their population growth(in percent) and asks the user to enter some data and when the population b bypass population a then the loop should stop.该表显示了 A 和 B 的人口及其人口增长(百分比),并要求用户输入一些数据,当人口 b 绕过人口 a 时,循环应该停止。 The sample code:**示例代码:**

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.

My code:我的代码:

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,2,0); New_Line;
     Put(Growth_2, 17,2,0); New_Line;
     Put(Population_B + (Population_B * Growth_B/100.0),19,2,0);
     end loop;
end loop;
end Population;

Your problem is indeed using a range-based for loop.您的问题确实是使用基于范围的 for 循环。 There are other forms.还有其他形式。

I'd probably go for something like我可能会去寻找类似的东西

--  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;

You need a more complete concept of the problem you are trying to solve.您需要对要解决的问题有一个更完整的概念。

The problem assumes a constant population growth rate for each country.该问题假设每个国家的人口增长率不变。 While this assumption simplifies the problem it is far from realistic.虽然这个假设简化了问题,但它远非现实。

Either country A or country B could start with the smaller initial population. A 国或 B 国都可以从较小的初始人口开始。 If the country starting with the smaller population also has the lowest population growth rate the smaller country will never surpass the larger country in population.如果从人口较少的国家开始的人口增长率也最低,那么较小的国家在人口上永远不会超过较大的国家。 How will your program deal with the possibility that either country could initially have the larger population?您的计划将如何处理任一国家最初可能拥有更多人口的可能性?

The following program deals with some of these problems but not all of them.下面的程序处理其中的一些问题,但不是全部。 Perhaps you can get an idea how to deal with all the problems by studying the following program.也许你可以通过学习下面的程序来了解如何处理所有问题。

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, Aft => 2, Fore => 8, Exp => 0);
      Put(Item => Cnt_B.Increase, Aft => 2, Fore => 8, Exp => 0);
      Put(Item => Cnt_B.Population, Aft => 2, Fore => 8, 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, Aft => 2, Fore => 8, Exp => 0);
      Put(" ----------");
      Put(Item => Country_B.Population, Aft => 2, Fore => 8, 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;

EDIT: Following is a version of the above program that does not use access types.编辑:以下是不使用访问类型的上述程序的一个版本。

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, Aft => 2, Fore => 8, Exp => 0);
      Put (Item => A.Population, Aft => 2, Fore => 8, Exp => 0);
      Put (Item => B.Increase, Aft => 2, Fore => 8, Exp => 0);
      Put (Item => B.Population, Aft => 2, Fore => 8, 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, Aft => 2, Fore => 8, Exp => 0);
   Put (" ----------");
   Put (Item => Country_B.Population, Aft => 2, Fore => 8, 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_A, Country_B);
      end loop;
      Put_Line
        ("In" & Year'Image & " country B bypassed country A in population.");
   end if;

end No_Access;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM