java作业第四章课后答案,课后习题第四章

1.编写一个程序,提示用户输入名和姓,然后以“名,姓”的格式打印

#include

int main(void){

char name[40];

char surname[40];

printf("Please input your first name:");

scanf("%s",name);

printf("Please input your last name:");

scanf("%s",surname);

printf("Hello,%s,%s.",name,surname);

return 0;

}

67f818b1268b5a0be0ebfcbeace5274c.png

2.编写一个程序,提示用户输入名字,并执行以下操作:

a.打印名字,包括双引号;

b.在宽度为20的字段右端打印名字,并包括双引号;

c.在宽度为20的字段左端打印名字,并包括双引号;

d.在比姓名宽度长3的字段中打印名字。

#include

#include

int main(void){

char name[40];

int width;

printf("Please input your first name:");

scanf("%s",name);

width = printf("\"%s\"\n.",name);

width -= 4;

printf("\"%20s\".\n",name);

printf("\"%-20s\".\n",name);

printf("\"%*s\".",(width=3),name);

system("pause");

return 0;

}

89419dfe7899f7b60e9d73ee783f18eb.png

3.编写一个沉痼,读取一个浮点数,首先以小数点计数法打印,然后以指数计数法打印,用下面的格式输出(系统不同,指数计数法显示的位数可能不同):

a. The input is 21.3 or 2.1e+001;

b. The input is +21.290 or 2.129E+001;

#include

#include

int main(void){

float input;

printf("Enter a float number:");

scanf("%f",&input);

printf("The input is %.If or %.1e\n",input,input);

printf("The input is %f or %e \n",input,input);

system("pause");

return 0;

}

b23eac9be13918865faf878bee21605d.png

4.编写一个程序,提示用户输入身高(以英寸为单位)和姓名,然后以下面的格式显示用户刚输入的信息。

Dabney, you are 6.208 feet tall.

使用float类型,并用“/”作为除号。如果你愿意,客人已要求用户以厘米为单位输入身高,并以米为单位显示出来。

#include

#include

int main(void){

float heigh;

char name[40];

printf("Enter your number:");

scanf("%s",name);

printf("Hi %s,how tall you are( inch ):",name);

scanf("%f",&heigh);

printf("%s,you are %.3f feet tall \n",name,heigh/12.0);

system("pause");

return 0;

}

a9bd0e4bd6133577fff8018cfbdc9a3b.png

5.编写一个程序,提示用户输入以兆位每日秒(Mbit/s)为单位的下载速度和以兆字节(MB)为单位的文件大小。程序中应计算文件下载时间。注意,这里的1字节等于8位。使用float类型,并用“/”作为出号。该程序要以下面的格式打印3个变量(下载速度,文件大小和下载时间)的值,显示小数点后两位数字。

At 18.12 megabits per second, a file of 2.20 megabytes download in 0,97 seconds.

#include

#include

int main(void){

float speed,size,time;

printf("Pleast input the net speed(megabits per second):");

scanf("%f",&speed);

printf("Pleast input the file size(megabyte):");

scanf("%f",&size);

time=size*8/speed;

printf("At %.2f megabits per second, a filr of %.2f megabytes download in %.2f seconds.",speed,size,time);;

system("pause");

return 0;

}

1ab46ec0181b0615d794ddd39c15ff5c.png

7.编写一个程序,将一个double类型的变量设置为1.0/3.0,一个 float 类型的变量设置为1.0/3.0 分别显示两次计算的结果各3次:一次显示小数点后面 6位数字:一次显示小数点后面 12位数字;-次显示小数点后面16位数字。程序中要包含float.h头文件,并显示FLT DIG和DBL DIG的值。1.0/3.0的值与这些值一致吗?

#include

#include

#include

int main(void)

{

double d_third = 1.0/3.0;

float f_third = 1.0/3.0;

printf("float of one third(6) = %.6f\n",f_third);

printf("float of one third(12) = %.12f\n",f_third);

printf("float of one third(16) = %.16f\n",f_third);

printf("double of one third(6) = %.6f\n",d_third);

printf("double of one third(12) = %.12f\n",d_third);

printf("double of one third(16) = %.16f\n",d_third);

printf("FLT_DIG in float.h is %d\n",FLT_DIG);

printf("DBL_DIG in float.h is %d\n",DBL_DIG);

system("pause");

return 0;

}

d567b947ffb87dc77e7a622c34b3f46e.png

8. 编写一个程序,提示用户输入旅行的里程和消耗的汽油量。然后计算并显示消耗每加仑汽油行驶的英里数,显示小数点后面一位数字。接下来,使用1加仑大约3.785升,1 英里大约为1.609千米,把单位是英里/加仑的值转换为升/100公里(欧洲通用的燃料消耗表示法),并显示结果,显示小数点后面1位数字。注意,美国采用的方案测量消耗单位燃料的行程(值越大越好),而欧洲则采用单位距离消耗的燃料测量方案(值越低越好)。使用#define 创建符号常量或使用const 限定符创建变量来表示两个转换系数。

#include

#include

#define GALLON_TO_LITRE 3.785

#define MILE_TO_KM 1.609

int main(void)

{

float range,oil;

printf("Pleast input the range you traveled(in mile):");

scanf("%f",&range);

printf("Pleast input the oil you spend(in gallon):");

scanf("%f",&oil);

printf("In USA, your oil wear is %.1f M/G\n",range/oil);

printf("In Europe, your oil wear is %.1fL/100KM",(oil*GALLON_TO_LITRE)/(range*MILE_TO_KM));

system("pause");

return 0;

}

bd9e69c6bc6d9de25447842f395605bb.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值