【问题标题】:Ada beginner Stack programAda 初学者 Stack 程序
【发布时间】:2013-04-17 11:06:50
【问题描述】:

基本上,我有 2 个文件(.adb 和 .ads)。我对 Ada 以及如何编译 2 个文件完全陌生。该程序是一个基本的堆栈实现。编译 .adb 文件时出现此编译错误。

$ gcc -c test_adt_stack.adb
abstract_char_stack.ads:22:01: end of file expected, file can have only one compilation unit

我拥有的 2 个文件是: abstract_char_stack.ads

-----------------------------------------------------------
package Abstract_Char_Stack is
  type Stack_Type is private;
  procedure Push(Stack : in out Stack_Type;
                 Item  : in Character);
  procedure Pop (Stack : in out Stack_Type;
                 Char  : out Character);
private
  type Space_Type is array(1..8) of Character;
  type Stack_Type is record
    Space : Space_Type;
    Index : Natural := 0;
  end record;
end Abstract_Char_Stack;
-----------------------------------------------------------
package body Abstract_Char_Stack is
----------------------------------------------
  procedure Push(Stack : in out Stack_Type;
                  Item : in Character) is
  begin
    Stack.Index := Stack.Index + 1;
    Stack.Space(Stack.Index) := Item;
  end Push;
--------------------------------------------
  procedure Pop (Stack : in out Stack_Type;
                 Char  : out Character) is
  begin
    Char := Stack.Space(Stack.Index);
    Stack.Index := Stack.Index - 1;
  end Pop;
--------------------------------------------
end Abstract_Char_Stack;

另一个是test_adt_stack.adb

-----------------------------------------------------------
with Ada.Text_IO; use Ada.Text_IO;
with Abstract_Char_Stack; use Abstract_Char_Stack;
procedure Test_ADT_Stack is
  S1 : Stack_Type;
  S2 : Stack_Type;
  Ch : Character;
begin
  Push(S1,'H'); Push(S1,'E');  
  Push(S1,'L'); Push(S1,'L');
  Push(S1,'O');                          -- S1 holds O,L,L,E,H

  for I in 1..5 loop
    Pop(S1, Ch);  
    Put(Ch);                             -- displays OLLEH
    Push(S2,Ch); 
  end loop;                              -- S2 holds H,E,L,L,O

  New_Line;
  Put_Line("Order is reversed");

  for I in 1..5 loop
    Pop(S2, Ch);
    Put(Ch);                             -- displays HELLO
  end loop;

end Test_ADT_Stack;
-----------------------------------------------------------

我做错了什么?我只想让它编译并显示它应该做什么。这是一项研究计划类型的任务。但是我不能让它编译或者不知道我是否做对了。

【问题讨论】:

    标签: stack ada abstract-data-type gnat


    【解决方案1】:

    问题在于 GNAT [和 FSF GNAT 是 GCC 使用的,IIRC] 不允许在单个文件中使用多个编译单元。 (这是由于他们管理图书馆的方式,但这对于初学者来说可能有点过于详细了。)

    解决方案,每个都需要自己的文件:

    • Abstract_Char_Stack 规范 (abstract_char_stack.ads)
    • Abstract_Char_Stack 正文 (abstract_char_stack.adb)
    • Test_ADT_Stack [过程] 正文 (test_adt_stack.adb)

    【讨论】:

    • 好的,所以我没有收到错误,但我怎样才能让它运行我的 test_adt_stack.adb 文件?我编译好了,接下来我应该运行什么?
    • gnatmake test_adt_stack.adb 应该尽一切努力构建可执行文件:查看文件夹内容。包括跟踪和自动构建所有依赖项(不需要 Makefile!)要运行它, ./test_stack 应该做(如果你在 Linux 上, . 通常不是路径的一部分。)当然有办法把所有一旦你掌握了基础知识,中间的 .o 文件就更整洁了......
    • Brian,新手 7 - 在基于 Unix 的系统上应该是 ./test_adt_stack,或者在 Windows 上应该是 test_adt_stack(可能是 .\test_adt_stack)。
    • 抱歉弄乱了可执行文件的名称!
    【解决方案2】:
    with Ada.Text_IO; use Ada.Text_IO;
    with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
    -----------------------------------------------------------
    
    procedure Test_ADT_Stack is
    
        enter code here
    
    package Abstract_Char_Stack is
      type Stack_Type is private;
      procedure Push(Stack : in out Stack_Type;
                     Item  : in Character);
      procedure Pop (Stack : in out Stack_Type;
                     Char  : out Character);
    private
      type Space_Type is array(1..8) of Character;
      type Stack_Type is record
        Space : Space_Type;
        Index : Natural := 0;
      end record;
      end Abstract_Char_Stack;
    
       use Test_ADT_Stack.Abstract_Char_Stack ;
        S1 : Stack_Type;
        S2 : Stack_Type;
        Ch : Character;
    
    -----------------------------------------------------------
    package body Abstract_Char_Stack is
    ----------------------------------------------
      procedure Push(Stack : in out Stack_Type;
                      Item : in Character) is
      begin
        Stack.Index := Stack.Index + 1;
        Stack.Space(Stack.Index) := Item;
      end Push;
    --------------------------------------------
      procedure Pop (Stack : in out Stack_Type;
                     Char  : out Character) is
      begin
        Char := Stack.Space(Stack.Index);
        Stack.Index := Stack.Index - 1;
      end Pop;
    --------------------------------------------
    end Abstract_Char_Stack;
    
    
    -----------------------------------------------------------
    begin
      Push(S1,'H'); Push(S1,'E');  
      Push(S1,'L'); Push(S1,'L');
      Push(S1,'O');                          -- S1 holds O,L,L,E,H
    
      for I in 1..5 loop
        Pop(S1, Ch);  
        Put(Ch);                             -- displays OLLEH
        Push(S2,Ch); 
      end loop;                              -- S2 holds H,E,L,L,O
    
      New_Line;
      Put_Line("Order is reversed");
    
      for I in 1..5 loop
        Pop(S2, Ch);
        Put(Ch);                             -- displays HELLO
      end loop;
    
    end Test_ADT_Stack;
    -----------------------------------------------------------
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多