D 程式設計 - D 語言中的算術運算子



下表顯示了 D 語言支援的所有算術運算子。假設變數A的值為 10,變數B的值為 20,則:

運算子 描述 示例
+ 它將兩個運算元相加。 A + B 結果為 30
- 它從第一個運算元中減去第二個運算元。 A - B 結果為 -10
* 它將兩個運算元相乘。 A * B 結果為 200
/ 它將分子除以分母。 B / A 結果為 2
% 它返回整數除法的餘數。 B % A 結果為 0
++ 自增運算子將整數值增加一。 A++ 結果為 11
-- 自減運算子將整數值減少一。 A-- 結果為 9

示例

嘗試以下示例以瞭解 D 程式語言中可用的所有算術運算子:

import std.stdio; 
 
int main(string[] args) { 
   int a = 21; 
   int b = 10; 
   int c ;  
   
   c = a + b; 
   writefln("Line 1 - Value of c is %d\n", c ); 
   c = a - b; 
   writefln("Line 2 - Value of c is %d\n", c ); 
   c = a * b; 
   writefln("Line 3 - Value of c is %d\n", c ); 
   c = a / b; 
   writefln("Line 4 - Value of c is %d\n", c ); 
   c = a % b; 
   writefln("Line 5 - Value of c is %d\n", c ); 
   c = a++; 
   writefln("Line 6 - Value of c is %d\n", c ); 
   c = a--; 
   writefln("Line 7 - Value of c is %d\n", c ); 
   char[] buf; 
   stdin.readln(buf); 
   return 0; 
}

編譯並執行上述程式後,將產生以下結果:

Line 1 - Value of c is 31
  
Line 2 - Value of c is 11
  
Line 3 - Value of c is 210 
 
Line 4 - Value of c is 2 
 
Line 5 - Value of c is 1 
 
Line 6 - Value of c is 21
  
Line 7 - Value of c is 22
d_programming_operators.htm
廣告
© . All rights reserved.