关注公众号,后台回复
找书+ C++Primer
获取C++相关电子书。
选择结构
判断语句
C++ 编程语言提供了以下类型的判断语句。点击链接查看每个语句的细节。
|
|
---|---|
if 语句 |
|
if...else 语句 |
|
嵌套 if 语句 |
|
switch 语句 |
|
嵌套 switch 语句 |
|
#include<iostream>
usingnamespacestd;
intmain(){
int x, y, z;
// 让用户输入 x, y, z 的值
cout << "请输入 x 的值: ";
cin >> x;
cout << "请输入 y 的值: ";
cin >> y;
cout << "请输入 z 的值: ";
cin >> z;
// 单行 if
if (x < y) cout << "x 小于 y" << endl;
// 多行 if
if (x < y) {
cout << "x 小于 y" << endl;
cout << "这是一个多行 if 语句" << endl;
}
// 嵌套 if
if (x < y) {
if (y < z) {
cout << "y 小于 z" << endl;
}
else {
cout << "y 不小于 z" << endl;
}
}
else {
cout << "x 不小于 y" << endl;
}
cin.get(); // 使程序暂停,等待用户输入
return0;
}
? : 运算符(三目表达式同C)
我们已经在前面的章节中讲解了 条件运算符 ? :,可以用来替代 if...else 语句。它的一般形式如下:
Exp1 ? Exp2 : Exp3;
其中,Exp1、Exp2 和 Exp3 是表达式。请注意,冒号的使用和位置。
? 表达式的值是由 Exp1 决定的。如果 Exp1 为真,则计算 Exp2 的值,结果即为整个 ? 表达式的值。如果 Exp1 为假,则计算 Exp3 的值,结果即为整个 ? 表达式的值。
#include<iostream>
usingnamespacestd;
intmain(){
int x, y, z;
// 让用户输入 x, y, z 的值
cout << "请输入 x 的值: ";
cin >> x;
cout << "请输入 y 的值: ";
cin >> y;
cout << "请输入 z 的值: ";
cin >> z;
// 使用三目运算符改写嵌套 if
cout << (x < y ? (y < z ? "y 小于 z" : "y 不小于 z") : "x 不小于 y") << endl;
cin.get(); // 使程序暂停,等待用户输入
return0;
}
Switch结构
#include<iostream>
usingnamespacestd;
intmain(){
int score;
while (true) {
// 让用户输入分数
cout << "请输入分数(输入-1退出): ";
cin >> score;
// 检查是否输入-1以退出循环
if (score == -1) {
break;
}
// 使用 switch 和嵌套 switch 判断分数等级
switch (score / 10) {
case10:
case9:
switch (score) {
case100:
case99:
cout << "A++" << endl;
break;
case98:
case97:
case96:
case95:
cout << "A+" << endl;
break;
default:
cout << "A" << endl;
break;
}
break;
case8:
cout << "B" << endl;
break;
case7:
cout << "C" << endl;
break;
case6:
cout << "D" << endl;
break;
default:
cout << "E" << endl;
break;
}
}
return0;
}
循环结构
循环类型
C++ 编程语言提供了以下几种循环类型。点击链接查看每个类型的细节。
|
|
---|---|
while 循环 |
|
for 循环 |
|
do...while 循环 |
|
嵌套循环 |
|
while(condition)
{
statement(s);
}
for ( init; condition; increment ) // 均为空表示无限真值循环
{
statement(s);
}
do
{
statement(s);
}while( condition );
#include<iostream>
usingnamespacestd;
intmain(){
int i;
// do-while 循环
i = 1;
cout << "do-while 循环:" << endl;
do {
cout << i << " ";
i++;
} while (i <= 5);
cout << endl;
// while 循环
i = 1;
cout << "while 循环:" << endl;
while (i <= 5) {
cout << i << " ";
i++;
}
cout << endl;
// for 循环
cout << "for 循环:" << endl;
for (i = 1; i <= 5; i++) {
cout << i << " ";
}
cout << endl;
return0;
}
嵌套循环
#include<iostream>
usingnamespacestd;
intmain(){
cout << "99 乘法表:" << endl;
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
cout << j << " * " << i << " = " << j * i << "t";
}
cout << endl;
}
return0;
}
跳转语句-循环控制语句
循环控制语句更改执行的正常序列。当执行离开一个范围时,所有在该范围中创建的自动对象都会被销毁。
C++ 提供了下列的控制语句。点击链接查看每个语句的细节。
|
|
---|---|
break 语句 |
|
continue 语句 |
|
goto 语句 |
|
#include<iostream>
usingnamespacestd;
intmain(){
cout << "示例程序展示 break, continue 和 goto 的用法:" << endl;
for (int i = 1; i <= 20; i++) {
if (i == 5) {
cout << "遇到 5,使用 continue 跳过本次循环" << endl;
continue; // 跳过本次循环,继续下一次循环
}
if (i == 10) {
cout << "遇到 10,使用 break 退出当前循环" << endl;
break; // 退出当前循环
}
cout << "当前数字: " << i << endl;
}
// 使用 goto 跳转到标签 start2
cout << "使用 goto 跳转到标签 start2" << endl;
goto start2; //慎用goto,容易死循环。
start2:
for (int j = 11; j <= 20; j++) {
if (j == 15) {
cout << "遇到 15,使用 goto 跳转到标签 end" << endl;
goto end; // 跳转到标签 end
}
cout << "当前数字: " << j << endl;
}
end:
cout << "程序结束" << endl;
return0;
}
经典案例
经典案例-猜数字游戏
#include<iostream>
#include<cstdlib> // 包含 rand() 和 srand()
#include<ctime> // 包含 time()
usingnamespacestd;
intmain(){
srand(static_cast<unsignedint>(time(0))); // 使用当前时间作为随机数种子
int numberToGuess = rand() % 100 + 1; // 生成 1 到 100 之间的随机数
int guess;
int attempts = 0;
char playAgain;
do {
cout << "猜数字游戏开始!" << endl;
cout << "我已经想好了一个 1 到 100 之间的数字。" << endl;
// while 循环用于猜测数字
while (true) {
cout << "请输入你的猜测: ";
cin >> guess;
attempts++;
if (guess < numberToGuess) {
cout << "太小了!" << endl;
}
elseif (guess > numberToGuess) {
cout << "太大了!" << endl;
}
else {
cout << "恭喜你,猜对了!你用了 " << attempts << " 次猜对了数字。" << endl;
break;
}
}
// for 循环用于询问是否再次玩游戏
for (;;) {
cout << "你想再玩一次吗?(y/n): ";
cin >> playAgain;
if (playAgain == 'y' || playAgain == 'Y' || playAgain == 'n' || playAgain == 'N') {
break;
}
else {
cout << "无效输入,请输入 'y' 或 'n'。" << endl;
}
}
if (playAgain == 'y' || playAgain == 'Y') {
numberToGuess = rand() % 100 + 1; // 生成新的随机数
attempts = 0; // 重置尝试次数
}
} while (playAgain == 'y' || playAgain == 'Y');
cout << "感谢你玩猜数字游戏!" << endl;
return0;
}
经典案例-水仙花数
#include<iostream>
#include<cmath> // 包含 pow() 函数
usingnamespacestd;
/*
* 次方之和等于该数本身。例如,对于三位数来说,如果一个数等于其各位数字的立方和,则该数为水仙花数。
例如:
• 153 是一个三位数的水仙花数,因为 (1^3 + 5^3 + 3^3 = 153)。
• 370 是一个三位数的水仙花数,因为 (3^3 + 7^3 + 0^3 = 370)。
*/
intmain(){
int number = 100; // 从 100 开始,因为 100 是最小的三位数
cout << "三位数中的所有水仙花数如下:" << endl;
do {
int sum = 0;
int temp = number; // 临时变量,用于计算各位数字的立方和
while (temp > 0) {
int digit = temp % 10;
sum += pow(digit, 3);
temp /= 10; // temp=temp/10,每次循环往前近一位,去掉最后一位
}
if (sum == number) {
cout << number << " ";
}
number++;
} while (number <= 999); // 999 是最大的三位数
cout << endl;
return0;
}
拓展:四位水仙花数
经典案例-敲桌子游戏
#include<iostream>
#include<string> // 添加此行以包含字符串库
usingnamespacestd;
intmain(){
cout << "敲桌子游戏开始!" << endl;
for (int i = 1; i <= 100; i++) {
// 检查是否是 7 的倍数或者包含数字 7
if (i % 7 == 0 || to_string(i).find('7') != string::npos) {
cout << "敲桌子" << endl;
}
else {
cout << i << endl;
}
}
return0;
}
原文始发于微信公众号(泷羽Sec-静安):04x程序流程结构
免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论