Files
Linux-C-Notes/Chapter4/C4-流程控制.md
2024-04-01 15:38:16 +08:00

387 lines
5.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 目录
- [流程控制](#流程控制)
- [关键字](#关键字)
- [详解选择](#详解选择)
- [`if-else`](#if-else)
- [`switch-case`](#switch-case)
- [详解循环](#详解循环)
- [`while`](#while)
- [`do-while`](#do-while)
- [`for`](#for)
- [`goto`](#goto)
- [死循环](#死循环)
- [辅助控制](#辅助控制)
- [练习专题](#练习专题)
- [1](#1)
- [2](#2)
- [3](#3)
- [4](#4)
- [5](#5)
- [6](#6)
- [7](#7)
- [8](#8)
- [9](#9)
- [10](#10)
- [11](#11)
- [12](#12)
# 流程控制
1. 顺序,选择,循环
2. NS图流程图工具VisioDia
3. 简单结构与复杂结构:自然流程
- 顺序:语句逐句执行
- 选择:出现了一种以上的情况
- 循环:在某个条件成立的情况下,重复执行某个动作
## 关键字
- 选择:`if-else``switch-case`
- 循环:`while``do-while``for``if-goto`
- 辅助控制:`continue``break`
## 详解选择
### `if-else`
```c
// 格式
if(exp)
cmd;
// 或者:
if(exp)
cmd1;
else
cmd2;
```
> `else`只与离它最近的`if`匹配
```c
#include <stdio.h>
#include <stdlib.h>
/**
* score [90-100] A
* score [80-90) B
* score [70-80) C
* score [60-70) D
* score [0-60) E
*/
// 闰年的判断能被4整除但不能被100整除或者能被400整除
int main()
{
int year;
scanf("%d", &year);
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
printf("%d is leap year.\n", year);
else
printf("%d is not leap year\n.", year);
#if 0
int score;
printf("Enter a score:[0,100]:\n");
scanf("%d", &score);
if (score < 0 || score > 100)
{
fprintf(stderr, "Input error!\n");
exit(1);
}
if (score > 90)
puts("A");
else if (score > 80)
puts("B");
else if (score > 70)
puts("C");
else if (score > 60)
puts("D");
else
puts("E");
#endif
#if 0
if (score <= 100 && score >= 90)
puts("A");
if (score <= 90 && score >= 80)
puts("B");
if (score <= 80 && score >= 70)
puts("C");
if (score <= 70 && score >= 60)
puts("D");
if (score <= 60 && score >= 0)
puts("E");
#endif
#if 0
int a = 1, b = 1,c=2;
if (a == b)
if(b==c)
printf("a==b\n");
else
printf("a!=b\n")
注意else看的是最近的if
#endif
#if 0
int a = 9, b = 10;
if (b++ < a)
printf("1\n");
else
printf("0\n");
printf("a=%d,b=%d\n", a, b);
printf("%d\n", (++b < a, ++a, b++));
#endif
exit(0);
}
```
### `switch-case`
```c
// 格式
switch(exp)
{
case :
break;
case :
break;
......
default:
}
```
1. 最好的是考虑到所有的情况写出来在default进行报错而不是省略一个情况放到default里。
2. `case`后面要的是**常量或常量表达式**,例如放`score/10 >= 9`就是不行的。
```c
#include <stdio.h>
#include <stdlib.h>
int main()
{
int ch;
ch = getchar();
switch (ch)
{
case 'a':
case 'A':
printf("Ant\n");
break;
case 'b':
case 'B':
printf("Butterfly\n");
break;
case 'c':
case 'C':
printf("Cobra\n");
break;
default:
printf("Input error");
// break;
// default可以不用
}
#if 0
int score;
printf("Please enter:\n");
scanf("%d", &score);
if (score < 0 || score > 100)
{
fprintf(stderr, "EINVAL\n");
exit(1);
}
switch (score / 10)
{
case 10:
case 9:
puts("A");
break;
case 8:
puts("B");
break;
case 7:
puts("C");
break;
case 6:
puts("D");
break;
default:
puts("E");
break;
}
#endif
exit(0);
}
```
## 详解循环
### `while`
```c
// 最少执行0次
while(exp)
loop;
```
### `do-while`
```c
// 最少执行1次
do
{
loop;
}while(exp);
```
### `for`
```c
// 最少执行0次
for(exp1;exp2;exp3)
loop;
```
### `goto`
- 慎重使用`if-goto`
- `goto`实现的是 无条件的跳转,且不能跨函数跳转
### 死循环
```c
while(1);
for(;;);
```
`ctrl + c`杀掉死循环。
### 辅助控制
`break``continue`
## 练习专题
### 1
> A以每年10%的单利息投资了100美元B以每年5%的复合利息投资了100美元。
>
> 求需要多少年B的投资总额超过A并且输出当时各自的资产总额
### 2
> 从终端读入数据直到输入0为止计算出其中的偶数的个数和平均值奇数的个数和平均值
### 3
> 从终端输入若干字符,统计元音字母
### 4
> 写出fibonacci数列的前40项不能用数组
>
> 1, 1, 2, 3, 4, 5, 8, ...
### 5
> 输出九九乘法表
### 6
> 百钱买百鸡公鸡5元母鸡3元鸡仔1元算出来买的各自多少只
### 7
> 输出1000内的水仙花数
>
> 153: 1+125+27 =153
### 8
> 求出1000以内的所有的质数
>
> 2 3 5 7 11 13 17
### 9
> 在终端实现如下输出
>
> ABCDEF
>
> BCDEF
>
> CDEF
>
> DEF
>
> EF
>
> F
### 10
> 包括钻石型
>
> ```bash
> *
> * *
> * * *
> * *
> *
> ```
### 11
> 从终端输入N个数以字母Q/q作为终止求和。
### 12
> 从半径为1开始输出圆的面积直到面积大于100为止