所以我用Gnat编译器编译Ada。我有一个持有记录的队列,其中一个记录变量是名为name的Unbounded_String。这就是我现在正在测试的,尽管所有记录变量都应该在循环之外可访问。我遇到问题的代码是
with ada.text_io, stacks, queues, ada.Strings.Unbounded, ada.Strings.Unbounded.Text_IO, ada.Integer_Text_IO;
use ada.Text_IO, ada.Strings.Unbounded, ada.Strings.Unbounded.Text_IO, ada.Integer_Text_IO;
procedure eliminate is
type person is record
index: Positive;
age: Positive;
skill: natural;
wins: natural;
loss: natural;
name: Unbounded_String;
end record;
package stkPkg is new stacks(person);
use stkPkg;
output: Stack;
package quePkg is new Queues(person);
use quePkg;
noLossQ : Queue;
count : Positive := 1;
input : Person;
begin
while not end_of_file loop
input.name := get_line;
get(input.skill);
get(input.age);
skip_line;
input.index := count;
count := count + 1;
input.wins := 0;
input.loss := 0;
enqueue(input, noLossQ);
put_line(front(noLossQ).name);
end loop;
put_line(front(noLossQ).name);
end eliminate;
如果你运行它,你会注意到循环内部put_line(前面(noLossQ).name)工作得很好,但是一旦在循环之外它什么都不做或打印出一个空白行。 put_line工作的循环结束,循环结束,然后put_line不起作用;该代码如下。
enqueue(input, noLossQ);
put_line(front(noLossQ).name);
end loop;
put_line(front(noLossQ).name);
这是我的队列实现
with Unchecked_Deallocation, ada.Text_IO;
use ada.Text_IO;
package body queues is
function is_Empty (Q: Queue) return Boolean is
begin
return ((Q.front = null) and (Q.back = null));
end is_Empty;
procedure Dequeue (Q : in out Queue) is
begin
if is_Empty(Q) then
raise Queue_Empty;
else
Q.Back := Q.Back.Next;
if Q.Back = null then
Q.Front := null;
end if;
end if;
end Dequeue;
procedure Enqueue (Item: ItemType; Q: in out Queue) is
tmp : QueueNodePointer := new QueueNode'(Item, null);
begin
if is_Full(q) then
raise Queue_Full;
else
if Q.Back = Null then
Q.Back := tmp;
end if;
if Q.Front /= null then
q.front.next := tmp;
end if;
q.Front := tmp;
end if;
end Enqueue;
function Front (Q: Queue) return ItemType is
begin
if is_Empty(Q) then
raise Queue_Empty;
else
return Q.back.data;
end if;
end Front;
end queues;
我正在使用的重定向文件:
Person one
500 50
Person 2
400 50
Person 3
300 50
Person 4
200 50
Person 5
100 50
我无法弄清楚为什么它只能在循环中工作,任何帮助都表示赞赏。在此先感谢!!
答案 0 :(得分:2)
似乎问题必须在您的队列实现中。您应该正确测试它,以确保它具有您期望的属性。