Show 例子 1
优质
小牛编辑
126浏览
2023-12-01
下表显示了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-- gives 9 |
例子 (Example)
尝试以下示例来理解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