C++ 基础【01】

it2024-07-04  50

本文代码仓库地址: gitee码云笔记仓库地址


源文件:Hello_World.cpp

#include <iostream> using namespace std; // String 头文件 #include <string> // 解决 system 不明确的问题 #include <cstdlib> // time 系统时间头文件 #include <ctime> int main() { int a = 1; // 打印 a cout << a << endl; // sizeof(); 可以统计数据类型所占内存大小 cout << "int类型的内存大小:" << sizeof(int) << endl << endl; cout << "变量 a 的内存大小:" << sizeof(a) << endl; // 换行 cout << endl; cout << endl; // 键盘输入关键字 cin // 键盘输入 int 类型 cout << "请输入 int 类型的值:"; cin >> a; cout << "您输入的值为:" << a << endl; // 布尔类型除了0代表假,其余的都代表真 bool fol = false; cout << "请输入 bool 类型的值"; cin >> fol; cout << "布尔类型 非0即为真,您输入的为:" << fol << endl; if (fol) { cout << "是True就进来我这个里面" << endl; } else { cout << "是False就进来我这个里面" << endl; } cout << endl; // cout << "请输入进入的入口号:"; // cin >> a; switch (a) { case 0: cout << "我是0的入口" << endl; break; case 1: cout << "我是1的入口" << endl; break; case 2: cout << "我是2的入口" << endl; break; case 3: cout << "我是3的入口" << endl; break; default: cout << "我是最后不满足的出口哦" << endl; break; } // while循环 int i = 0; while (i < 5) { cout << i << endl; i++; } // rand() 伪随机数 开始都是41【在0-99之间】 int num01 = rand() % 100; cout << num01 << endl; // 添加随机数种子,作用是利用当前系统时间生成随机数,防止每次随机数都一样 srand((unsigned int) time(NULL)); num01 = rand() % 100; cout << num01 << endl; // do ... while ...循环 do { cout << num01 << "我是 do ... while ..." << endl; num01 = num01 + 10; } while (num01 < 100); // 水仙花数 // for循环 for (int i = 100; i < 1000; i++) { // 遍历出所有的三位数 // 分别算出这个数的个位十位百位 // 个位 int digits = i % 10; // 十位 int tens = (i / 10) % 10; // 百位 int hundreds = i / 100; int sum = digits * digits * digits + tens * tens * tens + hundreds * hundreds * hundreds; if (i == sum) { // 判断这个数是不是水仙花数 cout << i << "是水仙花数" << endl; } } // continue:不继续执行下面的的代码,然后进行下一次循环 // 输出 1 -- 10 之间的偶数 for (int i = 1; i < 11; i++) { if (i % 2 == 0) { cout << i << ":我是10以内的偶数"; } else { // 如果 i 是奇数就会 continue 一下,后面的代码就不会执行 continue; } cout << ",妙啊~" << endl; } /* 下面就会跳过某些代码去执行下面的代码【标记命名建议全大写】 * goto 标记; * 某些代码; * 标记: * 下面的代码; */ goto FLAG; cout << "某些代码!!!" << endl; cout << "某些代码!!!" << endl; FLAG: cout << "下面的代码!!" << endl; cout << "下面的代码!!" << endl; // 黑窗体暂停 system("pause"); return 0; }

rand() 伪随机数 开始都是41 int num01 = rand() % 61; 随机数的值为0 ~ 60 int num01 = rand() % 61 + 40; 随机数的值为40 ~ 100


在任何地方使用 exit(0); 系统都会直接退出 system(“cls”); 清屏 strlen(ch); 获取有效长度


一点点笔记,以便以后翻阅。

最新回复(0)