# learing-basic-cpp **Repository Path**: whisper21/learing-basic-cpp ## Basic Information - **Project Name**: learing-basic-cpp - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-12-12 - **Last Updated**: 2022-01-28 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # learning_basic_cpp 学习C++ 20.11.17 三 数组是存储数据的强而有力的手段 1. 一维数组 1.1 数组的定义 > 数组的定义方式和变量类似 ```C++ #include #include using namespace std; int main() { int a[10], b[20]; float f[33]; double d[123]; char c[21]; return 0; } ``` 1.2 数组的初始化 在main函数内部,初始化的数组中的元素随机的 ```C++ #include #include using namespace std; int main() { int a[3] = {0,1,2}; //含有3个元素的数组,元素分别是0,1,2 int b[] = {0,1,1}; int b[] = {0,1,1}; int b[] = {0,1,1}; //维度是3的数组 int c[5] = {0,1,2}; int c[5] = {0,1,2}; int c[5] = {0,1,2}; //等价与c[] = {0,1,2,0,0} char d[3] = {'a', 'b', 'c'}; char d[3] = {'a', 'b', 'c'}; char d[3] = {'a', 'b', 'c'}; //字符数组的初始化 return 0; } ``` 1.3 访问数组元素 通过下标访问数组 ```C++ #include #include using namespace std; int main() { int a[3] = {0,1,2}; //数组下标从0开始 cout << a[0] << ' ' << a[1] << ' '<< a[2] << endl; a[0] = 5; cout << a[5] << endl; return 0; } ``` 20.11.16 二 1. 做题练习 2. 看解析视频解决所遇到的问题 20.11.15 一 循环语句 1. 跳跃语句 1.1 break 可以提前从循环中退出,一般语句与if语句搭配 例:判断一个大于一的数是否是质数 ```C++ #include using namespace std; int main() { int n; cin >> n; bool is_prime = true; for (int i = 2; i < n; i++ ) if (n % i == 0) { is_prime = false; break; } if (is_prime) cout << "yes" << endl; else cout << "no" << endl; return 0; } ``` 1.2 continue 可以直接跳到当前循环体的结尾。作用与if语句类似 例:求1~100中所有偶数的和 ```C++ #include using namespace std; int main() { int sum = 0; for (int i = 1; i<= 100; i++) { if (i % 2 == 1) continue; sum += i; } cout << sum << endl; return 0; } ``` 1. 多层循环 例: ```C++ #include using namespace std; int main() { for (int i =0, k = 1; i < 10; i++ ) { for (int j = 0; j < 10; j++, k++ ) { cout << k << ' '; } cout << endl; } return 0; } ``` 21.11.13 六 判断语句 1. 条件表达式 1.1 与&& 1.2 或 || 1.3 非 例: ```C++ #include #include using namespace std; int main() { int a,b, c; cin >> a >> b >> c; if (a >= b && a >= c) cout << a << endl; else if (b >= a && b >= c) cout << b << endl; else cout << c << endl; return 0; } ``` 循环结构 1. while循环 解释:while循环就是循环版的if语句。 两者的不同: if语句是判断一次,如果条件成立,则执行后面的语句; while是每次判断,如果成立,则执行循环体中的语句,否则停止。 例: ```C++ #include using namespace std; int main() { int i = 0; while (i < 10) { cout << i << endl; i++ ; } return 0; } ``` 避免写出死循环 1. do while循环(不常用) do while语句与while语句非常相似。唯一的区别是,do while语句限制性循环体后检查条件。不管条件的值如何,我们都要至少执行一次循环。 例: ```C++ #include using namespace std; int main() { int x = 1; while (x < 1) { cout << "x!" << endl; x++ ; } int y = 1; do { cout << "y!" << endl; } while (y < 1); return 0; } ``` 2. for 循环 基本思想:把控制循环次数的变量从循环体中剥离. ```C++ for (init-statement : condition: expression) { statement } ``` - init-statement可以是声明语句、表达式、空语句,一般用来初始化循环变量; - condition 是条件表达式,和while中的条件表达式作用一样;可以为空,空语句表示true - expression 一般负责修改循环变量,可以为空 例: ```C++ #include using namespace std; int main() { int sum = 0; for (int i = 1, j = 10; i < j; i++, j--) { sum += i * j; } cout << sum << endl; return 0; } ``` 21.11.11 四 1. 这两天都在做题,熟悉写的过程和所学的语句 2. 打卡了y总的习题课,解决了做题时遇到的问题,明天继续打卡y总的视频 21.11.08 一 判断语句 1. printf的用法 1.1 使用`printf`时最好添加头文件 `#include` 1.2 Int、float、double、char等类型的输出格式; 回车也是一个字符,用’\n’表示 1.3 所有输出的变量均可包含在一个字符串中 1.4 数字宽度 - 最小%8.3f,表示这个浮点数的最小宽度为8,保留3位小数,当宽度不足时在前面补空格 - %-8.3f,表示最小宽度为8,保留3位小数,当宽度不足时在后面补上空格 - %08.3f,表示最小宽度为8,保留3位小数,当宽度不足时在前面补上0 2. 基本if-else语句 当条件成立时,执行某些语句;否则执行另一些语句。 例: ``` C++ #include #include using namespace std; int main() { int score; cin >> score; if (score >= 60) { cout << "及格" << endl; } cout << "结束" << endl; return 0; } ``` 当只有一条语句时,大括号可以省略 if和else后不加分号 3. 常用比较运算符 (1) 大于 > (2) 小于 < (3) 大于等于 >= (4) 小于等于 <= (5) 等于 ==(两个等号) (6) 不等于 != 4. if_else连写 ```C++ #include #include using namespace std; int main() { int s; cin >> s; if (s >= 85) { printf("A"); } else if (s >= 70) { printf("B"); } else if (s >= 60) { printf("C"); } else { printf("D"); } return 0; } ``` 21.11.06 六 1. 今天做了输入与输出的题,对于各种变量的输出方式熟悉了很多,能更快的写完一道题 2. 知道了一种函数sqrt 21.11.05 五 1. 不同变量的输出 |类别|符号| |---|---| |`int`|`%d`| |`float`|`%f`| |`double`|`%lf`| |`char`|`%c`| |`longlong`|`%lld`| 2. 运算符号的含义 |符号|意义| |---|---| |`*`|`数学的乘`| |`/`|`数学的除`| |`...`|`...`| |`%`|`取模运算除整后的余数`| 3. 变量的强制转化 3.1 强制转换 ``` C++ #include using namespace std; int main() { int a = 5; float b =(float)a; cout << b << endl; return 0; } ``` 3.1 转换结果 两个不同类型做运算,输出结果会转化为精度更好的类型 4. 自变量本身的加减 只对整数有用 a++,++a对于a这个变量本身的影响一样 定义b= a++,d= ++a; b,d输出的值不同 例: ``` C++ #include using namespace std; int main() { int a = 5; int b = a++; cout << ' ' << b << endl; int c = 5; int d = ++c; cout << ' ' << d << endl; return 0; } ``` 2021.11.03 三 1. 简单的混合运算 ``` C++ #include using namespace std; int main() { int A, B, C, D; cin >> A >> B >> C >> D; cout << "DIFERENCA = " << A * B - C * D << endl; return 0; } ``` 1. 小数的简单运算 ```C++ #include int main() { int a, b, c, d; scanf("%d%d%d%d", &a, &b, &c, &d); printf("DIFERENCA = %d\n", a * b - c * d); return 0; } ``` 21.11.01一 1. 头文件 ```C++ #include #include using namespace std; int main() { return 0; } ``` 2. hello world ```C++ #include using namespace std; int main() { cout<<"Hello World"< using namespace std; int main() { int a, b; cin >> a >> b; cout << a + b<< endl; return 0; } ```