天天看點

C Primier Plus(第六版)第5章 程式設計練習 VS2019測試

第1題

/*程式設計練習5.1把分鐘轉化為小時和分鐘*/
#include<stdio.h>
const int M_PER_H = 60;  //每小時60分鐘

int main(void)
{
	int time,min, hour;

	printf("Please input the time in minutes(<=0 to quit):\n");
	scanf_s("%d", &time);
	while (time > 0) {
		hour = time / M_PER_H;
		min = time % M_PER_H;
		printf("The time is %d hours and %d minutes.\n", hour, min);
		printf("Please input the time in minutes(<=0 to quit):\n");
		scanf_s("%d", &time);
	}
	printf("Done!\n");

	return 0;
}
           

第2題

/*程式設計練習5.2輸入一個整數,列印該數及大10的數*/
#include<stdio.h>

int main(void)
{
	int input, max;

	printf("Print continue 10 numbers.\n");
	printf("Please input the start number:\n");
	scanf_s("%d", &input);
	max = input + 10; //列印上限
	while (input <= max) {
		printf("%d\t", input);
		input++;
	}
	printf("\nDone!");

	return 0;
}
           

第3題

/*程式設計練習5.3輸入天數,轉換為周和天*/
#include<stdio.h>
#define D_PER_W  7

int main(void)
{
	int input, days, weeks;

	printf("CONVERT DAYS TO WEEKS(<=0 TO QUIT)\n");
	printf("Please input the number of days:\n");
	scanf_s("%d", &input);
	while (input > 0) {
		weeks = input / D_PER_W;
		days = input % D_PER_W;
		printf("%d days are %d weeks, %d days.\n", input, weeks, days);
		printf("Please input the number of days again.\n");
		scanf_s("%d", &input);
	}
	printf("DONE!\n");

	return 0;
}
           

第4題

/*程式設計練習5.4輸入身高,并分别勇英尺英寸為機關現實*/
#include<stdio.h>
const float CM_PER_INCH = 2.54;		//每英寸2.54厘米
const float CM_PER_FEET = 30.48;	//每英尺30.48厘米

int main(void)
{
	float height, inch;
	int feet;

	printf("CONVERT CENTIMETERS TO INCHES AND FEET\n");
	printf("Please enter a height in centimeters(<=0 TO QUIT):\n");
	scanf_s("%f", &height);
	while (height > 0) {
		feet = height / CM_PER_FEET;
		inch = (height - feet * CM_PER_FEET) / CM_PER_INCH;
		printf("%.1f cm = %d feet, %.1f inches\n", height, feet, inch);
		printf("Please enter a height in centimeters(<=0 TO QUIT):\n");
		scanf_s("%f", &height);
	}
	printf("DONE!\n");

	return 0;
}
           

第5題

/*程式設計練習5.5修改程式清單5.13,使用者輸入一個數,并計算從1到該數的和*/
#include<stdio.h>

int main(void)
{
	int count, i = 0, sum = 0;	//i為計算個數

	printf("SUM FROM 1 TO COUNT\n");
	printf("Please input the count:\n");
	scanf_s("%d", &count);
	while (i <= count) {
		sum = sum + i;
		i++;
	}
	printf("sum = %d\n", sum);

	return 0;
}
           

第6題

/*程式設計練習5.6計算平方和*/
#include<stdio.h>

int main(void)
{
	int count, sum=0;

	printf("Please input the count:\n");
	scanf_s("%d", &count);
	while (count > 0) {
		sum = sum + count * count;
		count--;		
	}
	printf("sum = %d\n", sum);

	return 0;
}
           

第7題

/*程式設計練習5.7計算立方值*/
#include<stdio.h>
double cube(double n);

int main(void)
{
	double input;

	printf("Please input a number.\n");
	scanf_s("%lf", &input);
	cube(input);

	return 0;
}

double cube(double n)
{
	printf("The cube of %.2lf is %.2lf\n", n, n*n*n);
}
           

第8題

/* 程式設計練習5.8求模運算*/
#include<stdio.h>

int main(void) 
{
	int sec_oper, first_oper;

	printf("This program computes moduli.\n");
	printf("Enter an integer to server as the second operand:\n");
	scanf_s("%d", &sec_oper);
	printf("Now enter the first operand:\n");
	scanf_s("%d", &first_oper);
	while (first_oper > 0) {
		printf("%d %% %d is %d\n", first_oper, sec_oper, first_oper % sec_oper);
		printf("Enter next number for first operand(<=0 to quit):\n");
		scanf_s("%d", &first_oper);
	}
	printf("Done\n");

	return 0;
}
           

第9題

/*程式設計練習5.9溫度機關華氏攝氏開氏轉換*/
#include<stdio.h>

int Temperatures(double n);
int main(void)
{
	double fah;

	printf("CONVERT FAHRENHEIT TO CELSIUS AND KELVIN\n");
	printf("Please input a fahrenheit to start:\n");
	while (scanf_s("%lf",&fah) == 1) {	
		//用double類型,scanf語句參數要用雙精度lf
		Temperatures(fah);
		printf("Please input the temperatures in fahrenheit(q to quit):\n");
		}
	printf("Bye!");

	return 0;
}

int Temperatures(double n)
{
	double cel, kel;
	const double CEL_TO_KEL = 273.16;
	cel = 5.0 / 9.0 * (n - 32.0);
	kel = cel + CEL_TO_KEL;
	printf("%.2f farenheit equals %.2f celsius, or %.2f kelvin .\n", n, cel, kel);

	return 0;
}
           

繼續閱讀