-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp_3_12_chapter4.cpp
More file actions
121 lines (107 loc) · 2.55 KB
/
cpp_3_12_chapter4.cpp
File metadata and controls
121 lines (107 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <iostream>
#include <string>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
void test1()
{ // 测试,decltype作用于表达式获得一个引用类型
int *a;
int c;
decltype(*a) b{c};
std::cout << b << std::endl;
int i{1};
std::cout << i << std::endl;
// std::cout << i++ << " " << ++i << " " << (i++) << std::endl; // 何时对运算对象求值是未定义的,由编译器决定,是错误的语句,加上()来确定
}
void exercise4_4()
{ // 计算运算
std::cout << (12 / 3 * 4 + 5 * 15 + 24 % 4 / 2) << std::endl;
std::cout << (-30 * 3 + 21 / 5) << '\n'
<< (-30 + 3 * 21 / 5) << '\n'
<< (30 / 3 * 21 % 5) << '\n'
<< (-30 / 3 * 21 % 4) << std::endl;
}
void exercise4_6()
{ // 判断整数为奇数还是偶数
int nA;
std::cin >> nA;
if (nA % 2)
std::cout << "ji" << std::endl;
else
std::cout << "ou" << std::endl;
}
void exercise4_10()
{ // 输入数字 42时停止
int nA;
while (cin >> nA && nA != 42)
cout << nA << endl;
}
void exercise4_11()
{ // 测试4个值 确保a大于b,b大于c, c大于d
vector<int> vTest;
int nTemp;
for (int i = 0; i < 4; ++i)
{
cin >> nTemp;
vTest.push_back(nTemp);
}
for (auto j = vTest.begin() + 1; j != vTest.end(); ++j)
{
if (*(j - 1) > *j)
{
cout << "wrong" << endl;
return;
}
}
cout << "right" << endl;
}
void test2()
{ // 判断 i!=j<k
// int i = 1;
// int j = 2;
// int k = 3;
// cout << (i != j < k) << endl;
}
void test3()
{ // 测试int char 大小
cout << sizeof(int) << " " << sizeof(char) << endl;
}
void test4()
{ // 测试 将void*指针转换回原来的类型并且进行操作
double dB = 1, *pP = &dB;
void* pQ = pP;
cout << *static_cast<double*>(pQ) << endl;
}
void test5()
{ // 测试类型转换的const_cast
int nA = 1, *pQ = &nA;
const int *pP = &nA;
// *pP = 2; wrong
// *pQ = 2;
int *pR = const_cast<int*>(pP);
*pR = 3; // 如果对象本身(nA)是常量,通过pR写值是未定义的
cout << nA << endl;
}
void exercise4_36()
{
int i = 3;
double d = 3.14;
cout << (i *= static_cast<int>(d)) << endl;
}
int main(int argc, char const *argv[])
{
// test1();
// exercise4_4();
// exercise4_6();
// exercise4_10();
// exercise4_11();
// test2();
// test3();
// test4();
// test5();
// exercise4_36();
return 0;
}