《C Primer Plus》第五章编程题

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

#include <stdio.h>
#define CYCLE 60

int main(void)
{
	int minute;
	printf("Please enter the minutes you want to change(enter q to quit):\n");
	while((scanf("%d",&minute))==1)
	{
		printf("The minutes is %2d hours and %2d minutes.\n",minute/CYCLE,minute%CYCLE);
		printf("Enter another minutes to continue(q to quit):\n");
	}
	return 0;
}

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

#include <stdio.h>
 
int main(void)
{
	int num,temp;
	printf("Please enter an integer:\n");
	scanf("%d",&num);
	temp = num;
	do{
		printf("%d\t",num);
	}while (++num<=temp+10);
	return 0;
}

3、编写一个程序,该程序要求用户输入天数,然后将该值转换为周数和天数。

#include <stdio.h>
#define WEEK 7
int main(void)
{
	int days=0;
	printf("Please enter the number of days(<0 to quit):\n");
	scanf("%d",&days);
	while(days>0){
		printf("The number of days are %d weeks,%d days.\n",days/WEEK,days%WEEK);
		days = 0;
		printf("Another days to change:\n");
		scanf("%d",&days);
	}
	return 0;
}

4、改写用来找到20个数之和的程序,用一个变量来代替20.

#include <stdio.h>

int main(void)
{
	int i = 0;
	int num = 0;
	int sum = 0;
	printf("Please enter a number of days:\n");
	scanf("%d",&num);
	while(i++<num){
		sum = sum + i;
	}
	printf("The sum is %d.\n",sum);
	return 0;
}
5、修改4的程序。使之显示平方的和。

#include <stdio.h>

int main(void)
{
	int i = 0;
	int num = 0;
	int sum = 0;
	printf("Please enter a number of days:\n");
	scanf("%d",&num);
	while(i++<num){
		sum = sum + i*i;
	}
	printf("The sum is %d.\n",sum);
	return 0;
}


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

#include <stdio.h>
double cube(float n);

int main(void)
{
	float n;
	double result;
	printf("Please enter a float:\n");
	scanf("%f",&n);
    result = cube(n);
	printf("n's cube is :%.2lf.\n",result);
	return 0;
}
double cube(float n){
	return n*n*n;
}

8、编写一个程序,该程序要求用户输入一个华氏温度。程序以double类型读入温度值,并将它作为一个参数传递给用户提供的函数Temperatures()。该函数将计算相应的摄氏温度和绝对温度。下面是将华氏温度转换成摄氏温度的方程:

Celsius = 1.8*Fahrenheit +32.0

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

Kelvin = Celsius +273.16

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

#include <stdio.h>
void Temperatures(double Farhrenheit);

int main(void)
{
	double Farhrenheit;
	printf("Please enter a Farhrenheit(<0 to quit):\n");
	scanf("%lf",&Farhrenheit);
	while(Farhrenheit>0){
		Temperatures(Farhrenheit);
		Farhrenheit = 0;
		printf("Please enter another Farhrenheit(<0 to quit):\n");
		scanf("%lf",&Farhrenheit);
	}
	printf("Thank you for using.");
	return 0;
}

void Temperatures(double Farthenheit){
	double Celsius,Kelvin;
	Celsius = 1.8 * Farthenheit + 32.0;
	printf("The Celsius is %.2lf.\n",Celsius);
	Kelvin = Celsius+273.16;
	printf("The Kelvin is %.2lf.\n",Kelvin);
}

未用CONST定义常量。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值