c++教程2操作符

2操作符

  • 算数运算
  • 赋值
  • 比较
  • 逻辑运算
  • 条件检查
  • size
  • 优先级
  • 数据类型转换

2.1 算数运算

arithmetic.cpp

#include <iostream>
using namespace std;

int main()
{
  int a = 8, b = 4;

  cout << "Addition result: " << (a + b) << endl;
  cout << "Subtraction result: " << (a - b) << endl;
  cout << "Multiplication result: " << (a * b) << endl;
  cout << "Division result: " << (a / b) << endl;
  cout << "Modulus result: " << (a % b) << endl;

  cout << "Postfix increment: " << a++ << endl;
  cout << "Postfix result: " << a << endl;
  cout << "Prefix increment: " << ++b << endl;
  cout << "Prefix result: " << b << endl;

  return 0;
}

执行

# ./arithmetic
Addition result: 12
Subtraction result: 4
Multiplication result: 32
Division result: 2
Modulus result: 0
Postfix increment: 8
Postfix result: 9
Prefix increment: 5
Prefix result: 5

2.2赋值

assign.cpp

#include <iostream>
using namespace std;

int main()
{
  int a , b;

  cout << "Assign values:  ";
  cout << "a = " << (a = 8) << "   ";
  cout << "b = " << (b = 4);

  cout << endl << "Add & assign:  ";
  cout << "a += b (8 += 4 ) a = " << (a += b);

  cout << endl << "Subtract & assign: ";
  cout << "a -= b (12 -= 4 ) a = " << (a -= b);

  cout << endl << "Multiply & assign: ";
  cout << "a *= b (8 *= 4 ) a = " << (a *= b);

  cout << endl << "Divide & assign: ";
  cout << "a /= b (32 /= 4 ) a = " << (a /= b);

  cout << endl << "Modulus & assign: ";
  cout << "a %= b (8 %= 4 )  a = " << (a %= b);

  cout << endl;

  return 0;
}

执行

# ./assign
Assign values:  a = 8   b = 4
Add & assign:  a += b (8 += 4 ) a = 12
Subtract & assign: a -= b (12 -= 4 ) a = 8
Multiply & assign: a *= b (8 *= 4 ) a = 32
Divide & assign: a /= b (32 /= 4 ) a = 8
Modulus & assign: a %= b (8 %= 4 )  a = 0

2.3比较

comparison.cpp

#include <iostream>
using namespace std;

int main()
{
  int nil = 0 , num = 0 , max = 1;
  char cap = 'A' , low = 'a';

  cout << "Equality comparisons: ";
  cout << "(0 == 0) " << (nil == num) << "(true)  ";
  cout << "(A == a) " << (cap == low) << "(false)";

  cout << endl << "Inequality comparison:  ";
  cout << "(0 != 1) " << (nil != max) << "(true)";

  cout << endl << "Greater comparison:  ";
  cout << "(0 > 1) " << (nil > max) << "(false)";

  cout << endl << "Lesser comparison:  ";
  cout << "(0 < 1) " << (nil < max) << "(true)";

  cout << endl << "Greater or equal comparison: ";
  cout << "(0 >= 0) " << (nil >= num) << "(true)";

  cout << endl << "Lesser or equal comparison: ";
  cout << "(1 <= 0) " << (max <= num) << "(false)";

  cout << endl;

  return 0;
}

执行

# ./comparison
Equality comparisons: (0 == 0) 1(true)  (A == a) 0(false)
Inequality comparison:  (0 != 1) 1(true)
Greater comparison:  (0 > 1) 0(false)
Lesser comparison:  (0 < 1) 1(true)
Greater or equal comparison: (0 >= 0) 1(true)
Lesser or equal comparison: (1 <= 0) 0(false)

2.4逻辑运算

#include <iostream>
using namespace std ;

int main()
{
  int a = 1 , b = 0 ; 

  cout << "AND logic: " << endl ;
  cout << "(a && a) " << (a && a) << "(true)  " ;
  cout << "(a && b) " << (a && b) << "(false)  " ;
  cout << "(b && b) " << (b && b) << "(false)" << endl ;

  cout << endl << "OR  logic: " << endl ;
  cout << "(a || a) " << (a || a) << "(true)  " ;
  cout << "(a || b) " << (a || b) << "(true)  " ;
  cout << "(b || b) " << (b || b) << "(false)" << endl ;

  cout << endl << "NOT logic: " << endl ;
  cout << "a = " << a << "  !a= " << !a << "  " ;
  cout << "b = " << b << "  !b= " << !b << endl ;

  return 0 ;
}

执行

# ./logic
AND logic:
(a && a) 1(true)  (a && b) 0(false)  (b && b) 0(false)

OR  logic:
(a || a) 1(true)  (a || b) 1(true)  (b || b) 0(false)

NOT logic:
a = 1  !a= 0  b = 0  !b= 1

2.5条件表达式

C++程序员最喜欢的测试操作符可能是?:"三元"操作符。这个运算符首先评估一个表达式的真假条件,然后根据评估结果返回两个指定值中的一个。由于这个原因,它也被称为"条件"运算符。

( test-expression ) ? if-true-return-this: if-false-return-this;

ternary.cpp

#include <iostream>
using namespace std;

int main()
{
  int a, b, max;
  a = 1, b = 2;

  cout << "Variable a value is: ";
  cout << ( (a != 1) ? "not 1, " : "1, " );
  cout << ( (a % 2 != 0) ? "odd" : "even" );

  cout << endl << "Variable b value is: ";
  cout << ( (b != 1) ? "not 1, " : "1, " );
	cout << ( (b % 2 != 0) ? "odd" : "even" );

  max = (a > b) ? a : b;
  cout << endl << "Greater value is: " << max << endl; 

  return 0;
}

执行

# ./ternary
Variable a value is: 1, odd
Variable b value is: not 1, even
Greater value is: 2

参考资料

2.6 size

变量的声明会分配系统内存,分配给该变量的值将被存储在那里。为此分配的内存量由你的系统和数据类型决定。
通常情况下,int数据类型被创建为默认的"long"值,它可以存储从+2,147,483,647到-2,147,483,648的数值。另一方面,如果int数据类型默认被创建为"short"值,它只能存储从+32,767到-32,768的值。

在声明变量时,可以在int关键字前加上short或long限定符,明确指定首选范围。如果确定永远不会超出限定范围,short int关键字可以节省内存空间。

在声明int变量时,默认情况下它可以包含正整数或负整数,也就是所谓的"有符号"值。如果变量始终只包含正整数,可以将其限定为无符号变量,以增加其最大可能值。通常情况下,unsigned int的取值范围为0到65,535之间,unsigned long int的取值范围为0到 4,294,967,295 之间。

任何变量的内存大小都可以使用 C++ 的 sizeof 运算符来确定。要检查的变量名称可以在sizeof运算符名称后的可选括号中指定。

sizeof.cpp

#include <iostream>
using namespace std;

int main()
{
  int num; int nums[50];
  short int number; unsigned int max;
  double pi; float decimal;
  char letter; char letters[50];
  bool isTrue;

  cout << "int size: " << sizeof( num ) << endl;
  cout << "50 int size: " << sizeof( nums ) << endl;
  cout << "short int size: " << sizeof( number ) << endl;
  cout << "unsigned int size: " << sizeof( max ) << endl;

  cout << "double size: " << sizeof( pi ) << endl ;
  cout << "float size: " << sizeof( decimal ) << endl;
  cout << "char size: " << sizeof( letter ) << endl;
  cout << "50 char size: " << sizeof( letters )<< endl;
  cout << "bool size: " << sizeof( isTrue ) << endl;

	return 0;
}

执行

# ./sizeof
int size: 4
50 int size: 200
short int size: 2
unsigned int size: 4
double size: 8
float size: 4
char size: 1
50 char size: 50
bool size: 1

2.7优先级

下表按降序列出了运算符优先级--最上面一行的运算符优先级最高;下面一行的运算符优先级依次降低。C++对包含多个相同优先级运算符的表达式进行运算的顺序是由"运算符关联性"决定的--将运算符分组为左侧运算符(LTR)或右侧运算符(RTL)。

#include <iostream>
using namespace std;

int main()
{
  // Multiply, add default precedence.
  int num = 1 + 4 * 3;
  cout << endl << "Default order:  " << num << endl;

  // Forced precedence.
  num = ( 1 + 4 ) * 3;
  cout << "Forced order:  " << num << endl << endl;

  // Left to right default direction.
  num = 7 - 4 + 2;
  cout<< "Default direction: " << num << endl;

  // Forced direction.
  num = 7 - ( 4 + 2 );
  cout << "Forced direction: " << num << endl;

  return 0;
}

执行

# ./precedence

Default order:  13
Forced order:  15

Default direction: 5
Forced direction: 1

2.8转换数据类型

.....
存储在变量中的任何数据都可以通过一个称为"casting"的过程强制(胁迫)到一个不同数据类型的变量中。

variable-name = ( data-type ) variable-name;

这是传统的转换形式,在C编程语言中也能找到。C++中的另一种新形式是使用带角括号的static_cast关键字,如下所示:

variable-name = static_cast < data-type > variable-name;

新版本避免了括号的使用,从而可以在源代码中更容易地识别转换,因为括号很容易与表达式中的括号混淆。较新形式的转换更受欢迎,但较旧形式的转换仍被广泛使用。

要准确存储算术运算的结果,通常需要进行转换,因为一个整数除以另一个整数总是会产生一个整数结果。例如,整数除以 7/2 的截断整数结果是3。

要存储精确的浮点运算结果,需要将结果转换成合适的数据类型,例如浮点数,如下所示:

float result = ( float ) 7 / 2 ;

或者使用较新的转存形式:

float result = static_cast < float > 7 / 2 ;

无论哪种情况,都应注意运算符先例在执行算术运算前会将第一个操作数转换为指定的数据类型,因此该语句最好写为

float result = static_cast < float > ( 7 ) / 2 ;

括号中的表达式 ( 7 / 2 ) 将首先对整数执行算术运算,因此整数结果将在投到 float 变量之前被截断,这不是我们想要的效果!

#include <iostream>
using namespace std;

int main()
{
  int num = 7 , factor = 2;
  char letter = 'A';
  float result = 0.0;

  // Plain division.
  cout << "Integer division: " << ( num / factor ) << endl;

  // Cast int to float.
  result = (float) (num) / factor;
   cout << "Cast division float: " << result << endl;

  // Cast char to int.
  num = static_cast <int> (letter);
  cout << "Cast character int: " << num << endl;

  // Cast int to char.
  letter = static_cast <char> (70);
  cout << "Cast integer char: " << letter << endl;

  return 0;
}

执行

# ./cast
Integer division: 3
Cast division float: 3.5
Cast character int: 65
Cast integer char: F

热门相关:凤逆天下:腹黑魔君妖娆后   我有一个进化点   惊世第一妃:魔帝,宠上身!   圣人门徒   凤逆天下:战神杀手妃