C Primer Plus第六版第五章编程练习答案

5.11编程练习

//Programming Exercise 1
/*把用分钟表示的时间转换成用小时和分钟表示的时间,
使用#define创建一个表示60的常量或const常量。
通过while loop让用户重复输入值,直到用户输入小于或等于0的值才停止循环
*/
#include <stdio.h>
#define MIN_P_HOUR 60	//一小时的分钟数
int main(void)
{	
	int sec;
	int min;
	int hour;

	printf("Enter the  time in minute (<=0 to quit): \n");
	scanf("%d", sec);
	while (sec > 0)
	{

	}
	hour = sec / MIN_P_HOUR;
	min = sec % MIN_P_HOUR;
	printf("the time equals to: %d hour %d minutes. \n");
	
}

//Programming Exercise 2
/*提示用户输入一个整数,然后打印从该数到比该数大10的所有整数
例如输入5,则打印5~15的所有整数,包括5和15,
要求打印的各值之间要有一个空格、制表符或换行符分开
*/
#include <stdio.h>
int main(void)
{
	int num;

	printf("Please enter a number: \n");
	scanf("%d", &num);
	while (num <= num+10)
	{
		printf("\t%d", num);
	}

	return 0;
}
//Programming Exercise 3
/*提示用户输入天数,然后将其转换成周数和天数
例如用户输入18,则转换成2周4天。以下面的格式显示结果
18 days are 2 weeks, 4 days.
通过while loop使用户重复输入天数
,直到用户输入一个非正值时,循环结束
*/
#include <stdio.h>
#define DAYS_P_WEEK 7	//一周的天数
int main(void)
{
	unsigned int days;
	unsigned int weeks;
	unsigned int left;

	printf("Please enter a number of days(<=0 to quit): \n");
	scanf("%d", &days);
	while (days > 0)
	{
		weeks = days / DAYS_P_WEEK;
		left = days % DAYS_P_WEEK;
		printf("%u days are %u weeks, %u days \n",
			days, weeks, left);
		 printf("Please enter a number of days(<=0 to qiut): \n");
		 scanf("%u", days);
	}
	
	return 0;
}
//Programming Exercise 4
/*提示用户输入一个身高(单位:厘米)
并分别以厘米和英寸为单位显示该值,允许有小数部分
程序应该能让用户重复输入身高,直到用户输入一个非正值。
其输出示例如下:
Enter a height in centimeters: 182
182.0 cm = 5 feet, 11.7 inches
Enter a height in centimeters (<=0 to quit): 168.7
168.0 cm = 5 feet, 6.4 inches
Enter a height in centimeters (<=0 to qiut): 0
bye
*/
#include <stdio.h>
#define INCH_P_CM 0.3937008	//一厘米是0.3937008英寸
#define FEET_P_CM 0.0328084	//一厘米是0.032808英尺
#define INCH_P_FEET 12	//一英尺(feet)是12英寸(inch)
int main(void)
{
	float height;
	int feet;
	float inch;
	
	printf("Enter a height in centimeters: ");
	scanf("%f", &height);
	while(height > 0)
	{
		feet = (int)(height * FEET_P_CM) ;
		inch = (cm * INCH_P_CM) - (feet * INCH_P_FEET);
		printf("%.1f cm = %d feet, %.1 inches \n");
		printf("Enter a height in centimeters (<=0 to quit): ");
		scanf("%f", &height);
	}
	printf("bye \n");

	return 0;
}
//Programming Exercise 5
/*
修改Listing5.13,使其可以和用户进行交互,根据用户输入的数进行计算
即,用一个读入的变量来代替20
*/
#include <stdio.h>
int main(void)
{	
	int count, sum;
	int end;	//存放用户输入的数据
	
	count = 0;
	sum = 0;
	printf("Enter a number to add for one to it: \n");
	scanf("%d", &end);
	while (count++ < end)
		sum = sum + count;
	printf("sum = %d \n", sum);

	return 0;
}
//Programming Exercise 6
/*
修改编程练习5的程序,使其能计算整数的平方和
(可以认为,第一天赚$1,第二天赚$4,第三天赚$9以此类推)
修改程序,使其可以跟用户进行交互,即根据用户的输入来计算
(即,用用户输入的第一个值来代替20)
*/
#include <stdio.h>
int main(void)
{
	int count;
	int sum;
	int end;

	count = 0;
	sum = 0;
	printf("Enter a number and i will return from\
1 to sum of all its cubes: \n");
	while (count++ < end)
	{
		sum = count * count * count + sum;
	}
	printf("sum of all its cube: %d \n", sum);

	return 0;
}
//Programming Exercise 7
/*
提示用户输入一个double类型的数据,并打印该数的立方
自己设计一个函数,并打印立方值
main函数要把用户输入的值传递给该函数
*/
#include <stdio.h>
int main(void)
{
	double num;
	double cube;
	
	printf("Please input a number and ");
	printf("I will give you its cube: \n");
	scanf("%lf", &num);
	cube = cube(num);
	printf("The cube is : %f: ", cube);

	return 0;
}
double cube(double num)
{
	return num * num * num;
}
//Programming Exercise 8
/*
编写一个程序,显示求模运算的结果
把用户输入的第一个整数作为求模运算符的第二个运算对象
该数在运算过程中保持不变。
后用户输入的数作为第一个运算对象。
当用户输入一个非正值时,循环结束
其输出示例如下:
This program computes moduli.
Enter an integer to serve as the second operand :  256
Now enter the first operand :  438
438 % 256 is 182
Enter next number for first operand (<= 0 to quit) : 
1234567 % 256 is 135
Enter next number for first operand (<= 0 to quit) :  0
Done
*/
#include <stdio.h>
int main(void)
{
	int first_op;
	int sec_op;
	int result ;
	
	printf("This program computes moduli. \n");
	printf("Enter an integer to serve as the second operand :  ");
	scanf("%d", &sec_op);
	printf("Now enter the first operand :  ");
	scanf("%d", &sec_op);
	while (first_op > 0)
	{
		result = first_op % sec_op;
		printf("%d %% %d is %d \n", first_op, sec_op, result);
		printf("Enter next number for first operand (<= 0 to quit) :  ");
		scanf("%d", &first_op);
	}

	return 0;
}
//Programming Exercise 9
/*要求用户输入一个华氏温度。程序读取double类型的值作为温度值
并把该值作为参数传递给一个用户自定义的函数Temperatures()函数。
该函数计算摄氏温度和开氏温度(卡尔文温标)
并以小数点后两位数字的精度显示3种温度。
要使用不同的温标来表示这三个值。
Temperatures()中使用const创建温度转换中使用的变量。
main()中使用循环让用户重复输入温度,当用户输入q或其他非数字时,循环结束
*/
#include <stdio.h>
int main()
{
	float fahrenheit;	//华氏温度
		
	while (scanf("%f", &fahrenheit) == 1)
	{
		Temperatures(fahrenheit);
	}

	return 0;
}
void Temperatures(float fahrenheit)
{
	float celsius;	//摄氏温度
	float kelvin;	//卡氏温度(卡尔文温标)
	const float left = 32.0;		//用来存储常量32.0
	const float add = 237.16;	//从来存储常量237.16

	celsius = 5.0 / 9.0 * (Fahrenheit - left)
	kelvin = fahrenheit + add;
	printf("Fahrenheit: %.2f  Celsius: %.2f Kelvin: %.2f", fahrenheit, celsius, kelvin);
}

猜你喜欢

转载自blog.csdn.net/weixin_42912350/article/details/82808159