版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
c語言上課筆記2(Cclassnotes2)Constantdefinition:#defineconstantnameconstantvalueNotedefinesconstantsyntaxwithoutsemicolons(()),usuallydefiningconstantsontheheaderofthesource(togetherwith#include)Cases:#definePI3.14Printf(N=%d,M=%f,N,M);//printf("N=%d,M=%f,3,3.14);//(mainlycompletedareplacementfunction)Variabledefinition:Grammar:datatypevariablename[=value]Intnumber;//defineavariablenumberIntnumber2-20//defineavariablenumber2andassignittotheinitialvalueis20Number=10;//the10assignmentofthevariablenumberThesecondmethoddefines:int,Number1,number2=20,number3=10;Printf(/,number2=%dnumber3=%d\n,number2",number3");%dstandsforastationsymbol,whichisonlyanintegerdataThefirst%dcorrespondstothenumber2Thesecond%dcorrespondstothenumber3Namingrulesforvariables(identifiers):Thevariablenamecancontainletters,numbersand_(underlined)assemblyThevariablenamecannotcontainanyspecialcharacters,suchas:outsideexcept#,comma,space,etc.Thevariablenamemustbealphabeticor_(underlined)atthebeginningVariablenamescannotcontainwhitespacecharacters(newlinecharacters,spaces,andtabs)calledwhitespacecharactersCertainwordsintheClanguage(suchasdoubleandfloat)arecalledreservedwords,whichhavespecialmeaningsandcannotbeusedasvariablenamesTheClanguageiscasesensitive,sothevariablepriceandthevariablePRICEaretwodifferentvariablesNote:1.variablename,knowitsname,knowitsmeaning2.$canbeusedShort16bitOnethousandtwohundredandthirty-fourtrillionandfivehundredandsixty-sevenbillioneighthundredandninetymilliononehundredandtwenty-threethousandfourhundredandfifty-sixOnethousandonehundredandonetrillionandtenbilliononehundredmilliononehundredandonethousandandtenBinarydecimalsystem000000000000000000000000000000001100000000000000102OnethousandonehundredandeleventrillionandonehundredandelevenbilliononehundredandelevenmilliononehundredandeleventhousandonehundredandelevenFloatfl=3.1345f;//32Doublef2=3333.4;//64Integerdata:Short2bytesInt4bytesFloating-pointdata:Float4bytesDouble8bytesCharacterdata:Char1bytesTheunitofcomputerrepresentationis1024ascarry(usually1000)1bytes(byte),8bits,00000000111111111KB=1O24bytes1MB=1024KB1GB=1024MB1TB=1024GBCharactertype:charCharacterliteral:‘0','A'B',Han'(wrong.AsinChinese,incomputerisexpressedin2bytes)a97'A'65Arithmeticoperation:?一?Twooperandisanintegerdivisor,note:integerdivisoranddividendsymbolsthesameresultispositive,negativereturnThe%twooperandcanonlybeanintegerdivisor.TheremaindernoteistheresultofthesignedsymbolSincetheincrease++1cases:n=20;n++;〃n++andn=n+Iequivalent-minus1Note:(n+30)++;errorOnlyreferstothevariable-++(++-preandpost)Sum=n++;//postaddsum=n;n=n+l;Sum=++n;//prefixincrementn=n+l;sum=n;Cases:Intx=2;Floaty-5;Floats;S=y/x;〃y=y+x;Printf s);Assignmentoperation:=assigntheresultoftheexpressionontherighttotheleftvariable//n=n+40=n+=40;*-AverageRelationaloperator:(theresultofthecalculationisalogicalvalue,trueorfalse)<二==notequalto==equalNote:intheClanguage,A’〇"meansAfalse","non0"meanstrueLogicaloperator:Andwhenthetwologicandshortcircuitconditionsistrue,theresultsaretruemultiplicationprocessingshortcircuitlogicoraslongasaconditionistrue,theresultisreallyadditiveItistruethatthelogicisnottakenandthereverseisfalse,andthefalsenegationistrue011000110010andtheequivalentofmultiplicationTheequivalentof0121||additionSum=(5II(>2)(n++)>0);and//1|saidshortcircuitoroperationWhatisashortcircuitand/or:whentheexpressionontheleftiscalculatedandtheresultoftheentireexpressioncanbedetermined,theexpressionontherightisnolongercomputedExample:dataisreceivedfromthekeyboardttinclude<stdio.h>ttinclude<stdlib.h>Void,main(){Intn;Floatm;Intx;Printf(enterthevalueofN:\n);Scanf(%d,&n);//receiveinputfromthekeyboardvaluePrintf(enterthevalueofM:\n);Scanf("%f",&m);Amemorymap:728placeThreethousandfivehundredandseventy-threeThreethousandfivehundredandseventy-fourThreethousandfivehundredandseventy-fiveThreethousandfivehundredandseventy-six357703578035790inti;358030i=30;00009&i00010000110001300014000150001600017000180001900020000210002200023000240002500026Address4GAssignment:
discountthebook,sellathalfprice,printtheoriginalpriceofthebook,andthenprintthebookpriceishalfpricefromthekeyboardinputa5bitinteger,printoutputbits,ten,100,thousand,10000,suchas:54352bitsfor2,tenfor500,3for1000,5for4entertheradiusofacirclefromthekeyboard,calculatetheareaofthecircleandoutputtheresultentertheradiusandheightofacylinderfromthekeyboard,calculatethevolumeofthecylinder,andoutputtheresultSizeofoperator:Syntax:sizeof(datatype)sizeof(variable)Sizeof(float)sizeof(x)例.intx=20.shorts(m=20.floatf;doubled;charc;printf('%d”,sizeof((c)).printf和scanf是格式輸出輸入語句printf(格式字符串,參數(shù).參數(shù)n).scanf(格式字符串,地址L地址n);and->取變量a的內(nèi)存地址printf("xxxxx");轉(zhuǎn)義字符\n\tthe\\(2)例:printf("thecheckingforexistence:d:\\testl.exe\").從鍵盤上接上數(shù)據(jù).注:1.scanf語句中的格式字符中盡量不要除%d%f%Id%If這些占位符之外的其它字符特別小心不要加上、n.接收數(shù)據(jù)是存放在ー個(gè)內(nèi)地址里面變量名必須加上&.接收數(shù)據(jù)以空格或換行等分隔(%c他可以接收空格和換行)scanf(%d%d\n\n”,x,m);getchar從鍵盤上讀取ー個(gè)字符可以接收鍵盤上任意字符()例:cahrx=getchar().fflush(stdin);/Z清空輸入緩沖區(qū)putchar把ー個(gè)字符型變量輸出到屏幕上去(變量名)例:putchar(x).注getcharputcahr只對(duì)針對(duì)字符數(shù)據(jù)處理.'\n''an'\032"a'""二級(jí)考試都是表示是ー個(gè)字符.選擇語句.單分支當(dāng)條件表達(dá)式為真時(shí)執(zhí)行語句!,反之不執(zhí)行語法:if(條件表達(dá)式)語句!.例.intx=20,30.if(x)(y){printf(by\n");)特殊用法.if(0)(printf("xxxxx");)雙分支:當(dāng)條件表達(dá)式為真是執(zhí)行語句1,反之執(zhí)行語句2.if(條件表達(dá)式)語句!.else(語句2.)注:1.if語句中{}{}里面可以有零條或多條語句表示代表塊.2.當(dāng){}{}可以省略掉只有一個(gè)條語句時(shí).例.intx=70,y=30;if(x)(y)!printf(x>y").printf(x>y").printf(x>y").elseprintf(">").)多分支ー種使用if語嵌套2種switch語句.if(條件表達(dá)式DIif(條件表達(dá)式2){語句!.)else語句2.elseStatement2;Thenestingofaifstatementistheexecutionofaifstatement.ThestatementofthecodeblockisnotaifstatementCases:ttinclude<stdio.h>Void,main()//betweenstudentachievementstatistics90-100-89-80excellent70good 79in60--69latticeisnotverycellInts;Printf〈pleaseenterastudent,sscore:,z);Scanf("/d",&s);//79If(s>=0ands<=100)If(s=90)Printf(“90to100betweengood");Else//0—89If(s>=80)Printf(80-89good);Else//0—79If(s>=70)Printf("70一一79");Else//0-69If(s>=60)Printf(pass);Else//059Printf("fail");ElsePrintf("thestudentsyouenteredincorrectly."!!");6000tax:(60003500-socialsecurity)*10%105switchStatementsThecomputingformula,whentheexpressionresultswithconstantequaltoexecutethestatementfollowingthebreakstatement,untilImet;orholdaswitchstatementbeforefinallyoutoftheentireswitchstatement;ifatallcasestatementscannotfindthesameconstant,thenthelinefollowingthedefalultstatementSwitch(expression)Caseconstant1:Sentence;Break;Caseconstant2:Sentence;Break;Default:Sentence;Break;)break;jumpoutofcurrentswitchexpressionscanonlybestoredinmemory,whichareintegers(char,bool,int),andfloatingpointtypescannotbeusedCases:ttinclude<stdio.h>ttinclude<stdbool.h>//addthecorrespondingheaderVoid,main()Charx;Switch(x){Default:Printf("nocorrespondingvalue");Break;Case'a':Printf("youentered1");//break;Case'b';Printf("youentered2");//break;Case99:Printf("youentered3");//break;Case4:Printf("youentered4");//break;Multiplechoicestatements:ifstatementsnested,switchstatementstwotocompleteCompare:Typically,switchstatementsareusedwhenhandlingafixedvalueTheifstatementisusuallyusedwhendealingwiththevalueofanintervalsegmentLoopstatement:whiledo...WhileforSyntax:For(initialvalue);Condition;steplength)Loopbodystatement;)Initialvalue:initializationasaloopCondition:theloopbodyexecuteswhentheconditionistrueStepsize:addseveralcountsatatimeNote:theinitialvalueandsteplengthcanbeomitted,buttheloopconditionmustbewrittenoutWhentheloopconditionisalwaystrue,itisdead,andinrealprocessdevelopment,itsometimesusesadeadloopFor((1;)i)Cases:Void,main()Inti;//asacounterFor(i=l;iく=360;i++)(Printf(%d,\n,I);))Dowhilesyntax:Do(Loopbody;}while(condition);Whentheconditionistrue,theloopbodyisexecuted,regardlessofthecondition,andtheloopbodyisexecutedVoid,main()Intsum=O;//statisticsyoupaymemymoneyDoPrintf(hitsyoua\n);Sum=sum+100;//moneyWhile(sum<1000);Whilesyntax:Whentheconditionistrue,theloopbodyisexecuted,theconditionisfirstjudged,andthentheloopbodyisdeterminedWhile(condition)Loopbody:Cases:Void,main()Intsum=0;//statisticsyoupaymemymoneyWhile(sum<1000)(Printf("sendsomeone,paythemoney,orkillyou,\n!");Sum=sum+100;))Homework:choosedocomprehensiveproblemsoffreshman:inputfromthekeyboardbycycle(numberunknown)Cprogramdesignthiscoursescore,totalscoreandscorestatistics,whentheinputisnegative,thestopinputstatistics,finally,printoutthetotalscore,thetotalnumberofstatistics,andtheaveragescore#include<stdio.h>(main)Thestatisticalresultsof/Floatsum=0;//totalscorestatisticsIntcount=l;//thenumberofStatisticsFloatave=0;//averageFloatm;Printf("pleaseenter%dstudent'sscore:",count);Scanf(%f,&m);//enteraresultWhile(m>=0){//statisticsSum=sum+m;Count++;Printf("pleaseenter%dstudent'sscore:",count);Scanf(%f,&m);//enteraresultCount—;//theactualnumberofstudentsAve=sum/count;Printf(thetotalscoreis%f,thenumberofstudentsis:%d,theaveragescoreis:%f,sum,count,ave);Continue;//whenperformingcontinuesaidtheendofthecycle,continuetoimplementthenextcycleWhen/afterthecontinue,thecontinueloopstatementisnotexecuted(thiscycle)Note:continuecanonlybeusedinloopstatementsBreak;Whenthebreakstatementisexecuted,itmeansexitingtheentireloop,Note:usedinswitchstatementsandloopstatementsSummary:comparisonofthethreemajorcyclesThewhilefirstdeterminesthattheexecutionisusuallyusedinthecaseofanindefinitenumberofloops,andthatitisimpossibletoexecutetheloopatonetimeThedowhileexecutesfirstandthenjudges,usuallyusingtheloopbodyatleastonceintheuncertainnumberofcyclesForfirstjudgeandthenexecute,usuallyinthecaseofdeterminingthenumberofcycles,andmaynotexecutetheloopatonetimeLoopnesting:NestedanotherloopwithintheloopbodyCases:ttinclude<stdio.h>(main){*AtAtAtResult*/Intj;For(i=l;iく=4;i++)For(i=l;iく=4;i++)For(j=l;jく=9;j++)Printf(*);Printf("\n");//printeachline,line^include<stdio.h>(main)Printf(*);Printf("\n");//printeachline,line^include<stdio.h>(main)AtAtAtResult*/Inti;Intj;1=1;While(i<=4)//cycle{J=l;While(j<=9)Printf(*);J++;***Array:usedtostoremorethanoneelementIntSI,S2,S3//storageofthreegradestudentsIntx[100];//defineanarraytostoreonehundredstudentachievementDefininganarray:AnarrayisacollectionofthesamedatatypeSyntaxformat:datatype,arrayname[numberofelements]Thedatatypeofthearrayname[]={value1,avalueof2.Thevalueofn}//...AndgivehimthedefinitionofanarrayinitializationTodefineanarray,intdefaultsto0,thefloatingpointtypeis0.OF,andchardefaultsto<、〇'Cases:Floats[5];//arraydefinitionfiveelementsizeoffloat,whichcanonlybeinstalledfloattypevalueisequivalenttothedefinitionofthe5variablesoftypefloatCharch[20];Atthesametimetheinitialarray//definearraysInta[10]={l,2,3,4,5,6,7,8,9,0};Ints[]={l,2,3,4,5,6,7,8,9,0};Ints2[20]={};Arrayaccess:Thearrayname[subscriptN]'meanstheelementaccessingthesubscriptNAccessthefirstelementarrayname[0]Accessthesecondelementarrayname[1]Note:thesubscriptofanarraybeginsat0andcannotbecrossedwhenaccessing//arrayttinclude<stdio.h>Void,main()Ints[5];10//SdefinesanarrayofstudentachievementIntsum=0;For(i=0;iく=4;i++)!Printf〈enterthevalueofthefirst%delement”:i+1");Scanf("%d",&s[i]);)For(i=0;iく=4;i++)Sum=sum+s[i];Printf("%d",sum);)Two-dimensionalarray:Syntax:Thedatatypeofthearrayname[ロ]thenumberofrowsofcolumns;Thedatatypeofthearrayname[linenumberN][columnnumberM]=turningvalue1,2..ValueM},...{}}..;Note:whenatwo-dimensionalarrayisdefined,therowindexcanbeomittedandcolumnindexshouldnotbeomittedForexample:ints[3][3];//defineatwo-dimensionalarrayofthreerowsandthreecolumnscolumnLine000102101112202122Visit:row,columnarrayname[].S[0][0]accesseselementsfromrowstocolumnsS[2][2]accessesthreerowsofthreecolumnelementsExample:s[〇][0]=20;S[0][l]=30;Intarray[3][4];//defineanarray//initializeArray[0][0]=lll;//thefirstlineinitializationArray[0][1]=111;Array[〇][2]=111;Array[0][3]=111;Array[1][0]=lll;//initializesecondArray[1][1]=111;Array[1][2]=111;Array[1][3]=111;Array[2][0]=lll;//initializethirdArray[2][1]=111;Array[2][2]=111;Array[2][3]=111;Secondmethodsforaccessingtwo-dimensionalarrays:Intarray[3][4];//defineanarray/arraynamerowcolumnindex[]ロInt,I,j;For(i=0;i<3;i++)(For(j=0;j<4;j++)Array[i][j]=20;Printf("\n");)Stringsaredouble-citedExample:〃ABCD","LDG"Note:tobeseparatedfromcharacterconstants,(a'\n'Cdoesnothavestringdatatypes,butusesthechararraytosto
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 《眾人行管理咨詢網(wǎng)》課件
- 運(yùn)動(dòng)器材銷售工作總結(jié)
- 2013年高考語文試卷(湖北)(空白卷)
- 租車服務(wù)員工作總結(jié)
- 2006年江西高考語文真題及答案
- 驅(qū)動(dòng)未來新型汽車
- 2023年-2024年項(xiàng)目管理人員安全培訓(xùn)考試題附解析答案可打印
- 2023年-2024年項(xiàng)目部管理人員安全教育培訓(xùn)試題及參考答案【A卷】
- 2023-2024安全培訓(xùn)考試題及答案【名校卷】
- 2023年-2024年項(xiàng)目部安全培訓(xùn)考試題答案完美
- 學(xué)術(shù)不端行為治理研究
- 企業(yè)文化、戰(zhàn)略與電力能源知識(shí)參考題庫練習(xí)卷含答案(一)
- 福建南平武夷高新技術(shù)產(chǎn)業(yè)控股集團(tuán)有限公司招聘筆試沖刺題2024
- 2024年設(shè)備維修部管理制度(6篇)
- GB/T 45083-2024再生資源分揀中心建設(shè)和管理規(guī)范
- 精神科護(hù)理工作計(jì)劃例文
- 2024山地買賣合同模板
- 河北省承德市2023-2024學(xué)年高一上學(xué)期期末物理試卷(含答案)
- 【初中化學(xué)】二氧化碳的實(shí)驗(yàn)室制取教學(xué)課件-2024-2025學(xué)年九年級(jí)化學(xué)人教版上冊(cè)
- 出租車行業(yè)服務(wù)質(zhì)量提升方案
- 景區(qū)安全管理教育培訓(xùn)
評(píng)論
0/150
提交評(píng)論