资源描述:
《c语言程序设计 (何钦铭 颜晖 著) 高等教育出版社第三章 课后答案》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、练习3-2#includevoidmain(){floatx;inty;printf("PleaseInputx:");scanf("%f",&x);if(x>0)y=1;elseif(x=0)y=0;elsey=-1;printf("sign(%f)=%d",x,y);}练习3-3#includevoidmain(){inti,letter,digit,space,other;charch;letter=digit=space=other=0;printf("PleaseInput15charactors:");fo
2、r(i=1;i<=15;i++){ch=getchar();if(ch==''
3、
4、ch=='')space++;elseif(ch>='0'&&ch<='9')digit++;elseif((ch>='A'&&ch<='Z')
5、
6、(ch>='a'&&ch<='z'))letter++;elseother++;}printf("letters=%d,digits=%d,spaces=%d,others=%d",letter,digit,space,other);}练习3-6#includevoidmain(){charch;print
7、f("PleaseInputgrade(A~E):");ch=getchar();switch(ch){case'A':printf("Thescoreisbetween90--100.");break;case'B':printf("Thescoreisbetween80--89.");break;case'C':printf("Thescoreisbetween70--79.");break;case'D':printf("Thescoreisbetween60--69.");break;case'E':printf("Thescorei
8、sbetween0--59.");break;default:printf("Inputerror!");break;}}练习3-7#includevoidmain(){inti,ch;floatprice;for(i=1;i<=5;i++){printf("[1]apples");printf("[2]pears");printf("[3]oranges");printf("[4]grapes");printf("[0]Exit");printf("Pleasechoice(0~4):");scanf("%d"
9、,&ch);if(ch==0)break;switch(ch){case1:price=3.0;break;case2:price=2.5;break;case3:price=4.1;break;case4:price=10.2;break;default:price=0.0;break;}printf("Thepriceis%.1f.",price);}}练习3-8#includevoidmain(){intyear;printf("Pleaseinputayear:");scanf("%d",&year);if((year%4==0
10、&&year%100!=0)
11、
12、(year%400==0))printf("Yes.");elseprintf("No.");}练习3-9#includevoidmain(){inti,n,num=0;floatscore,total=0;printf("Pleaseinputnumbersofstudent:");scanf("%d",&n);for(i=1;i<=n;i++){printf("Pleaseinputscore#%d(0~100):",i);scanf("%f",&score);if(score>=60)num++
13、;total=total+score;}printf("Theaveragescoreis%.2f.",total/n);printf("Thenumbersofpassingis%d.",num);}练习3-10#includevoidmain(){doublex,y;printf("Pleaseinputx:");scanf("%lf",&x);if(x>15)y=2.5*x-10.5;elseif(x<0)y=0;elsey=4*x/3;printf("y=f(%lf)=%lf",x,y);}习题3-1#include14、tdio.h>#includevoi