学习《C++ Primer Plus》(第二篇)
变量
C++变量命名规则:
- 在名称中只能使用字母、数字和下划线
- 名称的第一个字符不能是数字
- 区分大写字符和小写字符
- 以两个下划线或下划线和大写字母开头的名称被保留给实现(编译器及其使用的资源)使用。以一个下划线开头的名称被保留给实现,用作全局标识符
- C++对于名称的长度没有限制,名称中所有的字符都有意义,但有些平台有长度限制
C++整数类型的宽度是与系统有关的:
- short至少16位
- int至少与short一样长
- long至少32位,且至少与int一样长
- long long至少64位,且至少与long一样长
// limits.cpp
#include <iostream>
#include <climits>
int main()
{
using namespace std;
int n_int = INT_MAX;
short n_short = SHRT_MAX;
long n_long = LONG_MAX;
long long n_llong = LLONG_MAX;
cout << "int is " << sizeof(int) << " bytes." << endl;
cout << "short is " << sizeof n_short << " bytes." << endl;
cout << "long is " << sizeof n_long << " bytes." << endl;
cout << "long long is " << sizeof n_llong << " bytes." << endl;
cout << endl;
cout << "Maximum values:" << endl;
cout << "int: " << n_int << endl;
cout << "short: " << n_short << endl;
cout << "long: " << n_long << endl;
cout << "long long: " << n_llong << endl << endl;
cout << "Minimum int value = " << INT_MIN << endl;
cout << "Bits per byte = " << CHAR_BIT << endl;
return 0;
}
C++初始化语法
// 方式1
int owls = 101;
// 方式2
int wrens(432);
// 方式3
int emus{7};
int rheas = {12};
int rocs = {}; // 初始化为0
int psychics{}; // 初始化为0
注意:如果不对函数内部定义的变量进行初始化,该变量的值将是不确定的。其值为该变量创建之前,相应内存单元保存的值。
数据超出类型限制产生的后果:
// exceed.cpp
#include <iostream>
#define ZERO 0
#include <climits>
int main()
{
using namespace std;
short sam = SHRT_MAX;
unsigned short sue = sam;
cout << "Sam has " << sam << " dollars and Sue has " << sue;
cout << " dollars deposited." << endl
<< "And $1 to each account." << endl << "Now ";
sam = sam + 1;
sue = sue + 1;
cout << "Sam has " << sam << " dollars and Sue has " << sue;
cout << " dollars deposited.\nPoorSam!" << endl;
sam = ZERO;
sue = ZERO;
cout << "Sam has " << sam << " dollars and Sue has " << sue;
cout << " dollars deposited." << endl;
sam = sam - 1;
sue = sue - 1;
cout << "Sam has " << sam << " dollars and Sue has " << sue;
cout << " dollars deposited." << endl << "Lucky Sue!" << endl;
return 0;
}
整型字面值:
// hexoct1.cpp
// 三种基数的整型字面值
#include <iostream>
int main()
{
using namespace std;
int chest = 42;
int waist = 0x42;
int inseam = 042;
cout << "Monsieur cuts a strinking figure!\n";
cout << "chest = " << chest << " (42 in decimal)\n";
cout << "waist = " << waist << " (0x42 in hex)\n";
cout << "inseam = " << inseam << " (042 in octal)\n";
return 0;
}
// hexoct2.cpp
// 显示不同进制的数
#include <iostream>
using namespace std;
int main()
{
using namespace std;
int chest = 42;
int waist = 42;
int inseam = 42;
cout << "Monsieur cuts a strinking figure!" << endl;
cout << "chest = " << chest << " (decimal for 42)" << endl;
cout << hex;
cout << "waist = " << waist << " (hexadecimal for 42)" << endl;
cout << oct;
cout << "inseam = " << inseam << " (octal for 42)" << endl;
return 0;
}
char
C++对字符使用单引号,对字符串使用双引号。
// chartype.cpp
// char类型
#include <iostream>
int main()
{
using namespace std;
char ch;
cout << "Enter a character: " << endl;
cin >> ch;
cout << "Hola! ";
cout << "Thank you for the " << ch << " character." << endl;
return 0;
}
// morechar.cpp
// char类型和int类型对照
#include <iostream>
int main()
{
using namespace std;
char ch = 'M';
int i = ch;
cout << "The ASCII code for " << ch << " is " << i << endl;
cout << "Add one to the character code:" << endl;
ch = ch + 1;
i = ch;
cout << "The ASCII code for " << ch << " is " << i << endl;
cout << "Displaying char ch using cout.put(ch): ";
cout.put(ch);
cout.put('!');
cout << endl << "Done" << endl;
return 0;
}
// bondini.cpp
// 转义序列
#include <iostream>
int main()
{
using namespace std;
cout << "\aOperation \"HyperHype\" is now activated!\n";
cout << "Enter your agent code: ________\b\b\b\b\b\b\b\b";
long code;
cin >> code;
cout << "\aYou entered " << code << "...\n";
cout << "\aCode verified! Proceed with Plan Z3!\n";
return 0;
}
bool类型
bool的值:true、false。
当bool转换为int时,true为1,false为0。
任何数字值或指针值都可以被隐式转换为bool值。任何非零值都被转换为true,零被转换为false。
const限定符
// 创建常量的通用格式
const type name = value;
const比#define好的地方
- 能够明确指定类型
- 可以指定作用域
- const可以用于更复杂的类型
浮点数
浮点数表示方法
- 常规表示
- E表示法
// floatnum.cpp
// 浮点类型
#include <iostream>
int main()
{
using namespace std;
cout.setf(ios_base::fixed, ios_base::floatfield);
float tub = 10.0 / 3.0;
double mint = 10.0 / 3.0;
const float millon = 1.0e6;
cout << "tub = " << tub;
cout << ", a millon tubs = " << millon * tub;
cout << ", \nand ten millon tubs = ";
cout << 10 * millon * tub << endl;
cout << "mint = " << mint << " and a millon mints = ";
cout << millon * mint << endl;
return 0;
}
// fltadd.cpp
// 浮点数的精度问题
#include <iostream>
int main()
{
using namespace std;
float a = 2.34E+22f;
float b = a + 1.0f;
cout << "a = " << a << endl;
cout << "b - a = " << b - a << endl;
return 0;
}
C++算数运算符
// arith.cpp
// 算术运算符
#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;
}
// divide.cpp
// 除法运算
#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 = ";
cout << 9.0 / 5.0 << endl;
cout << "Mixed division: 9.0/5 = " << 9.0 / 5 << endl;
cout << "double constants: 1e7/9.0 = ";
cout << 1e7 / 9.0 << endl;
cout << "float constants: 1e7f/9.0f = ";
cout << 1e7f / 9.0f << endl;
return 0;
}
// modulus.cpp
// 取余运算符
#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;
}
// init.cpp
// 初始化的类型转换
#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;
}
强制类型转换:
(typename) value;
typename (value);
// typecast.cpp
// 强制类型转换
#include <iostream>
int main()
{
using namespace std;
int auks, bats, coots;
auks = 19.99 + 11.99;
bats = (int)19.99 + (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 ";
cout << static_cast<int>(ch) << endl;
return 0;
}
auto关键字:可以进行自动类型推断。
编程练习
// 编写一个小程序,要求用户使用一个整数指出自己的身高(单位为英寸),
// 然后将身高转换为英尺和英寸。该程序使用下划线字符来指示输入位置。
// 另外,使用一个const符号常量来表示转换因子。
#include <iostream>
int main()
{
using namespace std;
const int Feet2Inch = 12;
int inch;
cout << "Enter your height(inch): ____\b\b\b\b";
cin >> inch;
int feet = inch / Feet2Inch;
inch = inch % Feet2Inch;
cout << "Your height is " << feet << " feet and "
<< inch << " inch" << endl;
return 0;
}
// 计算BMI
#include <iostream>
int main()
{
using namespace std;
const int Feet2Inch = 12;
const double Inch2Meter = 0.0254;
const double Kilogram2Pound = 2.2;
int feet, inch, pound;
cout << "Enter your height(feet and inch): ";
cin >> feet >> inch;
cout << "Enter your weight(pound): ";
cin >> pound;
double meter = (feet * Feet2Inch + inch) * Inch2Meter;
double kilogram = pound / Kilogram2Pound;
double bmi = kilogram / (meter * meter);
cout << "Your BMI = " << bmi << endl;
return 0;
}
// 以度、分、秒的形式输入纬度,以度的形式输出
#include <iostream>
int main()
{
using namespace std;
const double Degree2Minute = 60.0;
const double Minute2Second = 60.0;
cout << "Enter a latitude in degrees, minutes, and seconds: " << endl;
int degree, minute, second;
cout << "First, enter the degrees: ";
cin >> degree;
cout << "Next, enter the minutes of arc: ";
cin >> minute;
cout << "Finally, enter the seconds of arc: ";
cin >> second;
double result = degree + minute / Degree2Minute + second / Minute2Second / Degree2Minute;
cout << degree << " degrees, " << minute << " minutes, "
<< second << " seconds = " << result << " degrees" << endl;
return 0;
}
// 输入秒,输出天、小时、分钟、秒
#include <iostream>
int main()
{
using namespace std;
const int HoursOfDay = 24;
const int MinutesOfHour = 60;
const int SecondsOfMinute = 60;
cout << "Enter the number of seconds: ";
long seconds;
cin >> seconds;
long tmp = seconds;
int days = seconds / (HoursOfDay * MinutesOfHour * SecondsOfMinute);
seconds = seconds % (HoursOfDay * MinutesOfHour * SecondsOfMinute);
int hours = seconds / (MinutesOfHour * SecondsOfMinute);
seconds = seconds % (MinutesOfHour * SecondsOfMinute);
int minutes = seconds / SecondsOfMinute;
seconds = seconds % SecondsOfMinute;
cout << tmp << " seconds = " << days << " days, " << hours << " hours, "
<< minutes << " minutes, " << seconds << " seconds" << endl;
return 0;
}
// 中国人口占比
#include <iostream>
int main()
{
using namespace std;
long long populationOfChina;
long long populationOfWorld;
cout << "Enter the world's population: ";
cin >> populationOfWorld;
cout << "Enter the population of China: ";
cin >> populationOfChina;
double percentage = 1.0 * populationOfChina / populationOfWorld;
cout << "The population of China is " << percentage * 100
<< "% of the world population." << endl;
return 0;
}
// 每100公里耗油量
#include <iostream>
int main()
{
using namespace std;
double gas, distance;
cout << "Enter gas weight(L): ";
cin >> gas;
cout << "Enter distance(km): ";
cin >> distance;
double per = 100 * gas / distance;
cout << "100km use " << gas << "L gas" << endl;
return 0;
}
// 耗油量转换
#include <iostream>
int main()
{
using namespace std;
const double Gallon2Litre = 3.875;
const double HundredKm2Mile = 62.14;
double e;
cout << "Enter oil consumption(L/100km): ";
cin >> e;
double us = HundredKm2Mile / (e / Gallon2Litre);
cout << e << " L/100km = " << us << " mpg" << endl;
return 0;
}