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

 1 .编写一个程序。将用分钟表示的时间转换成以小时和分钟表示的时间。使用 #define 或者 const 来创建一个代表 60 的符号常量。使用 while 循环来允许用户重复键入值,并且当键入一个小于等于 0 的时间时终止循环。

#include <stdio.h>
#define minutestohour 60;

int main(void)
{
	int minutes, mins, hours;
	
	printf("请输入需要转换的分钟数:");
	scanf("%d",& minutes);
	while (minutes > 0)
	{
		hours = minutes / minutestohour;
		mins = minutes % minutestohour;
		printf("%d minutes = %d hours %d mins.\n", minutes, hours, mins);
		printf("请输入下一个需要转换的值(输入0或负数退出):\n");
		scanf("%d",& minutes);
	}
	printf("感谢使用!");

	return 0;
}

2 .编写一个程序,此程序要求输入一整数,然后打印出从(包括)输入的值到(包括)比输入的值大 10 的所有整数值(也就是说,如果输入为 5 ,那么输出就从 5 到 15 )。要求在各个输出值之间用空格、制表符或换行符分开。

#include <stdio.h>

int main(void)
{
	int a,i;
	i = 0;
	printf("请输入一个整数:");
	scanf("%d",&a);

	while (i <= 10)
	{
		printf("%d\n", a + i);
		i++;
	}
	return 0;
}

3 .编写一个程序,该程序要求用户输入天数,然后将该值转换为周数和天数。例如,此程序将把 18 天转换成 2 周 4 天。用下面的格式显示结果:

#include <stdio.h>
#define daystoweek 7
int main(void)
{
	int days, week, day1;
	printf("Enter the number of days:\n");
	scanf("%d", &days);

	while (days > 0)
	{
		week = days / daystoweek;
		day1 = days % daystoweek;
		printf("%d days are %d weeks,%d days.\n", days, week, day1);
		printf("Continue enter the number of days(Enter 0 or a negative number to exit):\n");
		scanf("%d",& days);
	}
	printf("Exit!");
	return 0;
}

4 .编写一个程序让用户按厘米输入一个高度值,然后,程序按照厘米和英尺英寸显示这个高度值。允许厘米和英寸的值出现小数部分。程序允许用户继续输入,直到用户输入一个非正的数值。程序运行的示例如下面所示:

#include <stdio.h>
#define cmtoinch  0.3937008 //1厘米=0.3937008英寸
#define cmtofeet   0.032808 //1厘米=0.032808英尺
#define feettoinch 12       //1英尺= 12 英寸

int main(void)
{
	float height;              //输入身高
	int feet;                 //英尺
	float inch;               //英寸
	printf("Enter a height in centimeters:");
	scanf("%f\n", &height);
	while (height > 0)
	{
		feet = height * cmtofeet;
		inch = (height * cmtoinch) - (feettoinch * feet);
		printf("%.1f cm = %d feet, %.1f inches\n", height, feet, inch);
		printf("Enter a height in centimeters (<=0 to quit): ");
		scanf("%f", &height);
	}
	printf("bye");
	return 0;
}

 

 5. 改写用来找到前 20 个整数之和的程序 addemup.c (程序清单 5.13 )(如果您愿意,可以把 addemup.c 程序看成是一个计算如果您第一天得到 $l ,第二天得到 $2 ,第三天得到 $3 ,以此类推,您在 20 天里会挣多少钱的程序)。修改该程序,目的是您能交 q 地告诉程序计算将进行到哪里。也就是说,用一个读入的变量来代替 20 。

#include <stdio.h>
int main(void)
{
	int count, sum;
	int n;
	printf("Enter the number of days:\n");
	scanf("%d",&n);
	count = 0;
	sum = 0;
	while (count++ < n)
	{
		sum = sum + count;
	}
	printf("in past  %d days,you made $%d in all .", n, sum);//计算第n天拿到的钱数
	return 0;
}

6. 现在修改编程练习 5 中的程序,使它能够计算整数平方的和(如果您喜欢,可以这样认为:如果您第一天得到 $l ,第二天得到 $4 ,第三天得到 $9 ,以此类推您将得到多少钱。这看起来像一个很好的买卖)。 C 没有平方函数,但是您可以利用 n 的平方是 n*n 的事实。

#include <stdio.h>
int main(void)
{
	int count, sum;
	int square;
	int n;
	printf("Enter the number of days:\n");
	scanf("%d",&n);
	count = 0;
	sum = 0;
	while (count++ < n)
	{
		square = count * count;
		sum = sum + square;
	}
	printf("in past  %d days,you made $%d in all .", n, sum);//计算第n天拿到的钱数
	return 0;
}

7 .编写一个程序,该程序要求输入一个 float 型数并打印该数的立方值。使用您自己设计的函数来计算该值的立方并且将它的立方打印出来。 main() 程序把输入的值传递给该函数。

#include <stdio.h>

void cube(double a)
{
	long double c;
	c = a* a* a;
	printf("The cube is %lf.", c);
	
}
int main(void)
{
	double value;
	printf("Enter a double value:\n");
	scanf("%lf", &value);
	cube(value);
	return 0;
}

8.编写一个程序,显示求模运算的结果。

#include <stdio.h>

int main(void)
{
	int second;
	int first;
	printf("This program computes moduli.\n");
	printf("Enter the integer to serve as the second operand:");
	scanf("%d", &second);
	printf("Now enter the first operand:");
	scanf("%d", &first);
	while (first > 0)
	{
		int outcome;
		outcome = first % second;
		printf("%d %% %d is %d\n", first, second, outcome);
		printf("Enter next number for first operand(<= 0 to quit):");
		scanf("%d", &first);
	}
	printf("Done");
	return 0;
}

9 .编写一个程序,该程序要求用户输入一个华氏温度。程序以 double 类型读入温度值,并将它作为一个参数传递给用户提供的函数 Temperatures() 。该函数将计算相应的摄氏温度和绝对温度,并以小数点右边有两位数字的精度显示这三种温度。它应该用每个值所代表的温度刻度来标识这 3 个值。下面是将华氏温度转换成摄氏温度的方程:

  通常用在科学上的绝对温度的刻度是 0 代表绝对零,是可能温度的下界。下面是将摄氏温度转换为绝对温度的方程:

 Kelvin=Celsius+273.16

 Temperatures() 函数使用 const 来创建代表该转换里的 3 个常量的符号。 main() 函数将使用一个循环来允许用户重复地输入温度,当用户输入 q 或其他非数字值时,循环结束。

#include <stdio.h>
void Temperatures(double h);
void Temperatures(double h)            //h华氏,s摄氏,k开氏
{
	const double stoh = 5.0 / 9.0;
	const double ktos = 273.16;
	double s, k;
	s = stoh * (h - 32.0);
	k = s + ktos;

	printf("%.2lf℉ = %.2lf℃ = %.2lfK\n", h, s, k);
}
int main(void)
{
	double H;
	printf("Enter the Fahrenheit temperature:");
	
	while (scanf("%lf", &H)== 1)
	{
		Temperatures(H);
		printf("Please enter the Fahrenheit temperature(enter q to quit): \n");
		scanf("%lf", &H);
	}
	printf("Bye!");
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值