1. 程式人生 > >C Primer Plus(第6版)第五章程式設計練習答案

C Primer Plus(第6版)第五章程式設計練習答案

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("Minute aka: %d hour %d minutes. \n",
        		 hour, min);
        		scanf("%d", &sec);	
	}

	return 0;
}

//Programming Exercise 2
/*提示使用者輸入一個整數,然後列印從該數到比該數大10的所有整數
例如輸入5,則列印5~15的所有整數,包括5和15,
要求列印的各值之間要有一個空格、製表符或換行符分開
*/
#include <stdio.h>
int main(void)
{
	int num;
	int count = 0;
	
	printf("Please enter a number: \n");
	scanf("%d", &num);
	while (count++ <= 10)
	{
		printf("%5d", num);
		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 = (height * INCH_P_CM) - (feet * INCH_P_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 \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");
	scanf("%d", &end);
	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>
double do_cube(double num);	//不要忘記函式原型
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 = do_cube(num);
	printf("The cube is : %f: ", cube);

	return 0;
}
double do_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", &first_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;	//華氏溫度
	
	printf("Please input a temperature in fahrenheit(enter q to quit): \n");	
	while (scanf("%f", &fahrenheit) == 1)
	{
		Temperatures(fahrenheit);
		 printf("Please input a temperature in fahrenheit(enter q to quit): \n");
	}
	printf("Bye Bye ! \n");

	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 \n", fahrenheit, celsius, kelvin);
}