一、课上练习
习题链接
- 输出课上练习: L1051
- 类型转换课上练习: L1052
示例代码
1// 画正十六边形
2#include "CTurtle.hpp"
3#include <bits/stdc++.h>
4using namespace std;
5using namespace cturtle;
6
7int main() {
8 // 创建一个画布屏幕
9 TurtleScreen scr;
10 // 创建一个画图图标
11 Turtle turtle(scr);
12
13 turtle.right(50);
14 turtle.forward(22.5);
15 turtle.right(50);
16 turtle.forward(22.5);
17 turtle.right(50);
18 turtle.forward(22.5);
19 turtle.right(50);
20 turtle.forward(22.5);
21 turtle.right(50);
22 turtle.forward(22.5);
23 turtle.right(50);
24 turtle.forward(22.5);
25 turtle.right(50);
26 turtle.forward(22.5);
27 turtle.right(50);
28 turtle.forward(22.5);
29 turtle.right(50);
30 turtle.forward(22.5);
31 turtle.right(50);
32 turtle.forward(22.5);
33 turtle.right(50);
34 turtle.forward(22.5);
35 turtle.right(50);
36 turtle.forward(22.5);
37 turtle.right(50);
38 turtle.forward(22.5);
39 turtle.right(50);
40 turtle.forward(22.5);
41 turtle.right(50);
42 turtle.forward(22.5);
43 turtle.right(50);
44 turtle.forward(22.5);
45
46 // 暂停程序
47 system("pause");
48 // 程序结束
49 return 0;
50}
1// 基础数据类型的输出
2#include "CTurtle.hpp"
3#include <bits/stdc++.h>
4using namespace std;
5using namespace cturtle;
6
7int main() {
8 cout << 10 << endl;
9 cout << "10: " << sizeof(10) << endl;
10 cout << sizeof(2147483647) << endl;
11 cout << sizeof(2147483648) << endl;
12 cout << sizeof(int) << endl;
13 cout << sizeof(long long) << endl;
14 cout << 10.4 << endl;
15 cout << sizeof(10.4) << endl;
16 cout << sizeof(float) << endl;
17 cout << sizeof(double) << endl;
18 cout << 10.0 << endl;
19 cout << "10.0: " << sizeof(10.0) << endl;
20 cout << "Hello, world!" << endl;
21 cout << "e" << endl;
22 cout << sizeof("e") << endl;
23 cout << 'e' << endl;
24 cout << sizeof('e') << endl;
25 cout << 1 << endl;
26 cout << 1.0 << endl;
27 cout << '1' << endl;
28 cout << true << endl;
29 cout << false << endl;
30 cout << (bool)3.5 << endl;
31 cout << float(3.5) << endl;
32 cout << static_cast<int>(3.5) << endl;
33 cout << 'a' + 1 << endl;
34 cout << true + 'a' << endl;
35 return 0;
36}
1#include <bits/stdc++.h>
2using namespace std;
3
4int main() {
5 cout << "10 + 3 = " << 10 + 3 << endl;
6 cout << "10 * 3 = " << 10 * 3 << endl;
7 cout << "10 / 3 = " << 10 / 3 << endl;
8 return 0;
9}
1#include <bits/stdc++.h>
2using namespace std;
3
4int main() {
5 cout << (char)48 << endl;
6 cout << bool(10) << endl;
7 cout << (int)'A' << endl;
8 cout << (int)10.9 << endl;
9 cout << char('z' - 1) << endl;
10 return 0;
11}
二、知识总结
四种基础数据类型
整数型
| 关键字 | 内存大小 | 数据范围 |
|---|
| short | 16位/2字节 | -2^15 ~ 2^15 - 1 / -32768 ~ 32767 |
| int(默认) | 32位/4字节 | -2^31 ~ 2^31 - 1 / -2147483648 ~ 2147483647 |
| long | 32位/4字节 | -2^31 ~ 2^31 - 1 / -2147483648 ~ 2147483647 |
| long long | 64位/8字节 | -2^63 ~ 2^63 - 1 / -9223372036854775808 ~ 9223372036854775807 |
| unsigned short | 16位/2字节 | 0 ~ 2^16 - 1 / 0 ~ 65535 |
| unsigned int | 32位/4字节 | 0 ~ 2^32 - 1 / 0 ~ 4294967295 |
| unsigned long | 32位/4字节 | 0 ~ 2^32 - 1 / 0 ~ 4294967295 |
| unsigned long long | 64位/8字节 | 0 ~ 2^64 - 1 / 0 ~ 18446744073709551615 |
浮点型
| 关键字 | 内存大小 | 数据范围 |
|---|
| float | 32位/4字节 | ±3.4×10^38 / 小数点后6~7位 |
| double(默认) | 64位/8字节 | ±1.7×10^308 / 小数点后15~16位 |
| long double | 128位/16字节 | ±3.4×10^3932 / 小数点后33~34位 |
字符型
- 关键字:
char - 内存大小:8位/1字节
- 存储方式:整数形式存储
- 存储范围:0~127
- 编码方式:ASCII码
ASCII码表
常用ASCII码值:
| ASCII值 | 字符 | 说明 |
|---|
| 0 | \0 | 空字符 |
| 32 | (空格) | 空格字符 |
| 48 | 0 | 数字0 |
| 57 | 9 | 数字9 |
| 65 | A | 大写字母A |
| 90 | Z | 大写字母Z |
| 97 | a | 小写字母a |
| 122 | z | 小写字母z |
| 127 | DEL | 删除字符 |
ASCII码规律:
- 数字
0~9 对应 48~57(连续排列) - 大写字母
A~Z 对应 65~90(连续排列) - 小写字母
a~z 对应 97~122(连续排列) - 大写字母与对应小写字母的ASCII值相差32(例如
A=65, a=97)
布尔型
- 关键字:
bool - 内存大小:8位/1字节
- 存储方式:整数形式存储
- 存储范围:1(true) / 0(false)
四种数据类型之间的转换
| 转换方向 | 从整数型 | 从浮点型 | 从字符型 | 从布尔型 |
|---|
| 转为整数型 | - | 直接去除小数部分,如 (int)3.5=3 | 根据ASCII对应转换,如 (int)'0'=48 | (int)true=1,(int)false=0 |
| 转为浮点型 | 直接转为对应小数,如 (double)3=3 | - | 根据ASCII对应转换,如 (double)'0'=48 | (double)true=1,(double)false=0 |
| 转为字符型 | 根据ASCII对应转换,如 (char)48='0' | 先转为整型再根据ASCII对应转换,如 (char)48.8='0' | - | (char)true='\1',(char)false='\0' |
| 转为布尔型 | 非0整数转为真,0转为假,如 (bool)3=1 | 非0小数转为真,0转为假,如 (bool)3.5=1 | 根据ASCII对应转换,如 (bool)'0'=1,(bool)'\0'=0 | - |
三、课后练习
- 输出课下练习: L1053
- 类型转换课下练习: L1054