2011-11-27 75 views
0

基本上我试图堆栈一条记录(1条记录是2个字符串变量,并且有3条记录,但我想暂时至少叠加1条记录)。我想设置想这样的事情对我的算法:Ada - 堆叠记录?

客户:给出的数值是在记录字符串,堆栈过程调用(即PUSH,POP,陈列) 套餐:记录被声明,项目被推入/弹出到堆栈,显示堆栈。

我一般都有问题。我试着把它保留在本地。它确实打开文件并读入字符串(尝试读取整数值,工作正常),我已经在客户端程序中使用类似的设置进行了测试(而不是将它存储为长度为40的字符串的记录)。然而,当我输出它时,我会得到的是一堆随机符号(如╤cß≈Ä),没有包含文件的文字。

这里是我的代码片段:

包装规格:

StackMaximum: constant integer := 10; 
    TYPE StackItem IS Record 
str1: string (1..20); 
str2: string (1..20); 
end record; 

    type Stack is PRIVATE 

    PROCEDURE Push (Item: IN StackItem; AStack: IN OUT Stack); 
    PROCEDURE display (AStack : in Stack); 

包体:

procedure Push (Item: in StackItem; 
      AStack: in out Stack) is 
begin 
if AStack.Top < StackMaximum then 
    AStack.Top := AStack.Top + 1; 
    AStack.Store(AStack.Top) := Item; 
else 
    raise StackOverflow; 
end if; 
END Push; 

    procedure display(AStack: in stack) is 
    BEGIN 

    FOR I IN 1..AStack.Top LOOP 
     Put(AStack.Store(I.lastname)); 
    END LOOP; 
    END display; 

    PRIVATE 
    type int_arry is array (1..StackMaximum) of StackItem; 
    type Stack is record 
     Store: int_arry; 
     Top: integer range 0..StackMaximum; 
    END RECORD; 

客户:

Lt:   Integer; 
New_Stack2: Stack; 
A:   StackItem; 
Stackitems: Ada.Text_IO.File_Type; 

    Get_Line(File => Stackitems, Item => A.str1, Last => Lt); 
    Get_Line(File => Stackitems, Item => A.str2, Last => Lt); 

    Push(A, New_Stack1); 
    display(New_Stack1); 

文件(只包含“这种情况。 .var。“):

This is the test input for the file var. 

对于我在做什么这个部分有什么建议吗?而且,这里是我的其他设置,其中我一直是所有地方:

客户:

Lt:   Integer; 
AB:   String(1..40); 
New_Stack2: Stack; 
A:   StackItem; 
Stackitems: Ada.Text_IO.File_Type; 

begin 

    Get_Line(File => Stackitems, Item => AB, Last => Lt); 

    Put(item=> AB); 

end; 

这是给了我所有的符号。但它是在文件中阅读,我只是不知道为什么我得到了糟糕的输出。

+0

什么是输入文件的[encoding](http://en.wikipedia.org/wiki/Character_encoding)? – trashgod

+0

文件名:info2.dat – Lance

+0

这就是文件_name_;尝试将_content_更改为8位ASCII文本。 – trashgod

回答

1

也许在您的type Stack的定义中,您应该将Top初始化为0?

type Stack is record 
    Store: int_arry; 
    Top: Integer range 0 .. StackMaximum := 0; 
end record;; 
0

如果你的东西,可能会短于变量定义使用Get(或Get_Line),您可以选择存储长度。

您已经在此使用变量Lt。现在,您必须限制Put调用中的变量:Put(item => AB(1 .. Lt));