|
第二,三章习题解析
习题 2
2.1
以下不正确的C语言标识符是()。
A.AB1 B.a2_b C._ab3 D.4ab解:选D。C语言标识符只能一字母和下划线开始。
2.2
下面程序位变量x,y,z赋初值2.5,然后在屏幕上打印这些变量的值。程序存在错误,请改正错误,并写出程序的正确运行结果。
#include <stdio.h>
int main(void)
{
printf (&#34;These values are :\n&#34;);
int x = y = 2.5;
printf (&#34;x = %d\n&#34;,X);
printf (&#34;y = %d\n&#34;,y);
printf (&#34;z = %d\n&#34;,z);
return 0;
}解 :程序改正如下:
#include <stdio.h>
int main(void)
{
printf (&#34;These values are :\n&#34;);
int x,y,z; //先定义变量x,y,z
x = y = z; //统一赋值
printf (&#34;x = %d\n&#34;,x); //将输出值参数表中的X改为x
printf (&#34;y = %d\n&#34;,y);
printf (&#34;z = %d\n&#34;,z);
return 0;
}输出结果如下:
These values are :
x = 2
y = 2
z = 2习题 3
3.1
分析并写出下列程序的运行结果。
(1)
#inlcude <stdio.h>
int main(void)
{
int a = 12,b = 3;
float x = 18.5,y = 4.6;
printf (&#34;%f\n&#34;,(float)(a*b)/2);
printf (&#34;%d\n&#34;,(int)x%(int)y);
return 0;
}
(2)
#include <stdio.h>
int main(void)
{
int x = 32,y = 81,p,q;
p = x++;
q = y--;
printf (&#34;%d %d\n&#34;,p,q);
printf (&#34;%d %d\n&#34;,x,y);
return 0;
}解:
(1)
a*b = 12*3 =36
(flaot)(a*b)/2 = 18.000000 //一般情况flaot占4个字节,能存储7位数字
(int)x%(int)y = 18%4 =2故程序输出如下:
18.000000
2(2)
程序输出如下
32 81
33 803.2
参考例3.1程序,从键盘任意输入一个三位数,编程计算并输出它的逆序数(忽略证书前的正负号)。例如,输入-123,则忽略符号,由123分离出其百位1,十位2,个位3,然后计算3*100+2*10+1=321,并输出321。解:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void)
{
int a= 10000;
int c,b,d;
while((a>999 || a<-999) && (a>99 || a<-99)) //判断输入数据是否位三位数
{
printf (&#34;Please enter a three digits: &#34;);
scanf (&#34;%d&#34;,&a);
}
if (a<0) a = fabs(a); // 将负数转换为其的绝对值
c = a / 100;
b = (a-c*100)/10;
d = (a-c*100-b*10); //分离百,十,个位上的数
a = d*100 + b*10 + c;
printf (&#34;%d&#34;,a);
return 0;
}3.3
设银行定期存款的年利率rate位2.23%,已知存款期位n年,存款本金为capital元,试编程一复利方式计算并输出n年后的本利之和deposit。解:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int year,i;
float capital,deposit,rate = 0.0225;
printf (&#34;Please enter your capital: &#34;);
scanf (&#34;%f&#34;,&capital);
printf (&#34;Please enter your deposit period : &#34;);
scanf (&#34;%d&#34;,&year);
for (i=1;i<=year;i++)
{
capital = capital *(1 + rate);
deposit = capital;
}
printf (&#34;After %d years,your deposit is %.2fyuan &#34;,year,deposit);
return 0;
}3.4

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void)
{
float a,b,c,x1,x2;
a=b=c=0;
while (b*b-4*a*c<=0)
{
printf (&#34;Please enter a,b and c :&#34;);
scanf (&#34;%f %f %f&#34;,&a,&b,&c);
}
x1=(-b+sqrt(b*b-4*a*c))/(2*a);
x2=(-b-sqrt(b*b-4*a*c))/(2*a);
printf (&#34;%.2f %.2f&#34;,x1,x2);
return 0;
}3.5

解:
宏定义:
#include <stdio.h>
#include <stdlib.h>
#define PI 3.14159
int main(void)
{
float r;
printf (&#34;Please enter the radius of the ball: &#34;);
scanf (&#34;%f&#34;,&r);
printf (&#34;The volume of the ball is %.2f\n&#34;,4*PI*r*r*r/3);
printf (&#34;The surface area of the ball is %.2f\n&#34;,4*PI*r*r);
return 0;
}const 常量:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void)
{
const float PI = 3.14159;
float r;
printf (&#34;Please entre the radius of the ball :&#34;);
scanf (&#34;%f&#34;,&r);
printf (&#34;The volume of the ball is %.2f\n&#34;,(4/3)*PI*pow(r,3));
printf (&#34;The surface area of the ball is %.2f\n&#34;,4*PI*pow(r,2));
return 0;
} |
|