第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;
}