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

3 3.1 简单的猜数游戏 例3-1 简单的猜数游戏。输入你所猜的整数(假定 3.1.1 程序解析
3.1 简单的猜数游戏 例3-1 简单的猜数游戏。输入你所猜的整数(假定 1~100内),与计算机产生的被猜数比较,若相等, 显示猜中;若不等,显示与被猜数的大小关系。 程序解析 二分支结构和if – else语句 多分支结构和else – if 语句

4 3.1.1 程序解析 多层缩进的书写格式 使程序层次分明 # include <stdio.h> int main(void)
{ int mynumber = 38; int yournumber; printf("Input your number: "); scanf("%d", &yournumber); if(yournumber == mynumber) printf("Ok! you are right!\n"); else if(yournumber > mynumber ) printf("Sorry! your number is bigger than my number!\n"); printf("Sorry! your number is smaller than my number!\n"); return 0; } 程序解析 Input your number:48 Sorry! your number is bigger than my number! Input your number:38 Ok! you are right! 多层缩进的书写格式 使程序层次分明

5 3.1.2 二分支结构和 if-else 语句 if (表达式) 语句1 if (表达式) 语句1 else 语句2 一条语句 语句1
真(非0) 假(0) 语句1 表达式 语句2 真(非0) 假(0)

6 判断数字的奇偶性 例3-2 输入1个整数,判断该数是奇数还是偶数。 读入一个整数 if (该数能被2整除) 则该数为偶数 else
该数为奇数 number % 2 == 0

7 此处条件内由于只有一条语句,故{ }可省略
源程序-判断数字的奇偶性 #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; Enter a number: 1028 Tne number is even. Enter a number: 329 Tne number is odd. 此处条件内由于只有一条语句,故{ }可省略

8 统计学生的成绩 例3-3 输入一个正整数n,再输入n个学生的成绩,计算平均分,并统计不及格成绩的个数。
for(i = 1; i <= n; i++){ 输入1个学生的成绩 grade 累加成绩 total 统计不及格成绩的个数count }

9 源程序-统计成绩 #include <stdio.h> int main(void) { int count, i, n;
double grade, total; printf("Enter n: "); scanf("%d", &n); total = 0; count = 0; for(i = 1; i <= n; i++){ printf("Enter grade #%d: ", i); scanf ("%lf", &grade); total = total + grade; if(grade < 60) count++; } printf("Grade average = %.2f\n", total/n); printf("Number of failures = %d\n", count); return 0; 源程序-统计成绩 Enter n: 4 Enter grade #1: 67 Enter grade #2: 54 Enter grade #3: 88 Enter grade #4: 73 Grade average = 70.50 Number of failures = 1 此处省略else

10 3.1.3 多分支结构和else – if 语句 else-if 语句是最常用的实现多分支 (多路选择)的方法。 一般格式为:
语句1; else if(表达式2) 语句2; …… else if(表达式n-1) 语句n-1; else 语句n;

11 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次比较

12 改写例3-1中的判断过程,用else-if实现
if(yournumber == mynumber) /* 若相等,显示猜中 */ printf("Ok! you are right!\n"); else /* 若不等,比较大小 */ if(yournumber > mynumber ) printf("Sorry! your number is bigger than my number!\n"); else printf("Sorry! your number is smaller than my number!\n"); if(yournumber == mynumber) printf("Ok! you are right!\n”); else if(yournumber > mynumber ) printf("Sorry! your number is bigger than my number!\n"); else printf("Sorry! your number is smaller than my number!\n");

13 更改例2-4中的分段计算水费的问题 例3-4 例2-4中提出的分段计算水费的问题。 居民应交水费y(元)与月用水量x(吨)的函数
关系式修正如下,并编程实现。

14 源程序-分段计算水费 Enter x: -0.5 f(-0.50) = 0.00 Enter x: ? Enter x: 9.5
# 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; Enter x: -0.5 f(-0.50) = 0.00 Enter x: ? Enter x: 9.5 f(9.50) = 12.67 Enter x: 21.3 f(21.30) = 42.75

15 3.2 四则运算 3.2.1 程序解析 3.2.2 字符类型 3.2.3 字符型数据的输入和输出 3.2.4 逻辑运算
3.2 四则运算 例3-5 求解简单的四则运算表达式。 输入一个形式如“操作数 运算符 操作数”的四 则运算表达式,输出运算结果。 程序解析 字符类型 字符型数据的输入和输出 逻辑运算

16 3.2.1 程序解析 例3-5 输入一个形式如“操作数 运算符 操作数”的四则运算表达式,输出运算结果。
程序解析 例3-5 输入一个形式如“操作数 运算符 操作数”的四则运算表达式,输出运算结果。 # include <stdio.h> int main(void) { double value1, value2; char op; printf("Type in an expression: "); scanf("%lf%c%lf", &value1, &op, &value2); if(op == '+') printf("=%.2f\n", value1 + value2); else if(op == '-') printf("=%.2f\n", value1 - value2); else if(op == '*') printf("=%.2f\n", value1 * value2); else if(op == '/') printf("=%.2f\n", value1 / value2); else printf("Unknown operator\n"); return 0; } Type in an expression: =7.90

17 3.2.2 字符类型 char op; ( op == '+' ) ( op == '-' ) 字符型数据 char为类型名称;
字符类型 char为类型名称; op为char类型变量 char op; ( op == '+' ) ( op == '-' ) 字符型数据 字符变量:op 字符常量: '+' '-' '*' '/'

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

19 字符变量 char op; 定义字符变量op,用于存放字符型数据。 op = '+'; 将字符型常量‘+’ 赋值给字符型变量op

20 3.2.3 字符型数据的输入和输出 调用scanf和printf输入输出字符 double value1, value2;
字符型数据的输入和输出 调用scanf和printf输入输出字符 double value1, value2; char operator; printf(“Type in an expression: ”); scanf(“%lf%c%lf”, &value1, &op, &value2); printf(“%.2f %c %.2f”, value1, op, value2); 操作数和运算符 之间不能出现 空格(' ') Type in an expression:

21 字符输入函数getchar() 字符输出函数putchar() char ch; ch = getchar(); putchar (ch);
输入一个字符 char ch; ch = getchar( ); 字符输出函数putchar() 输出一个字符 putchar(输出参数); a a? getchar()和putchar()只能处理单个字符的输入和输出,调用一次函数,只能输入或输出一个字符。 字符常量或字符变量

22 3.2.4 逻辑运算 -1 <= x <= 1 x >= -1 并且 x <= 1
逻辑运算 char ch; printf("Enter a character: "); ch = getchar(); if ((ch >= ’a’ && ch <= ’z’) || (ch >= ’A’ && ch <= ’Z’)) printf( “It is a letter.\n”); else printf(“It is not a letter.\n ”); -1 <= x <= 1 x x >= -1 并且 x <= 1 x >= -1 && x <= 1

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

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

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

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

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

28 例3-7 输入10个字符,统计其中英文字母、数字字符和其他字符的个数。
#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; 例3-7 输入10个字符,统计其中英文字母、数字字符和其他字符的个数。 input 10 characters: Reold 123? letter=5, digit=3, other=2

29 3.3 查询自动售货机中商品的价格 例3-8 查询自动售货机中商品的价格 3.3.1 程序解析 3.3.2 switch语句
程序解析 switch语句 多分支结构

30 程序解析 假设自动售货机出售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

31 [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");

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

33 用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

34 求解简单表达式 例3-9 输入一个形式如“操作数 运算符 操作数”的四则运算表达式,输出运算结果。 (要求用switch语句实现) 例如:
输入: 输出:7.9

35 源程序 错误 case op==‘+’: # include <stdio.h> int main(void)
{ char op; double value1, value2; printf("Type in an expression: "); scanf("%lf%c%lf", &value1, &op, &value2); switch(op){ 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; } 源程序 错误 case op==‘+’: Type in an expression: =7.9 如果除数为0?

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

37 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 的值 其他

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

39 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

40 3.3.3 多分支结构 分支结构一般分为二分支和多分支两种结构 二分支结构用基本的 if 语句实现 多分支结构用实现方法:
else – if 语句 switch语句 嵌套的 if - else语句

41 嵌套的 if 语句 if (表达式) 语句1 else 语句2 if 语句 if 语句 例3-1简单的猜数游戏。 …
if(yournumber == mynumber) printf("Ok! you are right!\n"); else if(yournumber > mynumber ) printf("Sorry! your number is bigger than my number!\n"); printf("Sorry! your number is smaller than my number!\n");

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

43 例3-11求解简单表达式。要求对除数为0的情况作特别处理。
# include <stdio.h> int main(void) { double value1, value2; char op; printf("Type in an expression: "); scanf("%lf%c%lf", &value1, &op, &value2); if(op == '+') printf("=%.2f\n", value1 + value2); else if(op == '-') printf("=%.2f\n", value1 - value2); else if(op == '*') printf("=%.2f\n", value1 * value2); else if(op == '/') if(value2 != 0) /* 嵌套的if,判断除数是否为0 */ printf("=%.2f\n", value1 / value2); else printf("Divisor can not be 0!\n"); printf("Unknown operator!\n"); return 0; } 例3-11求解简单表达式。要求对除数为0的情况作特别处理。 Type in an expression: =7.9 Type in an expression: 3.4/0 Divisor can not be 0!

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

45 改变else 和 if 的配对 例3-12 改写下列 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;

46 本章总结 分支结构: 分支结构: switch语句 数据类型:char型 运算符与表达式 分支结构程序的综合设计
正确理解if语句和switch语句 的执行机制; 掌握各类关系表达式、逻辑 表达式的运用; 能合理运用分支语句熟练编写 分支结构类的程序; 分支结构: if-else语句 else if 分支结构: switch语句 case后为常量表达式 break的使用 数据类型:char型 运算符与表达式 逻辑运算符、关系运算符 逻辑表达式 分支结构程序的综合设计


Download ppt "Chap 3 分支结构 3.1 简单的猜数游戏 3.2 四则运算 3.3 查询自动售货机中商品的价格."

Similar presentations


Ads by Google