3.7 运算符及其使用
3.7.1 赋值
赋值操作由运算符“=”实现。这意味着“取右边的值(右值)并把它拷贝给左边(左值)”。右值可以是任意的变量、常量或能产生值的表达式,但是左值必须是一个明确命名的变量。
3.7.2 数学运算符
基本的数学运算符和在大多数的编程语言中使用的一样:加(+)、减(-)、除(/)、乘(*)和取模(%;从整数相除得到余数)。整数相除会截取结果的整数部分(不舍入)。浮点数不能使用取模运算符。
C和C++也使用一种简化的符号来同时执行操作和赋值。即:+=、-=、*=、/=、%=等。下面例子显示了数学运算符的使用:
//:C03:Mathops.cpp
// Mathematical operators
#include <iostream>
using namespace std;
// A maco to display a string and a value
#define PRINT(STR, VAR) cout << STR " = " << VAR << endl;
int main()
{
int i, j, k;
float u, v, w;
cout << "enter an integer: ";
cin >> j;
cout << "enter another integer: ";
cin >> k;
PRINT("j", j);
PRINT("k", k);
i = j + k;
PRINT("j + k", i);
i = j - k;
PRINT("j - k", i);
i = j * k;
PRINT("j - k", i);
i = j / k;
PRINT("j / k", i);
i = j % k;
PRINT("j % k", i);
// The following only woeks with integers:
j %= k;
PRINT("j %= k", j);
cout << "Enter a floating-point number: ";
cin >> v;
cout << "Enter another floating-point number: ";
cin >> w;
PRINT("v", v);
PRINT("w", w);
u = v + w;
PRINT("v + w", u);
u = v - w;
PRINT("v - w", u);
u = v / w;
PRINT("v / w", u);
u = v * w;
PRINT("v * w", u);
// The following works for ints,chars,and doubles too:
u += v;
PRINT("u += v", u);
u -= v;
PRINT("u -= v", u);
u *= v;
PRINT("u *= v", u);
u /= v;
PRINT("u /= v", u);
return 0;
}
3.7.2.1 预处理宏介绍
跟在宏名后面的括号中的参数会被闭括号后面的所有代码替代。只要在调用宏的地方,预处理程序就删除原来名字并替代代码,所以使用宏名时编译器不会报告任何错误信息,它并不对参数做任何类型检查。
3.7.3 关系运算符
关系运算符在操作数之间建立一种关系。如果关系为真,则产生布尔值true;如果关系为假,则产生布尔值false。关系运算符有:小于(<)、大于(>)、小于等于(<=)、大于等于(>=)、等于(==)、不等于(!=)。
3.7.4 逻辑运算符
逻辑运算符“与”(&&)和“或”(||)依据它们的参数的逻辑关系产生true和false。
注意:在C++中,如果语句是非零值则为true,如果是零则为false。 例:
//:C03:Boolean.cpp
// Relational and logical operators
#include <iostream>
using namespace std;
int main()
{
int i, j;
cout << "Enter an integer: ";
cin >> i;
cout << "Enter another integer: ";
cin >> j;
cout << "i > j is " << (i > j) << endl;
cout << "i < j is " << (i < j) << endl;
cout << "i >= j is " << (i >= j) << endl;
cout << "i <= j is " << (i <= j) << endl;
cout << "i == j is " << (i == j) << endl;
cout << "i != j is " << (i != j) << endl;
cout << "i && j is " << (i && j) << endl;
cout << "i || j is " << (i || j) << endl;
cout << " (i < 10) && (j < 10) is " << ((i < 10) && (j < 10)) << endl;
}
3.7.5 位运算符
位运算符允许在一个数中处理个别的位。位运算符对参数中的相应位做布尔代数运算来产生结果。
如果连个输入位都是1,则“与”运算符(&)在结果位上产生1,否则为0.如果两个输入位有一个是1,则“或”运算符(|)在结果位上长生1,只有当两个输入位都是0时,结果位才为0.如果两个输入位之一是1而不是同时为1,则位的异或运算法xor(^)的结果位为1.位的“非”运算符(~)是一个一元运算符。
位运算符可以和“=”结合来统一运算和赋值:&=、|=、^=等。
3.7.6 移位运算符
移位运算符也 是对位的操纵。左移位运算符(<<)引起运算符左边的操作数向左移动,移动位数由运算符后面的操作数指定。右移位运算符(>>)引起运算符左边的操作数向右移动,移动位数由运算符后面的操作数指定。例:
// :C03:printBinary.h
// Display a byte in binary
void printBinary(const unsigned char val);
实现为:
// :C03:PrintBinary.cpp
#include <iostream>
using namespace std;
void printBinary(const unsigned char val)
{
for (int i = 7; i >= 0; i--)
{
if (val & (1 << i))
{
cout << "1";
}
else
{
cout << "0";
}
}
}
最后,在例子中使用下面的函数显示位操作运算符:
// :C03:Bitwise.cpp
// printBinary
// Demonstration of bit manipulation
#include <iostream>
#include "PrintBainary.h"
using namespace std;
// A macro to save typing;
#define PR(STR, EXPR) \
cout << STR; printBinary(EXPR); cout << endl;
int main()
{
unsigned int getval;
unsigned char a, b;
cout << "Enter a number between 0 and 255: ";
cin >> getval;
a = getval;
PR("a in binary: ", a);
cout << "Enter a number between 0 and 255: ";
cin >> getval;
b = getval;
PR("b in binary: ", b);
PR("a | b = ", a | b);
PR("a & b = ", a & b);
PR("a ^ b = ", a ^ b);
PR("~a = ", ~a);
PR("~b = ", ~b);
// An interesting bit pattern;
unsigned char c = 0x5A;
PR("c in binary: ", c);
a |= c;
PR("a |= c; a = ", a);
b &= c;
PR("b &= c; b = ", b);
b ^= c;
PR("b ^= c; b = ", b);
}