C Primer Plus第六版第五章運算符,表達式,語句課後題



//5.1
#include<stdio.h>
#define MIN 60
int main(void)
{
	int min, hour, sec;
	
	printf("Please enter minutes.\n");
	scanf("%d",&min);
	while (min > 0)
	{
		hour = min / MIN;
		sec = min % MIN;
		if(hour > 1)
			printf("%d minutes is %d hours and %d seconds.\n",min, hour, sec);
		else
			printf("%d minutes is %d hour and %d seconds.\n",min, hour, sec);
		
		scanf("%d",&min);		
	}
	
	return 0;
} 

 

 


//5.2
#include<stdio.h>
int main(void)
{
	int n,x;

	printf("Please enter a integer.");
	scanf("%d",&x);
	n=x+10;
	while(x<=n)
    {
        printf("%d\n",x);
        x++;
    }
	return 0;
}
//5.3
#include<stdio.h>
#define WEEK 7
int main(void)
{
	int first_days, last_days, weeks;
	
	printf("How many days?\n");
	scanf("%d",&first_days);
	
	while(first_days > 0)
		{
			weeks = first_days/WEEK;
			last_days = first_days%WEEK;
			printf("%d days are %d weeks, %d days.\n",first_days, weeks, last_days);
			scanf("%d",&first_days);
		}
		
	return 0;
}
//5.4
#include<stdio.h>
#define CM_PER_FEET 30.48
#define CM_PER_INCHES 2.54
int main(void)
{
	float height_cm;
	int height_feet;
	float height_inches;
	
	printf("Enter a height in centimeters: ");
	scanf("%f",&height_cm);
	while(height_cm > 0)
		{
			height_feet = (int)(height_cm/CM_PER_FEET);
			height_inches = (height_cm - height_feet*CM_PER_FEET) / CM_PER_INCHES;
			printf("%.1f cm = %d feet, %.1f inches\n",height_cm, height_feet, height_inches);
			printf("Enter a height in centimeters (<=0 to quit): ");
			scanf("%f",&height_cm); 
		}
		printf("bye");
		
		return 0;
}
//5.5
#include<stdio.h>                                 
int main(void)
{
	int count, sum, n;
	sum = 0;
	count = 0;
	
	printf("How many days dou you want to work?");
	scanf("%d",&n);
	while(count++ < n)
		{
			sum = sum + count;
		}
	printf("sum = %d\n",sum);
	
	return 0;
} 

 

//5.6

#include<stdio.h>                                 
int main(void)
{
	int count, sum, n;
	sum = 0;
	count = 0;
	
	printf("How many days dou you want to work?");
	scanf("%d",&n);
	while(count++ < n)
		sum = sum + count*count;
	
	printf("sum = %d\n",sum);
	
	return 0;
}

 

//5.7
#include<stdio.h> 
void cubic(void);
int main(void)
{	
	printf("Enter a number in double type.\n");
	cubic();
	
	return 0;
}
void cubic(void)
{
	double number;
	scanf("%lf",&number);
	printf("The cube of this number is %f",number*number*number);
} 

 

//5.8
#include<stdio.h> 
int main(void)
{
	int second_operand = 0;
	int first_operand = 0;
	
	printf("This program computers moduli.\n");
	printf("Enter an integer to serve as the second operand: \n");
	scanf("%d",&second_operand);
	printf("Now enter the first operand: \n");
	scanf("%d",&first_operand);
	printf("%d %% %d is %d\n",first_operand,second_operand,first_operand%second_operand);
	printf("Enter next number for first operand (<= 0 to quit): \n");
	scanf("%d",&first_operand);
	while (first_operand > 0)
		{
			printf("%d %% %d is
%d\n",first_operand,second_operand,first_operand%second_operand);
			printf("Enter next number for first operand (<= 0 to quit): \n");
			scanf("%d",&first_operand);
		}
		
	printf("Done\n");
}

 

//5.9
#include<stdio.h>
void Temperature(double Fahrenheit_tem);
int main(void)
{
	double Fahrenheit_tem = 0.0;
	
	printf("Please enter the Fahrenheit temperature: \n");
	while(scanf("%lf",&Fahrenheit_tem) == 1)
	{
		Temperature(Fahrenheit_tem);
		printf("Please enter the Fahrenheit temperature(enter q to quit): \n");
	}
	
	return 0;
}
void Temperature(double Fahrenheit_tem)
{
	const double FAHRENHEIT_VAL = 32.0;
	const double KELLOGGS_VAL = 273.16;
	const double CELSIUS_TEM = 5.0/9.0*(Fahrenheit_tem - FAHRENHEIT_VAL);
	const double KELLOGGS_TEM = CELSIUS_TEM + KELLOGGS_VAL;

	printf("Celsius: %.2lf  Fahrenheit:%.2lf  Kellogg's:%.2lf\n",CELSIUS_TEM , Fahrenheit_tem, KELLOGGS_TEM);
	
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章