2016-05-07 32 views
1

我必须制作一个程序,它有一个1000毫秒的有效等待时间,然后在while循环内的那段时间内增加一个变量。然后,这个变量必须在每个循环中初始化为0。使用Ada.Real_Time每1000毫秒增量变量

有人可以给我一个线索如何做到这一点?我有这个代码,但它根本不工作,只是编译错误。

with Ada.Text_IO; use Ada.Text_IO; 
with Ada.Real_Time; use Ada.Real_Time; 
package body waittime is 
    task body periodictime is 

     use type Time; 
     use type Time_Span; 

     Poll_Time : Ada.Real_Time.Time := 5; -- time to start polling 
     WaitVar : Natural := 0; 
     WaitTime : constant Time_Span := Milliseconds (1000); 
    begin 
     loop 
      delay until Poll_Time; 
      Poll_Time = Poll_Time + WaitTime; 
      WaitVar := WaitVar+1; 
      Put_Line (WaitVar); 
     end loop; 
    end periodictime; 
end waittime; 
+2

阿达,阿达,阿达,艾达 - 我累的人不知道怎么写的名字。你可以把你得到的错误 - 会帮助你更容易和更快。 – darkestkhan

+1

通常最好说错误信息实际上是什么,而不是期望我们下载你的代码,编写一个包规范,然后自己运行编译器。 –

+0

什么“然后,这个变量必须在每个循环中初始化为0”,是什么意思? –

回答

-1

这里我给你写了一些示例代码:http://www.filedropper.com/ada-periodic。您可以通过编译此即UNIX终端* gnatmake 名.adb./main

运行中有三个文件:main.adb,periodic.ads和periodic.adb 在main.adb有程序启动程序。这个过程有无限循环。并且此文件包括周期性包(通过使用与代码周期

periodic.ads有包的声明。此包装中只有一项任务,即自动启动主要程序。

periodic.adb有一个包的定义。有一个任务定义包含无限循环,延迟时间为,直到结构。你必须获得当前的时钟时间,并在你想等待的时间段内增加它。由于这个操作,你等待一段时间,做点什么,然后等下一次。

2

已经写入的(OK,明显)封装规格,与-gnatfl编译给出

1. with Ada.Text_IO; use Ada.Text_IO; 
2. with Ada.Real_Time; use Ada.Real_Time; 
3. package body waittime is 
4.  task body periodictime is 
5.   use type Time; 
6.   use type Time_Span; 
7. 
8.   Poll_Time : Ada.Real_Time.Time := 5; -- time to start polling 
               | 
    >>> expected private type "Ada.Real_Time.Time" 
    >>> found type universal integer 

9.   WaitVar : Natural := 0; 
10.   WaitTime : constant Time_Span := Milliseconds (1000); 
11.  begin 
12.   loop 
13.    delay until Poll_Time; 
14.    Poll_Time = Poll_Time + WaitTime; 
          | 
    >>> "=" should be ":=" 

15.    WaitVar := WaitVar+1; 
16.    Put_Line (WaitVar); 
       1   3 
    >>> no candidate interpretations match the actuals: 
    >>> missing argument for parameter "Item" in call to "put_line" declared at a-textio.ads:259 
    >>> expected type "Standard.String" 
    >>> found type "Standard.Integer" 
    >>> ==> in call to "Put_Line" at a-textio.ads:263 

17.   end loop; 
18.  end periodictime; 
19. end waittime; 

在线路16上的误差是Put_Line期望一个String; Ada不会即时进行转换。您可以通过说Natural’Image (Waitvar)(标准Ada)或GNAT扩展Waitvar’Img来产生字符串表示。

第14行的错误很容易修复。

第8行的错误解释了它的要求,但要解决它需要另一个更改。我想你应该

  1. 初始化Ada.Real_Time.Clock
  2. 交换线13和14