跳到內容

Ada 程式設計/分隔符/*

來自華夏公益教科書,開放的書籍,開放的世界

Ada. Time-tested, safe and secure.
Ada。經時間考驗,安全可靠。

運算子

[編輯 | 編輯原始碼]

標準操作

[編輯 | 編輯原始碼]

算術乘法

[編輯 | 編輯原始碼]

"*" 運算子定義為所有數字型別上的算術乘法。

function "*" (Left, Right : T) return T;
A : constant Float   := 5.0 * 2.0;  -- A is now 10.0
B : constant Integer := 5 * 2;      -- B is also 10
工作示例
[編輯 | 編輯原始碼]
檔案:operator_multiply.adb (檢視, 純文字, 下載頁面, 瀏覽所有)
with Ada.Text_IO;

procedure Operator_Multiply is
   A : constant Float   := 5.0 * 2.0;  -- A is now 10.0
   B : constant Integer := 5 * 2;      -- B is also 10

   package T_IO renames Ada.Text_IO;
   package F_IO is new  Ada.Text_IO.Float_IO (Float);
   package I_IO is new  Ada.Text_IO.Integer_IO (Integer);

begin
   T_IO.Put ("A = ");
   F_IO.Put (
      Item => A,
      Fore => 3,
      Aft  => 1,
      Exp  => 0);
   T_IO.New_Line;
   T_IO.Put ("B = ");
   I_IO.Put (
      Item  => B,
      Width => 3,
      Base  => 10);
   T_IO.New_Line;
end Operator_Multiply;

常見的非標準操作

[編輯 | 編輯原始碼]

字元複製

[編輯 | 編輯原始碼]

建立一個字串,其中單個字元被複制 n 次。

function "*" (Left : Natural; Right : Character) return String;

除了標準字串之外,此 運算子 也為 Bounded_String 和 Unbounded_String 定義。

A : constant String := 10 * 'X';  -- A is filled with 10 X
工作示例
[編輯 | 編輯原始碼]

字元複製 運算子Ada.Strings.Fixed 的一部分。你需要withuse 包來使 運算子 可見。

檔案:operator_multiply_2.adb (檢視, 純文字, 下載頁面, 瀏覽所有)
with Ada.Text_IO;
with Ada.Strings.Fixed;

procedure Operator_Multiply_2 is
   use Ada.Strings.Fixed;

   A : constant String := 10 * 'X';  -- A is filled with 10 X

   package T_IO renames Ada.Text_IO;

begin
   T_IO.Put_Line ("A = " & A);
end Operator_Multiply_2;

字串複製

[編輯 | 編輯原始碼]

建立一個字串,其中源字串被複制 n 次。

function "*" (Left : Natural; Right : String) return String;

除了標準固定字串之外,此 運算子 也為 Bounded_String 和 Unbounded_String 定義。

A : constant String := 3 * "Hello ";  -- A is filled with 3 Hello
工作示例
[編輯 | 編輯原始碼]

字串複製 運算子Ada.Strings.Fixed 的一部分。你需要withuse 包來使運算子可見。

檔案:operator_multiply_3.adb (檢視, 純文字, 下載頁面, 瀏覽所有)
with Ada.Text_IO;
with Ada.Strings.Fixed;

procedure Operator_Multiply_3 is
   use Ada.Strings.Fixed; 

   A : constant String := 3 * "Hello ";  -- A is filled with 3 Hello.

   package T_IO renames Ada.Text_IO;

begin
   T_IO.Put_Line ("A = " & A);
end Operator_Multiply_3;

另請參見

[編輯 | 編輯原始碼]

華夏公益教科書

[編輯 | 編輯原始碼]

Ada 95 參考手冊

[編輯 | 編輯原始碼]

Ada 2005 參考手冊

[編輯 | 編輯原始碼]



Ada 運算子
and and then > + abs &
or or else >= - mod
xor = < * rem in
not /= <= ** / not in
華夏公益教科書