第4章选择结构南京审计学院计算机科学与技术系孙玉星2020/1/202/61HIT-CProgramming本章学习内容算法的描述方法–常用算法(累加累乘、统计、递推迭代、穷举)选择结构及相关控制语句(第四章)循环结构及相关控制语句(第五章)结构化程序设计的基本思想Skill:–Mapproblemtosolutioninflowchartandpseudocodeforms–Beabletodevelopaprogramcontainingselectionandloopcontrolstructure2020/1/203/61HIT-CProgrammingConsiderthefollowing….Problem:烤蛋糕(BakingaCake)Howtosolve:1.Start2.将烤箱预热3.准备一个盘子4.在盘子上抹上一些黄油5.将面粉、鸡蛋、糖和香精混合在一起搅拌均匀6.将搅拌好的面粉团放在盘子上7.将盘子放到烤箱内8.End实际生活中的算法AlgorithminRealLife2020/1/204/61HIT-CProgramming‘DivideandConquer’Strategy(分治策略)inAlgorithmProblem:准备早餐(PrepareaBreakfast)1.Start2.准备早餐3.End2020/1/205/61HIT-CProgramming1.Start2.准备早餐2.1准备一个金枪鱼三明治2.2准备一些薯条2.3冲一杯咖啡3.End‘DivideandConquer’Strategy(分治策略)inAlgorithm2020/1/206/61HIT-CProgramming1.Start2.准备早餐2.1准备一个金枪鱼三明治2.1.1拿来两片面包2.1.2准备一些金枪鱼酱2.2准备一些薯片2.3冲一杯咖啡3.End‘DivideandConquer’Strategy(分治策略)inAlgorithm2020/1/207/61HIT-CProgramming1.Start2.准备早餐2.1准备一个金枪鱼三明治2.1.1拿来两片面包2.1.2准备一些金枪鱼酱2.2准备一些薯片2.2.1将土豆切成片2.2.2油炸这些土豆片2.3冲一杯咖啡3.End‘DivideandConquer’Strategy(分治策略)inAlgorithm2020/1/208/61HIT-CProgramming‘DivideandConquer’Strategy(分治策略)inAlgorithm1.Start2.准备早餐2.1准备一个金枪鱼三明治2.1.1拿来两片面包2.1.2准备一些金枪鱼酱2.2准备一些薯片2.2.1将土豆切成片2.2.2油炸这些土豆片2.3冲一杯咖啡2.3.1烧些开水放入杯中2.3.2在水杯中加入一些咖啡和糖3.End2020/1/209/61HIT-CProgrammingWhatistheconnectionbetweenthesereallifeprocessesandalgorithm?Somethingtoponder…2020/1/2010/61HIT-CProgramming算法(Algorithm)的概念数据结构+算法=程序–只对面向过程的语言(C)成立面向对象程序=对象+消息算法:–为解决一个具体问题而采取的确定的有限的操作步骤,仅指计算机能执行的算法–Aspecificandstep-by-stepsetofinstructionsforcarryingoutaprocedureorsolvingaproblem,usuallywiththerequirementthattheprocedureterminateatsomepoint2020/1/2011/61HIT-CProgramming算法的特性有穷性–在合理的时间内完成确定性,无歧义如果x≥0,则输出Yes如果x≤0,则输出No有效性–能有效执行负数开平方没有输入或有多个输入有一个或多个输出2020/1/2012/61HIT-CProgramming算法的表示方法自然语言描述传统流程图(Flowchart)–在1966年,Bohra与Jacopini提出N-S结构化流程图–1973年,美国学者I.Nassi和B.Shneiderman提出伪码(Pseudocode)表示2020/1/2013/61HIT-CProgramming流程图(Flowchart)Flowchartrepresentsalgorithmgraphically.Start/EndSymbolSemanticProcessInput/OutputTestConnectorFlowofactivities2020/1/2014/61HIT-CProgramming问题求解步骤(ProblemSolvingProcess)InputProcessOutputFirstidentifytheinputandoutputoftheproblem.2020/1/2015/61HIT-CProgrammingExample1:买苹果,计算价钱Calculateanddisplaythepriceofanumberofapplesifthequantityinkgandpriceperkgaregiven.•quantity•pricePerkgpriceprice=quantity*pricePerkgInputProcessOutput2020/1/2016/61HIT-CProgramming流程图(Flowchart):CalculatePriceofApplesInputquantityStartpricequantity*pricePperkgInputpricePerkgOutputpriceEndIfnecessary,use‘Divide&Conquer’strategytodecomposetheproblemintosmallerandmanageablesubproblems.2020/1/2017/61HIT-CProgrammingmain(){scanf(%d,&quantity);scanf(%d,&pricePerkg);price=quantity*pricePerkg;printf(%d,price);}CProgram:CalculatePriceofApplesInputquantityStartpricequantity*pricePperkgInputpricePerkgOutputpriceEndIt’snotcomplete!Declarethevariables…2020/1/2018/61HIT-CProgrammingmain(){intquantity,price_per_kg,price;scanf(%d,&quantity);scanf(%d,&pricePerkg);price=quantity*pricePerkg;printf(%d,price);}CProgram:CalculatePriceofApplesInputquantityStartpricequantity*pricePperkgInputpricePerkgOutputpriceEndWelldone!But…whatarethey?2020/1/2019/61HIT-CProgrammingmain(){intquantity,price_per_kg,price;scanf(%d,&quantity);scanf(%d,&pricePerkg);price=quantity*pricePerkg;printf(%d,price);}CProgram:CalculatePriceofApplesInputquantityStartpricequantity*pricePperkgInputpricePerkgOutputpriceEndStartDeclaration}InputProcessOutputEnd2020/1/2020/61HIT-CProgrammingAnoutlineofaprogram,writteninaformthatcaneasilybeconvertedintorealprogrammingstatements.Itresemblestheactualprogramthatwillbeimplementedlater.However,itcannotbecompilednorexecuted.伪码(Pseudocode)1.Start2.Readquantity3.Readprice_per_kg4.pricequantity*price_per_kg5.Printprice6.EndPseudocodenormallycodesthefollowingactions:–Initialisationofvariables–Assignmentofvaluestothevariables–Arithmeticoperations–Relationaloperations2020/1/2021/61HIT-CProgramming顺序(Sequence)结构的NS图给变量赋值–赋值表达式语句赋值表达式;price=quantity*pricePerkg;输入输出数据–标准库函数调用语句scanf(%d,&pricePerkg);printf(%d,price);NS图BA2020/1/2022/61HIT-CProgrammingExample2:CalculatetheMinimum计算两个数中的最小者.•num1•num2min????InputProcessOutput2020/1/2023/61HIT-CProgrammingABY条件PBNAY条件P选择结构(分支结构)(SelectionStructure)NS图传统流程图2020/1/2024/61HIT-CProgrammingNoYesFlowchart:CalculatetheMinimumInputnum1Inputnum2Outputminnum1num2?minnum2minnum1StartEnd2020/1/2025/61HIT-CProgrammingscanf(%d%d,&num1,&num2);if(num1num2)min=num1;elsemin=num2;printf(%d,min);NoYesInputnum1Inputnum2Outputminnum1num2?minnum2minnum1StartEndCProgram:CalculatetheMinimum2020/1/2026/61HIT-CProgrammingmain(){intnum1,num2,min;scanf(%d%d,&num1,&num2);if(num1num2)min=num1;elsemin=num2;printf(%d,min);}}SelectionStructureCProgram:CalculatetheMinimum2020/1/2027/61HIT-CProgrammingif-elseSingleSelectionDoubleSelectionMultipleSelectionifif–else-if选择结构(分支结构)(SelectionStructure)2020/1/2028/61HIT-CProgramming单分支选择结构(SingleSelection)StepaconditionStepmStepnStepbtruefalsestepaconditionstepmstepnstepbtruefalsePseudocodeStructurestepaifconditionistruestar