2.1变量,数据和数据类型
定义变量的方法:类似 int apple_count {15}
无符号的整数类型:在带符号的整数类型前面加上unsigned关键字,例如unsigned char,unsigned short或unsigned long,就可以指定只存储非负值的整数类型。每个不带符号的类型都不同于带符号的类型,但是占用相同的内存空间。
定义有固定值的变量
Const unsigned int toe_count{2u};
const告诉编译器,toe_count不能被修改。尝试修改就会在编译间标记为错误,使用const关键字可以固定任何类型的变量值。
2.2整形字面量
十进制整形字面量:不带符号的整形字面量有u或U后缀。
Long long类型的字面量分别有L和LL后缀。
十六进制的整形字面量:都要加上ox或者OX前缀。
八进制的整形字面量:都要加上O作为前缀。
二进制字面量:写为带有Ob或者OB的一系列二进制数(0或1)
2.3整数的计算
转换码,英尺,英寸
#include <iostream>
int main(){
unsigned int yards{},feet{},inches{};
std::cout<< "enter a distance as yards, feet, and inches"
<< "with the three values separated by spaces:"
<< std::endl;
std::cin>> yards>> feet>> inches;
const unsigned int feet_per_yard{3u};
const unsigned int inches_per_foot{12u};
unsigned int total_inches{};
total_inches=inches+inches_per_foot*(yards*feet_per_yard+feet);
std::cout<< "the distances corresponds to "<< total_inches<<"inches.\n";
std::cout<< "enter a distance in inches: ";
std::cin>>total_inches;
feet=total_inches/inches_per_foot;
yards=feet/feet_per_yard;
feet=feet%feet_per_yard;
std::cout<<"the distances corresponds to "
<<yards<<"yards "
<<feet<<"feet "
<<inches<<"inches."<<std::endl;
}
2.5 using的声明和指令
Using namespace std;
2.6 sizeof运算符
Sizeof运算符可以得到某类型,变量或表达式结果占用的字节数
2.8定义浮点变量
<cfloat>标准库:<cfloat>标准库头文件包含于编译器的浮点操作信息。
FLT_ ,DBL_和LDBL_表示分别与float,double和long double类型相关
FLT_EPSILON DBL_EPSILON和LDBL_EPSILON是可以加到1.0上的最小值,并且得到不同的结果。
FLT_MANT_DIG,DBL_MANT_DIG,LDBL_MANT_DIG是尾数的位数。
FLT_MAX,DBL_MAX,LDBL_MAX是可以表示的最大的非零浮点数。
FLT_MIN,DBL_MIN,LDBL_MIN是可以表示的最小的非零浮点数。
#include <iostream>
#include <cfloat>
using namespace std;
int main(){
cout<<"the mantissa for type float has "
<<FLT_MANT_DIG
<<" bits."
<<endl;
cout<<"the maximum value of type float is "
<<FLT_MAX<<endl;
cout<<"the minimum non-zero value of type float is "
<<FLT_MIN
<<endl;
return 0;
}
无效的浮点结果
一个非0的正数除以0时,结果就是+infinity
一个非0的负数除以0时,结果就是-infinity
0/0 或者 无穷大/无穷大 就是显示NAN
#include <iostream>
int main(){
double a{1.5},b{},c{},result{};
result = a / b;
std::cout<<a<<" / "<<b<<" = "<<result<<std::endl;
std::cout<<result<<" + "<<a<<" = "<<result + a<<std::endl;
result = b / c;
std::cout<<b<<" / "<<c<<" = "<<result<<std::endl;
return 0;
}
2.9数值函数
<cmath>头文件
下面是一个浮点示例,假设要构建一个圆形的池塘来养鱼,通过研究发现,必须保证该池塘的表面积为2平方英尺,才能确保每条鱼有6英寸长。本例需要确定池塘的直径,以确保鱼有足够的空间。
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main(){
//2 square feet pond surface for every 6 inches of fish
const double fish_factor{2.0/0.5};
const double inches_per_foot{12.0};
const double pi{3.14159265};
double fish_count{};//number of fish
double fish_length{};//average length of fish
cout<<"Enter the number of fish you want to keep: ";
cin >>fish_count;
cout<<"enter the average fish length in inches: ";
cin >>fish_length;
fish_length /= inches_per_foot;//convert to feet
//calculate the required surface area
double pond_area{fish_count*fish_length*fish_factor};
//calculate the required surface area
double pond_diameter(2.0*sqrt(pond_area/pi));
cout<<"\npond diameter required for"<<fish_count<<" fish is "
<<scientific<<setprecision(2)
<<pond_diameter<<" feet.\n";
}
2.10流输的格式化
头文件<iomanip>
▲setw(n)用法:通俗地讲就是预设宽度
如 cout<<setw(5)<<255<<endl;
结果是:
(空格)(空格)255
▲setfill(char c) 用法:就是在预设宽度中如果已存在没用完的宽度大小,则用设置的字符c填充
如 cout<<setfill('@')<<setw(5)<<255<<endl;
结果是:
@@255
▲setbase(n)用法:将数字转换为 n 进制.
如 cout<<setbase(8)<<setw(5)<<255<<endl;
cout<<setbase(10)<<setw(5)<<255<<endl;
cout<<setbase(16)<<255<<endl;
结果是:
(空格)(空格)377
(空格)(空格) 255
(空格)(空格) f f
▲setprecision(n)用法:
使用setprecision(n)可控制输出流显示浮点数的数字个数。C++默认的流输出数值有效位是6。
如果setprecision(n)与setiosflags(iOS::fixed)合用,可以控制小数点右边的数字个数。setiosflags(ios::fixed)是用定点方式表示实数。
如果与setiosnags(ios::scientific)合用, 可以控制指数表示法的小数位数。setiosflags(ios::scientific)是用指数方式表示实数。
例如,下面的代码分别用浮点、定点和指数方式表示一个实数:
#include <iostream.h>
#include <iomanip.h> //要用到格式控制符
void main()
{
double amount = 22.0/7;
cout <<amount <<endl;
cout <<setprecision(0) <<amount <<endl
<<setprecision(1) <<amount <<endl
<<setprecision(2) <<amount <<endl
<<setprecision(3) <<amount <<endl
<<setprecision(4) <<amount <<endl;
cout <<setiosflags(ios::fixed);
cout <<setprecision(8) <<amount <<endl;
cout <<setiosflags(ios::scientific)
<<amount <<endl;
cout <<setprecision(6); //重新设置成原默认设置
}
运行结果为:
3.14286
3
3
3.1
3.14
3.143
3.14285714
3.14285714e+00
该程序在32位机器上运行通过。
在用浮点表示的输出中,setprecision(n)表示有效位数。
第1行输出数值之前没有设置有效位数,所以用流的有效位数默认设置值6:第2个输出设置了有效位数0,C++最小的有效位数为1,所以作为有效位数设置为1来看待:第3~6行输出按设置的有效位数输出。
在用定点表示的输出中,setprecision(n)表示小数位数。
第7行输出是与setiosflags(ios::fixed)合用。所以setprecision(8)设置的是小数点后面的位数,而非全部数字个数。
在用指数形式输出时,setprecision(n)表示小数位数。
第8行输出用setiosflags(ios::scientific)来表示指数表示的输出形式。其有效位数沿用上次的设置值8
▲setw(n)用法:是设置域宽。
就是你的输出要占多少个字符
比如:
cout<<setw(5)<<12345<<endl;
就输出
12345
cout<<setw(6)<<12345<<endl;
输出
(空格)12345
而如果你要输出的字符宽度超出了setw(n)的n值,就按输出字符的宽度输出。
如你的cout<<setw(4)<<12.3456<<endl;
就输出12.3456
2.11混合的表达式和类型转换
Static_cast<type_to_convert_to>(表达式)
#include <iostream>
using namespace std;
int main(){
const unsigned int feet_per_yard{3};
const unsigned int inches_per_foot{12};
double length{};
unsigned int yards{};
unsigned int feet{};
unsigned int inches{};
cout<<"enter a length in yards as decimal: ";
cin >>length;
yards = static_cast<unsigned int>(length);
feet = static_cast<unsigned int>((length-yards)*feet_per_yard);
inches= static_cast<unsigned int>(length*feet_per_yard*inches_per_foot) % (inches_per_foot);
cout<<length<<" yards converts to "
<<yards <<" yards "
<<feet <<" feet "
<<inches<<" inches "
<<endl;
return 0;
}
2.12确定数值的上下限
std::numeric_limits<double>::max();
std::numeric_limits<double>::min();表示数值类型的最大值和最小值
std::numeric_limits<type_name>::digits返回二进制数字的位数
#include <iostream>
#include <limits>
using namespace std;
int main(){
cout<<"the range for type short is from "
<<numeric_limits<short>::min() <<" to "
<<numeric_limits<short>::max() <<endl;
cout<<"the range for type int is from "
<<numeric_limits<int>::min() <<" to "
<<numeric_limits<int>::max() <<endl;
cout<<"the range for type long is from "
<<numeric_limits<long>::min() <<" to "
<<numeric_limits<long>::max() <<endl;
cout<<"the range for type double is from "
<<numeric_limits<double>::min()<<" to "
<<numeric_limits<double>::max()<<endl;
cout<<"the range for type float is from "
<<numeric_limits<float>::min()<<" to"
<<numeric_limits<float>::max()<<endl;
cout<<"the range for type long double is from "
<<numeric_limits<long double>::min()<<" to "
<<numeric_limits<long double>::max()<<endl;
return 0;
}
2.13使用字符变量
#include <iostream>
using namespace std;
int main(){
char a{'A'};
char letter{a+5};
++a;
a += 3;
cout<<"a is '"<<a
<<"' which is code "<<hex<<showbase
<<static_cast<int>(a)<<endl;
return 0;
}
Lvalue 和 rualue
lvalue表示引用了内存中的一个地址
rualue表示临时存储结果