您好,欢迎访问三七文档
TheCProgrammingLanguageIntroductionCisageneral-purposeprogramminglanguageCprovidesthefundamentalcontrol-flowconstructionsCisarelatively“low-level”languageCprovidesnooperationstodealdirectlywithcompositeobjectsCoffersonlystraightforward,single-threadcontrolflowCisindependentofanyparticularmachinearchitectureSomeofCoperatorshavethewrongprecedenceSomepartsoftheCsyntaxcouldbebetterChapter1.ATutorialIntroductionLetusbeginwithaquickintroductiontoCWritingaprograminCtoprintthewords:hello,worldInC,theprogramtoprint”hello,world”is:#includestdio.hmain(){printf(“hello,world\n”);}It’seasy!includeinformationaboutstandardlibrarymainfunctionwithnoargumentscalllibraryfunctionnamedprintfstatementsofmainareenclosedinbraceTheprogramwouldbewritten:#includestdio.hmain(){printf(“hello,“);printf(“world”);printf(“\n”);}【example】Calculate1+2+3+……+100◆Howtocalculatea+b(ifa=1,b=2)main()•{inta,b,sum;variablesdeclearedbeforeused•a=1;b=2;sum=0;•sum=a+b;•printf(“%d\n”,sum);•}◆Howtocalculatea+b+c(ifa=1,b=2,c=3)main()•{inta,b,c,sum;•a=1;b=2;c=3;sum=0;•sum=a+b;•sum=sum+c;•printf(“%d\n”,sum);•}Understand?Letmesee.【example】Calculate1+2+3+……+100◆Howtocalculate1+2+3+……+100•main()•{•inti,sum;•i=1;sum=0;•while(i=100){•sum=sum+i;•i=i+1;•}•printf(“%d\n”,sum);•}It’seasytoo!Ifa,binputtedfromkeyboardwithdifferentvalues?◆Howtocalculatea+b(ifa,binputted)main(){inta,b,sum;scanf(“%d%d”,&a,&b);sum=a+b;printf(“%d\n”,sum);}•Ican!inputfromkeyboardEXAMPLE:编1-1/2+1/3-1/4+......,下面是编的程序,但是结果是0.000,可能是小数整数没有转换好,可是不知道哪里错了?????#includemath.hmain(){floatt,i,sum,n;i=1.0;sum=0.0;t=1;n=1.0;while(n=100000){t=t*(-1);i=t/n;sum=sum+i;n=n+1;};printf(%.4f\n,i);}EXAMPLE:大小字符转换:(pleaseanswer)charc;小变大:if()大变小:if()1.2VariablesandArithmeticExpressions•InC,allvariablesmustbedeclaredbeforetheyareused.Formats:typevariablesExamplesintfahr,celsius;intlower,upper,step;floattotal;•InC,anexpressioniscomposedwith:constantsvariablesfunctionsoperatorsExamples:fahr=fahr+step1.2VariablesandArithmeticExpressionsProgramExamplesFahrenheitCelsius0-1720-640460158026100371204814060160711808220093220104260126280137300148Includestdio.h/*printfFahrenheit-Celsiustableforfahr=0,20,40,…,300*/main(){intfahr,celsius;intlower,upper,step;lower=0;/*lowerlimitofTtable*/upper=300;/*upperlimit*/step=20;/*stepsize*/fahr=lower;while(fahr=upper){celsius=5*(fahr-32)/9;printf(“%d\t%d\n”,fahr,celsius);fahr=fahr+step;}}1.2VariablesandArithmeticExpressionsTheprintfusefulformat:•%dprintasdecimalinteger;•%6dprintasdecimalinteger,asatleast6characterswide;•%fprintasfloatingpoint;•%6fprintasfloatingpoint,asatleast6characterswide;•%.2fprintasfloatingpoint,2charactersafterdecimalpoint;•%6.2fprintasfloat,atleast6wideand2charactersafterdecimalpoint;•%oprintasoctal;•%xprintashexadecimal;•%cprintasacharacter;•%sprintascharacterstring;•%%print%itself1.3TheFORstatementRewritedprogramoftemperatureconverterusingFOR:#includestdio.h/*printFahrenheit-Celsiustable*/main(){intfahr;for(fahr=0;fahr=300;fahr=fahr+20)printf(“%3d%6.1f\n”,fahr,(5.0/9.0)*(fahr-32));}1.4SymbolicConstantsSymbolicConstantsisaparticularstringofcharacters.Format:#definenamereplacement-textFunction:Anyoccurrenceofnamewillbereplacedbythecorrespondingreplacement-text.Examples:#defineLOWER0/*lowerlimitoftable*/#defineUPPER300/*UPPERlimitoftable*/#defineSTEP20/*steplimitoftable*//*printFahrenheit-Celsiustable*/main(){intfahr;for(fahr=LOWER;fahr=UPPER;fahr=fahr+STEP)printf(“%3d%6.1f\n”,fahr,(5.0/9.0)*(fahr-32));}1.5CharacterInputandOutputImportantView:InC,themodelofInput&Outputaresupportedbythestandardlibrary,andNOTthestatementsofC.ThisisverydifferentofPASCAL.TowsimplestI/Ofunctions:.getchar()——readoncharacteratatimefromtextstreamc=getchar();.putchar()——writeonecharacteratatimefromtextstreamputchar(c);1.5CharacterInputandOutput•FileCopyingAlgorithmProgram:#includestdio.h/*copyinputtooutput;1stversion*/main(){intc;c=getchar();while(c!=EOF){putchar(c);c=getchar();}}readacharacterwhile(characterisnotend-of-fileindicator)outputthecharacterjustreadreadacharacter1.5CharacterInputandOutput•Filecopyingprogram(2ndversion)#includestdio.h/*copyinputtooutput;2ndversion*/main(){intc;while((c=getchar())!=EOF)putchar(c);}NOTE:c=getchar()!=EOFisequivalenttoc=(getchar()!=EOF)1.5CharacterInputandOutput•CharacterCounting•Version1Version2++meansincrementbyone.++ncnc=nc+1#includestdio.hmain(){longnc;nc=0;while(getchar()!=EOF)++nc;printf(“%ld\n”,nc);}#includestdio.hmain(){doublenc;for(nc=0;getchar()!=EOF;++nc);printf(“%.0f\n”,nc);}1.5CharacterInputandOutputLineCounting#includestdio.h/*countlinesininput*/main(){intc,nl;nl=0;while((c=getchar())!=EOF)if(c==‘\n’)++nl;printf(“%d\n”,nl);}1.5CharacterInputandOutputWordCounting:countlines,wordsandcharactersininput#includestdio.h#defineIN1/*insideaword*/#defineOUT0/*outsideaword*/main(){intc,nl,nw,nc,state;state=OUT;nl=nw=nc=0;while((c=getchar())!=EOF){++nc;if(c==‘\n’)++nl;if(c==‘’||c==‘\n’||c==‘\t’)state=OUT;elseif(state==OUT){state=IN;++nw;}}printf(“%d%d%d\n”,nl,nw,nc);}1.6ArraysArrayExample:Tocountdig
本文标题:浙江大学C语言课件
链接地址:https://www.777doc.com/doc-2231257 .html