int、long、long long和short表示整型类型,C语言规定它们表示数的范围short
除去布尔型和扩展的字符型之外,其它整型可以划分为**带符号的(signed)和无符号的(unsigned)**两种。带符号类型可以表示正数、负数和0,无符号类型则仅能表示大于等于0的值。类型int、short、long和long long都是带符号的,通过在这些类型名前添加unsigned就可以得到无符号类型,例如unsigned long。类型unsigned int可以缩写为unsigned;
float和double用于表示浮点数,其中,float表示单精度浮点数,double表示双精度浮点数。执行浮点数运算选用double,这是因为float通常精度不够而且双精度浮点数和单精度浮点数的计算代价相差无几。事实上,对于某些机器来说,双精度运算甚至比单精度还快。long double提供的精度在一般情况下是没有必要的,况且它带来的运算时消耗也不容忽视。
利率(rat):float,本金(principal):long long,付款(payment):long long。
利率一般是小数点后保留四位有效数字,float 合适;
因为float和double都是浮点数, 都有取值范围, 都有精度范围.浮点数与通常使用的小数不同,使用中,往往难以确定。所以说,float和double都是不能用来表示精确的类型的.金额必须是完全精确的计算, 故不能使用double或者float.本金和付款使用最大的带符号整型表示.
32
4294967264
32
-32
0
0
#include
int main()
{
unsigned u=10,u2=42;
std::cout<
字面值 | 数据类型 |
---|---|
‘a’ | char |
L’a’ | wchar_t |
“a” | const char * |
L"a" | const wchar_t * |
字面值 | 数据类型 |
---|---|
10 | int |
10u | unsigned |
10L | long |
10uL | unsigned long |
012 | int八进制9 |
0xC | int十六进制12 |
字面值 | 数据类型 |
---|---|
3.14 | double |
3.14f | float |
3.14L | long double |
字面值 | 数据类型 |
---|---|
10 | int |
10u | unsigned |
10. | double |
10e-2 | double |
有区别,第二个表达式数值以0开头表示八进制,但是09不合法
字面值 | 数据类型 |
---|---|
“Who goer withF\145rgus?\012” | const char *其中还有两个八进制转义序列 |
3.14e1L | long double |
1024f | float |
3.14L | long double |
#include
int main()
{
std::cout<<"\062\115\xA";
std::cout<<"\x32\x9\x4d\012";
return 0;
}
int input_value;std::cin>>intput_value
double i={3.14}
double salary,wage;salary=wage=9999.99
std::string global_str;//空字符串
int global_int;//0
int main()
{
int local_int;//未定义
std::string local_str;//空字符串
}
100
100 45
10 10
#include
int main()
{
int *ptr,a;
//更改指针的值
ptr=nullptr;
ptr=&a;
//更改指针所指对象的值
*ptr=3;
std::cout<<"a="<
引用(reference)为对象起了另外一个名字,引用类型引用(refers to)另外一种类型。
指针(pointer)是"指向(point to)"另外一种类型的复合类型。与引用类似,指针也实现了对其他对象的间接访问。然而指针与引用相比又有很多不同点:
1. 指针本身就是一个对象,允许对指针赋值和拷贝,而且在指针的生命周期内它可以先后指向几个不同的对象;
2. 指针无须在定义时赋初值;
3. 因为引用不是对象,没有实际地址,所以不能定义指向引用的指针;
4. 除特殊情况,其他所有指针的类型都要和它所指向的对象严格匹配;
int i=42;
定义整型变量i,初始化为42
int *p1=&i;
定义整型指针p1,初始化为变量i的地址
*p1=*p1**p1;
指针p1所指对象赋值为其自身值的平方
不能,所以推荐使用智能指针。
void类型指针可以存放任意类型对象的地址,而lp的指针类型与对象不匹配。
int null=0, *p=&null;
2-34.cpp: In function ‘int main()’:
2-34.cpp:20:11: 错误:invalid conversion from ‘int’ to ‘int*’ [-fpermissive]
20 | d=42;
| ^~
| |
| int
2-34.cpp:24:11: 错误:invalid conversion from ‘int’ to ‘const int*’ [-fpermissive]
24 | e=42;
| ^~
| |
| int
2-34.cpp:28:10: 错误:向只读形参‘g’赋值
28 | g=42;
| ^~
#include
int main()
{
int i=0,&r=i;
const int ci=i,&cr=ci;
auto a=r;
std::cout<<"a="<
const int i=42;
auto j=i;//int
const auto &k=i;//int
auto *p=&i;//const int
const auto j2=i,&k2=i;//int
#include
int main()
{
int a=3,b=4;
decltype(a) c=a;
decltype((b)) d=a;
++c;
++d;
std::cout<<"a="<
a int 4
b int 4
c int 4
d int 4
a int 3
b int 4
c int 3
d int & 3
auto和decltype的区别主要有三方面:
1. auto类型说明符用编译器计算变量的初始值来推断其类型,而decltype虽然也让编译器分析表达式并得到它的类型,但是不实际计算表达式的值。
2. 编译器推断出来的auto类型有时候和初始值的类型并不完全一样,编译器会适当地改变结果类型使其更符合初始化规则。例如,auto一般会忽略掉顶层const,而把底层const保留下来。与之相反,decltype会保留变量的顶层const。
3. 与auto不同,decltype的结果类型与表达式密切相关,如果变量名加上了一对括号,则得到的类型与不加括号时会有不同。如果decltype使用的是一个不加括号的变量,则得到的结果就是该变量的类型;如果给变量加上了一层或多层括号,则编译器将推断得到引用类型。
#include
struct Foo{}
int main()
{
return 0;
}
2-39.cpp:2:13: 错误:结构定义后需要‘;’
2 | struct Foo{}
| ^
| ;
struct Sales_data
{
std::string Bookno;
unsigned int sold={0};
double revenue{0.0};
};
2-41-1.cpp
#include
#include
struct Sales_data
{
std::string Bookno;
unsigned int sold={0};
double revenue{0.0};
};
int main()
{
Sales_data temp;
double price=0.0;
while(std::cin>>temp.Bookno)
{
std::cin>>temp.sold>>price;
temp.revenue=temp.sold*price;
std::cout<
2-41-2.cpp
#include
#include
struct Sales_data
{
std::string Bookno;
unsigned int sold={0};
double revenue{0.0};
};
int main()
{
Sales_data temp1,temp2;
double price=0;
std::cin>>temp1.Bookno>>temp1.sold>>price;
temp1.revenue=temp1.sold*price;
price=0;
std::cin>>temp2.Bookno>>temp2.sold>>price;
temp2.revenue=temp2.sold*price;
if(temp1.Bookno==temp2.Bookno)
std::cout<
2-41-3.cpp
#include
#include
struct Sales_data
{
std::string Bookno;
unsigned int sold={0};
double revenue{0.0};
};
int main()
{
Sales_data temp1,temp2;
double price=0.0;
if(std::cin>>temp1.Bookno)
{
std::cin>>temp1.sold>>price;
temp1.revenue=temp1.sold*price;
price=0;
while(std::cin>>temp2.Bookno)
{
std::cin>>temp2.sold>>price;
temp2.revenue=temp2.sold*price;
price=0;
if(temp1.Bookno==temp2.Bookno)
{
temp1.sold+=temp2.sold;
temp1.revenue+=temp2.revenue;
}
else
{
std::cerr<<"not the same isbn"<
2-41-4.cpp
#include
#include
struct Sales_data
{
std::string Bookno;
unsigned int sold={0};
double revenue{0.0};
};
int main()
{
Sales_data total,temp;
double price=0.0;
if(std::cin>>total.Bookno)
{
std::cin>>total.sold>>price;
total.revenue=total.sold*price;
while(std::cin>>temp.Bookno)
{
std::cin>>temp.sold>>price;
temp.revenue=temp.sold*price;
price=0;
if(total.Bookno==temp.Bookno)
{
total.sold+=temp.sold;
total.revenue+=temp.revenue;
}
else
{
std::cout<
2-42-1.cpp
#include
#include
#include "Sales_data.h"
int main()
{
Sales_data temp;
double price=0.0;
while(std::cin>>temp.Bookno)
{
std::cin>>temp.sold>>price;
temp.revenue=temp.sold*price;
std::cout<
2-42-2.cpp
#include
#include
#include "Sales_data.h"
int main()
{
Sales_data temp1,temp2;
double price=0;
std::cin>>temp1.Bookno>>temp1.sold>>price;
temp1.revenue=temp1.sold*price;
price=0;
std::cin>>temp2.Bookno>>temp2.sold>>price;
temp2.revenue=temp2.sold*price;
if(temp1.Bookno==temp2.Bookno)
std::cout<
2-42-3.cpp
#include
#include
#include "Sales_data.h"
int main()
{
Sales_data temp1,temp2;
double price=0.0;
if(std::cin>>temp1.Bookno)
{
std::cin>>temp1.sold>>price;
temp1.revenue=temp1.sold*price;
price=0;
while(std::cin>>temp2.Bookno)
{
std::cin>>temp2.sold>>price;
temp2.revenue=temp2.sold*price;
price=0;
if(temp1.Bookno==temp2.Bookno)
{
temp1.sold+=temp2.sold;
temp1.revenue+=temp2.revenue;
}
else
{
std::cerr<<"not the same isbn"<
2-42-4.cpp
#include
#include
#include "Sales_data.h"
int main()
{
Sales_data total,temp;
double price=0.0;
if(std::cin>>total.Bookno)
{
std::cin>>total.sold>>price;
total.revenue=total.sold*price;
while(std::cin>>temp.Bookno)
{
std::cin>>temp.sold>>price;
temp.revenue=temp.sold*price;
price=0;
if(total.Bookno==temp.Bookno)
{
total.sold+=temp.sold;
total.revenue+=temp.revenue;
}
else
{
std::cout<