Ada - 堆叠记录?

发布于 2024-12-18 03:01:01 字数 2076 浏览 6 评论 0原文

基本上我正在尝试堆叠一条记录(1条记录是2个字符串变量,其中有3个,但我想暂时至少堆叠1个)。我认为设置对于我的算法来说是这样的:

客户端:将值赋予记录中的字符串,调用堆栈过程(即推送、弹出、显示) 包:声明记录,将项目推入/弹出到堆栈,显示堆栈。

我总体上遇到了麻烦。我尝试将其全部保留在本地。它确实打开文件并读取字符串(尝试读取整数值,工作正常),我已经使用类似的设置在客户端程序中进行了测试(而不是记录,我将其存储到长度为 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);

文件(仅包含“This..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;

这就是我获得所有这些符号的原因。但它正在读取文件,我只是不知道为什么我得到了错误的输出。

Basically I'm trying stack a record (1 record is 2 string variables, and there's 3 of them but I'd like to stack at least 1 for the time being). I'm thinking the setup would like something like this for my algorithm:

Client: Values are given to strings in record, procedures for stack are called (ie, push, pop, display)
Package: Record is declared, items are pushed/popped onto stack, display stack.

I'm having trouble in general. I tried keeping it all local. It does open the file and read in the strings (tried it out reading in an integer value, works fine), this I have tested in the client program using a similar setup (instead of a record I stored it to a string of length 40). However when I'd go to output it all I'd get is a bunch of random symbols (such as ╤cß≈Ä), no words like the file contained.

Here are my code fragments:

Package spec:

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

Package body:

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;

Client:

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

File (Only contains "This..var."):

This is the test input for the file var.

Any suggestions for what I'm doing wrong with this part? Also here's my other setup where I kept it all local:

Client:

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;

This is what got me all those symbols. But it is reading in the file, I just have no idea why I'm getting the bad output.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

擦肩而过的背影 2024-12-25 03:01:01

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

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

Perhaps in your definition of type Stack you should initialize Top to 0?

type Stack is record
   Store: int_arry;
   Top: Integer range 0 .. StackMaximum := 0;
end record;;
酒废 2024-12-25 03:01:01

如果您对可能比定义的变量短的内容使用Get(或Get_Line),则必须存储长度。

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

If you use Get (or Get_Line) on something that might be shorter than the variable was defined, you have to store the length.

You already use the variable Lt for this. Now you have to limit the variable in your Put call: Put(item => AB(1 .. Lt));

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文