1、第5章 选择控制结构,哈尔滨工业大学 计算机科学与技术学院,本章学习内容, 算法的描述方法 用于单分支控制的if语句 用于双分支控制的if-else语句 用于多路选择的switch语句 break语句在switch语句中的作用 关系运算符 条件运算符 逻辑运算符 程序测试,生活中的问题求解: Problem: 烤蛋糕(Baking a Cake) How to solve:Start将烤箱预热准备一个盘子在盘子上抹上一些黄油将面粉、鸡蛋、糖和香精混合在一起搅拌均匀将搅拌好的面粉团放在盘子上将盘子放到烤箱内End,5.1生活中与计算机中的问题求解 (Problem Solving Process
2、),分治策略 (“Divide and Conquer“ Strategy ),Problem: 准备早餐( Prepare a Breakfast),1. Start 2. 准备早餐 3. End,1. Start 2. 准备早餐2.1 准备一个金枪鱼三明治2.2 准备一些薯条2.3 冲一杯咖啡 3. End,分治策略 (“Divide and Conquer“ Strategy ),1. Start 2.准备早餐2.1 准备一个金枪鱼三明治2.1.1 拿来两片面包2.1.2 准备一些金枪鱼酱 2.2 准备一些薯片2.3 冲一杯咖啡 3. End,分治策略 (“Divide and Conq
3、uer“ Strategy ),1. Start 2.准备早餐2.1 准备一个金枪鱼三明治2.1.1 拿来两片面包2.1.2 准备一些金枪鱼酱 2.2 准备一些薯片2.2.1 将土豆切成片2.2.2 油炸这些土豆片2.3 冲一杯咖啡 3. End,分治策略 (“Divide and Conquer“ Strategy ),分治策略 ( “Divide and Conquer“ Strategy ),1. Start 2.准备早餐2.1 准备一个金枪鱼三明治2.1.1 拿来两片面包2.1.2 准备一些金枪鱼酱 2.2 准备一些薯片2.2.1 将土豆切成片2.2.2 油炸这些土豆片2.3 冲一杯咖
4、啡2.3.1 烧些开水放入杯中2.3.2 在水杯中加入一些咖啡和糖 3. End,5.2算法的概念及其描述方法,面向对象程序 = 对象 + 消息 面向过程的程序 = 数据结构 + 算法 计算机中的算法( Algorithm ) 为解决一个具体问题而采取的、确定的、有限的操作步骤,仅指计算机能执行的算法 A specific and step-by-step set of instructions for carrying out a procedure or solving a problem, usually with the requirement that the procedure t
5、erminate at some point,5.2算法的概念及其描述方法,算法的特性 有穷性 在合理的时间内完成 确定性,无歧义 如果x0,则输出Yes;如果x0,则输出No 有效性 能有效执行 负数开平方 没有输入或有多个输入 有一个或多个输出,5.2算法的概念及其描述方法,算法的描述方法 自然语言描述 传统流程图(Flowchart) 在1966年,Bohra 与 Jacopini 提出 N-S结构化流程图 1973年,美国学者I.Nassi 和 B.Shneiderman 提出 伪码(Pseudocode)表示,流程图(Flowchart),Flowchart represents a
6、lgorithm graphically.,计算机中的问题求解过程,Example :买苹果,计算价钱 Calculate and display the price of a number of apples if the quantity in kg and price per kg are given.,quantitypricePerkg,price,price = quantity * pricePerkg,Input,Process,Output,First identify the input and output of the problem.,顺序结构( Sequence S
7、tructure),给变量赋值 赋值表达式语句赋值表达式 ;price = quantity*pricePerkg; 输入输出数据 标准库函数调用语句scanf(“%d“, ,【例5.1】计算两整数的最大值,num1num2,max,?,Input,Process,Output,Double Selection,选择结构(分支结构) (Selection Structure),5.3关系运算符与关系表达式,5.4用于单分支控制的条件语句 (Single Selection),step a,condition,step m,step n,step b,true,false,Pseudocode
8、Structurestep a if startstep mstep n end_if step b,if Statement,The structure is similar to single selection (flowchart),Syntax:if (expression)statement; orif (expression)statement1;statement2;,if Statement,The structure is similar to single selection (flowchart),Syntax:if (expression)statement; ori
9、f (expression)statement1;statement2;,#include main() int a, b, max;printf(“Input a,b:“);scanf(“%d,%d“, ,Input a,b: _,Input a,b: 20 15 _,Input a,b: 20 15 max = 20 _,【例5.1】计算两整数的最大值,Pseudocode StructureStep a if startStep mStep n end_if else startStep xStep y end_else Step z,Step a,condition,Step m,St
10、ep n,Step z,true,false,Step x,Step y,5.5用于双分支控制的条件语句( Double Selection),if - else Statement,The structure is similar to double selection (flowchart),Syntax:if (expression)statement1;elsestatement2;,orif (expression) statement1;statement3; else statement2;statement4;,Flowchart: Calculate the Maximum,
11、Input a and b,Output max,a b?,max b,max a,Start,End,【例5.2】计算两整数的最大值,scanf(“%d,%d“, ,if (a b)max = a; elsemax = b;,Turn Flowchart to C Program,【例5.2】计算两整数的最大值,printf(“max = %dn“, max);,#include main() int a, b, max;printf(“Input a, b:“);scanf(“%d,%d“, ,if (a b)max = a;if (a = b)max = b;,【例5.2】计算两整数的最
12、大值,#include main() int a, b, max;printf(“Input a, b:“);scanf(“%d,%d“, ,max = a b ? a : b;,5.6条件运算符和条件表达式,【例5.3】,5.7用于多分支控制的条件语句 (Multiple Selection),Multi-way ifStep a if (expression1) Step m if (expression2) Step nStep z,Step a,expression1,Step m,Step n,Step z,true,false,expression2,true,false,5.7用
13、于多分支控制的条件语句 (Multiple Selection),Cascaded ifStep a if (expression1) Step m else if (expression2) Step nelse Step x Step z,Step a,expression1,Step m,Step n,Step z,true,false,expression2,true,false,Step x,5.8用于多路选择的switch语句,The structure is similar to multiple selection (flowchart),switch (expression)
14、 case value1 :statement1;break;case value2 :statement2;break; default :statementX;break; ,Important Rule !,switch (expression) case value1 :statement1;break;case value2 :statement2;break; default :statementX;break; ,5.8用于多路选择的switch语句,注意!,Example: switch (month) case 1:printf(“Januaryn“);break;case
15、2:printf(“Februaryn“);break;case 3:printf(“Marchn“);break;default:printf(“Othersn“);break;printf(“End“);,January _,January End _,5.8用于多路选择的switch语句,Example: switch (month) case 1:printf(“Januaryn“);break;case 2:printf(“Februaryn“);break;case 3:printf(“Marchn“);break;default:printf(“Othersn“);break;p
16、rintf(“End“);,March _,March End _,5.8用于多路选择的switch语句,Example: switch (month) case 1:printf(“Januaryn“);break;case 2:printf(“Februaryn“);break;case 3:printf(“Marchn“);break;default:printf(“Othersn“);break;printf(“End“);,5.8用于多路选择的switch语句,Example: switch (month) case 1:printf(“Januaryn“);break;case 2
17、:printf(“Februaryn“);case 3:printf(“Marchn“);break;default:printf(“Othersn“);break;printf(“End“);,5.8用于多路选择的switch语句,Example: switch (month) case 1:printf(“Januaryn“);break;case 2:printf(“Februaryn“);case 3:printf(“Marchn“);break;default:printf(“Othersn“);break;printf(“End“);,February _,March_,End _
18、,5.8用于多路选择的switch语句,Example: switch (month) case 1:printf(“Januaryn“);break;case 2:printf(“Februaryn“);case 3:printf(“Marchn“);break;default:printf(“Othersn“);break;printf(“End“);,And if month = 1 ?,And if month = 34 ?,5.8用于多路选择的switch语句,【例5.5】 计算器程序,编程设计一个简单的计算器程序,要求用户从键盘输入如下形式的表达式:操作数1 运算符op 操作数2然
19、后,计算并输出表达式的值指定的运算符为加(+)减(-)乘(*)除(/),main() int data1, data2; /*定义两个操作符*/char op; /*定义运算符*/printf(“Please enter the expression:“);scanf(“%d%c%d“, ,【例5.5】,思考题,语句if(0=data2)的必要性避免“除零错误“ 1998年11月,科学美国人杂志描述了美国导弹巡洋舰约克敦号上的一起事故,除零错导致军舰推进系统的关闭 为什么不用if (data2 = 0)? 如果要求输入的算术表达式中的操作数和运算符之间可以加入任意多个空格符,那么程序如何修改?
20、,main() int data1, data2; char op; printf(“Please enter the expression:“);scanf(“%d %c%d“, ,【例5.5】,思考题,如果要求对浮点数进行运算,那么程序如何修改? 修改例5.5程序,使其能进行浮点数的算术运算,同时允许使用字符*、x与X作为乘号,并且允许输入的算术表达式中的操作数和运算符之间可以加入任意多个空格符。,main() float data1, data2; char op; printf(“Please enter the expression:“);scanf(“%f %c%f“, ,【例5.
21、6】,Symbol Description& 与(AND)当且仅当两者都为真,则结果为真| 或(OR) 只要两者中有一个为真,结果就为真! 非(NOT),5.9逻辑运算符和逻辑表达式 ( Logical Operators ),! & |,高,低,ch是大写英文字母 (ch = A) & (ch = Z)判断某一年year是否是闰年的条件是满足下列二者之一 能被4整除,但不能被100整除; 能被400整除;year%4=0 & year%100!=0 | year%400=0优先级: % = (!=) & |(year%4=0) & (year%100!=0) | (year%400=0),5
22、.9逻辑运算符和逻辑表达式 ( Logical Operators ),Example:(a = 1) & (b+ = 5),( 0 = 1 ) & ( b+ = 5 ),0 & ( b+ = 5 ),0,复合表达式(Compound Expression)的值,尽量使用最少的操作数来确定表达式的值,这就意味着表达式中的某些操作数可能不会被计算,5.10本章扩充内容,测试的主要方式 给定特定的输入,运行被测软件 检查软件的输出是否与预期结果一致 测试用例的选取方法 尽量覆盖所有分支,减少重复覆盖 测试的目的 通过运行测试用例找出软件中的Bug 成功的测试在于发现迄今为止尚未发现的Bug 测试人
23、员的主要任务是站在使用者的角度,通过不断使用和攻击,尽可能多地找出Bug 测试的过程就像黑客的攻击过程,专门找软件漏洞,5.10本章扩充内容,采用测试用例,通过运行程序查找程序错误的方法 实质是一种抽样检查,彻底的测试是不可能的 彻底的测试不现实,要考虑时间、费用等限制,不允许无休止的测试 测试只能证明程序有错,不能证明程序无错E.W.Dijkstra 测试能提高软件质量,但提高软件质量不能依赖于测试,软件测试方法的分类,白盒测试(结构测试) 在完全了解程序的结构和处理过程的情况下,按照程序内部的逻辑测试程序,检验程序中的每条逻辑路径是否都能按预定要求正确工作 主要用于测试的早期 黑盒测试(功
24、能测试) 把系统看成一个黑盒子,不考虑程序内部的逻辑结构和处理过程,只根据需求规格说明书的要求,设计测试用例,检查程序的功能是否符合它的功能说明 主要用于测试的后期,#include #include main() float a, b, c;printf(“Input the three edge length:“);scanf(“%f, %f, %f“, ,错在哪里?,【例5.7】判断三角形的类型,【例5.7】判断三角形的类型,一般三角形,不是三角形,等边,等腰,直角,等腰 直角,有交叉关系的用并列的if 非此即彼的用if-else,注意!,main() float a, b, c;int flag = 1;if (a+bc ,【例5.7】,main() if (a+bc ,【例5.7】,错在哪里?,main() if (a+bc ,【例5.7】,main() if (a+bc ,等腰在先 等边在后 是否可以?,【例5.7】,Questions and answers,