1. 程式人生 > >C Primer Plus 6 第五章程式設計練習

C Primer Plus 6 第五章程式設計練習

1.
#include<stdio.h>
#define M 60	//一小時等於60分鐘

int main(void)
{
	int hour;	//小時格式
	int min;	//分鐘格式
	int a;	//小時分鐘格式中的分鐘

	printf("Please enter a time(min):");
	scanf("%d", &min);
	while(min>0)
	{
		hour = min / M;	//小時
		a = min % M;	//分鐘
		printf("%d min is %d hour %d min.\n", min, hour, a);
		printf("Do you want to exit, enter a time(time<=0).\n");
		scanf("%d", &min);
	}
	printf("Done!\n");
	return 0;
}

2.

//列印包括輸入數在內的比輸入數大10的所有整數
#include<stdio.h>
int main(void)
{
	int a;	//使用者輸入數
	int i = 0;	//計數器
	printf("Enter a number(int):");
	scanf("%d", &a);
	printf("%d", a);	//原數

	while(++i<=10)	//列印10次
	{
		a++;
		printf(" %d", a);	//各數間空一格
	}
	printf("\n");	//最後一個數列印之後換行
	return 0;
}

3.

//將天數以"XX周XX天"的格式顯示
#include<stdio.h>
#define M 7	//一週=7天

int main(void)
{
	int day;
	int week;
	int left;

	printf("Enter a day number:");
	scanf("%d", &day);

	while(day>0)
	{
		week = day / M;	//周
		left = day % M;	//天
		printf("%d is %d week, %d day.\n", day, week, left);
		printf("you can continue(enter 0 to quit):");
		scanf("%d", &day);
	}
	printf("Done!\n");
	return 0;
}

4.

//將XX釐米用"XX英寸XX英尺"的格式顯示
#include<stdio.h>
#define M 30.48	//一英尺=30.48釐米
#define N 2.54	//一英寸=2.54釐米

int main(void)
{
	float cm;
	int feet;	//利用int型進行截斷
	float inch;
	float left;	//階段部分轉換成英寸

	printf("Enter your height in centimeters:");
	scanf("%f", &cm);

	while(cm>0)
	{
		feet = cm / M;
		left = cm - (feet * M);
		inch = left / N;
		printf("%.1f cm = %d feet, %.1f inchs\n", cm, feet, inch);
		printf("Enter your height in cmtimeters(<=0 to quit):");
		scanf("%f", &cm);  
	}
	printf("Done!\n");
	return 0;
}

5.

#include <stdio.h>
int main(void)
{
	int count, sum;
	int i;

	count = 0;
	sum = 0;
	printf("Enter the number of your workdays:");
	scanf("%d", &i);

	while(count++<i)
	{
		sum = sum + count;
	}
	printf("sum = %d\n", sum);

	return 0;
}

6.

//計算整數的平方
#include<stdio.h>
int main(void)
{
	int m;
	int n;
	int i;

	m = 0;
	n = 0;

	printf("Enter a number:");
	scanf("%d", &i);

	while(m++<i)
	{
		n = m * m;
		printf("%d:\t%d\n", m, n);
	}
	return 0;
}

7.

//設計一個計算並列印浮點數立方值的函式
#include<stdio.h>
void three(double n);	//函式原型

int main(void)
{
	double m;

	printf("Enter a number(double):");
	scanf("%lf", &m);	//lf(小寫)轉換說明表明讀入double型別的值,Lf則是long double型別!
	three(m);	//函式呼叫(實參)

	return 0;
}
void three(double n)	//形參
{
	printf("%.2lf:\t%.2lf\n", n, n*n*n);
}

8.

//求模運算
#include<stdio.h>
int main(void)
{
	const int b;	//運算物件2
	int a;	//運算物件1

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

9.

//華氏溫度轉攝氏溫度和開氏溫度
#include<stdio.h>
void Temperatures(double x);	//函式原型
int main(void)
{
	double m;	//華氏溫度值

	printf("輸入一個華氏溫度值:");
	while(scanf("%lf", &m)==1)	//scanf()函式返回讀取資料的數量,若讀取數字則返回1,讀取字元則不返回1
	{
		Temperatures(m);
		printf("輸入一個華氏溫度:");
		scanf("%lf", &m);
	}
	printf("Done!\n");

	return 0;
}
//函式功能:將華氏溫度轉換為攝氏溫度和開氏溫度並列印三個值
void Temperatures(double x)
{
	const double a = 5.0;
	const double b = 9.0;
	const double c = 32.0;
	const double d = 273.16;
	double n;	//攝氏溫度
	double k;	//開氏溫度

	n = a / b * (x - c);	//計算攝氏溫度
	k = n + d;	//計算開氏溫度

	printf("華氏溫度\t攝氏溫度\t開氏溫度\n");
	printf("%-15.2lf\t%-15.2lf\t%-15.2lf\n", x, n, k);
}