📜  C++运算符

📅  最后修改于: 2020-09-25 05:03:48             🧑  作者: Mango

在本教程中,我们将借助示例学习C++中不同类型的运算符 。在编程中, 运算符是对值或变量进行运算的符号。

运算符是对变量和值执行运算的符号。例如, +是用于加法的运算符 ,而-是用于减法的运算符 。

C++中的运算符可分为6种类型:

1. C++算术运算符

算术运算运算符用于对变量和数据执行算术运算。例如,

a + b;

在这里, + 运算符用于添加两个变量ab 。同样,C++中还有其他各种算术运算运算符 。

Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo Operation (Remainder after division)

示例1:算术运算符

#include 
using namespace std;

int main() {
    int a, b;
    a = 7;
    b = 2;

    // printing the sum of a and b
    cout << "a + b = " << (a + b) << endl;

    // printing the difference of a and b
    cout << "a - b = " << (a - b) << endl;

    // printing the product of a and b
    cout << "a * b = " << (a * b) << endl;

    // printing the division of a by b
    cout << "a / b = " << (a / b) << endl;

    // printing the modulo of a by b
    cout << "a % b = " << (a % b) << endl;

    return 0;
}

输出

a + b = 9
a - b = 5
a * b = 14
a / b = 3
a % b = 1

在这里, 运算符 +-*如我们预期的那样分别计算加法,减法和乘法。

/分部运算符

注意我们程序中的操作(a / b)/ 运算符是除法运算符。

从上面的示例可以看出,如果一个整数除以另一个整数,我们将得到商。但是,如果除数或被除数是浮点数,我们将以小数形式得到结果。

In C++,

7/2 is 3
7.0 / 2 is 3.5
7 / 2.0 is 3.5
7.0 / 2.0 is 3.5

%模运算符

模运算符 %计算余数。当a = 9除以b = 4 ,余数为1

注意: % 运算符只能与整数一起使用。

增减运算符

C++还提供了递增和递减运算符: ++--++将操作数的值增加1 ,而--将其减小1

例如,

int num = 5;

// increasing num by 1
++num;

此处, num的值从其初始值5增加到6

示例2:增量和减量运算符

// Working of increment and decrement operators

#include 
using namespace std;

int main() {
    int a = 10, b = 100, result_a, result_b;

    // incrementing a by 1 and storing the result in result_a
    result_a = ++a;
    cout << "result_a = " << result_a << endl;


    // decrementing b by 1 and storing the result in result_b   
    result_b = --b;
    cout << "result_b = " << result_b << endl;

    return 0;
}

输出

result_a = 11
result_b = 99

在上面的程序中,我们使用++-- 运算符作为前缀 。我们还可以将这些运算符用作postfix

这些运算符用作前缀与用作后缀时略有不同。

要了解有关这些运算符的更多信息,请访问递增和递减运算符。

2. C++赋值运算符

在C++中,赋值运算符用于将值赋给变量。例如,

// assign 5 to a
a = 5;

在这里,我们为变量a分配了5的值。

Operator Example Equivalent to
= a = b; a = b;
+= a += b; a = a + b;
-= a -= b; a = a - b;
*= a *= b; a = a * b;
/= a /= b; a = a / b;
%= a %= b; a = a % b;

示例2:赋值运算符

#include 
using namespace std;

int main() {
    int a, b, temp;

    // 2 is assigned to a
    a = 2;

    // 7 is assigned to b
    b = 7;

   // value of a is assigned to temp
   temp = a;    // temp will be 2
   cout << "temp = " << temp << endl;

    // assigning the sum of a and b to a
    a += b;       // a = a +b
    cout << "a = " << a << endl;

    return 0;
}

输出

temp = 2
a = 9

3. C++关系运算符

关系运算符用于检查两个操作数之间的关系。例如,

// checks if a is greater than b
a > b;

在这里, >是一个关系运算符。它检查a是否大于b

如果该关系为true ,则返回1 ,如果该关系为false ,则返回0

Operator Meaning Example
== Is Equal To 3 == 5 gives us false
!= Not Equal To 3 != 5 gives us true
> Greater Than 3 > 5 gives us false
< Less Than 3 < 5 gives us true
>= Greater Than or Equal To 3 >= 5 give us false
<= Less Than or Equal To 3 <= 5 gives us true

示例4:关系运算符

#include 
using namespace std;

int main() {
    int a, b;
    a = 3;
    b = 5;
    bool result;

    result = (a == b);   // false
    cout << "3 == 5 is " << result << endl;

    result = (a != b);  // true
    cout << "3 != 5 is " << result << endl;

    result = a > b;   // false
    cout << "3 > 5 is " << result << endl;

    result = a < b;   // true
    cout << "3 < 5 is " << result << endl;

    result = a >= b;  // false
    cout << "3 >= 5 is " << result << endl;

    result = a <= b;  // true
    cout << "3 <= 5 is " << result << endl;

    return 0;
}

输出

3 == 5 is 0
3 != 5 is 1
3 > 5 is 0
3 < 5 is 1
3 >= 5 is 0
3 <= 5 is 1

注意 :关系运算符用于决策和循环。

4. C++逻辑运算符

逻辑运算符用于检查表达式是true还是false 。如果表达式为true ,则返回1;如果表达式为false ,则返回0

Operator Example Meaning
&& expression1 && expression 2 Logical AND.
True only if all the operands are true.
|| expression1 || expression 2 Logical OR.
True if at least one of the operands is true.
! !expression Logical NOT.
True only if the operand is false.

在C++中,逻辑运算符通常用于决策制定。为了进一步了解逻辑运算符,我们来看以下示例,

Suppose,
a = 5
b = 8

Then,

(a > 3) && (b > 5) evaluates to true
(a > 3)  && (b < 5) evaluates to false

(a > 3) || (b > 5) evaluates to true
(a > 3) || (b < 5) evaluates to true
(a < 3) || (b < 5) evaluates to false

!(a == 3) evaluates to true
!(a > 3) evaluates to false

示例5:逻辑运算符

#include 
using namespace std;

int main() {
    bool result;

    result = (3 != 5) && (3 < 5);     // true
    cout << "(3 != 5) && (3 < 5) is " << result << endl;

    result = (3 == 5) && (3 < 5);    // false
    cout << "(3 == 5) && (3 < 5) is " << result << endl;

    result = (3 == 5) && (3 > 5);    // false
    cout << "(3 == 5) && (3 > 5) is " << result << endl;

    result = (3 != 5) || (3 < 5);    // true
    cout << "(3 != 5) || (3 < 5) is " << result << endl;

    result = (3 != 5) || (3 > 5);    // true
    cout << "(3 != 5) || (3 > 5) is " << result << endl;

    result = (3 == 5) || (3 > 5);    // false
    cout << "(3 == 5) || (3 > 5) is " << result << endl;

    result = !(5 == 2);    // true
    cout << "!(5 == 2) is " << result << endl;

    result = !(5 == 5);    // false
    cout << "!(5 == 5) is " << result << endl;

    return 0;
}

输出

(3 != 5) && (3 < 5) is 1
(3 == 5) && (3 < 5) is 0
(3 == 5) && (3 > 5) is 0
(3 != 5) || (3 < 5) is 1
(3 != 5) || (3 > 5) is 1
(3 == 5) || (3 < 5) is 0
!(5 == 2) is 1
!(5 == 5) is 0

逻辑运算符程序的说明

5. C++按位运算符

在C++中,按位运算运算符用于对单个位执行操作。它们只能与charint数据类型一起使用。

Operator Description
& Binary AND
| Binary OR
^ Binary XOR
~ Binary One's Complement
<< Binary Shift Left
>> Binary Shift Right

要了解更多信息,请访问C++按位运算运算符。

除了运算符以上所讨论的,还有一些其他的运算符,如sizeof?.&等,不能整齐地分为一种或另一种类型。我们将在后面的教程中了解有关这些运算符的更多信息。