Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chap 3 分支结构 3.1 统计输入的一批字符中各类字符的数量 3.2 查询自动售货机中商品的价格 3.3 分支结构程序设计.

Similar presentations


Presentation on theme: "Chap 3 分支结构 3.1 统计输入的一批字符中各类字符的数量 3.2 查询自动售货机中商品的价格 3.3 分支结构程序设计."— Presentation transcript:

1 Chap 3 分支结构 3.1 统计输入的一批字符中各类字符的数量 3.2 查询自动售货机中商品的价格 3.3 分支结构程序设计

2 3.1 统计输入的一批字符中各类字符的数量 例3-1 输入10个字符,统计其中英文字母、数字字符和其他字符的个数。 3.1.1 程序解析
3.1 统计输入的一批字符中各类字符的数量 例3-1 输入10个字符,统计其中英文字母、数字字符和其他字符的个数。 程序解析 字符类型 字符数据的输入和输出 3.1.4 逻辑运算 3.1.5 else-if 语句

3 3.1.1 程序解析 多层缩进的书写格式 使程序层次分明 #include <stdio.h> int main(void)
{ int digit, i, letter, other; char ch;   digit = letter = other = 0; printf(“Enter 10 characters: "); for(i = 1; i <= 10; i++){ ch = getchar(); /* 从键盘输入一个字符,赋值给变量 ch */ if((ch >= 'a' && ch <= 'z' ) || ( ch >= 'A' && ch <= 'Z')) letter ++; else if(ch >= '0' && ch <= '9') /* 如果ch是数字字符 */ digit ++; else other ++; } printf("letter=%d,digit=%d,other=%d\n",letter,digit,other); return 0; 程序解析 input 10 characters: Reold 123? letter=5, digit=3, other=2 多层缩进的书写格式 使程序层次分明

4 字符类型 char ch; (ch >= 'a' && ch <= 'z' ) || ( ch >= 'A' && ch <= 'Z') (ch >= '0' && ch <= '9') 字符型数据 字符变量:ch 字符常量: 'a'

5 字符常量 'a' 'z' 'A' 'Z' '0' '9' ' ' '\n' ASCII字符集:列出所有可用的字符
'0'-'9' 'A'-'Z' 'a'-'z' 区分数字 1 和数字字符 '1'

6 字符变量 char ch; 定义字符变量ch,用于存放字符型数据。 ch = 'A';

7 3.1.3 字符型数据的输入和输出 字符输入函数getchar 字符输出函数putchar 输入一个字符 char ch;
字符型数据的输入和输出 字符输入函数getchar 输入一个字符 char ch; ch = getchar( ); 字符输出函数putchar 输出一个字符 putchar(输出参数); char ch; ch = getchar(); putchar (ch); putchar (‘?'); 字符常量或字符变量

8 调用scanf和printf输入输出字符
double value1, value2; char operator; printf(“Type in an expression: ”); scanf(“%lf%c%lf”, &value1, &operator, &value2); printf(“%.2f %c %.2f”, value1, operator, value2); Type in an expression:

9 逻辑运算 (ch >= 'a' && ch <= 'z' ) || ( ch >= 'A' && ch <= 'Z') ch >= '0' && ch <= '9‘ 关系运算逻辑值(true/false) 逻辑运算

10 3种逻辑运算符 逻辑与 && 逻辑或 || 逻辑非 ! X && Y X || Y X Y X Y ! X X

11 逻辑运算符的含义 逻辑与 && 逻辑或 || 逻辑非 !
逻辑与 && 逻辑或 || 逻辑非 ! (x>1)&&(y>1) (x>1)||(y>1) (x>1) !(x>1) 即 x<=1

12 逻辑运算符的功能 逻辑与 && 逻辑或 || 逻辑非 ! a b a&&b a||b !a 假 假 假 假 真 假 真 假 真 真
逻辑与 && 逻辑或 || 逻辑非 ! a b a&&b a||b !a 假 假 假 假 真 假 真 假 真 真 真 假 假 真 假 真 真 真 真 假

13 逻辑表达式 逻辑表达式:用逻辑运算符将逻辑运算对象连接起来的式子。 (ch >= 'a') && (ch <= 'z')
或: ch >= 'a' && ch <= 'z' (ch >= 'a' && ch <= 'z') || ( ch >= 'A' && ch <= 'Z') 判断ch 是否为英文字母

14 条件的表示 例3-2 写出满足下列条件的C表达式。 ch 是空格或者回车 ch == ' ' || ch == '\n' number是偶数
year 是闰年,即 year 能被 4 整除但不能被 100 整除,或 year 能被 400 整除 (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)

15 else – if 语句 if((ch >= 'a' && ch <= 'z' )||( ch >= 'A' && ch <= 'Z')) letter ++; else if(ch >= '0' && ch <= '9') digit ++; else other ++; 实现多路选择

16 else – if 语句 if (表达式1) 语句1 else if (表达式2) 语句2 ……
if((ch >= 'a' && ch <= 'z' )||( ch >= 'A' && ch <= 'Z')) letter ++; else if(ch >= '0' && ch <= '9') digit ++; else other ++; if (表达式1) 语句1 else if (表达式2) 语句2 …… else if (表达式n-1) 语句n-1 else 语句n 表达式1 表达式2 语句1 语句2 语句n-1 语句n 表达式n-1

17 分段计算水费 例3-3 分段计算水费

18 源程序-分段计算水费 # include <stdio.h> int main(void) { double x, y;
printf("Enter x:"); scanf("%lf", &x); if (x < 0){ y = 0; } else if (x <= 15){ y = 4 * x / 3; else{ y = 2.5 * x ; printf("f(%.2f) = %.2f\n", x, y); return 0;

19 3.2 查询自动售货机中商品的价格 例3-4 查询自动售货机中商品的价格 程序解析 switch语句

20 程序解析 假设自动售货机出售4种商品,薯片(crisps)、爆米花(popcorn)、巧克力(chocolate)和可乐(cola),售价分别是每份3.0、2.5、4.0和3.5元。在屏幕上显示以下菜单,用户可以连续查询商品的价格,当查询次数超过5次时,自动退出查询;不到5次时,用户可以选择退出。当用户输入编号1~4,显示相应商品的价格;输入0,退出查询;输入其他编号,显示价格为0。 [1] Select crisps [2] Select popcorn [3] Select chocolate [4] Select cola [0] Exit

21 [1] Select crisps [2] Select popcorn [3] Select chocolate [4] Select cola [0] Exit Enter choice: 1 price = 3.0 Enter choice: 7 price = 0.0 [1] Select crisps Enter choice: 0 Thanks #include <stdio.h> Int main(void) { int choice, i; double price; for( i = 1; i <= 5; i++) { printf("[1] Select crisps \n"); printf("[2] Select popcorn \n"); printf("[3] Select chocolate \n"); printf("[4] Select cola \n"); printf("[0] exit \n");   printf("Enter choice: "); scanf("%d", &choice);   if(choice == 0) break; switch (choice) { case 1: price=3.0; break; case 2: price=2.5; break; case 3: price=4.0; break; case 4: price=3.5; break; default: price=0.0; break; } printf("price = %0.1f\n", price); printf("Thanks \n");

22 3.2.2 switch语句 处理多分支选择问题,3种情况 1、在switch语句的每个语句段中都使用break语句
case 常量表达式1:语句段1; break; case 常量表达式2:语句段2 ; break; ....… case 常量表达式n:语句段n ; break; default : 语句段n+1 ; break; }

23 用else-if 如何实现? switch (choice) { switch(表达式){
case 1: price=3.0; break; case 2: price=2.5; break; case 3: price=4.0; break; case 4: price=3.5; break; default: price=0.0; break; } switch(表达式){ case 常量表达式1:语句段1; break; case 常量表达式2:语句段2 ; break; ....… case 常量表达式n:语句段n ; break; default : 语句段n+1 ; break; } 用else-if 如何实现? 表达式的值=常量表达式 2 的值 表达式 语句段1 语句段2 语句段n 语句段n+1 表达式的值=常量表达式 1 的值 表达式的值=常量表达式 n 的值 其他 break

24 求解简单表达式 例3-5 输入一个形式如“操作数 运算符 操作数”的四则运算表达式,输出运算结果。 例如: 输入:3.1+4.8
输出:7.9

25 源程序 # include <stdio.h> int main(void)
{ char operator; double value1, value2; printf("Type in an expression: "); scanf("%lf%c%lf", &value1, &operator, &value2); switch(operator){ case '+': printf("=%.2f\n", value1+value2); break; case '-': printf("=%.2f\n", value1-value2); case '*': printf("=%.2f\n", value1*value2); case '/': printf("=%.2f\n", value1/value2); default: printf("Unknown operator\n"); } return 0; 源程序 Type in an expression: =7.9

26 2、在switch中不使用break switch(表达式){ case 常量表达式1:语句段1; case 常量表达式2:语句段2;
....… case 常量表达式n:语句段n; default : 语句段n+1; }

27 switch(表达式){ case 常量表达式1:语句段1; case 常量表达式2:语句段2; ....…
case 常量表达式n:语句段n; default : 语句段n+1; } switch (choice) { case 1: price=3.0; case 2: price=2.5; case 3: price=4.0; case 4: price=3.5; default: price=0.0; } price=? 表达式的值=常量表达式 2 的值 表达式 语句段1 语句段2 语句段n 语句段n+1 表达式的值=常量表达式 1 的值 表达式的值=常量表达式 n 的值 其他

28 3、在switch的某些语句段中使用break
例3-6 输入10个字符,分别统计出其中空格或回车、数字和其他字符的个数。 例3-1 输入10个字符,统计其中英文字母、数字字符和其他字符的个数。

29 int main(void) { int blank, digit, i, other; char ch; blank = digit = other = 0; printf("Enter 10 characters: "); for(i = 1; i <= 10; i++){ ch = getchar(); switch (ch){ case ' ' : case '\n': blank ++; break; case '0' : case '1' : case '2' : case '3' : case '4' : case '5' : case '6' : case '7' : case '8' : case '9' : digit ++; default: other ++; } printf("blank=%d, digit=%d, other=%d\n", blank, digit, other); return 0; Enter 10 characters: Reold 123? blank=1, digit=3, other=6

30 3.3 分支结构程序设计 分支结构一般分为二分支和多分支两种结构 用条件语句(if 和 switch)实现选择

31 3.3.1 二分支结构和基本的 if 语句 if (表达式) 语句1 if (表达式) 语句1 else 语句2 一条语句 语句1 表达式
语句1 表达式 语句2

32 判断数字的奇偶性 例3-7 输入1个整数,判断该数是奇数还是偶数。 number % 2 == 0

33 源程序-判断数字的奇偶性 #include <stdio.h> int main(void) { int number;
printf("Enter a number: "); scanf("%d", &number); if(number % 2 == 0){ printf("Tne number is even. \n"); } else{ printf("Tne number is odd. \n"); return 0;

34 求绝对值 例3-8 输入1个整数,输出它的绝对值 。 当number < 0时,number = - number;
例3-8 输入1个整数,输出它的绝对值 。 当number < 0时,number = - number; 当number >= 0时,number = ?

35 源程序-求绝对值 #include <stdio.h> int main(void) { int number;
printf("Enter a number: "); scanf("%d", &number); if(number < 0){ number = -number; } printf("The absolute value is %d.\n", number); return 0; Enter a number: 10 The absolute value is 10. Enter a number: -300 The absolute value is 300.

36 3.3.2 多分支结构与嵌套的 if 语句和switch语句
多分支结构有多种形式 实现方法: 嵌套的 if 语句 else – if 语句 嵌套的 if - else语句 switch语句

37 嵌套的 if 语句 if (表达式) 语句1 else 语句2 else – if 语句 嵌套的 if - else语句 if 语句

38 else – if 语句 if (表达式1) 语句1 else if(表达式2) 语句2 …… else if(表达式n-1) 语句n-1
else 语句n else – if 语句 表达式1 表达式2 语句1 语句2 语句n-1 语句n 表达式n-1 n个分支需要n-1次比较

39 嵌套的 if – else 语句 if(表达式1) else if(表达式2) 语句1 else 语句2 if(表达式3) 语句3
语句4 表达式2 语句3 语句1 语句2

40 2种嵌套if语句的比较 if (x < 1) y = x + 1; else if (x < 2) y = x + 2;
y=x+3 if (x < 1) y = x + 1; else if (x < 2) y = x + 2; else y = x + 3; x<1 x<2 y=x+1 y=x+2 y=x+3 if (x < 2) if (x < 1) y = x + 1; else y = x + 2; else y = x + 3;

41 else 和 if 的匹配 if(表达式1) else else 与最靠近它的、没有与别的 else 匹配过的 if 匹配 if(表达式1)

42 改变else 和 if 的配对 例3-10 改写下列 if 语句,使 else 和第1个 if 配对。 if (x < 2)
if (x < 1) y = x + 1; else y = x + 2; 每条语句的执行条件? if (x < 2){ if (x < 1) y = x + 1; } else y = x + 2; if (x < 2) if (x < 1) y = x + 1; else; else y = x + 2;

43 本章要点 什么是分支结构?它的作用是什么? switch 语句中的 break 起什么作用?
逻辑运算和关系运算的相同之处是什么? 它们之间又有什么不同? 字符型数据在内存中是如何存储的?


Download ppt "Chap 3 分支结构 3.1 统计输入的一批字符中各类字符的数量 3.2 查询自动售货机中商品的价格 3.3 分支结构程序设计."

Similar presentations


Ads by Google