From bacc769f09c71e36f332b8d37f9df528d7df9ed4 Mon Sep 17 00:00:00 2001 From: lzy Date: Fri, 22 Mar 2024 16:51:44 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E5=9B=9B=E7=AB=A0=E5=AE=8C=E7=BB=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Chapter4/C4-流程控制.md | 385 ++++++++++++++++++++++++++++++++++++++++ Chapter4/if.c | 87 +++++++++ Chapter4/loop.c | 87 +++++++++ Chapter4/switch.c | 64 +++++++ 4 files changed, 623 insertions(+) create mode 100644 Chapter4/C4-流程控制.md create mode 100644 Chapter4/if.c create mode 100644 Chapter4/loop.c create mode 100644 Chapter4/switch.c diff --git a/Chapter4/C4-流程控制.md b/Chapter4/C4-流程控制.md new file mode 100644 index 0000000..aa0ccce --- /dev/null +++ b/Chapter4/C4-流程控制.md @@ -0,0 +1,385 @@ +- [流程控制](#流程控制) + - [关键字](#关键字) + - [详解选择](#详解选择) + - [`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图,流程图(工具:Visio,Dia) +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 +#include + +/** + * 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 +#include + +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为止 \ No newline at end of file diff --git a/Chapter4/if.c b/Chapter4/if.c new file mode 100644 index 0000000..1e9885b --- /dev/null +++ b/Chapter4/if.c @@ -0,0 +1,87 @@ +#include +#include + +/** + * 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); +} \ No newline at end of file diff --git a/Chapter4/loop.c b/Chapter4/loop.c new file mode 100644 index 0000000..472d91d --- /dev/null +++ b/Chapter4/loop.c @@ -0,0 +1,87 @@ +#include +#include + +#define LEFT 1 +#define RIGHT 100 + +int main() +{ + int i; + int sum = 0; + int start, end = 10; + + i = LEFT; + +loop: + sum += i; + i++; + + if (i <= RIGHT) + goto loop; + + printf("sum = %d\n", sum); + +#if 0 + i = LEFT; + for (;;) + { + sum += i; + i++; + if (i > RIGHT) + break; + } + printf("sum = %d\n", sum); +#endif + +#if 0 +do +{ + menu(); + scanf(); + if() + something(); +} while (); + +#endif + +#if 0 + printf("Please enter:\n"); + scanf("%d", &start); + + while (start <= end) + { + sum += start; + start++; + } + + do + { + sum += start; + start++; + } while (start <= end); + + printf("start = %d\n", start); + printf("end = %d\n", end); +#endif + +#if 0 + i = LEFT; + do + { + sum += i; + i++; + } while (i <= RIGHT); + printf("sum=%d\n", sum); +#endif + +#if 0 + while (i <= RIGHT) + { + sum += i; + i++; + } + printf("sum=%d\n", sum); +#endif + + exit(0); +} \ No newline at end of file diff --git a/Chapter4/switch.c b/Chapter4/switch.c new file mode 100644 index 0000000..829dd43 --- /dev/null +++ b/Chapter4/switch.c @@ -0,0 +1,64 @@ +#include +#include + +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); +} \ No newline at end of file