C++ 基础【01】

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


源文件: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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小印丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值