习题
1. 假如我国国民生产总值的年增长率为9%,计算10年后我国国民生产总值与现在相比增长多少百分比,计算公式为 p = ( 1 + r ) ^ n r为年增长率,n为年数,p为与现在相比的倍数。
#include
#include
int main()
{
double r = 0.09;
int n = 10;
double p;
p = pow((1 + r), n);
printf("After 10 years, increase percentage is: %.2lf\n", p);
}
2. 存款利息的计算。有1000元,想存5年,可按以下5种办法存:
(1) 一次存5年期。
(2) 先存2年期,到期后将本息再存3年期。
(3) 先存3年期,到期后将本息再存2年期。
(4) 存1年期,到期后将本息再存1年期,连续存5次。
(5) 存活期存款。活期利息每一季度结算一次。
2007年12月的银行存款利息如下:
1年期定期存款利息为4.14%;
2年期定期存款利息为4.68%;
3年期定期存款利息为5.4%;
5年期定期存款利息为5.85%;
活期存款利息为0.72%(活期存款每一季度结算一次利息)。
如果r为年利率,n为存款年数,则计算本息和的公式为
1年期本息和:P=1000*(1+r);
n年期本息和:P=1000*(1+n*r);
存n次1年期的本息和:P=1000*(1+r)^n;
活期存期本息和:P=1000*(1+r/4)^4n。
说明:1000*(1+r/4)是一个季度的本息和。
#include
#include
int main()
{
double principal = 1000;
double pOne = principal * (1 + 5 * 0.0585);
double pTwo = principal * (1 + 2 * 0.0468);
pTwo = pTwo * (1 + 3 * 0.054);
double pThree = principal * (1 + 3 * 0.054);
pThree = pThree * (1 + 2 * 0.0468);
double pFour = principal * pow(1 + 0.0414,5);
double pFive = principal * pow(1 + 0.0072, 4 * 5);
printf("deposit 5 years, principal and interest is: %.2f\n", pOne);
printf("first deposit 2 years, then 3 years, principal and interest is: %.2f\n", pTwo);
printf("first deposit 3 years, then 2 years, principal and interest is: %.2f\n", pThree);
printf("deposit every 1 year, principal and interest is: %.2f\n", pFour);
printf("current deposit every quarter, principal and interest is: %.2f\n", pFive);
}
3. 购房从银行贷了一笔款d,准备每月还款额为p,月利率为r,计算多少月能还清。设d为300000,p为6000,r为1%。对求得的月份取小数点后一位,对第2位按四舍五入处理。
提示:计算还清月数m的公式如下:
m=logp-log(p-d*r)/log(1+r)
可以将公式改写位
m=log(p/(p-d*r))/log(1+r)
C的库函数中有求对数的函数log10,是求以10为底的对数,log(p)表示log p。
#include
#include
int main()
{
double d = 300000;
double p = 6000;
double r = 0.01;
double m = log10(p / (p - d * r)) / log10(1 + r);
m = (int)(m * 10 + 0.5) / 10.0;
printf("about %.1f months pay off the loan.\n", m);
}
4. 分析下面的程序:
#include
int main()
{ char cOne, cTwo;
cOne = 97;
cTwo = 98;
printf("cOne=%c,cTwo=%c\n", cOne, cTwo);
printf("cOne=%d,cTwo=%d\n", cOne, cTwo);
return 0;
}
(1)运行时会输出什么信息?为什么?
C语言中支持基本类型char和int的自动转换,也就是说这段代码中虽然c1和c2定义时都是字符型,但赋值整型数据也不会报错,它们之间是ACSII表中字符和ASCII码的对应关系。例如,字符a的ASCII码值为97,字符b的ASCII码值为98,如上图输出所示。
(2)如果将程序第4,5行改为
cOne=197;
cTwo=198;
运行时会输出什么信息?为什么?
如图所示,c1和c2以字符输出时的值为?是因为ASCII表中只有128个字符,197和198都超出了范围,所以值是随机的,因此得出字符型变量赋值整型数据时,范围是0~127。同时c1和c2以整型输出时的结果其实是197和192的补码形式。
(3)如果将程序第3行改为
int cOne, cTwo;
运行时会输出什么信息?为什么?
修改后,(1)问的结果不变,因为int和char可以自动转换。(2)问中的结果同样不变,因为197和198依旧超出ASCII表的范围。
5. 用下面的scanf函数输入数据,使a=3,b=7,x=8.5,y=71.82,cOne='A',cTwo='a'。问在键盘上如何输入?
#include
int main()
{
int a, b;
float x, y;
char cOne, cTwo;
scanf_s("%d%d",&a,&b);
scanf_s("%f%e",&x,&y);
scanf_s("%c%c",&cOne,sizeof(cOne), &cTwo,sizeof(cTwo));
printf("a=%d,b=%d\n",a,b);
printf("x=%f,y=%f\n",x,y);
printf("cOne=%c,cTwo=%c",cOne,cTwo);
return 0;
}
a=3b=7<回车>
8.5 71.82Aa<回车>
6. 请编程序将"China"译成密码,密码规律是:用原来的字母后面第4个字母代替原来的字母。例如,字母"A"后面第4个字母是“E”,用"E"代替"A"。因此,"China"应译为"Glmre"。请编一程序,用赋初值的方法使cOne,cTwo,cThree,cFour,cFive这个变量的值分别为'C','h','i','n','a',经过运算,使cOne,cTwo,cThree,cFour,cFive分别变为‘G','l','m','r','e'。分别用putchar函数和printf函数输出这5个字符。
#include
int main()
{
char cOne = 'C';
char cTwo = 'h';
char cThree = 'i';
char cFour = 'n';
char cFive = 'a';
cOne += 4;
cTwo += 4;
cThree += 4;
cFour += 4;
cFive += 4;
putchar(cOne);
putchar(cTwo);
putchar(cThree);
putchar(cFour);
putchar(cFive);
putchar('\n');
printf("%c%c%c%c%c\n", cOne, cTwo, cThree, cFour, cFive);
}
7. 设圆半径r=1.5,圆柱高h=3,求圆周长、圆面积、圆球表面积、圆球体积、圆柱体积。用scanf输入数据,输出计算结果,输出时要求有文字说明,取小数点后2位数字。请编程序。
#include
#include
#define PI 3.1415926
int main()
{
double r, h;
printf("Please input circle radius and cylinder height:");
scanf_s("%lf %lf", &r, &h);
double circle_circumference = 2 * PI * r;
double circle_area = PI * r * r;
double sphere_surface_area = 4 * PI * r * r;
double sphere_volume = (4.0 / 3.0) * PI * r * r * r;
double cylinder_volume = PI * r * r * h;
printf("circle_circumference is %.2lf\n", circle_circumference);
printf("circle_area is %.2lf\n", circle_area);
printf("sphere_surface_area is %.2lf\n", sphere_surface_area);
printf("sphere_volume is %.2lf\n", sphere_volume);
printf("cylinder_volume is %.2lf\n", cylinder_volume);
}
8. 编程序,用getchar函数读入两个字符串给cOne和cTwo,然后分别用putchar函数和printf函数输出这两个字符。思考以下问题:
#include
int main()
{
char cOne, cTwo;
cOne = getchar();
cTwo = getchar();
printf("\nputchar:");
putchar(cOne);
putchar(cTwo);
printf("\n\nprintf: %c%c", cOne, cTwo);
return 0;
}
(1) 变量cOne和cTwo应定义为字符型还是整型?或两者皆可?
答案:字符型或整型二者皆可。
(2) 要求输出cOne和cTwo值得ASCII码,应如何处理?用putchar函数还是printf函数?
答案:用printf函数,将%c改为%d即可。
(3) 整型变量与字符变量是否在任何情况下都可以互换?如:
char cOne, cTwo;
与
int cOne, cTwo;
是否无条件地等价?
答案:若整型变量的值在0~127的范围内,二者是等价的,否则就不是。