P1
sizeof运算符返回数据类型(必须加括号)或者变量的大小,单位是字节。
例如:cout<<sizeof(int)<<endl;
#include<iostream>
#include<climits>
int main(void)
{
using namespace std;
short n_short = SHRT_MAX;
int n_int = INT_MAX;
long n_long = LONG_MAX;
long long n_llong = LLONG_MAX;
cout << "short is " << sizeof(short) << " bytes." << endl;
cout << "int is " << sizeof(int) << " bytes." << endl;
cout << "long is " << sizeof n_long << " bytes." << endl;
cout << "long long is " << sizeof n_llong << " bytes." << endl;
cout << "Maximum values: " << endl;
cout << "short is " << n_short << endl;
cout << "int is " << n_int << endl;
cout << "long is " << n_long << endl;
cout << "long long " << n_llong << endl;
return 0;
}
C++还有其他初始化方法以及大花括号初始化器的初始化方法。
P2
无符号变量与有符号变量的溢出
#include<iostream>
#include<climits>
using namespace std;
int main(void)
{
short sam = SHRT_MAX;
unsigned short sue = sam;
cout << "Sam has " << sam << " dolloars and Sue has " << sue << " dolloars deposited." << endl;
cout << "Add $1 to each account." <<endl;
sam += 1;
sue += 1;
cout << "Now,Sam has " << sam << " dolloars and Sue has " << sue << " dolloars deposited." << endl;
sam = 0;
sue = 0;
sam -= 1;
sue -= 1;
cout << "Now,Sam has " << sam << " dolloars and Sue has " << sue << " dolloars deposited." << endl;
return 0;
}
int 为计算机处理效率最高的长度,如无其他要求就选择int数据类型。
不同进制的显示
#include<iostream>
using namespace std;
int main(void)
{
int cheat = 42;
int waist = 042;
int inseam = 0x42;
cout << "cheat = " << cheat << "(in dec)" << endl;
cout << oct;
cout << "waist = " << waist << "(in oct)" << endl;
cout << hex;
cout << "inseam = " << inseam << " (in hex)" << endl;
return 0;
}
char类型
输入时cin将字母转化为对应的码值并存入变量中,输出时cout将变量中的码值转化为对应的字符。这个过程由变量类型引导。
#include<iostream>
#include<climits>
using namespace std;
int main(void)
{
char ch;
cout << "Enter a character: " << endl;
cin >> ch;
cout << "Hello,thank you for the " << ch << " character" << endl;
return 0;
}
#include<iostream>
#include<climits>
using namespace std;
int main(void)
{
char ch = 'M';
int i = ch;
cout << "The ASCII code for " << ch << " is" << i << endl;
cout << "Add noe to the character code: " << endl;
ch += 1;
i = ch;
cout << "The ASCII code for " << ch << " is" << i << endl;
cout << "Displaying char ch using cout.put(ch): "<<endl;
cout.put(ch);
cout.put('!');
return 0;
}
#include<iostream>
#include<climits>
using namespace std;
int main(void)
{
cout << "Good morning!" << endl;
cout << "Good morning\n";
return 0;
}
const 限定符
创建常变量 const int Months = 12;
应在声明时常量初始化,否则常量会是一个不确定的值,且无法修改。
例如:
const int toes;
toes = 10;
浮点数
float有效位数的限制,优先选择double。
#include<iostream>
using namespace std;
int main(void)
{
cout.setf(ios_base::fixed, ios_base::floatfield);
float tub = 10.0 / 3.0;
const float million = 1.0E6;
cout << " tub = " << tub << endl;
cout << "A million tubs = " << million * tub << endl;
cout << "Ten million tubs = " << 10 * million * tub << endl;
double mint = 10.0 / 3.0;
cout << " mint = " << mint << endl;
cout << "A million mints = " << million * mint << endl;
cout << "Ten million mints = " << 10 * million * mint << endl;
return 0;
}
浮点数的运算精度会降低
典例
#include<iostream>
using namespace std;
int main(void)
{
float a = 2.34E22;
float b = a + 1;
cout << "a = " << a << endl;
cout << "b - a = " << b - a << endl;
return 0;
}
这是由于float类型只能保证六到七位数字,因此修改第23位对值没有影响。
其中32位有效位是指二进制位,而6位指的是10进制。共24位,三位表示一个数的话可以表示八个,四位表示一个数可以表示六个。十进制三位不够四位嫌多,所以有效位应该在6到8之间。
由于计算机中存储位置有限,而有些小数永远无法通过乘2得到精确值,故运算过程中精度会降低。
#include<iostream>
using namespace std;
int main(void)
{
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;
}
C++运算符
除法运算符的行为取决于操作数类型
#include<iostream>
using namespace std;
int main(void)
{
cout.setf(ios_base::fixed,ios_base::floatfield);
cout << "Integer division: 9/5 = " << 9 / 5 << endl;
cout << "float division: 9.0/5.0 = " << 9.0 / 5.0 << endl;
cout << "MIxed division: 9.0/5 = " << 9 / 5 << endl;
cout << "double division: 1E7/9.0 = " << 1E7 / 9.0 << endl;
cout << "Float constance: 1E7f/9.0f = " << 1E7f / 9.0f << endl;
return 0;
}
对不同类型的数进行运算时,C++将其转化为同一类型,而浮点常量默认是double类型。
求模运算
#include<iostream>
int main(void)
{
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 = " << stone << " stone, " << pounds << " pounds." << endl;
return 0;
}
类型转换
以下情况会发生类型转换
1.将一种算数类型的值赋值给另一种算数类型时。——将转换为接收变量的类型
#include<iostream>
int main(void)
{
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;
}
2.表达式中包含不同的数据类型是。
3.将参数传递给函数时。——取决于函数原型
强制类型转换——不改变被转换变量的值。
(typename)value 或者 typename(value)或者static_cast<typename> (value)
#include<iostream>
int main(void)
{
using namespace std;
int auks, bats, coots;
auks = 19.99 + 11.99;//默认为double,计算后再转换为int类型
bats = (int)19.99 + (int)11.99;
coots = int(19.99) + int(11.99);
cout << "auks = " << auks << endl;
cout << "bats = " << bats << endl;
cout << "coots = " << coots << endl;
char ch = 'z';
cout << "The code for " << ch << " is " << int(ch) << endl;
cout << static_cast<int>(ch) << endl;
return 0;
}