IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> 【C++】学习笔记[二] -> 正文阅读

[C++知识库]【C++】学习笔记[二]

一、运算符

  • 加减乘除取模运算的练习代码
#include<iostream>
#include <string>
using namespace std;

int main()
{

	//加减乘除取余运算
	int a1 = 10;
	int b1 = 3;

	cout << a1 + b1 << endl;
	cout << a1 - b1 << endl;
	cout << a1 * b1 << endl;
	//两个整数相除结果依然是整数,将小数直接丢掉了
	cout << a1 / b1 << endl;

	//取模运算的本质就是求余数
	cout << a1 % b1 << endl;
	cout << 10 % 20 << endl;//10
	//10%0  报错,两数相除除数不为0,所以也做不了取模运算
	//10/0会报错,除数不为0


	double d1 = 0.5;
	double  d2 = 0.25;
	double d3 = 0.22;

	cout << d1 / d2 << endl;
	cout << d1 / d3 << endl;
	//cout << d1 % d2 << endl;//小数不能进行%运算
	return 0;
}

在这里插入图片描述
在这里插入图片描述

  • 前置++、后置++、前置–、后置–练习代码如下
#include<iostream>
#include <string>
using namespace std;

int main()
{
	//前置递增
	int a = 10;
	++a;//让a加一
	cout << "a:" << a << endl;

	//后置递增
	int b = 10;
	b++;
	cout << "b:" << b << endl;

	//前置和后置区别
	//前置递增:先让变量+1,然后进行表达式计算
	//后置递增:先进行表达式计算,然后让变量+1
	int a2 = 10;
	int b2 = ++a2 * 10;
	cout << "b2" << b2 << endl;//110

	int a3 = 10;
	int b3 = a3++ * 10;
	cout << "b3:" << b3 << endl;//100
	return 0;
}

三目运算符练习代码:

#include<iostream>
#include <string>
using namespace std;

int main()
{
	int a = 10;
	int b = 20;
	//如果a>b成立,则输出a,否则输出b
	int c = a > b ? a : b;
	cout << c << endl;//20

	int d = a < b ? a : b;
	cout << d << endl;//10
	//三目运算符返回一个变量
	return 0;
}

在这里插入图片描述

  • 逻辑运算符【&& || !】
#include<iostream>
using namespace std;


int main()
{
	int a = 10;
	int b = 0;
	cout << !a << endl;//0
	cout << (a && b) << endl;//0
	cout << (a || b) << endl;//1
	return 0;
}

在这里插入图片描述

  • && :同真为真,其余为假
  • || : 同假为假,其余为真

二、程序流程结构

C/C++支持最基本的三种程序运行结构:顺序结构、选择结构、循环结构

(一)选择结构

if语句

作用:执行满足条件的语句

if语句的三种形式:

  • 单行格式
  • 多行格式
  • 多条件的

练习代码如下:

  • 单行格式if语句
#include<iostream>
using namespace std;

int main()
{
	//单行格式if语句
	//用户输入一个分数,分数》600,考上一本大学

	int score = 0;
	cout << "请输入分数:" << endl;
	cin >> score;
	cout << "您输入的分数为:" << score << endl;
	if (score > 600)
	{
		cout << "恭喜考上一本大学" << endl;
	}
	return 0;
}

在这里插入图片描述

  • 多行格式if语句
#include<iostream>
using namespace std;

int main()
{
	//多行格式if语句
	//用户输入一个分数,分数》600,考上一本大学

	int score = 0;
	cout << "请输入分数:" << endl;
	cin >> score;
	cout << "您输入的分数为:" << score << endl;
	if (score > 600)
	{
		cout << "恭喜考上一本大学" << endl;
	}
	else
	{
		cout << "很遗憾,您没有达到一本分数线" << endl;
	}
	return 0;
}

在这里插入图片描述

  • 多条件的if语句
#include<iostream>
using namespace std;

int main()
{
	//多条件的if语句
	//输入一个分数,如果>600,视为考上一本
	//如果>500,视为考上二本
	//如果>400,视为考上三本
	//如果<=400,视为没有考上大学

	int score = 0;
	cout << "请输入您的分数" << endl;
	cin >> score;
	cout << "您输入的分数为:" << score << endl;

	if (score > 600)
	{
		cout << "恭喜考上一本大学" << endl;
	}
	else if (score > 500)
	{
		cout << "恭喜考上二本大学" << endl;
	}
	else if (score > 400)
	{
		cout << "恭喜考上三本大学" << endl;
	}
	else
	{
		cout << "很遗憾您没有考上大学!" << endl;
	}

	return 0;
}

在这里插入图片描述

  • 嵌套if语句练习代码如下:
#include<iostream>
using namespace std;

int main()
{
	int score = 0;
	cout << "请输入您的分数" << endl;
	cin >> score;
	cout << "您输入的分数为:" << score << endl;

	if (score > 600)
	{
		cout << "恭喜考上一本大学" << endl;
		if (score > 700)
		{
			cout << "恭喜考入北京大学" << endl;
		}
		else if (score > 650)
		{
			cout << "恭喜考入清华大学" << endl;
		}
		else
		{
			cout << "恭喜考入人民大学" << endl;
		}
	}
	else if (score > 500)
	{
		cout << "恭喜考上二本大学" << endl;
	}
	else if (score > 400)
	{
		cout << "恭喜考上三本大学" << endl;
	}
	else
	{
		cout << "很遗憾您没有考上大学!" << endl;
	}
	return 0;
}

在这里插入图片描述
案例练习:三只小猪称体重?

有三只小猪ABC,请分别输入三只小猪的体重,并且判断哪只小猪最重?

代码如下:

#include<iostream>
using namespace std;

int main()
{
	//三只小猪称体重

	//创建三只猪的体重变量
	int num1 = 0;
	int num2 = 0;
	int num3 = 0;

	//输入三只猪的重量
	cout << "请输入A的重量:" << endl;
	cin >> num1;

	cout << "请输入B的重量:" << endl;
	cin >> num2;

	cout << "请输入C的重量:" << endl;
	cin >> num3;

	cout << "A的体重为:" << num1 << endl;
	cout << "B的体重为:" << num2 << endl;
	cout << "C的体重为:" << num3 << endl;

	//判断哪只最重?
	if (num1 > num2)//A比B重
	{
		if (num1 > num3)//A比C重
		{
			cout << "小猪A最重" << endl;
		}
		else//C比A重
		{
			cout << "小猪C最重" << endl;
		}
	}
	else//B比A重
	{
		if (num1 > num3)//A比C重
		{
			cout << "小猪B最重" << endl;
		}
		else//C比A重
		{
			cout << "小猪C最重" << endl;
		}
	}

	return 0;
}

在这里插入图片描述

switch语句

作用:执行多条件分支语句

练习代码如下:

#include<iostream>
using namespace std;


int main()
{
	//给一个电影进行评分
	//10 9 经典
	// 7 8 非常好
	//6 5 一般
	//5分以下  烂片

	int score = 0;
	cout << "请输入你电影的评分:" << endl;
	cin >> score;
	cout << "你打的分数为:" << score << endl;

	switch (score)//依据score来打分
	{
	case 10:
		cout << "你认为该电影是经典电影" << endl;
		break;//退出当前分支
	case 9:
		cout << "你认为该电影是经典电影" << endl;
		break;
	case 8:
		cout << "你认为该电影非常好" << endl;
		break;
	case 7:
		cout << "你认为该电影非常好" << endl;
		break;
	case 6:
		cout << "你认为该电影一般好" << endl;
		break;
	case 5:
		cout << "你认为该电影一般好" << endl;
		break;
	default:
		cout << "你认为这是烂片" << endl;
		break;
	}

	//if  switch语句有什么区别?
	//switch不可以判断区间的范围,表达式类型只能是整数或者字符型
	//但是switch结构非常清晰,执行效率高
	//通常情况下,switch执行效率比if高一些
	return 0;
}

(二)循环结构

while循环

作用:满足循环条件,执行循环语句

练习代码如下:

#include<iostream>
using namespace std;

int main()
{
	//while循环,在写循环的时候一定要避免死循环
	//在屏幕中打印0-9十个数字

	int num = 0;
	while (num < 10)
	{
		cout << num << endl;
		num++;
	}
	return 0;
}

案例练习:系统随机生成一个1-100之间的数字,玩家进行猜测,提示玩家数字过大或过小,如果猜对恭喜玩家胜利,并退出游戏

案例流程图如下:

在这里插入图片描述
案例代码如下:

#include<iostream>
#include <ctime>
using namespace std;

int main()
{
	//添加随机数种子,保证每次随机数不同
	srand((unsigned int)time(NULL));

	//rand()%100  生成一个0-99的随机数

	int num = rand() % 100 + 1;
	cout << num << endl;

	int val = 0;//代表玩家输入的数据

	while (1)
	{
		cin >> val;
		if (val > num)
		{
			cout << "您猜测的值过大" << endl;
		}
		else if (val < num)
		{
			cout << "您猜测的值过小" << endl;
		}
		else
		{
			cout << "恭喜您猜测正确" << endl;
			break;
		}
	}
	return 0;
}

在这里插入图片描述

do…while循环

  • 与while的区别在于do…while先执行一次循环语句,再判断循环条件

练习代码如下:

#include<iostream>
using namespace std;

int main()
{
	//给屏幕打印0-9
	int num = 0;
	do
	{
		cout << num << endl;
		num++;
	} while (num < 10);

	return 0;
}

在这里插入图片描述

案例:水仙花数

水仙花数是指一个三位数,它的每个位上的数字的三次幂之和等于它本身,利用do…while语句,求出所有3位数中的水仙花数

案例代码如下:

#include<iostream>
using namespace std;

int main()
{
	//将所有的三位数输出100-999
	//在所有的三位数中找到水仙花数

	int num = 100;

	do
	{
		int a = 0;//个位
		int b = 0;//十位
		int c = 0;//百位


		a = num % 10;
		b = num / 10 % 10;
		c = num / 100;

		if (a * a * a + b * b * b + c * c * c == num)//判断是不是水仙花数
		{
			cout << num << endl;
		}
		num++;
	} while (num < 1000);

	return 0;
}

在这里插入图片描述

for循环

作用:满足循环条件,执行条件语句

练习代码如下:

#include<iostream>
using namespace std;

int main()
{
	//打印0-9
	int i = 0;
	for (i; i < 10; i++)
	{
		cout << i << endl;
	}

	for (int i = 0; i < 10; i++)
	{
		cout << i << endl;
	}
	return 0;
}

练习案例:从1-100,如果数字个位含有7,或者数字十位含有7,或者数字是7的倍数,我们打印敲桌子,其余数字直接打印输出

案例代码如下:

#include<iostream>
using namespace std;

int main()
{
	for (int i = 1; i <= 100; i++)
	{
		if (i % 10 == 7 || i / 10 % 10 == 7 || i % 7 == 0)
		{
			cout << "敲桌子" << endl;
		}
		else
		{
			cout << i << endl;
		}
	}
	return 0;
}

嵌套循环

作用:在循环中再嵌套一层循环,通常用来解决一些实际性问题

练习代码如下:

#include<iostream>
using namespace std;

int main()
{
	//在屏幕上打印一个8*8的星号矩阵

	for (int i = 0; i < 8; i++)
	{
		for (int j = 0; j < 8; j++)
		{
			cout << "*" << " ";
		}
		cout << endl;
	}
	return 0;
}

在这里插入图片描述

案例:乘法口诀表

案例代码如下:

#include <iostream>
using namespace std;

int main()
{
	//打印九九乘法表
	for (int i = 1; i <= 9; i++)
	{
		for (int j = 1; j <= i; j++)
		{
			cout << j << "*" << i << "=" << i * j << " ";
		}
		cout << endl;
	}
	return 0;
}

在这里插入图片描述

三、跳转语句

(一)break语句

练习代码如下:

#include <iostream>
using namespace std;

int main()
{
	//出现在switch语句中
	cout << "请选择副本难度" << endl;
	cout << "1、普通" << endl;
	cout << "2、中等" << endl;
	cout << "3、困难" << endl;

	int select = 0;

	cin >> select;
	
	switch (select)
	{
	case 1:
		cout << "选择了普通难度" << endl;
		break;
	case 2:
		cout << "选择了中等难度" << endl;
		break;
	case 3:
		cout << "选择了困难难度" << endl;
		break;
	default:
		break;//这个break用来退出switch语句
	}


	//出现在循环语句中
	for (int i = 0; i < 10; i++)
	{
		if (i == 5)
		{
			break;//此时break可以退出该循环
		}
		cout << i << endl;
	}


	//出现在嵌套循环中
	for (int i = 0; i < 10; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			if (j == 5)
			{
				break;//退出内层循环(根据就近原则)
			}
			cout << "*" << " ";
		}
		cout << endl;
	}

	return 0;
}

(二)continue语句

作用:在循环语句中,跳过本次循环中余下的尚未执行的语句,执行下一次循环

练习代码如下:

#include <iostream>
using namespace std;

int main()
{
	for (int i = 0; i <= 100; i++)
	{
		//如果是奇数输出,偶数不输出
		if (i % 2 == 0)
		{
			continue;//遇到continue,该次循环中该条语句以后的语句不执行,重新进行新一轮循环
		}
		cout << i << endl;
	}
	return 0;
}

(三)goto语句

作用:可以无条件跳转语句

练习代码如下:

#include <iostream>
using namespace std;

int main()
{
	cout << "1" << endl;
	cout << "2" << endl;
	cout << "3" << endl;
	cout << "4" << endl;
	cout << "5" << endl;
	cout << "6" << endl;

	//goto语句
	cout << "1" << endl;
	cout << "2" << endl;
	goto FLAG;
	cout << "3" << endl;
	cout << "4" << endl;
	cout << "5" << endl;
	FLAG:
	cout << "6" << endl;
	return 0;
}

在这里插入图片描述
???

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-09-22 14:29:00  更:2021-09-22 14:29:24 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 0:00:34-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码