本篇文章给大家谈谈structured是什么意思?用法、例句,以及深入理解结构化编程(structured programming)思维对应的知识点,文章可能有点长,但是希望大家可以阅读完,增长自己的知识,最重要的是希望对各位有所帮助,可以解决了您的问题,不要忘了收藏本站喔。
万事万物都不是凭空产生的,都有一个从无到有的过程,包括结构化编程思想及其应用。
我们知道,计算机诞生后,最初的编程是“硬”编程,通过插拔连线的硬件连接方式来实现不同硬件模块的组合来完成不同的任务。随后,冯诺依曼提出“存储程序控制”概念,硬件方面,由内存和控制器实现这一概念,程序和数据加载到内存,由控制器读取程序,解释程序,控制其它部件协同工作。最初的“软”编程是用机器语言(指令集和程序都有二进制序列表示),然后是符号化的汇编语言,然后是英文词汇化、命名化的高级语言编程。
我们知道,汇编语言可以通过不同的跳转指令来实现选择和重复操作,到了高级语言,可以通过关系、逻辑表达式和goto语句来实现选择和重复操作。
各种跳转让程序的流程控制变得杂乱无序,随后,有人提出并证明了可用只有单入口和单出口的顺序、选择、循环(也称为重复或迭代)三种控制结构也可以同等实现程序的相同功能。
三种控制只涉及到由几个关键字(if、else、switch、while、for等)描述的简单的几种选择结构和循环结构。
使用三种控制结构来构建代码块、函数,由一组相关函数来完成一个较大的功能,这组相关函数放到一个文件中构成一个编译单元,一个或多个文件通过编译、链接可以构建一个应用程序,这便是基本的结构化编程思维。
(codeblockandstructuredprogramingthinking)
Some(mostlyancient)programminglanguagesletyoujumpdirectlyintothemiddleofablockofcodeusingthenotoriousGOTOstatement.Thisisparticularlydeadlyandstronglydiscouragedinmodernprogramming.Inassemblylanguageitiseasilypossibletosendcontroldirectlytonearlyanypointinaprogram.Writingassemblyprogramsthatworktakescarefuldiscipline.
一些(大部分是古老的)编程语言允许您使用臭名昭著的GOTO语句直接跳转到代码块的中间。这在现代编程中尤其致命,并且受到强烈的反对。在汇编语言中,很容易将控制直接发送到程序中的几乎任何一点。编写工作正常的汇编程序需要严格的纪律。
Bugshappenwhencontroljumpsoutof(orinto)ablockofcode.Theblockcannolongerberegardedasamodulethatperformsaparticularfunction,butmustbeexaminedstatementbystatement.
当控制跳出(或跳入)代码块时,就会发生错误。块不再被视为执行特定功能的模块,而必须逐个语句进行检查。
Youwouldliketothinkofacodeblockasa"blackbox"thatperformssomeparticularoperation.Justbeforecontrolenterstheblockeverythingisreadyfortheoperation,andwhencontrolleavestheblocktheoperationhasbeendonecorrectly.
您可以将代码块视为执行某些特定操作的“黑盒”。就在控制进入块之前,所有操作都已准备就绪,当控制离开块时,操作已正确完成。
Thisisaprimeideaofstructuredprogramming.Ablockofcodeisamodulethathasoneentrypoint(whereexecutionenterstheblock)andoneexitpoint(whereexecutionleavestheblock).Theentrypointhaswelldefinedentryconditions.Theexitpointhaswelldefinedexitconditions.
这是结构化编程的基本思想。代码块是具有一个入口点(执行进入块)和一个出口点(执行离开块)的模块。入口点具有定义良好的入口条件。退出点具有定义良好的退出条件。
Fortheblocktoexecutecorrectly,executionmuststartattheentrypoint,andtheentryconditionsmustbemet.Whenexecutionleavestheblock,theexitconditionsaretrue(iftheblockitselfisbugfree).
为了正确执行块,必须从入口点开始执行,并且必须满足入口条件。当执行离开块时,退出条件为true(如果块本身没有bug)。
Inflowchartingterms,aboxwithasingleentrypointandsingleexitpointisstructured.
在流程图术语中,构建了一个具有单个入口点和单个出口点的框。
Ablockshouldhaveonesetofentryconditionsandonesetofexitconditions.Theblockshouldbeamodulethatcanbeusedforbuildinglargerstructures.InC,blocksaremadebyputtingbracesaroundagroupofstatements.Thecodeinsideablockoftenhasitsowninternallogic,butoncetheblockiswrittenanddebuggeditshouldbeablackbox.
一个代码块应该有一组进入条件和一组退出条件。该块应该是可用于建造更大结构的模块。在C语言中,块是通过将一组语句放入大括号来构成的。块中的代码通常有自己的内部逻辑,但一旦编写和调试了块,它就应该是一个黑盒子。
Eachblockcanbethoughtofasamodulethatispartofthesolutiontoaproblem.
每个模块都可以看作是问题解决方案的一部分。
Withtheabovetwoblocks,theexitconditionsofthefirstblockmatchtheentryconditionstothesecondblock.
对于上述两个区块,第一个区块的出口条件与第二个区块的进入条件相匹配。
IftheexitconditionsofblockAarethecorrectentryconditionsforblockB,thenthetwoblockscanfollowinsequence.
如果A代码块的退出条件是B代码块的正确进入条件,则两个代码块可以依次进行。
Thesetoftwoblockscanberegardedasonebigblock.IftheentryconditionsforblockAarecorrect,thentheexitconditionsforblockAarecorrect,thentheentryconditionsforblockBarecorrect,then(finally)theexitconditionsforblockBarecorrect.
两个块的集合可视为一个大块。如果A代码块的进入条件正确,则A代码块的退出条件正确,代码块区的进入条件正确,然后(最后)B代码块的退出条件正确。
Intermsofthebigblock,iftheentryconditionsforthebigblockarecorrect,thentheexitconditionsforthebigblockarealsocorrect.
就大代码块而言,如果大代码块的进入条件正确,那么大区块的退出条件也正确。
Theblocksshouldworklikejigsawpuzzlepieceswheretheshapeofonepuzzleexactlymatchestheshapeofthefollowingpiece.Oncethetwopiecesarefittogethertheycanberegardedasabiggerpiecethatfitssomewhereinthepuzzle.
这些代码块应该像拼图一样工作,其中一个拼图的形状与下一个拼图的形状完全匹配。一旦这两块拼合在一起,它们就可以被视为一块更大的拼图,适合拼图中的某个地方。
Asequenceofblocksiscorrectiftheexitconditionsofeachblockmatchestheentryconditionsofthefollowingblock.Controlenterseachblockattheblock'sentrypoint,andleavesthroughtheblock'sexitpoint.Thewholesequencecanberegardedasasingleblock,withanentrypointandanexitpoint.
如果每个代码块的退出条件与下一个代码块的进入条件相匹配,则代码块序列是正确的。控制语句在块的入口点进入每个块,然后通过块的出口点离开。整个序列可以看作是一个单独的区块,有一个入口点和一个出口点。
(Twoormorecodeblocksinsequencearestructured)
Ultimately,iftheprogramisasequenceofsmallblocksthatfittogetherproperly,theentireprogramcouldbethoughtofasonebigblock.Whentheprogram'sentryconditionsaremet,thentheprogram'sexitconditionswillbemet.
归根结底,如果程序是一系列适当组合在一起的小程序块,那么整个程序可以被视为一个大程序块。当程序的进入条件得到满足时,程序的退出条件也将得到满足。
Withaprogram,theentryconditionsconsistoftheproblemtheprogramwaswrittentosolve,andtheexitconditionsarethesolutiontotheproblem.Ifeachoftheblocksoftheprogramarecorrect,thentheentireprogramiscorrect.
对于一个程序,进入条件包括编写程序要解决的问题,而退出条件是问题的解决方案。如果程序的每个块都正确,则整个程序都正确。
(Thealternationoftwocodeblocksisstructured)
If-then-elseissometimescalledalternation(becausetherearealternativechoices).Instructuredprogramming,eachchoiceisacodeblock.Ifalternationisarrangedasintheflowchartbelow,thenthereisoneentrypoint(atthetop)andoneexitpoint(atthebottom).Thestructureshouldbecodedsothatiftheentryconditionsaresatisfied,thentheexitconditionsarefulfilled(justlikeacodeblock).
If-then-else有时被称为选择(因为有其他选择)。在结构化编程中,每个选项都是一个代码块。如果按照下面流程图安排交替,则有一个入口点(顶部)和一个出口点(底部)。应对结构进行编码,以便在满足进入条件的情况下,满足退出条件(就像代码块一样)。
Anexampleofanentryconditionforanalternationstructureis:thediscriminantcontainsavaluethatiszeroorpositive.Theexitconditionmightbe:thecorrectnumberofrootshavebeencomputed..Thebranchingstructureisusedtofulfiltheexitcondition.
选择结构的输入条件的一个示例是:判别式包含零或正的值。退出条件可能是:已计算正确的根……分支结构用于满足退出条件。
InCandinmostlanguages,alternationisimplementedwithanif-statement:
在C语言和大多数语言中,选择使用if语句实现:
if(condition)\ntruebranch\nelse\nfalsebranch
WithC,theconditionevaluatesto0ortonon-zero.Zeroisregardedasfalseandanythingnon-zeroisregardedastrue.Often,thebranchesarecodeblocks:
对于C,条件的计算结果为0或非零。零被视为假,任何非零的东西都被视为真。分支通常是代码块:
if(condition)\n{\ntrueblock\n}\nelse\n{\nfalseblock\n}\n
Hereisasmallexample:
/*disccontainsthediscriminantofaquadraticequation*/\nif(disc>=0.0)\n{\ncomputerealroots\n}\nelse\n{\ncomputeimaginaryroots\n}4代码块的迭代是结构化的
(Theiterationofacodeblockisstructured)
Iteration(while-loop)alsohasoneentrypointandoneexitpoint.Theentrypointhasconditionsthatmustbesatisfiedandtheexitpointhasconditionsthatwillbefulfilled.Therearenojumpsintooroutofthestructure.
迭代(while循环)也有一个入口点和一个出口点。入口点具有必须满足的条件,出口点具有将要满足的条件。没有跳进或跳出结构。
InC,iterationmaybeimplementedwithawhilestatement.
在C中,迭代可以用while语句实现。
while(condition)\nstatement
conditionisanexpressionthatevaluatestozero(regardedasfalse)ornon-zero(regardedastrue).statementisexecutedwhenconditionistrue,thencontrolreturnstothewhile.Almostalways,thestatementisacodeblock:
条件是计算结果为零(视为false)或非零(视为true)的表达式。当条件为true时执行语句,然后控制返回到while。语句几乎总是是一个代码块:
while(condition)\n{\nblock\n}
Thestatementorblockthatfollowsthewhileshouldensurethatconditionwilleventuallybecomezero(false)andendtheloop.
while后面的语句或块应确保条件最终变为零(false)并结束循环。
Hereisthesyntaxoftheforstatement:
for(initialize;condition;bottom)\nbody
Almostalwaysthebodyisacodeblock:
for(initialize;condition;bottom)\n{\nbody\n}5具有单个入口点和单个出口点的结构(任何大小)相当于一个代码块
Inallofthestructuressofartherehasbeenanentrypointatthetopandanexitpointatthebottom.Therearenojumpsintooroutofthestructure.Theentrypointhasentryconditionsthatareexpectedtobesatisfied,andexitconditionsthatarefulfillediftheyare.Eachofthestructurescanbeconsideredtobeacodeblock.
到目前为止,在所有结构中,顶部有一个入口点,底部有一个出口点。没有跳进或跳出结构。入口点具有预期要满足的进入条件,如果满足,则具有要满足的退出条件。每个结构都可以被视为一个代码块。
ItisOKthatthereareseveralpathswithinastructure,butfromtheoutside,eachstructurelookslikeacodeblockwithoneentryandoneexit.
一个结构中有多条路径是可以的,但从外部看,每个结构就像一个代码块,有一个入口和一个出口。
Inflowchartingterms,anycodeblockcanbereplacedbyanyofthethreestructuressequence,alternation,oriteration.Or,goingtheotherdirection,ifthereisaportionoftheflowchartthathasasingleentrypointandasingleexitpoint,itcanbesummarizedasasinglecodeblock.
在流程图术语中,任何代码块都可以被序列、选择或迭代这三种结构中的任何一种替换。或者,如果流程图的一部分有一个入口点和一个出口点,则可以将其概括为一个代码块。
(Astructure(ofanysize)thathasasingleentrypointandasingleexitpointisequivalenttoacodeblock)
Forexample,saythatyouaredesigningaprogramtogothroughalistofsignedintegerscalculatingtheabsolutevalueofeachone.Youmight(1)firstregardtheprogramasoneblock,then(2)sketchintheiterationrequired,andfinally(3)putinthedetailsoftheloopbody.
例如,假设您正在设计一个程序来遍历一个有符号整数列表,计算每个整数的绝对值。您可以(1)首先将程序视为一个块,然后(2)在所需的迭代中绘制草图,最后(3)输入循环体的细节。
Or,youmightgotheotherway.Oncetheabsolutevaluecodeisworking,youcanregarditasasinglecodeblocktobeusedasacomponentofalargerprogram.
或者,你可以走另一条路。一旦绝对值代码正常工作,就可以将其视为单个代码块,用作更大程序的组件。
Aflowchartcanbecomearbitrarilycomplexandstillbestructured.Butiftherearemanyboxes,itishardtovisualizewhatisgoingon.Someoftheboxesshouldbegroupedintoafunctionthatisseparatelyflowcharted.
流程图可以变得任意复杂,并且仍然是结构化的。但如果有很多盒子,很难想象发生了什么。一些盒子应分组为单独绘制流程图的函数。
Inflowcharts,afunctioncallisshownasaboxwithdoublelinesonitssides.Thepictureshowsanif-then-elsestructurethatchoosesbetweentwofunctions.
在流程图中,函数调用显示为一个框,框的两侧有双线。图中显示了在两个函数之间进行选择的if-then-else结构。
Functionscanbecomplex.Ifthelogicofafunctionbecomestoocomplex,someofthatlogiccanbemadeintootherfunctions.Functionscancallfunctions,ofcourse.Ideally,thelogicofeachfunctioncorrespondstoaneasilyunderstoodstructuredflowchart.
功能可能很复杂。如果一个函数的逻辑变得过于复杂,则可以将其中一些逻辑转换为其他函数。当然,函数可以调用函数。理想情况下,每个功能的逻辑对应于易于理解的结构化流程图。
Aprogrammingteamconsistsofseveralindividualsallworkingonaproject.Ifthedesignofaprojectiswell-structured,theflowchartscorrespondtoprogramswhichcanbeassignedtoindividualprogrammers.Usuallytheprogramsconsistofindividualsourcefileswhichwillbeseparatelycompiledandthemlinkedtogethertoformanexecutable.
编程团队由几个个人组成,他们都在一个项目上工作。如果项目的设计结构良好,流程图对应的程序可以分配给各个程序员。通常,程序由单独的多个源文件组成,这些源文件将被单独编译,并将它们链接在一起以形成一个可执行文件。
TheabsolutevalueproblemisanexampleofTop-downstructureddesign.Whenusingthisdesignmethodyoustartwithjustoneprogramblock,andthenreplacethatblockwithoneofthestructuressequence,alternation,oriteration.Youthenrefinethenewflowchartbyreplacingoneofitsblockswithoneofthesamestructures.Continuereplacingblockswithstructuresuntiltheflowcharthasenoughdetailtoworkasadesignforaprogram.
绝对值问题是自顶向下结构化设计的一个例子。当使用这种设计方法时,您只需从一个程序块开始,然后将该块替换为序列、选择或迭代结构之一。然后,通过将新流程图的一个块替换为相同的结构之一来优化新流程图。继续用结构替换块,直到流程图有足够的细节作为程序的设计。
Forexample,saythatyouwanttodesignaprogramthataddsupintegersenteredbytheuser.Theinitialdesignmightlooklikethepictureasbelow.
例如,假设您想设计一个程序,将用户输入的整数相加。最初的设计可能如下图所示。
(Actually,youwouldprobablydothisstepinyourmindandnotactuallydrawtheflowchart.)
(实际上,您可能会在脑海中完成这一步,而不是实际绘制流程图。)
Usuallyprogramsneedaninitializationstep,amiddlestepthatdoesmostofthework,andafinalsteptofinishthework.
通常,程序需要一个初始化步骤、一个完成大部分工作的中间步骤和一个完成工作的最后步骤。
Theinitializationstepforourprobleminitializesthesumtozero.
问题的初始化步骤将总和初始化为零。
Thechartisstillnotdetailedenoughforprogramming.Itneedssomedetailabouthowallthoseintegersaretobereadin.Aloopisneeded.
该图表对于编程来说仍然不够详细。它需要一些关于如何读入所有这些整数的细节。需要一个循环。
Afterinitialization,theprogramloopsaslongasthereisdata.Whenitreachestheendofthedata,itwritesouttheanswerandexits.
初始化后,只要有数据,程序就会循环。当到达数据末尾时,它写出答案并退出。
Therearestillsomedetailslefttospecify,suchashowtheendofdataisdetermined.Thismightbedonewithaspecialsentinelvalue,orbypromptingtheuserfora"yes"or"no"response.Buttheover-alldesignfortheprogramwillbethesame.
还有一些细节需要指定,例如如何确定数据的结尾。这可以通过一个特殊的sentinel值来完成,或者通过提示用户“是”或“否”响应来完成。但该计划的总体设计将是相同的。
Inalmostallproblems(inlifeorinprogramming)ithelpstohaveahigh-levelplanofwhatyouaretryingtoachieve.Ahighlevelplankeepsyoufocusedonthetaskanddirectsyourefforts.Ifyoudon'thaveahigh-levelplan,youmaythinkonlyintermsofwhattodonextfrommomenttomoment,andyoumayneverreachyourgoal.Youmightflounderforever,makingsmallmovesthatdon'tadduptoprogress.Thisislikewanderingaimlesslythroughamurkywoodswithoutamap.Orliketryingtowriteanovelbypickingonewordatatime.Notlikelytosucceed.
在几乎所有的问题(生活中或编程中)中,有一个高层次的计划来帮助你实现目标。一个高水平的计划可以让你专注于任务并指导你的努力。如果你没有一个高水平的计划,你可能只考虑下一步要做什么,而你可能永远也达不到你的目标。你可能永远都在挣扎,做出一些小动作,但这些小动作加起来并不能带来进步。这就像在没有地图的阴暗森林中漫无目的地徘徊。或者喜欢一次只选一个词来写小说。不太可能成功。
Inprogramming,ahigh-leveldesignisgivenbyatop-levelstructuredflowchart(veryoftentheUniversalFlowChart,modifiedtofityourtask).Structuringforcesthecharttobeagenuinedesignandnotmerelyapoorly-conceivedcollectionofboxesandlines.Anyprogramcanbedescribedbyastructuredflowchart.Ifyoudesignyourprograminachartusingasmallnumberofboxes,itwillbeatop-leveldesign.
在编程中,高层设计由顶层结构化流程图(通常是通用流程图,经过修改以适合您的任务)给出。结构化迫使图表成为真正的设计,而不仅仅是构思拙劣的方框和线条集合。任何程序都可以用结构化流程图来描述。如果使用少量方框在图表中设计程序,则它将是顶级设计。
Nowyoucanmoresensiblyfocusonsmallertasks,andthenyetsmallertasks,usingthesamemethod.Thismethodofdivideandconquerisaverypowerfultechnique.
现在,使用相同的方法,您可以更明智地专注于较小的任务,然后再专注于更小的任务。这种分而治之的方法是一种非常强大的技术。
(Theideasofstructuredprogrammingworkforanyprogramminglanguage)
Structuringprinciplescanbeusedwithanyprogramminglanguage,evenwithunstructuredprogramminglanguageslikeassemblylanguageortheearlyversionsofBASICandFORTRAN.Withunstructuredlanguagesitisuptoyou(theprogrammer)toimplementstructureswithoutthesupportofthesyntaxofthelanguage.
结构化原则可以用于任何编程语言,即使是汇编语言或BASIC和FORTRAN的早期版本等非结构化编程语言。对于非结构化语言,您(程序员)可以在不支持该语言语法的情况下实现结构。
http://www.programmedlessons.org/StructuredC/Chap02/struct02_1.html
structured是什么意思?用法、例句和深入理解结构化编程(structured programming)思维的问题分享结束啦,以上的文章解决了您的问题吗?欢迎您下次再来哦!