c语言大学教程

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

第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):CalculatePriceofApplesInputquantityStartpricequantity*pricePperkgInputpricePerkgOutputpriceEndIfnecessary,use‘Divide&Conquer’strategytodecomposetheproblemintosmallerandmanageablesubproblems.2020/1/2017/61HIT-CProgrammingmain(){scanf(%d,&quantity);scanf(%d,&pricePerkg);price=quantity*pricePerkg;printf(%d,price);}CProgram:CalculatePriceofApplesInputquantityStartpricequantity*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:CalculatePriceofApplesInputquantityStartpricequantity*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:CalculatePriceofApplesInputquantityStartpricequantity*pricePperkgInputpricePerkgOutputpriceEndStartDeclaration}InputProcessOutputEnd2020/1/2020/61HIT-CProgrammingAnoutlineofaprogram,writteninaformthatcaneasilybeconvertedintorealprogrammingstatements.Itresemblestheactualprogramthatwillbeimplementedlater.However,itcannotbecompilednorexecuted.伪码(Pseudocode)1.Start2.Readquantity3.Readprice_per_kg4.pricequantity*price_per_kg5.Printprice6.EndPseudocodenormallycodesthefollowingactions:–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?minnum2minnum1StartEnd2020/1/2025/61HIT-CProgrammingscanf(%d%d,&num1,&num2);if(num1num2)min=num1;elsemin=num2;printf(%d,min);NoYesInputnum1Inputnum2Outputminnum1num2?minnum2minnum1StartEndCProgram: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

1 / 69
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功