包体和主程序.简单赋值 (Ada) [英] Package bodies and main programs. Simple assignment (Ada)

查看:59
本文介绍了包体和主程序.简单赋值 (Ada)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被困在艾达.我应该创建一个具有特定 Flag_Type 的包,该包可以被写入和读取以打印一个简单的标志.我想我已经设法使包广告和包体 adb 正确,但我在主程序的命令上挣扎.

Im stuck in Ada. I'm supposed to create a package with a specific Flag_Type that can be written to and read in order to print a simple flag. Id like to think Ive managed to make the package ads and the package body adb right but I struggle with the commands for the main program.

首先,输出应该是这样的:

First is first, the output is supposed to look like this:

Enter the flag name: Italys flag
Enter the flag height: 2
Enter the stripes width: 3
Enter the flags colors: GWR
Italys flag
+---------+
|GGGWWWRRR|
|GGGWWWRRR|
+---------+

现在,我的包 ADS 看起来像这样:

now, my package ADS looks like this:

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;

package Flagdrawer is

   type Flag_Type is private;


     procedure Get(Item: out Flag_Type);
   procedure Put(Item: in Flag_Type);

   private
   type Flag_Type is
      record

     Flag_Name: String(1..20);
     L : Integer;
     Flag_Height : Integer;
     Flag_Width : Integer;
     Flag_Colors : String(1..3);

      end record;
end Flagdrawer;

我的包体看起来像这样:

My package body looks like this:

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
with Flagdrawer; use Flagdrawer;

package body Flagdrawer is

   procedure Get(Item: out Flag_Type) is
   begin

      Get_Line(Item.Flag_Name, Item.L);
      Get(Item.Flag_Height);
      Get(Item.Flag_Width);
      Get(Item.Flag_Colors);

   end Get;

   procedure Put(Item: in Flag_Type) is

     Real_Width : Integer;

   begin
      Real_Width := Item.Flag_Width *3;
      Put(Item.Flag_Name(1..Item.L));
      New_Line;
      Put("+");
      for I in 1..Real_Width loop
     Put("-");
      end loop;
      Put_Line("+");

      for J in 1..Item.Flag_Height loop
     Put("!");
     for K in 1..Item.Flag_Width loop
        Put(Item.Flag_Colors(1));
        end loop;
        for L in 1..Item.Flag_Width loop
           Put(Item.Flag_Colors(2));
        end loop;
        for M in 1..Item.Flag_Width loop
           Put(Item.Flag_Colors(3));
        end loop;
        Put_Line("!");
      end loop;

      Put("+");
      for I in 1..Real_Width loop
     Put("-");
      end loop;
      Put_Line("+");

   end Put;

end Flagdrawer;

然后我非常缺乏的主程序看起来像这样:

and then my sorely lacking main program looks like this:

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
with Flagdrawer; use Flagdrawer;

procedure Tenta_Flagdrawah is

   F: Flag_Type;

begin

  Put_line("Enter the flags name (1): ");   
  Put_Line("Enter the flags height (2): ");
  Put_Line("Enter the stripes' width (3): ");
  Put_Line("Enter the RGB for the flag's colors (4): ");
  Get(F);

  New_Line;
  Put(F);

end Tenta_Flagdrawah;

我只习惯于有一个特定输入的赋值,比如 Get(F),其中 F 是 Fl​​ag_Type,现在它分布在多个变量上,我不知道如何合并它们.

Im only used to assignments that have one specific input, lets say Get(F) where F is Flag_Type, now its spread out over multiple variables and I dont know how to consolidate them.

过去我从这里得到了很好的回应,有没有人可以给我一些关于我哪里有错的提示?我知道我的主程序严重缺乏,但我不知道如何编码.

I've been getting good responses from here in the past, is there any chance anyone could give me some tips as to where Im at fault? I know my main program is severely lacking but I dont know how to code it.

在此先感谢您的帮助!

我找到了一个可行的解决方案(有点),该解决方案从这个主程序中注释掉了,我直接发给了 Simon Wright,因为我不太明白你的声明是什么意思.我把它们放在我的 MP 中,我不断收到项目的实际值必须是一个变量".我尝试使用 Item.Name 代替,但它声称它的前缀无效.你觉得我哪里做错了?

I found a solution that works (sort of) which is commented out of this main program I direct to Simon Wright as I didnt quite get what you meant with your declarations. I put them in my MP and I keep getting "actual for item must be a variable". I tried using Item.Name instead but it claims its an invalid prefix. Where did I go wrong you think?

主程序.adb

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
with Flagdrawer; use Flagdrawer;

procedure Tenta_Flagdrawah is

   F: Flag_Type;

subtype Color_String is String (1 .. 3);

procedure Get (Item : out Flag_Type;
               Name : in String;
               Height : in Natural;
               Stripe_Width : in Natural;
               Colors : in Color_String) is
begin

      Put("Enter the flag's name: ");
      Get(Name);
      Put("Enter the flag's height: ");
      Get(Height);

   end Get;
   begin

--   Put_line("Enter the flags name (1): ");
--   Put_Line("Enter the flags height (2): ");
--   Put_Line("Enter the stripes' width (3): ");
--   Put_Line("Enter the RGB for the flag's colors (4): ");
--   Get(F);


   New_Line;
   Put(F);

end Tenta_Flagdrawah;

推荐答案

你的很多麻烦都是 Get 过程;它实现了各个字段的所有文本输入,而无需参考主程序在做什么.

A lot of your trouble is the Get procedure; it implements all the text input for the various fields without reference to whatever the main program’s doing.

一般来说,在 抽象数据中进行 I/O 并不是一个好习惯像Flag一样输入;在调用程序中这样做要好得多.(我可以看到 Put 会很尴尬).

In general, it isn’t good practice to do I/O within an abstract data type like Flag; much better to do it in the calling program. (I can see that’d be awkward for Put).

您可以读取主程序中的参数并将它们提供给Get

You could read the parameters in the main program and supply them to Get,

subtype Color_String is String (1 .. 3);
procedure Get (Item : out Flag_Type;
               Name : in String;
               Height : in Natural;
               Stripe_Width : in Natural;
               Colors : in Color_String);

这意味着制定包装规范(抱歉,我忍不住要整理一下)

This would mean making the package spec (sorry, I couldn’t resist some tidying)

package Flagdrawer is

   type Flag_Type is private;

   subtype Color_String is String (1 .. 3);

   procedure Get (Item         :    out Flag_Type;
                  Name         : in     String;
                  Height       : in     Positive;
                  Stripe_Width : in     Positive;
                  Colors       : in     Color_String);

   procedure Put(Item: in Flag_Type);

private
   type Flag_Type is
      record
         Name         : String (1 .. 20);
         Name_Len     : Natural;
         Height       : Positive;
         Stripe_Width : Positive;
         Colors       : Color_String;
      end record;

end Flagdrawer;

并在包体中实现 Get

procedure Get (Item         :    out Flag_Type;
               Name         : in     String;
               Height       : in     Positive;
               Stripe_Width : in     Positive;
               Colors       : in     Color_String) is
begin
   --  Don’t exceed the 20-character limit on the stored Name
   Item.Name_Len := Natural'Min (Item.Name'Length, Name'Length);
   Item.Name (1 .. Item.Name_Len) := Name (Name'First .. Item.Name_Len);
   Item.Height := Height;
   Item.Stripe_Width := Stripe_Width;
   Item.Colors := Colors;
end Get;

主程序是

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Flagdrawer; use Flagdrawer;

procedure Tenta_Flagdrawah is

   F: Flag_Type;

begin
   Put("Enter the flags name: ");
   --  Need a declare block so that Name 
   --  is as long as the user input
   declare
      Name : constant String := Get_Line;
      Height : Positive;
      Stripe_Width : Positive;
      Colors : Flagdrawer.Color_String;
   begin
      Put("Enter the flag's height: ");
      Get (Height);
      Put("Enter the stripes' width: ");
      Get (Stripe_Width);
      Put("Enter the RGB for the flag's colors: ");
      Get (Colors);
      Get(F,
          Name => Name,
          Height => Height,
          Stripe_Width => Stripe_Width,
          Colors => Colors);
   end;

   New_Line;
   Put(F);

end Tenta_Flagdrawah;

这篇关于包体和主程序.简单赋值 (Ada)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆