C Primer Plus 第6版 第五章 编程练习参考答案
编译环境VS Code+WSL GCC 源码在文末下载
/*第1题*************************/
#include<stdio.h>
#define MIN_TO_HOUR 60
int main(void)
{
int user_input_min = 0,user_output_min = 0,user_output_hour = 0;
printf("本程序将输入的时间(分钟)用小时和分表示\n");
printf("请输入需要转换的时间(分):");
scanf("%d",&user_input_min);
while (user_input_min > 0)
{
user_output_hour = user_input_min / MIN_TO_HOUR;
user_output_min = user_input_min % MIN_TO_HOUR;
printf("你输入的时间等于:%d小时%d分\n",user_output_hour,user_output_min);
//getchar();
printf("输入小于等于0的数字退出本程序\n");
printf("请输入需要转换的时间(分):");
scanf("%d",&user_input_min);
}
getchar();
return 0;
}
/*第2题*************************/
#include<stdio.h>
int main(void)
{
int user_input_num = 0,user_output_num = 0;
printf("本程序将在您输入一个数字后连续打印从该数字到大10的所有整数\n");
printf("请输入数字:");
scanf("%d",&user_input_num);
user_output_num = user_input_num;
printf("%d",user_output_num);
while (user_output_num++ < user_input_num + 10)
{
printf(" %d",user_output_num);
}
printf("\n");
getchar();
return 0;
}
/*第3题*************************/
#include<stdio.h>
int main(void)
{
int input_days,output_days,output_weeks;
printf("Input days format days & weeks:");
scanf("%d",&input_days);
output_days = input_days % 7;
output_weeks= input_days / 7;
printf("%d days are %d weeks, %d days.\n",input_days,output_weeks,output_days);
getchar();
return 0;
}
/*第4题*************************/
#include<stdio.h>
//#define CM_TO_INCH (1.0/2.54)
int main(void)
{
double input_cm,output_inch;
printf("Enter a height in centimeters:");
scanf("%lf",&input_cm);
printf("%f",input_cm);
while (input_cm > 0)
{
output_inch = input_cm / 2.54;
printf("%.1f CM = %.1f inchs\n",input_cm,output_inch);
printf("Enter a height in centimeters(<= 0 to quit):");
scanf("%Lf",&input_cm);
}
printf("bye\n");
getchar();
return 0;
}
/*第5题*************************/
/* addemup.c -- five kinds of statements */
#include <stdio.h>
int main(void) /* finds sum of first 20 integers */
{
int count, sum,days; /* declaration statement */
count = 0; /* assignment statement */
sum = 0; /* ditto */
days = 0;
printf("How many days:");
scanf("%d",&days);
while (count++ < days) /* while */
sum = sum + count; /* statement */
printf("sum = $%d\n", sum);/* function statement */
return 0; /* return statement */
}
/*第6题*************************/
/* addemup.c -- five kinds of statements */
#include <stdio.h>
int main(void) /* finds sum of first 20 integers */
{
int count, sum,days; /* declaration statement */
count = 0; /* assignment statement */
sum = 0; /* ditto */
days = 0;
printf("How many days:");
scanf("%d",&days);
while (count++ < days) /* while */
sum = sum + count * count; /* statement */
printf("sum = $%d\n", sum);/* function statement */
return 0; /* return statement */
}
/*第7题*************************/
#include<stdio.h>
double cube(double n);
int main(void)
{
double num;
printf("Input a number:");
scanf("%lf",&num);
num = cube(num);
printf("The Number^3 is:%f\n",num);
getchar();
return 0;
}
double cube(double n)
{
return n*n*n;
}
/*第8题*************************/
#include<stdio.h>
int main(void)
{
int input_num,moduli_num,resoult;
printf("This program computes moduli.\n");
printf("Enter an integer to save as the second operand:");
scanf("%d",&moduli_num);
printf("Now enter the first operand:");
scanf("%d",&input_num);
resoult = input_num % moduli_num;
while (input_num > 0)
{
printf("%d %% %d is %d\n",input_num,moduli_num,resoult);
if(0 == (moduli_num = resoult))/*当模=0时,下一次计算必定不能通过,直接退出循环*/
break;
printf("Enter next number for first operand (<=0 to quite):");
scanf("%d",&input_num);
resoult = input_num % moduli_num;
}
printf("Done\n");
getchar();
return 0;
}
/*第9题*************************/
/*输入dauble华摄氏度,并传入Temperatures()
Temperatures()计算并显示华摄氏度、摄氏度、开氏温度,精确到小数点2位
Temperatures()中用const创建转换中使用的变量(转换比例
main()函数中创建一个循环让用户重复输入,直到输入'q'或其他非数字)*/
#include<stdio.h>
void t_format(double hua_t);
int main(void)
{
double inpt_val;
printf("请输入需要转换的温度(华摄氏度):");
scanf("%lf",&inpt_val);
while (1)
{
t_format(inpt_val);
printf("输入'q' 以及非数字退出程序\n");
printf("请输入需要转换的温度(华摄氏度):");
if(scanf("%lf",&inpt_val) == 0)
break;
}
printf("程序退出……\n");
getchar();
return 0;
}
void t_format(double hua_t)
{
const double kai_t_val = 273.16,
she_t_val = 5.0 / 9.0;
double she_t,kai_t;
printf("华摄氏度: %.2f\n",hua_t);
printf("摄氏度: %.2f\n",she_t = she_t_val * (hua_t - 32.0));
printf("开氏温度: %.2f\n",kai_t = she_t + 273.16);
}