D編程 運(yùn)算符

2021-09-01 10:19 更新

運(yùn)算符是一個(gè)符號(hào),告訴編譯器執(zhí)行特定的數(shù)學(xué)或邏輯操作。 D語(yǔ)言包含豐富的內(nèi)置運(yùn)算符,并提供以下類型的運(yùn)算符-

  • 算術(shù)運(yùn)算符
  • 關(guān)系運(yùn)算符
  • 邏輯運(yùn)算符
  • 按位運(yùn)算符
  • 賦值運(yùn)算符

本章將逐一說(shuō)明算術(shù),關(guān)系,邏輯,按位,賦值和其他運(yùn)算符。

算術(shù)運(yùn)算符

下表顯示了D語(yǔ)言支持的所有算術(shù)運(yùn)算符。假設(shè)變量 A=10,變量 B=20,然后-

操作員說(shuō)明Example
+相加A + B=30
-相減A-B=-10
*相乘A * B=200
/相除B/A=2
取余B%A=0
++遞增A ++=11
-遞減A--=9

嘗試以下示例以了解D編程語(yǔ)言中可用的所有算術(shù)運(yùn)算符-

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; 
}

當(dāng)您編譯并執(zhí)行上述程序時(shí),它將產(chǎn)生以下結(jié)果-

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

關(guān)系運(yùn)算符

下表顯示了D語(yǔ)言支持的所有關(guān)系運(yùn)算符。假設(shè)變量 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編程語(yǔ)言中可用的所有關(guān)系運(yùn)算符-

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 b\n" ); 
   } else { 
      writefln("Line 1 - a is not equal to b\n" );
   } 
   
   if ( a < b ) { 
      writefln("Line 2 - a is less than b\n" ); 
   } else { 
      writefln("Line 2 - a is not less than b\n" ); 
   } 
   
   if ( a > b ) { 
      writefln("Line 3 - a is greater than b\n" ); 
   } else { 
      writefln("Line 3 - a is not greater than b\n" ); 
   } 
   
   /* 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 b\n" ); 
   } 
   if ( b >= a ) { 
      writefln("Line 5 - b is either greater than or equal to b\n" ); 
   } 
   return 0; 
}

當(dāng)您編譯并執(zhí)行上述程序時(shí),它將產(chǎn)生以下結(jié)果-

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

邏輯運(yùn)算符

下表顯示了D語(yǔ)言支持的所有邏輯運(yùn)算符。假設(shè)變量 A=1,變量 B=0,則-

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

嘗試以下示例以了解D編程語(yǔ)言中可用的所有邏輯運(yùn)算符-

import std.stdio;

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

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

   a = 0; 
   b = 10; 

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

當(dāng)您編譯并執(zhí)行上述程序時(shí),它將產(chǎn)生以下結(jié)果-

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

按位運(yùn)算符

按位運(yùn)算符對(duì)位進(jìn)行運(yùn)算并執(zhí)行逐位運(yùn)算。&,|和^的真值表如下-

pqp & qp | qp ^ q
00000
01011
11110
10011

假設(shè)A=60;和B =13。在二進(jìn)制格式中,它們將如下所示-

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

下表列出了D語(yǔ)言支持的按位運(yùn)算符。假設(shè)變量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編程語(yǔ)言中可用的所有按位運(yùn)算符-

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 %d\n", c ); 
   
   c = a | b;       /* 61=0011 1101 */ 
   writefln("Line 2 - Value of c is %d\n", c );
   
   c = a ^ b;       /* 49=0011 0001 */ 
   writefln("Line 3 - Value of c is %d\n", c ); 
   
   c = ~a;          /*-61=1100 0011 */ 
   writefln("Line 4 - Value of c is %d\n", c );  
   
   c = a << 2;     /* 240=1111 0000 */ 
   writefln("Line 5 - Value of c is %d\n", c );
   
   c = a >> 2;     /* 15=0000 1111 */ 
   writefln("Line 6 - Value of c is %d\n", c );
   
   return 0; 
} 

當(dāng)您編譯并執(zhí)行上述程序時(shí),它將產(chǎn)生以下結(jié)果-

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

賦值運(yùn)算符

D語(yǔ)言支持以下賦值運(yùn)算符-

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編程語(yǔ)言中可用的所有賦值運(yùn)算符-

import std.stdio;

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

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

當(dāng)您編譯并執(zhí)行上述程序時(shí),它將產(chǎn)生以下結(jié)果-

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

雜類運(yùn)算符

D語(yǔ)言支持其他幾個(gè)重要的運(yùn)算符包括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.
? :條件表達(dá)式If condition is true then value X: Otherwise value Y.

嘗試以下示例以了解D編程語(yǔ)言中可用的所有其他運(yùn)算符-

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

當(dāng)您編譯并執(zhí)行上述程序時(shí),它將產(chǎn)生以下結(jié)果-

value of a is  4 

*ptr is 4. 

Value of b is 30 

Value of b is 20

運(yùn)算符優(yōu)先級(jí)

在這里,優(yōu)先級(jí)最高的運(yùn)算符出現(xiàn)在表格的頂部,而優(yōu)先級(jí)最低的運(yùn)算符出現(xiàn)在表格的底部。在表達(dá)式中,優(yōu)先級(jí)較高的運(yùn)算符將首先求值。

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編程語(yǔ)言中可用的運(yùn)算符優(yōu)先級(jí)-

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 : %d\n",  e ); 
   
   e = ((a + b) * c) / d;    // (30 * 15 )/5 
   writefln("Value of ((a + b) * c)/d is  : %d\n" ,  e );  
   
   e = (a + b) * (c / d);   // (30) * (15/5) 
   writefln("Value of (a + b) * (c/d) is  : %d\n",  e );
   
   e = a + (b * c) / d;     //  20 + (150/5) 
   writefln("Value of a + (b * c)/d is  : %d\n" ,  e ); 
  
   return 0;
}

當(dāng)您編譯并執(zhí)行上述程序時(shí),它將產(chǎn)生以下結(jié)果-

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


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)