문제

아래 절차를 인쇄 할 때 -

procedure put (Date:Date_Type) is
begin
  Put(Integer'Image(Date.Day)); --'
  Put("-");
  Put(Integer'Image(Date.Month)); --'
  Put("-");
  Put(Integer'Image(Date.Year)); --'
end;

결과는 (예를 들어) : 1-1- 2010

내 질문은 모든 날짜 값 전에 한 문자의 간격을 방지하는 방법입니다. (일/월/년). 물론 나는 날/월/년 내부의 기록과 함께 날짜 절차를 사용하고 있습니다.

미리 감사드립니다.

도움이 되었습니까?

해결책

몇 가지 옵션이 있습니다.

  • 정수 값이 항상 음성이 없다는 것을 알고 있다면 문자열을 슬라이스하여 선행 공백을 생략 할 수 있습니다.
  • ada.strings.fixed.trim () 함수를 사용하여 빈 공백을 다듬을 수 있습니다.
  • ada.text_io.integer_io Instantiation (예 : 사전 정지 된 ada.integer_text_io)에서 put () 절차를 사용할 수 있습니다.

설명 할 코드는 다음과 같습니다.

with Ada.Text_IO;
with Ada.Integer_Text_IO;
with Ada.Strings.Fixed;

procedure Int_Image is

   use Ada.Text_IO;
   use Ada.Integer_Text_IO;
   use Ada.Strings.Fixed;

   N : Integer := 20;

   Raw_Image     : constant String := Integer'Image(N);

   Trimmed_Image : constant String := Trim(Raw_Image, Ada.Strings.Left);

   Sliced_Image  : constant String := Raw_Image(2 .. Raw_Image'Last);

begin
   Put_Line("Raw 'image    :" & Raw_Image & ":");
   Put_Line("Trimmed image :" & Trimmed_Image & ":");
   Put_Line("Sliced image  :" & Sliced_Image & ":");
   Put     ("'Put' image   :");
   Put     (N, Width => 0);
   Put_Line(":");
end Int_Image;

GNAT 수율로 이것을 컴파일하고 실행합니다.

$./int_image
Raw 'image    : 20:
Trimmed image :20:
Sliced image  :20:
'Put' image   :20:
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top