版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
AFirstBookofANSIC
FourthEditionChapter3ProcessingandInteractiveInput1AFirstBookofANSIC,FourthEditionObjectivesAssignmentMathematicalLibraryFunctionsInteractiveInputFormattedOutput2AFirstBookofANSIC,FourthEditionObjectives(continued)SymbolicConstantsCaseStudy:InteractiveInputCommonProgrammingandCompilerErrors3AFirstBookofANSIC,FourthEditionAssignmentThegeneralsyntaxforanassignmentstatementis variable=operand;Theoperandtotherightoftheassignmentoperator(=)canbeaconstant,avariable,oranexpressionTheequalsigninCdoesnothavethesamemeaningasanequalsigninalgebralength=25;isread“lengthisassignedthevalue25”Subsequentassignmentstatementscanbeusedtochangethevalueassignedtoavariablelength=3.7;length=6.28;4AFirstBookofANSIC,FourthEditionAssignment(continued)TheoperandtotherightoftheequalsigninanassignmentstatementcanbeavariableoranyvalidCexpression sum=3+7; product=.05*14.6;Thevalueoftheexpressiontotherightof=iscomputedfirstandthenthecalculatedvalueisstoredinthevariabletotheleftof=Variablesusedintheexpressiontotherightofthe=mustbeinitializediftheresultistomakesenseamount+1892=1000+10*5isinvalid!5AFirstBookofANSIC,FourthEditionAssignment(continued)Ifwidthwasnotinitialized,thecomputerusesthevaluethathappenstooccupythatmemoryspacepreviously(compilerwouldprobablyissueawarning)6AFirstBookofANSIC,FourthEditionAssignment(continued)=hasthelowestprecedenceofallthebinaryandunaryarithmeticoperatorsintroducedinSection2.4Multipleassignmentsarepossibleinthesamestatement a=b=c=25;All=operatorshavethesameprecedenceOperatorhasright-to-leftassociativityc=25;b=c;a=b;7AFirstBookofANSIC,FourthEditionImplicitTypeConversionsDatatypeconversionstakeplaceacrossassignmentoperatorsdoubleresult;result=4;//integer4isconvertedto4.0Theautomaticconversionacrossanassignmentoperatoriscalledanimplicittypeconversionintanswer;answer=2.764;//2.764isconvertedto2Heretheimplicitconversionisfromahigherprecisiontoalowerprecisiondatatype;thecompilerwillissueawarning8AFirstBookofANSIC,FourthEditionExplicitTypeConversions(Casts)Theoperatorusedtoforcetheconversionofavaluetoanothertypeisthecastoperator (dataType)expressionwheredataType
isthedesireddatatypeoftheexpressionfollowingthecastExample:Ifsumisdeclaredasdoublesum;,(int)sumistheintegervaluedeterminedbytruncatingsum’sfractionalpart9AFirstBookofANSIC,FourthEditionAssignmentVariationssum=sum+10isnotanequation—itisanexpressionthatisevaluatedintwomajorsteps10AFirstBookofANSIC,FourthEditionAssignmentVariations(continued)11AFirstBookofANSIC,FourthEditionAssignmentVariations(continued)12AFirstBookofANSIC,FourthEditionAssignmentVariations(continued)Assignmentexpressionslikesum=sum+25canbewrittenusingthefollowingoperators:+=-=*=/=%=sum=sum+10canbewrittenassum+=10price*=rateisequivalenttoprice=price*rateprice*=rate+1isequivalenttoprice=price*(rate+1)13AFirstBookofANSIC,FourthEditionAccumulatingThefirststatementinitializessumto0ThisremovesanypreviouslystoredvalueinsumthatwouldinvalidatethefinaltotalApreviouslystorednumber,ifithasnotbeeninitializedtoaspecificandknownvalue,isfrequentlycalledagarbagevalue14AFirstBookofANSIC,FourthEditionAccumulating(continued)15AFirstBookofANSIC,FourthEditionAccumulating(continued)16AFirstBookofANSIC,FourthEditionCountingAcountingstatementisverysimilartotheaccumulatingstatement variable=variable+fixedNumber;Examples:i=i+1;andm=m+2;Incrementoperator(++):variable=variable+1canbereplacedbyvariable++or++variable17AFirstBookofANSIC,FourthEditionCounting(continued)18AFirstBookofANSIC,FourthEditionCounting(continued)19AFirstBookofANSIC,FourthEditionCounting(continued)Whenthe++operatorappearsbeforeavariable,itiscalledaprefixincrementoperator;whenitappearsafteravariable,itiscalledpostfixincrementoperatork=++n;isequivalentton=n+1;//incrementnfirstk=n;//assignn'svaluetokk=n++;isequivalenttok=n;//assignn'svaluetokn=n+1;//andthenincrementn20AFirstBookofANSIC,FourthEditionCounting(continued)Prefixdecrementoperator:theexpressionk=--nfirstdecrementsthevalueofnby1beforeassigningthevalueofntokPostfixdecrementoperator:theexpressionk=n--firstassignsthecurrentvalueofntonandthenreducesthevalueofnby121AFirstBookofANSIC,FourthEditionCounting(continued)22AFirstBookofANSIC,FourthEditionMathematicalLibraryFunctions23AFirstBookofANSIC,FourthEditionMathematicalLibraryFunctions(continued)Theargumenttosqrtmustbefloating-pointvalue;passinganintegervalueresultsinacompilererrorReturnvalueisdouble-precisionMustinclude#include<math.h>24AFirstBookofANSIC,FourthEditionMathematicalLibraryFunctions(continued)25AFirstBookofANSIC,FourthEditionMathematicalLibraryFunctions(continued)26AFirstBookofANSIC,FourthEditionMathematicalLibraryFunctions(continued)Argumentneednotbeasingleconstant27AFirstBookofANSIC,FourthEditionMathematicalLibraryFunctions(continued)Thestep-by-stepevaluationoftheexpression 3.0*sqrt(5*33-13.91)/5 is(seenextslide)28AFirstBookofANSIC,FourthEditionMathematicalLibraryFunctions(continued)29AFirstBookofANSIC,FourthEditionMathematicalLibraryFunctions(continued)Determinethetimeittakesaballtohitthegroundafterithasbeendroppedfroman800-foottowertime=sqrt(2*distance/g),whereg=32.2ft/sec230AFirstBookofANSIC,FourthEditionMathematicalLibraryFunctions(continued)31AFirstBookofANSIC,FourthEditionInteractiveInput32AFirstBookofANSIC,FourthEditionInteractiveInput(continued)Thisprogrammustberewrittentomultiplydifferentnumbersscanf()isusedtoenterdataintoaprogramwhileitisexecuting;thevalueisstoredinavariableItrequiresacontrolstringasthefirstargumentinsidethefunctionnameparentheses33AFirstBookofANSIC,FourthEditionInteractiveInput(continued)Thecontrolstringpassedtoscanf()typicallyconsistsofconversioncontrolsequencesonlyscanf()requiresthatalistofvariableaddressesfollowthecontrolstringscanf("%d",&num1);34AFirstBookofANSIC,FourthEditionInteractiveInput(continued)35AFirstBookofANSIC,FourthEditionInteractiveInput(continued)ThisstatementproducesapromptAddressoperator(&)36AFirstBookofANSIC,FourthEditionInteractiveInput(continued)scanf()canbeusedtoentermanyvaluesscanf("%f%f",&num1,&num2);//"%f%f"isthesameAspacecanaffectwhatthevaluebeingenterediswhenscanf()isexpectingacharacterdatatypescanf("%c%c%c",&ch1,&ch2,&ch3);storesthenextthreecharacterstypedinthevariablesch1,ch2,andch3;ifyoutypexyz,thenxisstoredinch1,ablankisstoredinch2,andyisstoredinch3scanf("%c%c%c",&ch1,&ch2,&ch3);causesscanf()tolookforthreecharacters,eachcharacterseparatedbyexactlyonespace37AFirstBookofANSIC,FourthEditionInteractiveInput(continued)Inprintingadouble-precisionnumberusingprintf(),theconversioncontrolsequenceforasingle-precisionvariable,%f,canbeusedWhenusingscanf(),ifadouble-precisionnumberistobeentered,youmustusethe%lfconversioncontrolsequencescanf()doesnottestthedatatypeofthevaluesbeingenteredInscanf("%d%f",&num1,&num2),ifuserenters22.87,22isstoredinnum1and.87innum238AFirstBookofANSIC,FourthEditionCaution:ThePhantomNewlineCharacter39AFirstBookofANSIC,FourthEditionCaution:ThePhantomNewlineCharacter(continued)ThefollowingisasamplerunforProgram3.10:Typeinacharacter:mThekeystrokejustacceptedis109Typeinanothercharacter:Thekeystrokejustacceptedis1040AFirstBookofANSIC,FourthEditionCaution:ThePhantomNewlineCharacter(continued)41AFirstBookofANSIC,FourthEditionCaution:ThePhantomNewlineCharacter(continued)42AFirstBookofANSIC,FourthEditionAFirstLookatUser-InputValidation43AFirstBookofANSIC,FourthEditionAFirstLookatUser-InputValidation(continued)Aswritten,Program3.12isnotrobustTheproblembecomesevidentwhenauserentersanon-integervalueEnterthreeintegernumbers:1020.6820Theaverageof10,20,and-858993460is-286331143.333333Handlinginvaliddatainputiscalleduser-inputvalidationValidatingtheentereddataeitherduringorimmediatelyafterthedatahavebeenenteredProvidingtheuserwithawayofreenteringanyinvaliddata44AFirstBookofANSIC,FourthEditionFormattedOutput618124---148Outputisnotaligned45AFirstBookofANSIC,FourthEditionFormattedOutput(continued)618124---148Fieldwidthspecifier46AFirstBookofANSIC,FourthEditionFormattedOutput(continued)47AFirstBookofANSIC,FourthEditionFormatModifiersLeftjustification:printf("%-10d",59);producesthedisplay59٨٨٨٨٨٨٨٨Explicitsigndisplay:printf("%+10d",59);producesthedisplay٨٨٨٨٨٨٨+59Formatmodifiersmaybecombined%-+10dwouldcauseanintegernumbertobothdisplayitssignandbeleft-justifiedinafieldwidthof10spacesTheorderoftheformatmodifiersisnotcritical%+-10disthesame48AFirstBookofANSIC,FourthEditionOtherNumberBases[Optional]Thedecimal(base10)valueof15is15.Theoctal(base8)valueof15is17.Thehexadecimal(base16)valueof15isf.49AFirstBookofANSIC,FourthEditionOtherNumberBases(continued)50AFirstBookofANSIC,FourthEditionOtherNumberBases(continued)Thedecimalvalueoftheletterais97.Theoctalvalueoftheletterais141.Thehexvalueoftheletterais61.51AFirstBookofANSIC,FourthEditionOtherNumberBases(continued)52AFirstBookofANSIC,FourthEditionSymbolicConstantsLiteraldatareferstoanydatawithinaprogramthatexplicitlyidentifiesitselfLiteralvaluesthatappearmanytimesinthesameprogramarecalledmagicnumbersCallowsyoutodefinethevalueoncebyequatingthenumbertoasymbolicname#defineSALESTAX0.05#definePI3.1416Alsocalledsymbolicconstantsandnamedconstants53AFirstBookofANSIC,FourthEditionSymbolicConstants(continued)#signisasignaltoaCpreprocessor54AFirstBookofANSIC,FourthEditionCaseStudy:InteractiveInput55AFirstBookofANSIC,FourthEditionCaseStudy:InteractiveInput(continued)56AFirstBookofANSIC,FourthEditionCommonProgrammingErrorsForgettingtoassigninitialvaluestoallvariablesbeforethevariablesareusedinanexpressionCallingsqrt()withanintegerargumentForgettingtousetheaddressoperator,&,infrontofvariablenamesinascanf()functioncallNotincludingthecorrectcontrolsequencesinscanf()functioncallsforthedatavaluesthatmustbeenteredIncludingamessagewithinthecontrolstringpassedtoscanf()57AFirstBookofANSIC,FourthEditionCommonProgrammingErrors(continued)Terminatinga#definecommandtothepreprocessorwithasemicolonPlacinganequalsignina#definecommandwhenequatingasymbolicconstanttoavalueUsingtheincrementanddecrementoperatorswithvariablesthatappearmorethanonceinthesameexpressionBeingunwillingtotestaprogramindepth58AFirstBookofANSIC,FourthEditionCommonCompilerErrors59AFirstBookofANSIC,FourthEditionCommonCompilerErrors(continued)60AFirstBookofANSIC,FourthEditionSummary
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年更新版消防工程劳务派遣协议版B版
- 2024年新版股东权益合同范本版B版
- 2024年写字楼食堂餐饮承包服务协议3篇
- 2024奶粉行业婴幼儿奶粉品牌分销与销售合作合同3篇
- 2024年度旅游公司租车合同(含导游、翻译、住宿、餐饮)3篇
- 2024年智能手表采购与分销协议2篇
- 2024年度志愿服务资料保密协议3篇
- 2024年度新能源开发服务合同18篇
- 2024年房产交易协议模板指南版B版
- 2024年城市供排水合同5篇
- 宣布处分决定讲话
- 新时代大学生劳动教育智慧树知到答案章节测试2023年
- 影视摄制经费预算总表
- 考研英语二核心词汇整理
- 文物保护学概论课件ppt 第一章 文物与文物学
- JJF 1918-2021电容式加速度传感器校准规范
- 研究方向证明
- GB/T 10067.1-2019电热和电磁处理装置基本技术条件第1部分:通用部分
- 2021年新高考1卷英语试题讲评【知识建构+拓展延伸】课件
- 动脉血标本的采集(课件)
- 金沙遗址博物馆课件
评论
0/150
提交评论