arith.cpp: some C++ arithmetic
#include <iostream> int main() { using namespace std; float hats, heads; cout.setf(ios_base::fixed, ios_base::floatfield); cout << "Enter a number: "; cin >> hats; cout << "Enter another number: "; cin >> heads; cout << "hats = " << hats << "; heads = " << heads << endl; cout << "hats + heads = " << hats + heads << endl; cout << "hats - heads = " << hats - heads << endl; cout << "hats * heads = " << hats * heads << endl; cout << "hats / heads = " << hats / heads << endl; return 0; }
1. 编译输出:
Enter a number: 23.45 Enter another number: 67.89 hats = 23.450001; heads = 67.889999 hats + heads = 91.339996 hats - heads = -44.439999 hats * heads = 1592.020508 hats / heads = 0.345412
2. 代码详解:
运算符和操作数构成了表达式。
/:除法。第一个数除以第二个数。如果两个操作数都是整数,则结果为商的整数部分,小数部分被丢弃。
%:求模。生成第一个数除以第二个数的余数。两个操作数必须都是整型。如19%6 = 1。
由于float类型表示有效位数的能力有限,输出的结果不精确。对于float类型,C++只保证6位有效位。
算数运算符遵循的代数优先级:先乘除,后加减。*、/、%优先级相同,+、-优先级相同。
当两个运算符的优先级相同时,C++将看操作数的结合性(associativity)。乘除是从做到右结合。
divide.cpp: integer and floating-point division
#include <iostream> int main() { using namespace std; cout.setf(ios_base::fixed, ios_base::floatfield); cout << "Integer division: 9/5 = " << 9 / 5 << endl; cout << "Floating-point division: 9.0/5.0 = " << 9.0 / 5.0 << endl; cout << "Mixed division: 9.0/5 = " << 9.0 / 5 << endl; cout << "double constants: 1e7/9.0 = " << 1.e7 / 9.0 << endl; cout << "float constants: 1e7f/9.0f = " << 1.e7f / 9.0f << endl; return 0; }
1. 编译输出:
Integer division: 9/5 = 1 Floating-point division: 9.0/5.0 = 1.800000 Mixed division: 9.0/5 = 1.800000 double constants: 1e7/9.0 = 1111111.111111 float constants: 1.e7f/9.0f = 1111111.125000
2. 代码详解:
除法运算符(/)的行为取决于操作数的类型。
如果两个操作数都是整数,则C++执行整数除法。这意味着结果的小数部分将被丢弃,结果为整数。
如果其中一个或两个操作数是浮点数,则小数部分将保留,结果为浮点数。
浮点常量在默认情况下为double类型。
运算符重载(operator overloading): 使用相同的符号进行多种操作。
int类型:9/5;long类型:9L/5L;double类型:9.0/5.0;float类型:9.0f/5.0f。所以/为运算符重载。
modulus.cpp: uses % operator to convert lbs to stone
#include <iostream> int main() { using namespace std; const int Lbs_per_stn = 14; int lbs; cout << "Enter your weight in pounds:"; cin >> lbs; int stone = lbs / Lbs_per_stn; int pounds = lbs % Lbs_per_stn; cout << lbs << " pounds are " << stone << " stone, " << pounds << " pound(s).\n"; return 0; }
1. 编译输出:
Enter your weight in pounds: 195 195 pounds are 13 stone, 13 pound(s).
2. 代码详解:
求模运算符%:返回整数除法的余数。
求模运算符中,要求操作数均为整型。
init.cpp: type changes on initialization
#include <iostream> int main() { using namespace std; cout.setf(ios_base::fixed, ios_base::floatfield); float tree = 3; int guess(3.9832); int debt = 7.2E12; cout << "tree = " << tree << endl; cout << "guess = " << guess << endl; cout << "debt = " << debt << endl; return 0; }
1. 编译输出:
tree = 3.000000 guess = 3 debt = 2147483647
2. 代码详解:
初始化和赋值进行的转换
将浮点型转换为整型时,C++采取截取(丢弃小数部分)而不是四舍五入(查找最接近的整数)。
float值对于int变量来说可能太大了,C++并没有定义结果应该是什么。
typecast.cpp: forcing type changes
#include <iostream> int main() { using namespace std; int auks, bats, coots; auks = 19.99 + 11.99; bats = (int)19.9 + (int)11.99; coots = int (19.99) +int (11.99); cout << "auks = " << auks << ", bats = " << bats; cout << ", coots = " << coots << endl; char ch = 'Z'; cout << "The code for " << ch << " is "; cout << int(ch) << endl; cout << "Yes, the code is " << static_cast<int>(ch) << endl; return 0; }
1. 编译输出:
auks = 31, bats = 30, coots = 30 The code for Z is 90 Yes, the code is 90
2. 代码详解:
强制类型转换不会修改变量本身,而是创建一个新的、指定类型的值,可以在表达式中使用这个值。
格式:(typeName) value — C语言 或 typeName (value) — C++。
static_cast<>:可用于将值从一种数值类型转换为另一种数值类型。static_cast<typeName>。
知识扩展:
1. 将使用大括号的初始化称为列表初始化(list-initialization)。
它不允许缩窄(narrowing),即变量的类型可能无法表示赋给它的值。如不允许将浮点型转换位整型。
在不同的整型之间转换或将整型转换为浮点型可能被允许,条件是编译器知道目标变量能够正确地存储赋给它的值。
2. C++将bool、char、unsigned char、signed char 和 short值转换为int。
true被转换为1,false被转换为0。这些转换被称为整型提升(integeral promotion)。
3. 有符号整型从高到低:long long、long、int、short 和 signed char。无符号整型的排列顺序与有符号整型相同。类型char、signed char 和 unsigned char的级别相同。类型bool的级别最低。
4. 传递参数时的类型转换通常由C++函数原型控制。
5. 如果使用关键字auto,而不指定变量的类型,编译器将变量的类型设置成与初始值相同。
联系客服