04x程序流程结构

admin 2025年1月22日09:05:19评论7 views字数 5454阅读18分10秒阅读模式

关注公众号,后台回复 找书+ C++Primer 获取C++相关电子书。

选择结构

判断语句

C++ 编程语言提供了以下类型的判断语句。点击链接查看每个语句的细节。

语句
描述
if 语句
一个 if 语句 由一个布尔表达式后跟一个或多个语句组成。
if...else 语句
一个 if 语句 后可跟一个可选的 else 语句,else 语句在布尔表达式为假时执行。
嵌套 if 语句
您可以在一个 if 或 else if 语句内使用另一个 if 或 else if 语句。
switch 语句
一个 switch 语句允许测试一个变量等于多个值时的情况。
嵌套 switch 语句
您可以在一个 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;
}

04x程序流程结构
image-20250120100241518

? : 运算符(三目表达式同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;
}

04x程序流程结构
image-20250120100511647

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;
}


04x程序流程结构
image-20250120114048360

循环结构

循环类型

C++ 编程语言提供了以下几种循环类型。点击链接查看每个类型的细节。

循环类型
描述
while 循环
当给定条件为真时,重复语句或语句组。它会在执行循环主体之前测试条件。
for 循环
多次执行一个语句序列,简化管理循环变量的代码。
do...while 循环
除了它是在循环主体结尾测试条件外,其他与 while 语句类似。
嵌套循环
您可以在 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;
}

04x程序流程结构
image-20250120115404114

嵌套循环

#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;
}
04x程序流程结构
image-20250120161945774

跳转语句-循环控制语句

循环控制语句更改执行的正常序列。当执行离开一个范围时,所有在该范围中创建的自动对象都会被销毁。

C++ 提供了下列的控制语句。点击链接查看每个语句的细节。

控制语句
描述
break 语句
终止 loop 或 switch 语句,程序流将继续执行紧接着 loop 或 switch 的下一条语句。
continue 语句
引起循环跳过主体的剩余部分,立即重新开始测试条件。
goto 语句
将控制转移到被标记的语句。但是不建议在程序中使用 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;
}
04x程序流程结构
image-20250120163428616

经典案例

经典案例-猜数字游戏

#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;
}


04x程序流程结构
image-20250120120245397

经典案例-水仙花数

#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;
}
04x程序流程结构
image-20250120120810863

拓展:四位水仙花数

04x程序流程结构
image-20250120144932477

经典案例-敲桌子游戏

#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;
}
04x程序流程结构
image-20250120161234972

 

原文始发于微信公众号(泷羽Sec-静安):04x程序流程结构

免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2025年1月22日09:05:19
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   04x程序流程结构https://cn-sec.com/archives/3657765.html
                  免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉.

发表评论

匿名网友 填写信息