C Primer Plus(第六版)第五章编程题答案参考

当时没做第5题和第6题,什么时候有空记起来了再补吧(✽ ゚д゚ ✽)

5-1

#include <stdio.h>
#define MIN_SEC 60
int main(void) {
	int min;
	int hour;
	int re_min;
	printf("请输入时间(分钟)输入0退出程序:");
	scanf("%d", &min);
	while (min > 0) {
		hour = min / MIN_SEC;
		re_min = min % MIN_SEC;
		printf("%d分钟是%d小时%d分钟。\n", min, hour, re_min);
		printf("请输入时间(分钟)输入0退出程序:");
		scanf("%d", &min);
	}
	return 0;
}

5-2

#include <stdio.h>
int main(void) {
	int input;
	int counter = 0;
	printf("请输入一个整数:");
	scanf("%d", &input);
	while (counter++ < 11) {
		printf("%d\t", input);
		input++;
	}
	return 0;
}

5-3

#include <stdio.h>
int main(void) {
	int days;
	printf("请输入天数:");
	scanf("%d", &days);
	printf("%d days are %d weeks, %d days.\n", days, days / 7, days % 7);
	return 0;
}

5-4

//书中标答
#define FEET_TO_CM 30.48
#define INCH_TO_CM 2.54
int main(void) {
	float height;
	int feet;
	float inches;
	printf("Enter a height in centimeterts:\n");
	scanf("%f", &height);
	while (height > 0) {
		feet = height / FEET_TO_CM;
		inches = (height - feet * FEET_TO_CM) / INCH_TO_CM;
		printf("%.1f cm = %d feet, %.1f inches\n", height, feet, inches);
		printf("Enter a height in centimeterts(<=0 to quit):\n");
		scanf("%f", &height);
	}
	return 0;
}

5-7

#include <stdio.h>
double Lifang(double num);
int main(void) {
	double input;
	printf("请输入一个数:");
	scanf("%lf", &input);
	Lifang(input);
	return 0;
}
double Lifang(double num) {
	double num_3 = num * num * num;
	printf("%.2lf的立方为%.2lf\n", num, num_3);
	return num_3;
}

5-8

#include <stdio.h>
int main(void) {
	long first, second;
	printf("This program computes modull.\n");
	printf("Enter an integer to serve as the second operand:\n");
	scanf("%ld", &second);
	printf("Now enter the first operand:\n");
	scanf("%ld", &first);
	//printf("%ld %% %ld is %ld\n", first, second, first % second);
	while (first > 0) {
		printf("%ld %% %ld is %ld\n", first, second, (first % second));
		printf("Enter next number for first operand(<= 0 to quit):\n");
		scanf("%ld", &first);
	}
	printf("Done!\n");
	return 0;
}

5-9

#include <stdio.h>
double Temperatures(double tem);
int main(void) {
	double tem_hua;
	printf("请输入华氏温度:");
	int ret = scanf("%lf", &tem_hua);//若输入的是数字返回1,不是数字返回0
	while (ret == 1) {
		Temperatures(tem_hua);
		printf("请输入华氏温度(输入非数字退出程序):");
		ret = scanf("%lf", &tem_hua);
	}
	printf("Done!");
	return 0;
}
double Temperatures(double tem) {
	const float KAI = 273.16;
	const float SHE_1 = 5.0;
	const float SHE_2 = 9.0;
	const float SHE_3 = 32.0;
	double she, kai;
	she = SHE_1 / SHE_2 * (tem - SHE_3);
	kai = she + KAI;
	printf("%.2f华氏摄氏度等价于%.2f摄氏度,也等价于%.2f开氏度。\n", tem, she, kai);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值