- 208.33 KB
- 2022-04-22 11:32:21 发布
- 1、本文档共5页,可阅读全部内容。
- 2、本文档内容版权归属内容提供方,所产生的收益全部归内容提供方所有。如果您对本文有版权争议,可选择认领,认领后既往收益都归您。
- 3、本文档由用户上传,本站不保证质量和数量令人满意,可能有诸多瑕疵,付费之前,请仔细先通过免费阅读内容等途径辨别内容交易风险。如存在严重挂羊头卖狗肉之情形,可联系本站下载客服投诉处理。
- 文档侵权举报电话:19940600175。
'课后答案网www.khdaw.com《C语言程序设计》习题解答第二章数据类型、运算符和表达式一、简答题:1.常量有数值常量、字符常量、字符串常量和符号常量;字符u、l和f主要是对数值常量进行修饰。字符u和l对整型常量进行修饰:表明常量是否是unsigned类型以及是否是long型;字符l对实型常量进行修饰:表明常量之是否是float类型。2.字符常量用单引号‘’界定单个字符,内存空间只占一个字节;字符串常量用双引号界定零个或多个字符,字符串常量的结尾都有一个结束字符‘ ’。3.&&、||和!是构成逻辑表达式的逻辑运算符,表达式运算结果是0(真)或1(假);&、|和~是位操作运算符中的逻辑为运算符,对整型操作数的二进制位进行运算,结果为整型值。4.表达式在计算中根据运算符的优先级和结合型进行运算,在计算过程中要求运算符的两个操作数类型必须相同,根据数据类型之间的隐式转换规则对操作数的类型进行转换,最后运算符运算结果的类型就是表达式值的类型。5.类型转换有隐式转换和显示转换。隐士转换是在计算过程中根据隐士转换规则进行;显示转换利用强制转换运算符进行;在赋值运算中,表达式右边值得类型都强制转换成左边变量的类型。二、选择题:(1)D(2)B(3)D(4)D(5)B(6)D(7)D(8)C(9)C(10)C(11)C(12)A(13)A(14)D(15)A(16)A、C(17)A(18)A(19)9(20)A三、计算表达式的值:1.(1)24(2)10(3)96(4)0(5)0(6)02.(1)5(2)10(3)403.(1)0(2)1(3)3(4)16(5)14.(1)3.500000(2)3.0000005.(1)6(2)6(3)0(4)0四、写出给出式子的C语言表示形式:1.x*x+3*x+12.(a*x+b)/(c*y+d)*(m*x*x+n*y*y)3.sqrt(4*x*x*x+5*x*x+x+3)4.sin(x+0.5)*sin(x+0.5)+3*cos(2*x+4)5.(a/x)/(a/(a+y)+b/(a+b/z))6.labs(3-exp(x)*ln(1+x))第三章简单程序设计一、简答题:(略)二、选择题:(1)C(2)D(3)C(4)B(5)B(6)D(7)D(8)C(9)A(10)A三、编程实现1.voidmain(){inta,b;intmin;
课后答案网www.khdaw.comprintf("pleaseinputtwointegerdata:");scanf("%d%d",&a,&b);min=a>b?b:a;printf("theminis:%dn",min);}2.voidmain(){floatf;printf("pleaseinputfloatdata:");scanf("%f",&f);printf("thedatais%.2fn",f);}3.voidmain(){charch;printf("pleaseinputchar:");scanf("%c",&ch);printf("thethreecharactersare:%c,%c,%c.n",ch-1,ch,ch+1);}4.#includevoidmain(){floata,b,c;floatl,mid,area;printf("pleaseinputthreeeadge:");scanf("%f%f%f",&a,&b,&c);l=0.5*(a+b+c);mid=l*(l-a)*(l-b)*(l-c);area=sqrt(mid);printf("areais:%fn",area);}第四章数组一、简答题:(略)二、选择题:(1)C(2)A(3)C(4)B(5)D(6)B(7)C(8)B(9)A(10)B(11)C(12)C三、编写程序:1.#include#defineLEN128voidmain(){charstr1[LEN],str2[LEN];intlen1,len2;
课后答案网www.khdaw.comprintf("pleaseinputfirststring:n");gets(str1);printf("pleaseinputsecondstring:n");gets(str2);len1=strlen(str1);len2=strlen(str2);if(len1>len2)printf("thestringis:%sn",str1);if(len10)printf("thestringis:%sn",str1);elseprintf("thestringis:%sn",str2);}}2.#include#defineLEN128voidmain(){charstr1[LEN],str2[LEN];puts("pleaseinputfirststring:");gets(str1);puts("pleaseinputsecondstring:");gets(str2);strcat(str1,str2);printf("thesresultis:n%sn",str1);}第五章基本语句各小题的参考程序如下:1.#definePI3.14159voidmain(){floatr,h;floats,v;printf("pleaseinputrandh:");scanf("%f%f",&r,&h);s=PI*r*r;s+=2*PI*PI*r*h;v=1.0/3*PI*r*r*h;printf("theareais%fn",s);
课后答案网www.khdaw.comprintf("thevolumeis%fn",v);}2.voidmain(){floatx;inty;printf("inputx:");scanf("%f",&x);if(x>0)y=1;elseif(x==0)y=0;elsey=-1;printf("yis%dn",y);}3.voidmain(){intflag=0;intdata;printf("inputdata:");scanf("%d",&data);if(data%3==0)flag++;if(data%5==0)flag++;if(data%7==0)flag++;switch(flag){case0:printf("can"tdividedbyanyone!n");break;case1:printf("candividedbyonedata!n");break;case2:printf("candividedbytwodata!n");break;case3:printf("candividedbythreedata!n");}}4.#includevoidmain(){floata,b,c;floatl,s;printf("inputa,bandc:");scanf("%f%f%f",&a,&b,&c);
课后答案网www.khdaw.comif(a+b>c&&fabs(a-b)voidmain(){floatm,n,k;floatdet,x1,x2;printf("inputm,n,k:");scanf("%f%f%f",&m,&n,&k);det=n*n-4*m*k;if(det>=0){det=sqrt(det);x1=(-n+det)/(2*m);x2=(-n-det)/(2*m);if(det){printf("thefirstrootis%f.n",x1);printf("thesecondrootis%f.n",x2);}elseprintf("therootis%f.n",x1);}else{det=sqrt(-det);x1=-n/(2*m);x2=det/(2*m);x2=fabs(x2);printf("thefisrtvirtualrootis%f+%fi.n",x1,x2);printf("thescondvirtualrootis%f-%fi.n",x1,x2);}}6.voidmain(){floata,b,result;charch;
课后答案网www.khdaw.comprintf("pleaseinputexpression:");scanf("%f%c%f",&a,&ch,&b);switch(ch){case"+":result=a+b;break;case"-":result=a-b;break;case"*":result=a*b;break;case"/":if(b){result=a/b;break;}else{printf("DividedZero!n");return;}default:printf("expressionError!n");return;}printf("%f%c%f=%f.n",a,ch,b,result);}7.voidmain(){intn,num[6];inti,x;printf("pleaseinputnumofdata:");scanf("%d",&n);printf("inputdata:n");for(i=0;i0&&x<=5)num[x]++;}printf("theresultis:n");for(i=1;i<=5;i++)printf("thenumof%dis%d.n",i,num[i]);}8.voidmain(){intn,i;doublefac=1;printf("inputn:");scanf("%d",&n);for(i=1;i<=n;i++)fac*=i;printf("%d!=%.0fn",n,fac);}9.voidmain(){
课后答案网www.khdaw.comfloata,b,temp;floats;inti;a=1,b=2;for(i=1,s=0;i<=30;i++){s+=b/a;temp=b;b=a+b;a=temp;}printf("theresultis%f.n",s);}10.voidmain(){floatavg_age=0,avg_score=0;floatage,score;intnum=0;printf("inputageandscore:n");scanf("%f%f",&age,&score);while(age!=-1){avg_age+=age;avg_score+=score;num++;printf("inputageandscore:n");scanf("%f%f",&age,&score);}printf("theaverageageis%.1f.n",avg_age/num);printf("theaveragescoreis%.1f.n",avg_score/num);}11.voidmain(){intn1=0,n2=0,n3=0;intx,max,min;inti;printf("pleaseinputtendata:n");scanf("%d",&x);max=min=x;if(x>0)n1++;elseif(x==0)n2++;
课后答案网www.khdaw.comelsen3++;for(i=1;i<10;i++){scanf("%d",&x);if(min>x)min=x;if(max0)n1++;elseif(x==0)n2++;elsen3++;}printf("themaxis%d.n",max);printf("theminis%d.n",min);printf("thenumofpositiveis%d.n",n1);printf("thenumofZerois%d.n",n2);printf("thenumofnegativeis%d.n",n3);}12.voidmain(){intn;doublenum1=1,num2=1;printf("inputn:");scanf("%d",&n);while(n-->0){num1*=3;num2/=3;}printf("theresult1is%f.n",num1);printf("theresult2is%f.n",num2);}13.#includevoidmain(){char*s[9]={"one","two","three","four","five","six","seven","eight","nine"};longd;intnum[10],i=0,j;
课后答案网www.khdaw.comprintf("inputdata:");scanf("%ld",&d);while(d){num[i++]=d%10;d=d/10;}for(j=i-1;j>=0;j--)printf("%s",s[num[j]-1]);putchar("n");}14.voidmain(){floatpi;intn,flag;for(pi=1,n=2,flag=-1;1.0/(2*n-1)>1.0e-6;n++,flag*=-1)pi+=flag*1.0/(2*n-1);printf("pi=%fn",4*pi);}15.voidmain(){intx,i;intcount,sum;for(i=1,count=0,sum=0;i<=500;i++)if(i%3==2&&i%5==3&&i%7==2){count++;sum+=i;printf("%5d",i);if(count%5==0)printf("n");}printf("thesumis%d.n",sum);}16.#includevoidmain(){charch;intch_num=0,dig_num=0,other=0;printf("pleaseinputstring:n");while((ch=getchar())!="!")if(ch>="A"&&ch<="Z"||ch>="a"&&ch<="z")ch_num++;else
课后答案网www.khdaw.comif(ch>="0"&&ch<="9")dig_num++;elseother++;printf("thenumofcharacteris%d.n",ch_num);printf("thenumofnumberis%d.n",dig_num);printf("thenumofothercharacteris%d.n",other);}17.#defineN6voidmain(){inti,j;for(i=1;i<=N;i++){for(j=1;j<=N-i;j++)printf("%c","");for(j=1;j<=i;j++)printf("*");printf("n");}for(i=1;i<=N-1;i++){for(j=1;j<=i;j++)printf("%c","");for(j=1;j<=N-i;j++)printf("*");printf("n");}}18.#defineN6voidmain(){inti,j;for(i=1;i<=N;i++){for(j=1;j<=2*i;j++)printf("");for(j=1;j<=i;j++)printf("#");printf("n");}}19.#include
课后答案网www.khdaw.comvoidmain(){intflag=0,num=0;charch;printf("inputexpression:");while((ch=getchar())!=";"){if(ch=="(")num++;if(ch==")"){num--;if(num<0&&!flag)flag++;}}if(num==0&&!flag)printf("it"sOK!n");if(num>0&&!flag)printf("thenumof"("morethan")"!n");if(flag&&num>=0)printf("the")"ispriorof"("!n");if(flag&&num<-1)printf("the")"ispriorof"("!andthenumof")"morethan"("!n");}20.voidmain(){longd;intsum=0;printf("inputdata:");scanf("%ld",&d);while(d){sum+=d%10;d/=10;}printf("thesumof%ldis%d.n",d,sum);}21.#defineN4voidmain(){floatsc[N][6];floatavg[2][N],t;inti,j,no;
课后答案网www.khdaw.comprintf("pleaseinputNo.andscores:n");printf("No.s1s2s3s4s5n");for(i=0;i#defineN3voidmain(){intarray[N][N];intmax,min;intmax_index[2][N*N];intmin_index[2][N*N];
课后答案网www.khdaw.cominti,j,max_flag,min_flag;printf("inputarray:n");for(i=0;i=array[i][j]){if(min>array[i][j]){min_flag=0;min=array[i][j];}min_index[0][min_flag]=i;min_index[1][min_flag]=j;min_flag++;}}printf("themaxvalueis%d,",max);printf("it"sindex:");for(i=0;ii;j--)data[j]=data[j-1];data[i]=x;num++;printf("inserted%d!n",x);}elseif(chioce==2){printf("inputdeleteddata:");scanf("%d",&x);for(i=0;i#defineN128voidmain(){charstr1[N],str2[N];intm,n;printf("inputstring:n");gets(str1);n=strlen(str1);printf("inputthebeginposofcopy:");scanf("%d",&m);if(m>n)str2[0]=" ";elsestrcpy(str2,str1+m);printf("thesecondstringis:%sn",str2);}第六章程序设计方法各小题参考程序如下:1.voidmain(){inta,b,c;intgc,md;printf("inputa,b,c:");scanf("%d%d%d",&a,&b,&c);gc=a;while(1){if(a%gc==0&&b%gc==0&&c%gc==0)
课后答案网www.khdaw.combreak;gc--;}md=a*b*c/gc;printf("thegreatestcommandis%d.n",gc);printf("theminestcommandis%d.n",md);}2.voidmain(){inti,j,data,t;intnum[4];for(i=10;i<100;i++){data=i*i;j=0;t=data;while(data){num[j++]=data%10;data/=10;}if((num[1]+num[3]==10)&&(num[0]*num[2]==12))printf("%5d",t);}}3.voidmain(){inti,t;ints,s1;for(i=100;i<=300;i++){t=i,s=0,s1=1;while(t){s+=t%10;s1*=t%10;t/=10;}if(s==12&&s1==42)printf("%5d",i);}}4.voidmain(){
课后答案网www.khdaw.cominti,j,k;intnum[3],t,c=0;for(i=1;i<1000;i++){if(i%3==0){j=0,t=i;while(t){num[j++]=t%10;t/=10;}for(k=0;k1.0e-6;i++){ee/=i;e+=ee;
课后答案网www.khdaw.com}printf("e=%fn",e);}7.voidmain(){inti,a,t,sum;printf("theresultis:");for(i=100;i<1000;i++){sum=0,t=i;while(t){a=t%10;sum+=a*a*a;t/=10;}if(sum==i)printf("%5d",i);}}8.#defineN100voidmain(){inti,j,k,sum;intfac[N];for(i=1;i<1000;i++){sum=0,k=0;for(j=1;j=0;i--)printf("%c",ch[num[i]]);printf("n");}4.longn_dou(intn);voidmain(){longre;intd;printf("inputn:");scanf("%d",&d);re=n_dou(d);printf("theresultis:%ld.n",re);}longn_dou(intn){if(n==0)return0;returnn*n+n_dou(n-1);}5.doubleee(doubleexp){inti=1;
课后答案网www.khdaw.comdoublere=0,part_re=1;while(part_re>exp){re+=part_re;part_re*=(double)i/(2*i+1);i++;}returnre*2;}voidmain(){doubleresult,exp;printf("inputexp:");scanf("%lf",&exp);result=ee(exp);printf("e=%lf.n",result);}6.intmax(inta,intb){returna>b?a:b;}#defineMAX(x,y)((x)>(y)?(x):(y))voidmain(){inta,b,c;intm,m1;printf("inputdata:");scanf("%d%d%d",&a,&b,&c);m=max(a,b);m=max(m,c);printf("thevalueoffunction:%d.n",m);m1=MAX(a,b);m1=MAX(m1,c);printf("thevalueofMacro:%d.n",m1);}7.#includevoidcla(intdays,intweek_no){inti;printf("monwedthituefristasunn");for(i=1;i=N)m=1;}}voidmain(){intdata[N];inti;printf("input%ddata:n",N);for(i=0;i#defineN128voidmain(){charstr[N];char*p;intchap_num=0,lit_num=0,dit_num=0,bla_num=0,other=0;printf("inputstring:n");gets(str);p=str;while(*p++){if(*p>="A"&&*p<="Z")chap_num++;elseif(*p>="a"&&*p<="z")lit_num++;elseif(*p>="0"&&*p<="9")dit_num++;if(*p==""||*p=="t"||*p=="n")bla_num++;elseother++;}printf("theresultlist:n");printf("thenumofcaptalchar:%dn",chap_num);printf("thenumoflittlechar:%dn",lit_num);printf("thenumofdigitchar:%dn",dit_num);printf("thenumofblankchar:%dn",bla_num);printf("thenumofotherchar:%dn",other);}4.#defineN128voidmain(){charstr[N],*p;intnum=0,i;intdata,d[N];printf("inputstring:");gets(str);p=str;
课后答案网www.khdaw.comwhile(*p){while(*p<"0"||*p>"9")p++;data=0;while(*p>="0"&&*p<="9"){data*=10;data+=*p-"0";p++;}d[num++]=data;}printf("thedatanumis%d,itsare:n",num);for(i=0;ichar*str_mcpy(char*str1,char*str2,intm){char*p,*p1;intlen;p1=str2;len=strlen(str1);if(len