Šajā apmācībā mēs uzzināsim par dažādiem operatoru veidiem C ++, izmantojot piemērus. Programmējot, operators ir simbols, kas darbojas ar vērtību vai mainīgo.
Operatori ir simboli, kas veic darbības ar mainīgajiem un vērtībām. Piemēram, +
ir operators, ko izmanto saskaitīšanai, bet -
ir operators, kas tiek izmantots atņemšanai.
Operatorus C ++ var iedalīt 6 tipos:
- Aritmētiskie operatori
- Uzdevumu operatori
- Relāciju operatori
- Loģiskie operatori
- Operatori bitiem
- Citi operatori
1. C ++ aritmētiskie operatori
Aritmētiskos operatorus izmanto, lai veiktu aritmētiskās darbības ar mainīgajiem un datiem. Piemēram,
a + b;
Šeit +
operators tiek izmantots, lai pievienotu divus mainīgos a un b. Līdzīgi ir arī citi citi aritmētiskie operatori C ++.
Operators | Darbība |
---|---|
+ | Papildinājums |
- | Atņemšana |
* | Reizināšana |
/ | Nodaļa |
% | Modulo darbība (atlikums pēc sadalīšanas) |
1. piemērs: Aritmētiskie operatori
#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; )
Rezultāts
a + b = 9 a - b = 5 a * b = 14 a / b = 3 a% b = 1
Lūk, operatori +
, -
un *
aprēķināt saskaitīšanu, atņemšanu un reizināšanu attiecīgi, kā mēs varētu būt gaidāms.
/ Nodaļas operators
Ievērojiet darbību (a / b)
mūsu programmā. /
Operators ir sadalījums operators.
Kā redzams no iepriekš minētā piemēra, ja vesels skaitlis tiek dalīts ar citu veselu skaitli, mēs saņemsim koeficientu. Tomēr, ja dalītājs vai dividende ir peldošā komata skaitlis, rezultātu iegūsim decimāldaļās.
C ++ gadījumā 7/2 ir 3 7,0 / 2 ir 3,5 7 / 2,0 ir 3,5 7,0 / 2,0 ir 3,5
% Modulo operators
Moduļa operators %
aprēķina atlikušo daļu. Kad a = 9
dala ar b = 4
, atlikums ir 1 .
Piezīme . %
Operatoru var izmantot tikai ar veseliem skaitļiem.
Pieauguma un samazināšanas operatori
C ++ nodrošina arī pieauguma un samazināšanas operatorus: ++
un --
attiecīgi. ++
palielina operanda vērtību par 1 , bet --
samazina par 1 .
Piemēram,
int num = 5; // increasing num by 1 ++num;
Šeit num vērtība tiek palielināta līdz 6 no sākotnējās vērtības 5 .
2. piemērs: pieauguma un samazināšanas operatori
// 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; )
Rezultāts
rezultāts_a = 11 rezultāts_b = 99
Iepriekš minētajā programmā mēs izmantojām ++
un --
operatoru kā prefiksus . Šos operatorus mēs varam izmantot arī kā postfix .
Ir neliela atšķirība, ja šie operatori tiek izmantoti kā prefikss, salīdzinot ar tiem, kad tos izmanto kā postfiksu.
Lai uzzinātu vairāk par šiem operatoriem, apmeklējiet pieauguma un samazināšanas operatorus.
2. C ++ uzdevumu operatori
Programmā C ++ piešķiršanas operatorus izmanto, lai mainīgajiem piešķirtu vērtības. Piemēram,
// assign 5 to a a = 5;
Šeit mēs esam piešķīruši 5
mainīgajam a vērtību.
Operators | Piemērs | Līdzvērtīgs |
---|---|---|
= | 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. piemērs: uzdevumu operatori
#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; )
Rezultāts
temp = 2 a = 9
3. C ++ operāciju operatori
A relational operator is used to check the relationship between two operands. For example,
// checks if a is greater than b a> b;
Here, >
is a relational operator. It checks if a is greater than b or not.
If the relation is true, it returns 1 whereas if the relation is false, it returns 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 |
Example 4: Relational Operators
#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 < b; // false cout < 5 is " << result << endl; result = a < b; // true cout << "3 < 5 is " << result <= b; // false cout <= 5 is " << result << endl; result = a <= b; // true cout << "3 <= 5 is " << result << endl; return 0; )
Output
3 == 5 is 0 3 != 5 is 1 3> 5 is 0 3 = 5 is 0 3 <= 5 is 1
Note: Relational operators are used in decision making and loops.
4. C++ Logical Operators
Logical operators are used to check whether an expression is true or false. If the expression is true, it returns 1 whereas if the expression is false, it returns 0.
Operator | Example | Meaning |
---|---|---|
&& | expression1 && expression2 | Logical AND. True only if all the operands are true. |
|| | expression1 || expression2 | Logical OR. True if at least one of the operands is true. |
! | !expression | Logical NOT. True only if the operand is false. |
In C++, logical operators are commonly used in decision making. To further understand the logical operators, let's see the following examples,
Suppose, a = 5 b = 8 Then, (a> 3) && (b> 5) evaluates to true (a> 3) && (b 3) || (b> 5) evaluates to true (a> 3) || (b < 5) evaluates to true (a < 3) || (b 3) evaluates to false
Example 5: Logical Operators
#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 < 5); // false cout < 5) is " << result << endl; result = (3 != 5) || (3 < 5); // true cout << "(3 != 5) || (3 < 5) is " << result < 5); // true cout < 5) is " << result < 5); // false cout < 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; )
Output
(3 != 5) && (3 < 5) is 1 (3 == 5) && (3 5) is 0 (3 != 5) || (3 5) is 1 (3 == 5) || (3 < 5) is 0 !(5 == 2) is 1 !(5 == 5) is 0
Explanation of logical operator program
(3 != 5) && (3 < 5)
evaluates to 1 because both operands(3 != 5)
and(3 < 5)
are 1 (true).(3 == 5) && (3 < 5)
evaluates to 0 because the operand(3 == 5)
is 0 (false).(3 == 5) && (3> 5)
evaluates to 0 because both operands(3 == 5)
and(3> 5)
are 0 (false).(3 != 5) || (3 < 5)
evaluates to 1 because both operands(3 != 5)
and(3 < 5)
are 1 (true).(3 != 5) || (3> 5)
evaluates to 1 because the operand(3 != 5)
is 1 (true).(3 == 5) || (3> 5)
evaluates to 0 because both operands(3 == 5)
and(3> 5)
are 0 (false).!(5 == 2)
evaluates to 1 because the operand(5 == 2)
is 0 (false).!(5 == 5)
novērtē līdz 0, jo operands(5 == 5)
ir 1 (patiess).
5. Operatori C ++
Programmā C ++ operētājsistēmas bitiem tiek izmantotas, lai veiktu darbības ar atsevišķiem bitiem. Tos var izmantot tikai blakus char
un int
datu tipiem.
Operators | Apraksts |
---|---|
& | Binārais UN |
| | Binārs VAI |
^ | Binārais XOR |
~ | Binārais papildinājums |
<< | Binārā nobīde pa kreisi |
>> | Bināra maiņa pa labi |
Lai uzzinātu vairāk, apmeklējiet C ++ operatorus bitiem.
Neatkarīgi no operatoriem apspriesta iepriekš, ir daži citi operatori, piemēram sizeof
, ?
, .
, &
, uc, ko nevar glīti klasificēti vienā vai cita veida. Mēs uzzināsim vairāk par šiem operatoriem turpmākajās apmācībās.