阅读(944) (18)

D编程 运算符

2021-09-01 09:46:10 更新

运算符是一个符号,告诉编译器执行特定的数学或逻辑操作。 D语言包含丰富的内置运算符,并提供以下类型的运算符-

  • 算术运算符
  • 关系运算符
  • 逻辑运算符
  • 按位运算符
  • 赋值运算符

本章将逐一说明算术,关系,逻辑,按位,赋值和其他运算符。

算术运算符

下表显示了D语言支持的所有算术运算符。假设变量 A=10,变量 B=20,然后-

操作员说明Example
+相加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 %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

关系运算符

下表显示了D语言支持的所有关系运算符。假设变量 A=10,变量 B=20,则-

Operator描述Example
==相等(A == B) is not true.
!=不相等(A != B) is true.
>大于(A > B) is not true.
<小于(A < B) is true.
>=大于或等于(A >= B) is not true.
<=小于或等于(A <= B) is true.

尝试以下示例以了解D编程语言中可用的所有关系运算符-

import std.stdio;
  
int main(string[] args) { 
   int a = 21; 
   int b = 10; 
   int c ;  
   
   if( a == b ) { 
      writefln("Line 1 - a is equal to bn" ); 
   } else { 
      writefln("Line 1 - a is not equal to bn" );
   } 
   
   if ( a < b ) { 
      writefln("Line 2 - a is less than bn" ); 
   } else { 
      writefln("Line 2 - a is not less than bn" ); 
   } 
   
   if ( a > b ) { 
      writefln("Line 3 - a is greater than bn" ); 
   } else { 
      writefln("Line 3 - a is not greater than bn" ); 
   } 
   
   /* Lets change value of a and b */ 
   a = 5; 
   b = 20; 
   
   if ( a <= b ) { 
      writefln("Line 4 - a is either less than or equal to bn" ); 
   } 
   if ( b >= a ) { 
      writefln("Line 5 - b is either greater than or equal to bn" ); 
   } 
   return 0; 
}

当您编译并执行上述程序时,它将产生以下结果-

Line 1 - a is not equal to b 
 
Line 2 - a is not less than b
  
Line 3 - a is greater than b
  
Line 4 - a is either less than or equal to b
  
Line 5 - b is either greater than or equal to b

逻辑运算符

下表显示了D语言支持的所有逻辑运算符。假设变量 A=1,变量 B=0,则-

Operator描述Example
&&逻辑和(A && B) is false.
||逻辑或(A || B) is true.
!逻辑非!(A && B) is true.

尝试以下示例以了解D编程语言中可用的所有逻辑运算符-

import std.stdio;

int main(string[] args) {
   int a = 5;
   int b = 20;
   int c ;

   if ( a && b ) {
      writefln("Line 1 - Condition is truen" );
   }
   if ( a || b ) {
      writefln("Line 2 - Condition is truen" );
   }
   /* lets change the value of a and b */

   a = 0; 
   b = 10; 

   if ( a && b ) { 
      writefln("Line 3 - Condition is truen" ); 
   } else { 
      writefln("Line 3 - Condition is not truen" ); 
   } 
   
   if ( !(a && b) ) { 
      writefln("Line 4 - Condition is truen" ); 
   } 
   return 0;
}

当您编译并执行上述程序时,它将产生以下结果-

Line 1 - Condition is true 
 
Line 2 - Condition is true
  
Line 3 - Condition is not true
  
Line 4 - Condition is true

按位运算符

按位运算符对位进行运算并执行逐位运算。&,|和^的真值表如下-

pqp & qp | qp ^ q
00000
01011
11110
10011

假设A=60;和B =13。在二进制格式中,它们将如下所示-

A=0011 1100
B=0000 1101
-----------------
A&B=0000 1100
A | B=0011 1101
A ^ B=0011 0001
〜A=1100 0011

下表列出了D语言支持的按位运算符。假设变量A=60,变量B=13,则-

Operator描述Example
&按位和(A & B)=12, Means 0000 1100.
|按位或(A | B) gives 61. Means 0011 1101.
^按位异或(A ^ B) gives 49. Means 0011 0001
~按位非(~A ) gives -61. Means 1100 0011 in 2's complement form.
<<按位左移A << 2 give 240. Means 1111 0000
>>按位右移A >> 2 give 15. Means 0000 1111.

尝试以下示例以了解D编程语言中可用的所有按位运算符-

import std.stdio;

int main(string[] args) {  
   uint a = 60; /* 60=0011 1100 */   
   uint b = 13; /* 13=0000 1101 */ 
   int c = 0;  
   
   c = a & b;       /* 12=0000 1100 */  
   writefln("Line 1 - Value of c is %dn", c ); 
   
   c = a | b;       /* 61=0011 1101 */ 
   writefln("Line 2 - Value of c is %dn", c );
   
   c = a ^ b;       /* 49=0011 0001 */ 
   writefln("Line 3 - Value of c is %dn", c ); 
   
   c = ~a;          /*-61=1100 0011 */ 
   writefln("Line 4 - Value of c is %dn", c );  
   
   c = a << 2;     /* 240=1111 0000 */ 
   writefln("Line 5 - Value of c is %dn", c );
   
   c = a >> 2;     /* 15=0000 1111 */ 
   writefln("Line 6 - Value of c is %dn", c );
   
   return 0; 
} 

当您编译并执行上述程序时,它将产生以下结果-

Line 1 - Value of c is 12  

Line 2 - Value of c is 61
  
Line 3 - Value of c is 49
  
Line 4 - Value of c is -61
  
Line 5 - Value of c is 240
  
Line 6 - Value of c is 15

赋值运算符

D语言支持以下赋值运算符-

Operator描述Example
=赋值C=A + B assigns value of A + B into C
+=相加赋值C += A is equivalent to C=C + A
-=相减赋值C -= A is equivalent to C=C - A
*=相乘赋值C *= A is equivalent to C=C * A
/=相除赋值C /= A is equivalent to C=C/A
%=求余赋值C %= A is equivalent to C=C % A
<<=左移赋值C <<= 2 is same as C=C << 2
>>=右移赋值C >>= 2 is same as C=C >> 2
&=按位和赋值C &= 2 is same as C=C & 2
^=按全异或赋值C ^= 2 is same as C=C ^ 2
|=按位或赋值C |= 2 is same as C=C | 2

尝试以下示例以了解D编程语言中可用的所有赋值运算符-

import std.stdio;

int main(string[] args) {
   int a = 21;
   int c ;

   c =  a; 
   writefln("Line 1 -= Operator Example, Value of c=%dn", c );  
   
   c +=  a; 
   writefln("Line 2 - += Operator Example, Value of c=%dn", c );
   
   c -=  a; 
   writefln("Line 3 - -= Operator Example, Value of c=%dn", c );
   
   c *=  a; 
   writefln("Line 4 - *= Operator Example, Value of c=%dn", c ); 
   
   c /=  a; 
   writefln("Line 5 - /= Operator Example, Value of c=%dn", c );  
   
   c  = 200; 
   c = c % a; 
   writefln("Line 6 - %s= Operator Example, Value of c=%dn",'x25', c );
   
   c <<=  2; 
   writefln("Line 7 - <<= Operator Example, Value of c=%dn", c ); 
   
   c >>=  2; 
   writefln("Line 8 - >>= Operator Example, Value of c=%dn", c );
   
   c &=  2; 
   writefln("Line 9 - &= Operator Example, Value of c=%dn", c ); 
   
   c ^=  2; 
   writefln("Line 10 - ^= Operator Example, Value of c=%dn", c ); 
   
   c |=  2; 
   writefln("Line 11 - |= Operator Example, Value of c=%dn", c );
   
   return 0; 
}

当您编译并执行上述程序时,它将产生以下结果-

Line 1 -= Operator Example, Value of c=21
  
Line 2 - += Operator Example, Value of c=42
  
Line 3 - -= Operator Example, Value of c=21
  
Line 4 - *= Operator Example, Value of c=441
  
Line 5 - /= Operator Example, Value of c=21 
 
Line 6 - %= Operator Example, Value of c=11
  
Line 7 - <<= Operator Example, Value of c=44 
 
Line 8 - >>= Operator Example, Value of c=11 
 
Line 9 - &= Operator Example, Value of c=2

Line 10 - ^= Operator Example, Value of c=0 
 
Line 11 - |= Operator Example, Value of c=2

杂类运算符

D语言支持其他几个重要的运算符包括sizeof和?:

Operator描述Example
sizeof()返回变量的大小。sizeof(a), where a is integer, returns 4.
&返回变量的地址。&a; gives actual address of the variable.
*指向变量的指针。*a; gives pointer to a variable.
? :条件表达式If condition is true then value X: Otherwise value Y.

尝试以下示例以了解D编程语言中可用的所有其他运算符-

import std.stdio;

int main(string[] args) { 
   int a = 4; 
   short b; 
   double c; 
   int* ptr;

   /* example of sizeof operator */ 
   writefln("Line 1 - Size of variable a=%dn", a.sizeof ); 
   writefln("Line 2 - Size of variable b=%dn", b.sizeof ); 
   writefln("Line 3 - Size of variable c= %dn", c.sizeof );  
  
   /* example of & and * operators */ 
   ptr = &a; /* 'ptr' now contains the address of 'a'*/ 
   writefln("value of a is  %dn", a); 
   writefln("*ptr is %d.n", *ptr);  
   
   /* example of ternary operator */ 
   a = 10; 
   b = (a == 1) ? 20: 30; 
   writefln( "Value of b is %dn", b ); 
   
   b = (a == 10) ? 20: 30; 
   writefln( "Value of b is %dn", b ); 
   return 0; 
} 

当您编译并执行上述程序时,它将产生以下结果-

value of a is  4 

*ptr is 4. 

Value of b is 30 

Value of b is 20

运算符优先级

在这里,优先级最高的运算符出现在表格的顶部,而优先级最低的运算符出现在表格的底部。在表达式中,优先级较高的运算符将首先求值。

CategoryOperatorAssociativity
Postfix() [] -> . ++ - -Left to right
Unary+ - ! ~ ++ - - (type)* & sizeofRight to left
Multiplicative*/%Left to right
Additive+ -Left to right
Shift<< >>Left to right
Relational< <= > >=Left to right
Equality== !=Left to right
Bitwise AND&Left to right
Bitwise XOR^Left to right
Bitwise OR|Left to right
Logical AND&&Left to right
Logical OR||Left to right
Conditional?:Right to left
Assignment= += -= *= /= %=>>= <<= &= ^= |=Right to left
Comma,Left to right

尝试以下示例以了解D编程语言中可用的运算符优先级-

import std.stdio;

int main(string[] args) { 
   int a = 20; 
   int b = 10; 
   int c = 15; 
   int d = 5; 
   int e;
   
   e = (a + b) * c / d;      // ( 30 * 15 )/5 
   writefln("Value of (a + b) * c/d is : %dn",  e ); 
   
   e = ((a + b) * c) / d;    // (30 * 15 )/5 
   writefln("Value of ((a + b) * c)/d is  : %dn" ,  e );  
   
   e = (a + b) * (c / d);   // (30) * (15/5) 
   writefln("Value of (a + b) * (c/d) is  : %dn",  e );
   
   e = a + (b * c) / d;     //  20 + (150/5) 
   writefln("Value of a + (b * c)/d is  : %dn" ,  e ); 
  
   return 0;
}

当您编译并执行上述程序时,它将产生以下结果-

Value of (a + b) * c/d is : 90 
 
Value of ((a + b) * c)/d is  : 90
  
Value of (a + b) * (c/d) is  : 90 
 
Value of a + (b * c)/d is  : 50