




版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
AFirstBookofANSIC
FourthEditionChapter3ProcessingandInteractiveInputAFirstBookofANSIC,FourthEdition2ObjectivesAssignmentMathematicalLibraryFunctionsInteractiveInputFormattedOutputAFirstBookofANSIC,FourthEdition3Objectives(continued)SymbolicConstantsCaseStudy:InteractiveInputCommonProgrammingandCompilerErrorsAFirstBookofANSIC,FourthEdition6Assignment(continued)Ifwidthwasnotinitialized,thecomputerusesthevaluethathappenstooccupythatmemoryspacepreviously(compilerwouldprobablyissueawarning)AFirstBookofANSIC,FourthEdition7Assignment(continued)=hasthelowestprecedenceofallthebinaryandunaryarithmeticoperatorsintroducedinSection2.4Multipleassignmentsarepossibleinthesamestatement a=b=c=25;All=operatorshavethesameprecedenceOperatorhasright-to-leftassociativityc=25;b=c;a=b;AFirstBookofANSIC,FourthEdition8ImplicitTypeConversionsDatatypeconversionstakeplaceacrossassignmentoperatorsdoubleresult;result=4;//integer4isconvertedto4.0Theautomaticconversionacrossanassignmentoperatoriscalledanimplicittypeconversionintanswer;answer=2.764;//2.764isconvertedto2Heretheimplicitconversionisfromahigherprecisiontoalowerprecisiondatatype;thecompilerwillissueawarningAFirstBookofANSIC,FourthEdition9ExplicitTypeConversions(Casts)Theoperatorusedtoforcetheconversionofavaluetoanothertypeisthecastoperator (dataType)expressionwheredataType
isthedesireddatatypeoftheexpressionfollowingthecastExample:Ifsumisdeclaredasdoublesum;,(int)sumistheintegervaluedeterminedbytruncatingsum’sfractionalpartAFirstBookofANSIC,FourthEdition10AssignmentVariationssum=sum+10isnotanequation—itisanexpressionthatisevaluatedintwomajorstepsAFirstBookofANSIC,FourthEdition11AssignmentVariations(continued)AFirstBookofANSIC,FourthEdition12AssignmentVariations(continued)AFirstBookofANSIC,FourthEdition13AssignmentVariations(continued)Assignmentexpressionslikesum=sum+25canbewrittenusingthefollowingoperators:+=-=*=/=%=sum=sum+10canbewrittenassum+=10price*=rateisequivalenttoprice=price*rateprice*=rate+1isequivalenttoprice=price*(rate+1)AFirstBookofANSIC,FourthEdition14AccumulatingThefirststatementinitializessumto0ThisremovesanypreviouslystoredvalueinsumthatwouldinvalidatethefinaltotalApreviouslystorednumber,ifithasnotbeeninitializedtoaspecificandknownvalue,isfrequentlycalledagarbagevalueAFirstBookofANSIC,FourthEdition15Accumulating(continued)AFirstBookofANSIC,FourthEdition16Accumulating(continued)AFirstBookofANSIC,FourthEdition17CountingAcountingstatementisverysimilartotheaccumulatingstatement variable=variable+fixedNumber;Examples:i=i+1;andm=m+2;Incrementoperator(++):variable=variable+1canbereplacedbyvariable++or++variableAFirstBookofANSIC,FourthEdition18Counting(continued)AFirstBookofANSIC,FourthEdition19Counting(continued)AFirstBookofANSIC,FourthEdition20Counting(continued)Whenthe++operatorappearsbeforeavariable,itiscalledaprefixincrementoperator;whenitappearsafteravariable,itiscalledpostfixincrementoperatork=++n;isequivalentton=n+1;//incrementnfirstk=n;//assignn'svaluetokk=n++;isequivalenttok=n;//assignn'svaluetokn=n+1;//andthenincrementnAFirstBookofANSIC,FourthEdition21Counting(continued)Prefixdecrementoperator:theexpressionk=--nfirstdecrementsthevalueofnby1beforeassigningthevalueofntokPostfixdecrementoperator:theexpressionk=n--firstassignsthecurrentvalueofntonandthenreducesthevalueofnby1AFirstBookofANSIC,FourthEdition22Counting(continued)AFirstBookofANSIC,FourthEdition23MathematicalLibraryFunctionsAFirstBookofANSIC,FourthEdition24MathematicalLibraryFunctions(continued)Theargumenttosqrtmustbefloating-pointvalue;passinganintegervalueresultsinacompilererrorReturnvalueisdouble-precisionMustinclude#include<math.h>AFirstBookofANSIC,FourthEdition25MathematicalLibraryFunctions(continued)AFirstBookofANSIC,FourthEdition26MathematicalLibraryFunctions(continued)AFirstBookofANSIC,FourthEdition27MathematicalLibraryFunctions(continued)ArgumentneednotbeasingleconstantAFirstBookofANSIC,FourthEdition28MathematicalLibraryFunctions(continued)Thestep-by-stepevaluationoftheexpression 3.0*sqrt(5*33-13.91)/5 is(seenextslide)AFirstBookofANSIC,FourthEdition29MathematicalLibraryFunctions(continued)AFirstBookofANSIC,FourthEdition30MathematicalLibraryFunctions(continued)Determinethetimeittakesaballtohitthegroundafterithasbeendroppedfroman800-foottowertime=sqrt(2*distance/g),whereg=32.2ft/sec2AFirstBookofANSIC,FourthEdition31MathematicalLibraryFunctions(continued)AFirstBookofANSIC,FourthEdition32InteractiveInputAFirstBookofANSIC,FourthEdition33InteractiveInput(continued)Thisprogrammustberewrittentomultiplydifferentnumbersscanf()isusedtoenterdataintoaprogramwhileitisexecuting;thevalueisstoredinavariableItrequiresacontrolstringasthefirstargumentinsidethefunctionnameparenthesesAFirstBookofANSIC,FourthEdition34InteractiveInput(continued)Thecontrolstringpassedtoscanf()typicallyconsistsofconversioncontrolsequencesonlyscanf()requiresthatalistofvariableaddressesfollowthecontrolstringscanf("%d",&num1);AFirstBookofANSIC,FourthEdition35InteractiveInput(continued)AFirstBookofANSIC,FourthEdition36InteractiveInput(continued)ThisstatementproducesapromptAddressoperator(&)AFirstBookofANSIC,FourthEdition37InteractiveInput(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,eachcharacterseparatedbyexactlyonespaceAFirstBookofANSIC,FourthEdition38InteractiveInput(continued)Inprintingadouble-precisionnumberusingprintf(),theconversioncontrolsequenceforasingle-precisionvariable,%f,canbeusedWhenusingscanf(),ifadouble-precisionnumberistobeentered,youmustusethe%lfconversioncontrolsequencescanf()doesnottestthedatatypeofthevaluesbeingenteredInscanf("%d%f",&num1,&num2),ifuserenters22.87,22isstoredinnum1and.87innum2AFirstBookofANSIC,FourthEdition39Caution:ThePhantomNewlineCharacterAFirstBookofANSIC,FourthEdition40Caution:ThePhantomNewlineCharacter(continued)ThefollowingisasamplerunforProgram3.10:Typeinacharacter:mThekeystrokejustacceptedis109Typeinanothercharacter:Thekeystrokejustacceptedis10AFirstBookofANSIC,FourthEdition41Caution:ThePhantomNewlineCharacter(continued)AFirstBookofANSIC,FourthEdition42Caution:ThePhantomNewlineCharacter(continued)AFirstBookofANSIC,FourthEdition43AFirstLookatUser-InputValidationAFirstBookofANSIC,FourthEdition44AFirstLookatUser-InputValidation(continued)Aswritten,Program3.12isnotrobustTheproblembecomesevidentwhenauserentersanon-integervalueEnterthreeintegernumbers:1020.6820Theaverageof10,20,and-858993460is-286331143.333333Handlinginvaliddatainputiscalleduser-inputvalidationValidatingtheentereddataeitherduringorimmediatelyafterthedatahavebeenenteredProvidingtheuserwithawayofreenteringanyinvaliddataAFirstBookofANSIC,FourthEdition45FormattedOutput618124---148OutputisnotalignedAFirstBookofANSIC,FourthEdition46FormattedOutput(continued)618124---148FieldwidthspecifierAFirstBookofANSIC,FourthEdition47FormattedOutput(continued)AFirstBookofANSIC,FourthEdition48FormatModifiersLeftjustification:printf("%-10d",59);producesthedisplay59????????Explicitsigndisplay:printf("%+10d",59);producesthedisplay???????+59Formatmodifiersmaybecombined%-+10dwouldcauseanintegernumbertobothdisplayitssignandbeleft-justifiedinafieldwidthof10spacesTheorderoftheformatmodifiersisnotcritical%+-10disthesameAFirstBookofANSIC,FourthEdition49OtherNumberBases[Optional]Thedecimal(base10)valueof15is15.Theoctal(base8)valueof15is17.Thehexadecimal(base16)valueof15isf.AFirstBookofANSIC,FourthEdition50OtherNumberBases(continued)AFirstBookofANSIC,FourthEdition51OtherNumberBases(continued)Thedecimalvalueoftheletterais97.Theoctalvalueoftheletterais141.Thehexvalueoftheletterais61.AFirstBookofANSIC,FourthEdition52OtherNumberBases(continued)AFirstBookofANSIC,FourthEdition53SymbolicConstantsLiteraldatareferstoanydatawithinaprogramthatexplicitlyidentifiesitselfLiteralvaluesthatappearmanytimesinthesameprogramarecalledmagicnumbersCallowsyoutodefinethevalueoncebyequatingthenumbertoasymbolicname#defineSALESTAX0.05#definePI3.1416AlsocalledsymbolicconstantsandnamedconstantsAFirstBookofANSIC,FourthEdition54SymbolicConstants(continued)#signisasignaltoaCpreprocessorAFirstBookofANSIC,FourthEdition55CaseStudy:InteractiveInputAFirstBookofANSIC,FourthEdition56CaseStudy:InteractiveInput(continued)AFirstBookofANSIC,FourthEdition57CommonProgrammingErrorsForgettingtoassigninitialvaluestoallvariablesbeforethevariablesareusedinanexpressionCallingsqrt()withanintegerargumentForgettingtousetheaddressoperator,&,infrontofvariablenamesinascanf()functioncallNotincludingthecorrectcontrolsequencesinscanf()functioncallsforthedatavaluesthatmustbeenteredIncludingamessagewithinthecontrolstringpassedtoscanf()AFirstBookofANSIC,FourthEdition58CommonProgrammingErrors(continued)Terminatinga#definecommandtothepreprocessorwithasemicolonPlacinganequalsignina#definecommandwhenequatingasymbolicconstanttoavalueUsingtheincrementanddecrementoperatorswithvariablesthatappearmorethanonceinthesameexpressionBeingunwillingtotestaprogramindepthAFirstBookofANSIC,FourthEdition59CommonCompilerErrorsAFirstBookofANSIC,FourthEdition60CommonCompilerErrors(continued)AFirstBookofANSIC,FourthEdition61SummaryArithmeticcalculationscanbeperformedusingassignmentstatementsormathematicalfunctionsTh
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- T/CNFAGS 13-2024液體無(wú)水氨質(zhì)量分級(jí)及運(yùn)輸要求
- T/CHES 119-2023洪水演進(jìn)水動(dòng)力實(shí)時(shí)模擬技術(shù)規(guī)程
- T/CAEPI 72-2023袋式除塵用折式濾筒技術(shù)要求
- 專(zhuān)業(yè)消殺試題及答案
- 上海安全員c證考試題庫(kù)及答案
- 家具專(zhuān)賣(mài)店加盟合同協(xié)議書(shū)4篇
- 圖書(shū)管理系統(tǒng)詳細(xì)設(shè)計(jì)
- 鳥(niǎo)的雙重呼吸
- 工控裝備:溫度控制調(diào)節(jié)器項(xiàng)目績(jī)效評(píng)估報(bào)告
- Γ-FE2O3項(xiàng)目績(jī)效評(píng)估報(bào)告
- 第二次全國(guó)地名普查類(lèi)別
- 體育與健康(中職)教學(xué)參考
- 2024年四川省南充市中考英語(yǔ)試卷真題(含官方答案及解析)
- 2024全國(guó)能源行業(yè)火力發(fā)電集控值班員理論知識(shí)技能競(jìng)賽題庫(kù)(多選題)
- 衛(wèi)生室醫(yī)療質(zhì)量與安全管理領(lǐng)導(dǎo)小組、工作計(jì)劃、管理制度
- DL∕ T 1123-2009 火力發(fā)電企業(yè)生產(chǎn)安全設(shè)施配置
- JGT 352-2017 現(xiàn)澆混凝土空心結(jié)構(gòu)成孔芯模
- 2023年廣東肇慶醫(yī)學(xué)院教師招聘及其他工作人員考試真題
- 摩根大通的監(jiān)管合規(guī)應(yīng)對(duì)措施
- 2024全新股份合作協(xié)議樣板下載
- 二手房交易授權(quán)委托書(shū)樣式
評(píng)論
0/150
提交評(píng)論