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
广告