数据的存储方式——变量
一、课上练习
习题链接
示例代码
使用bianchang变量画正十六边形
1// 画正16边形代码
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 int bianchang;
14 bianchang = 40;
15
16 turtle.forward(bianchang);
17 turtle.right(22.5);
18 turtle.forward(bianchang);
19 turtle.right(22.5);
20 turtle.forward(bianchang);
21 turtle.right(22.5);
22 turtle.forward(bianchang);
23 turtle.right(22.5);
24 turtle.forward(bianchang);
25 turtle.right(22.5);
26 turtle.forward(bianchang);
27 turtle.right(22.5);
28 turtle.forward(bianchang);
29 turtle.right(22.5);
30 turtle.forward(bianchang);
31 turtle.right(22.5);
32 turtle.forward(bianchang);
33 turtle.right(22.5);
34 turtle.forward(bianchang);
35 turtle.right(22.5);
36 turtle.forward(bianchang);
37 turtle.right(22.5);
38 turtle.forward(bianchang);
39 turtle.right(22.5);
40 turtle.forward(bianchang);
41 turtle.right(22.5);
42 turtle.forward(bianchang);
43 turtle.right(22.5);
44 turtle.forward(bianchang);
45 turtle.right(22.5);
46 turtle.forward(bianchang);
47 turtle.right(22.5);
48
49 // 暂停程序
50 system("pause");
51 // 程序结束
52 return 0;
53}使用bianchang和jiaodu两个变量画正十六边形
1// 画正16边形代码
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 int bianchang = 40;
14 double jiaodu = 22.5;
15
16 turtle.forward(bianchang);
17 turtle.right(jiaodu);
18 turtle.forward(bianchang);
19 turtle.right(jiaodu);
20 turtle.forward(bianchang);
21 turtle.right(jiaodu);
22 turtle.forward(bianchang);
23 turtle.right(jiaodu);
24 turtle.forward(bianchang);
25 turtle.right(jiaodu);
26 turtle.forward(bianchang);
27 turtle.right(jiaodu);
28 turtle.forward(bianchang);
29 turtle.right(jiaodu);
30 turtle.forward(bianchang);
31 turtle.right(jiaodu);
32 turtle.forward(bianchang);
33 turtle.right(jiaodu);
34 turtle.forward(bianchang);
35 turtle.right(jiaodu);
36 turtle.forward(bianchang);
37 turtle.right(jiaodu);
38 turtle.forward(bianchang);
39 turtle.right(jiaodu);
40 turtle.forward(bianchang);
41 turtle.right(jiaodu);
42 turtle.forward(bianchang);
43 turtle.right(jiaodu);
44 turtle.forward(bianchang);
45 turtle.right(jiaodu);
46 turtle.forward(bianchang);
47 turtle.right(jiaodu);
48
49 // 暂停程序
50 system("pause");
51 // 程序结束
52 return 0;
53}C++风格输入方式示例代码
1#include <iostream>
2#include <string>
3using namespace std;
4
5int main() {
6 // 定义不同类型的变量
7 int number; //创建一个int类型的变量
8 double dnumber; // 创建一个double类型的变量
9 float fnumber; // 创建一个float类型的变量
10 char ch; // 创建一个char类型的变量
11
12 cin >> number >> dnumber >> fnumber >> ch; // 读入数据并存储在相应的变量中
13 cout << fixed << setprecision(2) << number << endl; //确定输出精度
14 cout << dnumber << endl; // 输出小数
15 cout << fnumber << endl; // 输出小数
16 cout << ch << endl; //输出字符
17
18 return 0; // 程序正常结束
19}C风格输入方式示例代码
1#include <iostream>
2//scanf和printf不需要std命名空间
3
4int main() {
5 int number; //创建一个int类型的变量
6 scanf("%d", &number); // 读取一个整数并存入number
7 printf("%d\n", number); // 输出整数
8
9 double dnumber; // 创建一个double类型的变量
10 scanf("%lf", &dnumber); // 读取一个小数并存入dnumber
11 printf("%lf\n", dnumber); // 使用.lf输出小数
12
13 float fnumber; // 创建一个float类型的变量
14 scanf("%f", &fnumber); // 读取一个小数并存入fnumber
15 printf("%.3f\n", fnumber); // 使用.3f输出小数,并使得输出精确到小数点后三位
16
17 char ch; // 创建一个char类型的变量
18 scanf(" %c", &ch); // 读取一个空格和一个字符并存入ch
19 printf("%c\n", ch); // 输出字符
20
21 return 0; // 程序正常结束
22}L1063 a+b问题示例代码(P1006)
1#include <bits/stdc++.h>
2using namespace std;
3int main() {
4 int a, b;
5 cin >> a >> b;
6 cout << a + b << endl;
7
8
9 return 0;
10}L1062 打印ASCII码示例代码(T1020)
1#include <bits/stdc++.h>
2using namespace std;
3
4int main() {
5 char ch;
6 cin >> ch;
7 cout << int(ch) << endl;
8
9 return 0;
10}L1061 输出浮点数示例代码(T1024)
1#include <bits/stdc++.h>
2using namespace std;
3
4int main() {
5 float number;
6 cin >> number;
7 printf("%.3f", number);
8
9 return 0;
10}二、知识总结
变量的定义与使用
- 定义:使用数据类型+变量名的方式来定义变量。
- 使用:使用变量的时候,直接使用变量名就可以了,如果在变量名前面再加上数据类型的话,就意味着重复定义该变量。
变量的定义与使用示例
int length; //创建一个名叫length的变量存储int类型的数据
length = 200; //给length赋值为200
double angle = 360.0 / 4; //创建一个名叫angle的变量存储double类型的数据,并赋值为90.0
cout << length << endl; //输出length变量中存储的数据内容
cout << angle << endl; //输出angle变量中存储的数据内容常量
在定义后不可修改的变量,只能在定义时赋值,使用const关键字表示该变量为常量。
常量定义示例
const int LENGTH = 200; //创建一个名叫LENGTH的常量并存储一个整数200变量的命名规则
- 只能使用英文字母、下划线和数字命名
- 不能使用数字开头
- 不能使用C++关键字
- 最好使用小写字母开头,如果多个字母分割可以使用大写字母分隔或下划线分隔的方式,例如:
studentName和student_name - 变量名区分大小写,
int a;和int A;是两个变量 - 下划线开头的变量一般代表特殊意义
- 全大写的变量一般表示常量
C++关键字清单
| auto | FALSE | static | using | if |
| goto | static_cast | bool | TRUE | public |
| namespace | else | and | const_cast | char |
| enum | protected | inline | for | not |
| dynamic_cast | short | struct | virtual | delete |
| do | xor | static_assert | long | class |
| override | this | switch | return | register |
| float | wchar_t | final | nullptr | case |
| try | explicit | double | sizeof | operator |
| void | default | extern | signed | typeid |
| const | friend | break | throw | unsigned |
| typedef | constexpr | template | continue | noexcept |
C++风格输入方式
使用cin函数+提取操作符(>>)输入数据进一个变量中,使用cout函数+插入操作符(<<)输出数据。不需要区分数据类型,直接读取变量。
C++风格输入输出示例
1#include <iostream>
2#include <string>
3using namespace std;
4
5int main() {
6 // 定义不同类型的变量
7 int number; //创建一个int类型的变量
8 double dnumber; // 创建一个double类型的变量
9 float fnumber; // 创建一个float类型的变量
10 char ch; // 创建一个char类型的变量
11
12 cin >> number >> dnumber >> fnumber >> ch; // 读入数据并存储在相应的变量中
13 cout << fixed << setprecision(2) << number << endl; //确定输出精度
14 cout << dnumber << endl; // 输出小数
15 cout << fnumber << endl; // 输出小数
16 cout << ch << endl; //输出字符
17
18 return 0; // 程序正常结束
19}C风格输入方式
使用scanf函数读入数据进一个变量中,使用printf函数输出数据。scanf和printf都需要区分数据类型。
C风格输入输出示例
1#include <iostream>
2//scanf和printf不需要std命名空间
3
4int main() {
5 int number; //创建一个int类型的变量
6 scanf("%d", &number); // 读取一个整数并存入number
7 printf("%d\n", number); // 输出整数
8
9 double dnumber; // 创建一个double类型的变量
10 scanf("%lf", &dnumber); // 读取一个小数并存入dnumber
11 printf("%lf\n", dnumber); // 使用.lf输出小数
12
13 float fnumber; // 创建一个float类型的变量
14 scanf("%f", &fnumber); // 读取一个小数并存入fnumber
15 printf("%.3f\n", fnumber); // 使用.3f输出小数,并使得输出精确到小数点后三位
16
17 char ch; // 创建一个char类型的变量
18 scanf(" %c", &ch); // 读取一个空格和一个字符并存入ch
19 printf("%c\n", ch); // 输出字符
20
21 return 0; // 程序正常结束
22}