C语言上机练习参考问题详解Word格式.docx
《C语言上机练习参考问题详解Word格式.docx》由会员分享,可在线阅读,更多相关《C语言上机练习参考问题详解Word格式.docx(134页珍藏版)》请在冰豆网上搜索。
%7c\n"
1-3已知某个圆的半径,编写一个程序,用来计算并显示面积。
要求:
将π定义为符号常量,并假设一个恰当的半径值。
#definePI3.14
{floatr=5,s;
s=PI*r*r;
Theareaofcircleis:
%.2f\n"
s);
Output
78.50
1-4已知两个整数20和10,编写程序,自定义函数add()将这两个数相加,自定义函数sub()计算这两个数的差,并按照下面形式显示计算结果:
20+10=30
20-10=10
intadd(inta,intb)
{return(a+b);
intsub(inta,intb)
{return(a-b);
{inta=20,b=10;
%d+%d=%d\n"
a,b,add(a,b));
%d-%d=%d\n"
a,b,sub(a,b));
20+10=30
20–10=10
1-5已知变量a、b和c的值,编写程序,用来计算并显示x的值,其中
请分别用以下数值运行该程序
(1)a=250,b=85,c=25
(2)a=300,b=70,c=80
{inta=250,b=85,c=25;
floatx;
x=1.0*a/(b-c);
x=%.2f\n"
x);
Output
(1)
x=4.17
Program
(2)
{inta=300,b=70,c=80;
/*试写成x=a/(b-c);
得到什么运行结果?
为什么?
*/
Output
(2)
x=-30.00
⏹第2章常量、变量及数据类型&
第3章运算符和表达式
3-1编写程序,求华氏温度100oF对应的摄氏温度。
计算公式如下:
式中:
c表示摄氏温度,f表示华氏温度。
(c定义为实型,f定义为整型)
Program
{intf=100;
floatc;
c=5.0*(f-32)/9;
/*如果是c=5*(f-32)/9;
会是什么结果?
Celsiusdegree(correspondingto%dFahrenheit)is:
%.2f.\n"
f,c);
Celsiusdegree(correspondingto100Fahrenheit)is:
37.78.
3-2一个物体从100m的高空自由落下,编写程序,求它在前3s内下落的垂直距离。
设重力加速度为10m/s2。
要求,将重力加速度定义为符号常量,尝试将其改为9.8m/s2,看结果有何不同?
#defineG10
{intt=3;
floats;
s=1.0/2*G*t*t;
/*如果是s=1/2*G*t*t;
Thefallingverticaldistance(in%dseconds)is:
t,s);
Thefallingverticaldistance(in3seconds)is:
45.00.
3-3将球的半径R定义为符号常量,计算球的表面积(4πR2)和体积(4/3*πR3)。
#defineR5.2
{floats,v;
s=4*PI*R*R;
v=4.0/3*PI*R*R*R;
Thesurfaceareaoftheball(radiusis%.2f)is:
%.2f,andthevolumeis:
R,s,v);
Thesurfaceareaoftheball(radiusis5.20)is:
339.62,andthevolume588.68.
3-4给定x、y和z的值,编写程序,使x等于y的值,y等于z的值,z等于x的值。
{intx=1,y=2,z=3,t;
Beforeswap:
x=%d,y=%d,z=%d.\n"
x,y,z);
t=x;
x=y;
y=z;
z=t;
/*变量t的作用是什么?
Afterswap:
x=1,y=2,z=3.
x=2,y=3,z=1.
3-5编写一个程序,给定一个浮点数(例如456.78),显示该数的十位数字与个位数字之和(例如5+6=11)。
{floatf=456.78;
intn,a,b;
n=f;
a=n%10;
/*赋值后,a是什么值?
b=n/10%10;
/*赋值后,b是什么值?
Thesumofthetensdigitandunitsdigitof%.2fis:
%d+%d=%d.\n"
f,b,a,a+b);
b=n%100/10;
/*该语句与上面程序不同,看看有何区别?
Thesumofthetensdigitandunitsdigitof456.78is:
5+6=11.
3-6某种物品每年折旧费的计算方法如下:
编写一个程序,当给定某物品的购买价格、使用年限和每年的折旧费时,计算出其废品价值。
{floatprice=120.65,year=3,depreciation=10.2,value;
value=price-year*depreciation;
Thescrapvalueis%.2f.\n"
value);
Thescrapvalueis90.05.
3-7在库存管理中,某单个物品的经济定购数EOQ由下面等式给定:
而最优的定购时间间隔TBO由下面等式给定:
编写程序,给定需求率(单位时间内的物品数)、生产成本(每个定购)和储备成本(单位时间内每种物品),计算EOQ和TBO。
math.h>
{intdemand=1000;
floatproduct_cost=3.5,storage_cost=0.63,eoq,tbo;
eoq=sqrt(2*demand*product_cost/storage_cost);
tbo=sqrt(2*product_cost/demand/storage_cost);
EOQis%.2f,andTBOis%.2f.\n"
eoq,tbo);
EOQis105.41,andTBOis0.11.
⏹第4章输入输出操作管理
4-1输入两个数,将它们交换后输出。
{intx,y,t;
Pleaseinput2numbers:
"
scanf("
%d%d"
&
x,&
y);
Beforeswap,the2numbersare:
%d,%d\n"
x,y);
y=t;
Afterswap,the2numbersare:
35/*Blueisinput*/
3,5
5,3
4-2输入一个十进制数,输出对应的八进制数和十六进制数。
{intn;
Pleaseinputadecimalnumber:
%d"
n);
Theoctalis%o,andthehexadecimalis%x.\n"
n,n);
10/*Blueisinput*/
Theoctalis12,andthehexadecimalisa.
考虑:
如何得到下面的输出?
Theoctalis012,andthehexadecimalis0xa.
4-3编写程序,输入3个整数,计算并输出它们的平均值。
{inta,b,c;
Pleaseinput3integers:
%d%d%d"
a,&
b,&
c);
Theaverageis%.2f.\n"
(a+b+c)/3.0);
47-19/*Blueisinput*/
Theaverageis-2.67.
4-4编写一个程序,读取x和y的值,显示下面表达式的值:
(1)
(2)
(3)
{floatx,y;
Pleaseinputxandy:
%f%f"
(1)(x+y)/(x-y)=%.2f\n"
(x+y)/(x-y));
(2)(x+y)/2=%.2f\n"
(x+y)/2);
(3)(x+y)(x-y)=%.2f\n"
(x+y)*(x-y));
3.54.1/*Blueisinput*/
(1)(x+y)/(x-y)=-12.67
(2)(x+y)/2=3.80
(3)(x+y)(x-y)=-4.56
4-5计算银行存款的本息。
编写程序,输入存款金额money、存期year和年利率rate,根据下列公式计算存款到期时的本息合计sum(税前),输出时保留两位小数。
{floatmoney,rate,sum;
intyear;
Pleaseinputthedepositmoney:
%f"
money);
Pleaseinputthedepositperiod:
%d"
year);
Pleaseinputtheannualinterestrate:
rate);
sum=money*pow(1+rate,year);
Thetotalprincipalandinterestis:
sum);
2500/*Blueisinput*/
3/*Blueisinput*/
0.023/*Blueisinput*/
2676.50
4-6输入圆柱的高h和半径r,求圆柱体积volume=π*r2*h。
{floatvolume,r,h;
Pleaseinputtheradiusofthecylinder:
r);
Pleaseinputtheheightofthecylinder:
h);
volume=PI*r*r*h;
Thevolumeofthecylinderis:
volume);
3.5/*Blueisinput*/
12.7/*Blueisinput*/
488.51
4-7编写一个程序,读取一个实数f,将其四舍五入的值赋值给整型变量n,输出n的值。
(尝试不用if语句完成本程序,并考虑负数是否适用)
{floatf;
intn,m;
Pleaseinputarealnumber:
f);
m=f*10;
m=m%10/5;
/*m是什么值?
n=n+m;
/*m有何作用?
Theroundedintegeris:
%d\n"
n);
Program
(2)如果不明白if语句,可以在学完第5章后再看。
n=f+0.5;
/*是否理解该语句?
3.6/*Blueisinput*/
4
-13.2/*Blueisinput*/
-13
4-8编写程序,读入两个两位数字的整数,并按照如下形式输出这两个整数的乘积。
45
*37
315
135
1665
提示:
注意格式!
{intx,y,m,n;
Pleaseinput2integers:
%5d\n*%3d\n------\n"
m=y%10;
n=y/10;
%5d\n%4d\n------\n%5d\n"
x*m,x*n,x*y);
⏹第5章判断与分支
5-1输入一个字符ch,根据不同情况进行输出:
(1)ch为小写字母,输出对应的大写字母;
(2)ch为大写字母,按照原样输出;
(3)ch为数字,输出对应的数字;
(4)ch为其他字符,输出“Othercharacter.”。
{charch;
Pleaseinputacharacter:
ch=getchar();
if(ch>
='
a'
&
&
ch<
z'
)
%c\n"
ch-('
-'
A'
));
elseif(ch>
Z'
printf("
ch);
elseif(ch>
0'
9'
printf("
%d\n"
ch-'
elseprintf("
Othercharacter.\n"
A/*Blueisinput*/
A
b/*Blueisinput*/
B
Output(3)
3
Output(4)
@/*Blueisinput*/
Othercharacter.
5-2为鼓励居民节约用水,自来水公司采用按月用水量分段计费的办法,居民应交水费y元与月用水量x吨的函数关系式如下(设x0)。
编写程序,输入用户的月用水量x吨,计算并输出该用户应支付的水费y元(保留两位小数)。
Pleaseinputthewaterconsumption:
x);
if(x<
=15)
y=4*x/3;
/*是否可以写成y=4/3*x;
?
区别在哪里?
else
y=2.5*x-10.5;
Thewaterpriceis:
%.2f.\n"
y);
27.9/*Blueisinput*/
59.25
5-3输入一个年份year,判断year是否为闰年。
year若为闰年,需要满足下列条件之一:
(1)能被4整除,但不能被100整除(如2004年是闰年,2100年不是闰年)
(2)能被400整除(如2000年是闰年)
{int