您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 能源与动力工程 > 辽宁石油化工大学《C语言程序设计》第二章 程序流程控制
2TheprocedureoftheprocedureiscontrolledSummaryofthechapter2C1996BohmJacopini2.1/Orderstructureandinput/outputmethod2.1.1COrderstructureofofClanguage1.1s1s2abab1.1·38·2Cgetchar()putchar()scanf()printf()gets()puts()scanf()printf()#includestdio.h#includestdio.h2.1.2Singlecharacterintput/output1Singlecharacteroutputputchar(c)c2.1A#includestdio.hmain(){charch;ch='A';putchar(ch);}A2Singlecharacterintputgetchar()2.2#includestdio.hmain(){charc;c=getchar();/**/putchar(c);/**/}a’a’a’a’2·39·Notegetchar()putchr(c)stdio.hC#includestdio.hC2.1.3Formatintput/output1Formatoutputprintf(,)%1Sign2Wideofthewordandprecision6.3630mmm.nmn602.3ASIICmain(){chara,b,c;a='B';b='a;c='6';printf(%c%c%c\n,a,b,c);printf(%d%d%d\n,a,b,c);}·40·2Ba66697543type2.12.1printf()%d%o%x%X%u%c%s%f%e%E%g%G%%%ld%lo%lx%lu00x0X65%f%e%2.4main(){floata,b,c;a=3.456;b=1.321;c=a+b;printf(%f+%f=%f\n,a,b,c);}3.456000+1.321000=4.7770002.5main(){inta=15,b=172;printf(%ditsoctalsystemsis%o\n,a,a);printf(%ditssexadecimalsystemsis%x\n,b,b);Explanation%d%D2·41·}15itsoctalsystemsis17172itssexadecimalsystemsisac2.6main(){inta=3,b=40,c=500;floatx=3.14159,y=789.7478658;doublez=123456789.1234567;printf(a=%db=%dc=%d\n,a,b,c);printf(a=%3d\tb=%3d\tc=%3d\n,a,b,c);printf(a=%-3d\tb=%-3d\tc=%-3d\n,a,b,c);printf(a*b*c=%d\n,a*b*c);printf(a*b*c=%ld\n,a*b*(long)c);printf(x=%f\ty=%f\n,x,y);printf(x=%5.2f\ty=%6.3f\n,x,y);printf(x=%e\ty=%E\n,x,y);printf(z=%g\n,z);}a=3b=40c=500a=3b=40c=500a=3b=40c=500a*b*c=-5536a*b*c=60000x=3.141590y=789.747866x=3.14y=789.748x=3.14159e+00y=7.89748E+02z=1.23457e+082Formatinputscanf(,)scanf()2.12.1scanf()·42·2%d%o%x%X%c%s%f%e%ld%lo%lx%hd%ho%hxm*%d%f%sNote%d%f2.7main(){inta,b,c,d;scanf(%d%d,&a,&b);c=a+b,d=a*b;printf(%d+%d=%d\n,a,b,c);printf(%d*%d=%d\n,a,b,d);}585+8=135*8=402.8main(){inta,b;scanf(%3d%3d,&a,&b);printf(a=%3d,b=%3d\n,a,b);scanf(%d,%d,&a,&b);printf(a=%d,b=%d\n,a,b);scanf(%d:%d,&a,&b);printf(a=%d,b=%d\n,a,b);}2·43·123456a=123,b=4563,4a=3,b=412:23a=12,b=232.9main(){chara,b;scanf(%c%c\n,&a,&b);printf(%c%c\n,a,b);scanf(%c%c,&a,&b);printf(%c%c\n,a,b);}mywewewemywe2.1032main(){chara,b;scanf(%c%*c%c,&a,&b);printf(%c%c\n,a,b);}dosds2.1.4/Characterbunchintput/output1Characterbunchoutputputs(s);sputs(s)s2.11#includestdio.hmain(){char*s=Iamastudent.;puts(s);·44·2}Iamastudent.1Characterbunchinputgets(s)sgets(s)s’/0’2.12#includestdio.hmain(){chars[20];gets(s);puts(s);}Iamastudent.Iamastudent.Notesputs(s)EOFgets(s)NULL2.2Choicestructure2.2.1CChoicestructureofClanguage2.2s1s2as1s2b2·45·2.22.2.2Conditionsentence1ifIfsentence1ifif2if1else212if(xy)printf(%d\n,x);if(xy)large=x;elselarge=b;2.13main(){floata,b,c,t;scanf(%f,%f,%f,&a,&b,&c);if(ab){t=a;a=b;b=t;}if(ac){t=a;a=c;c=t;}if(bc){t=b;b=c;c=t;}printf(%5.2f,%5.2f,%5.2f\n,a,b,c);}3,7,17.00,3.00,1.002.14aba=bmain()·46·2{inta,b,c;scanf(%d%d,&a,&b);if(a=b){c=a*b;printf(%d*%d=%d\n,a,b,c);};else{c=b/a;printf(%d/%d=%d\n,a,b,c);}}56060/5=12535*3=152IfNestedIfsentenceififififelse1ifelse2elseif1ifelseifelsenestedstructureif1if211else12else2121112122.15yx=−=)0(1)0(0)0(1xxxymain(){intx,y;scanf(%d,&x);if(x=0)if(x0)y=1;elsey=0;elsey=-1;printf(x=%d,y=%d\n,x,y);}-3x=-3,y=-12·47·5x=5,y=10x=0,y=02elseifelseifnestedstructureif11elseif221else22111221222.15if(x0)y=1;elseif(x0)y=0;elsey=-1;2.16#includestdio.hmain(){intx,y;printf(Pleaseinputintegernumberx,y:\n);scanf(%d%d,&x,&y);if(x==y)printf(x==y\n);elseif(xy)printf(xy\n);elseprintf(xy\n);}Pleaseinputintegernumberx,y:1872xy2.1702=++cbxaxAnalysea=0b=0x1=-c/ba0b*b-4ac=0#includestdio.h#includemath.hmain()·48·2{floata,b,c,delta,twoa,re,im;printf(entera,b,c:\n);scanf(%f%f%f,&a,&b,&c);if(a==0)if(b==0)printf(noanswerduetoinputerror!\n);elseprintf(thesinglerootis%f\n,-c/b);else{delta=b*b-4*a*c;twoa=2*a;re=-b/twoa;im=sqrt(fabs(delta))/twoa;if(delta=0.0)printf(realrootsare%.6f\t%.6f\n,re+im,re-im);elseprintf(complexrootsare%.6f+%.6fi\t\t%.6f-%.6fi\n,re,im,re,im);}}entera,b,c:0105thesinglerootis-0.500000entera,b,c:125complexrootare-1.000000+2.000000i-1.000000-2.000000ientera,b,c:152realrootsare-0.438447-4.561553Notesqrt()fabs()math.hCif(x)xx3switchswitchsentenceCswitchswitch{case1:1break;case2:2break;casen:nbreak;default:n+1}2·49·ii=1,2,,niiswitchii=1,2,,ndefault2.18A90100B8089C6079D059main(){intx;printf(pleaseinputx:\n);scanf(%d,&x);switch(x/10){case10:case9:printf(x=%dA\n,x);break;case8:printf(x=%dB\n,x);break;case7:case6:printf(x=%dC\n,x);break;case5:case4:case3:case2:case1:case0:printf(x=%dD\n,x);break;default:printf(x=%ddataerror!\n,x);}}pleaseinputx:65x=65C2.3Circulationstructure2.3.1CCirculationstructureofClanguage·50·22.3a2.3a2.3b2.32.3.2Circulationsentence1gotogotosentencegoto;gotogoto1if22.19ifgoto∑=1001nnmain(){inti,sum=0;i=1;loop:if(i=100){sum=sum+i;i++;gotoloop;}2·51·printf(%d,sum);}50502whilewhilesentencewhilewhile2.20∑=1001nnmain(){inti,sum=0;i=1;while(i=100){sum=sum+i;i++;}printf(%d,sum);}50502.21+−+−≈71513114/ππ610#includemath.hmain(){ints;floatn,t,pi;t=1;pi=0;n=1;s=1;while(fabs(t)=1e-6){pi=pi+t;n=n+2;s=-s;t=s/n;}pi=pi*4;printf(pi=%10
本文标题:辽宁石油化工大学《C语言程序设计》第二章 程序流程控制
链接地址:https://www.777doc.com/doc-275661 .html