高级语言程序设计 chapter9.ppt

上传人:xinsh****encai 文档编号:135756844 上传时间:2020-06-18 格式:PPT 页数:49 大小:1.38MB
返回 下载 相关 举报
高级语言程序设计 chapter9.ppt_第1页
第1页 / 共49页
高级语言程序设计 chapter9.ppt_第2页
第2页 / 共49页
高级语言程序设计 chapter9.ppt_第3页
第3页 / 共49页
高级语言程序设计 chapter9.ppt_第4页
第4页 / 共49页
高级语言程序设计 chapter9.ppt_第5页
第5页 / 共49页
点击查看更多>>
资源描述

《高级语言程序设计 chapter9.ppt》由会员分享,可在线阅读,更多相关《高级语言程序设计 chapter9.ppt(49页珍藏版)》请在金锄头文库上搜索。

1、AFirstBookofANSICFourthEdition Chapter9CharacterStrings AFirstBookofANSIC FourthEdition 2 Objectives StringFundamentalsLibraryFunctionsInputDataValidationFormattingStrings Optional CaseStudy CharacterandWordCountingCommonProgrammingandCompilerErrors AFirstBookofANSIC FourthEdition 3 StringFundamenta

2、ls Astringliteralisanysequenceofcharactersenclosedindoublequotes GoodMorning Alsocalled stringconstant stringvalue stringAstringisstoredasanarrayofcharactersterminatedbyanend of stringsymbolicconstantnamedNULL 0 AFirstBookofANSIC FourthEdition 4 StringInputandOutput gets acceptsandstoresthecharacter

3、stypedattheterminalintothecharacterarrayPressingtheEnterkeygeneratesanewlinecharacter n whichisinterpretedbygets astheend of characterentryAllthecharactersencounteredbygets exceptthenewlinecharacter arestoredinthemessagearray AFirstBookofANSIC FourthEdition 5 StringInputandOutput continued AFirstBoo

4、kofANSIC FourthEdition 6 StringInputandOutput continued Samplerun Enterastring Thisisatestinputofastringofcharacters Thestringjustenteredis Thisisatestinputofastringofcharacters AFirstBookofANSIC FourthEdition 7 StringInputandOutput continued Aprintf functioncallcanbeusedinplaceofaputs functioncallp

5、rintf s n message puts message Thiscorrespondencebetweentheoutputfunctionsisnotduplicatedbytheinputfunctionsscanf andgets scanf readsasetofcharactersuptoeitherablankspaceoranewlinecharacterscanf s message No isrequiredgets stopsacceptingcharactersonlywhenanewlineisdetected AFirstBookofANSIC FourthEd

6、ition 8 StringInputandOutput continued AFirstBookofANSIC FourthEdition 9 StringProcessing AFirstBookofANSIC FourthEdition 10 StringProcessing continued AFirstBookofANSIC FourthEdition 11 NOTE Becausetheexpressionstring2 i isonly0attheendofastringandnon 0foreveryothercharacter theexpressionwhile stri

7、ng2 i 0 canbereplacedbythesimplerexpressionwhile string2 i StringProcessing continued AFirstBookofANSIC FourthEdition 12 StringProcessing continued AFirstBookofANSIC FourthEdition 13 Becareful omittingtheparenthesescausestheentireexpressiontobeequivalenttoc getchar n StringProcessing continued AFirs

8、tBookofANSIC FourthEdition 14 StringProcessing continued AFirstBookofANSIC FourthEdition 15 LibraryFunctions Note Attemptingtocopyalargerstringintoasmallerstringcausesthecopytooverflowthedestinationarraybeginningwiththememoryareaimmediatelyfollowingthelastarrayelement AFirstBookofANSIC FourthEdition

9、 16 LibraryFunctions continued AFirstBookofANSIC FourthEdition 17 LibraryFunctions continued Whencomparingstrings theirindividualcharactersareevaluatedinpairs ifadifferenceisfound thestringwiththefirstlowercharacteristhesmallerone GoodBye islessthan Hello becausethefirst G inGoodByeislessthanthefirs

10、t H inHello Hello islessthan Hello becausethe 0 terminatingthefirststringislessthanthe inthesecondstring 123 isgreaterthan 122 because 3 in123isgreaterthan 2 in122 1237 isgreaterthan 123 because 7 in1237isgreaterthan 0 in123 AFirstBookofANSIC FourthEdition 18 LibraryFunctions continued AFirstBookofA

11、NSIC FourthEdition 19 LibraryFunctions continued AFirstBookofANSIC FourthEdition 20 LibraryFunctions continued Sampleoutput HelloislessthanHellothereThelengthofstring1is5charactersThelengthofstring2is11charactersAfterconcatenation string1containsthestringvalueHellothereWorld Thelengthofthisstringis1

12、8charactersTypeinasequenceofcharactersforstring2 It sawonderfuldayAftercopyingstring2tostring1 thestringvalueinstring1is It sawonderfuldayThelengthofthisstringis20charactersThestartingaddressofthestring1stringis 1244836 AFirstBookofANSIC FourthEdition 21 CharacterRoutines AFirstBookofANSIC FourthEdi

13、tion 22 CharacterRoutines continued AFirstBookofANSIC FourthEdition 23 ConversionRoutines AFirstBookofANSIC FourthEdition 24 ConversionRoutines continued AFirstBookofANSIC FourthEdition 25 InputDataValidation Successfulprogramsalwaystrytoanticipateinvaliddataandisolatesuchdatafrombeingacceptedandpro

14、cessedFirstvalidatethatthedataisofthecorrecttype ifnot requesttheusertore enterthedataExplainwhytheentereddatawasinvalidOneofthemostcommonmethodsofvalidatinginputdataistoacceptallnumbersasstringsEachcharactercanthenbecheckedtoensurethatitcomplieswiththedatatypebeingrequested AFirstBookofANSIC Fourth

15、Edition 26 InputDataValidation continued AFirstBookofANSIC FourthEdition 27 InputDataValidation continued AFirstBookofANSIC FourthEdition 28 InputDataValidation continued WecanuseisvalidInt inaloopthatcontinuallyrequestsanintegeruntilavalidintegervalueisenteredSetanintegervariablenamedisanIntto0doAc

16、ceptastringvalueIfthestringvaluedoesnotcorrespondtoanintegerDisplaytheerrormessage Invalidinteger Pleasere enter Sendcontrolbacktoexpressionbeingtestedbythedo whilestatementSetisanIntto1 thiscausesthelooptoterminate while isanIntis0 Returntheintegercorrespondingtotheenteredstring AFirstBookofANSIC FourthEdition 29 InputDataValidation continued AFirstBookofANSIC FourthEdition 30 CreatingaPersonalLibrary ProgrammerscreatetheirownlibrariesoffunctionsThispermitsthefunctionstobeincorporatedinanyprogr

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号