发布于 2015-08-16 14:53:57 | 129 次阅读 | 评论: 0 | 来源: 网络整理

下表列出了所有D语言支持的算术运算符。假设变量A=10和变量B=20,则:

运算符 描述 实例
+ 增加了两个操作数 A + B = 30
- 从第一中减去第二个操作数 A - B = -10
* 两个操作数相乘 A * B = 200
/ 通过取消分子分裂分子 B / A = 2
% 模运算符和其余整数除法 B % A = 0
++ 递增运算符相加整数值1 A++ = 11
-- 递减运算符通过减少整数值1 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 %dn", c );
   c = a - b;
   writefln("Line 2 - Value of c is %dn", c );
   c = a * b;
   writefln("Line 3 - Value of c is %dn", c );
   c = a / b;
   writefln("Line 4 - Value of c is %dn", c );
   c = a % b;
   writefln("Line 5 - Value of c is %dn", c );
   c = a++;
   writefln("Line 6 - Value of c is %dn", c );
   c = a--;
   writefln("Line 7 - Value of c is %dn", 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
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务