现附上我对第五章习题的解答,如有创新,欢迎交流
//5.11.1
//5.11.1将分钟数转化为小时和分钟
# include <stdio.h>
# define H_PER_MIN 60
int main(void)
{
int hours;
int minutes;
int last;
printf("Please enter the minutes:\n");
scanf("%d", &minutes);
while (minutes > 0)
{
hours = minutes / H_PER_MIN;
last = minutes % H_PER_MIN;
printf("%d minutes is %d hours and %d minutes\n", minutes, hours, last);
printf("0 or less than 0 to quit\n");
scanf("%d", &minutes);
}
printf("Done!");
return 0;
}
//5.11.2
//5.11.2输入一个整数,打印该数从该数到比该数大10的所有整数,中间用制表符间隔开
# include <stdio.h>
int main(void)
{
int i;
int j;
printf("Please enter a integer:\n");
scanf("%d", &i);
j = i + 10;
while (j >= i)
{
printf("%d\t", i++);
}
return 0;
}
//5.11.3
//5.11.3输入天数,转换成周数和天数
# include <stdio.h>
# define W_PER_DAY 7
int main(void)
{
int days, weeks, last;
printf("Please enter the days:\n");
scanf("%d", &days);
while (days > 0)
{
weeks = days / W_PER_DAY;
last = days % W_PER_DAY;
printf("%d days is %d weeks, %d days.\n", days, weeks, last);
printf("0 or less than 0 to quit\n");
scanf("%d", &days);
}
printf("Done!");
return 0;
}
//5.11.4
/提示用户输入身高(cm),转换成英寸
# include <stdio.h>
# define CM_PER_FT 0.0328
# define Ft_PER_IN 11.999
int main(void)
{
float cm;
float feet; //为了计算小数点后的数
float inch;
printf("Enter a height in centimeters:");
scanf("%f", &cm);
while(cm > 0)
{
feet = cm * CM_PER_FT;
inch = (feet - (int)feet) * Ft_PER_IN; //强制转换
printf("%.1f cm = %d feet, %.1f inches", cm, (int)feet, inch); // 强制转换
printf("\nEnter a height in centimeters (<=0 to quit):");
scanf("%f", &cm);
}
printf("bye");
return 0;
}
//5.11.5
//5.11.5赚钱相加
# include <stdio.h>
int main(void)
{
int count = 0;
int sum = 0;
int days;
printf("How many days do you want to caculate:\n");
scanf("%d", &days);
while (count <= days)
{
sum = sum + count;
count++;
}
printf("The sum = %d\n", sum);
printf("Done!");
return 0;
}
//5.11.6
//5.11.6平方和相加
# include <stdio.h>
int main(void)
{
int n = 1;
int n2;
int days;
int count = 1;
int sum;
printf("Please enter the days:\n");
scanf("%d", &days);
while (count <= days)
{
n2 = n * n;
n++;
count++;
sum = sum + n2;
}
printf("The sum = %d\n", sum);
}
//5.11.7
//5.11.7计算立方值
# include <stdio.h>
void cubic(void);
int main(void)
{
printf("Please enter a double type number:\n");
cubic();
return 0;
}
void cubic(void)
{
double n;
scanf("%lf", &n);
printf("The cubic n = %lf", n * n *n);
}
//5.11.8
//5.11.8显示求模运算结果
# include <stdio.h>
int main(void)
{
int first;
int second;
int last;
printf("This program computes moduli.\n");
printf("Enter an interger to serve as the second operand:");
scanf("%d", &second);
printf("Now enter the first operand:");
scanf("%d", &first);
while (first > 0)
{
last = first % second;
printf("%d %% %d is %d\n", first, second, last);
printf("Enter next number for first opeerand (<= 0 to quit):");
scanf("%d", &first);
}
printf("Done");
return 0;
}
//5.11.9
//输入华氏温度,输出摄氏度,开氏温标
# include <stdio.h>
void Temperatures(double fa);
int main(void)
{
double temp;
printf("Please enter a Fahrenheit temperature:\n");
while (scanf("%lf", &temp) == 1)
{
Temperatures(temp);
printf("Enter again (or q to quit)\n");
}
printf("Done");
return 0;
}
void Temperatures(double fa)
{
const a = 32.0;
const b = 273.16;
double ce, ke;
ce = 5.0 / 9.0 * (fa - a);
ke = ce + b;
printf("FA_tem:%.2lf,CE_tem: %.2lf,KE_tem: %.2lf\n", fa, ce, ke);
return;
}
如有不足,欢迎指正