5.11的习题答案,自己写的:)
环境: Windows、Dev-C++ 5.11、TDM-GCC 4.9.2。
1.
//5.11.1
#include
#define SIXTY 60
int main(void)
{
int minute;
printf("请输入分钟数,按0退出:\n");
while((scanf("%d",&minute) == 1) && minute > 0)
{
getchar() ;
printf("您输入的是%d分钟,",minute);
printf("转换后是 %d 小时 %.1d 分钟。\n",minute/SIXTY, minute%SIXTY);
printf("\n请输入分钟数,按0退出:\n");
}
printf("谢谢使用:)");
getchar();
return 0;
}
2.
//5.11.2
#include
int main(void)
{
int in_value,temp;
printf("请输入一个整数:\n");
scanf("%d",&in_value);
temp = in_value+10;
while(in_value <= temp)
printf("%d ",in_value++);
return 0;
}
3.
//5.11.3
#include
int main(void)
{
int days;
printf("Please enter days(0 to quit):\n");
while((scanf("%d",&days) == 1) && days > 0)
{
getchar();
printf("%d days are %d weeks, %d days.\n",days,days/7,days%7);
printf("\nPlease input days(enter 0 exit):\n");
}
printf("Thanks you for using:)");
getchar() ;
return 0;
}
4.
//5.11.4
//in = ft*12 = cm*0.3937
#include
int main(void)
{
float height;
printf("Enter a height in centimeters:");
while((scanf("%f",&height) == 1) && height > 0)
{
getchar();
printf("%.1f cm = %d feet, %.1f inches\n",height,(int)(height*0.3937/12),((height*0.3937/12)-(int)(height*0.3937/12))*12);
printf("Enter a height in centimeters (<=0 to quit):");
}
printf("bye");
getchar();
return 0;
}
//上面那一长溜的printf应该不好,应该多定义个变量,比如ft_height,开始的height改为cm_height。
5.
//5.11.5
/* addemup.c -- five kinds of statements */
#include
int main(void) /* finds sum of first 20 integers */
{
int count, sum; /* declaration statement */
int days;
printf("Please enter working days:");
scanf("%d",&days);
getchar();
count = 0; /* assignment statement */
sum = 0; /* ditto */
while (count++ < days) /* while */
sum = sum + count; /* statement */
printf("sum = %d\n", sum);/* function statement */
return 0; /* return statement */
}
//这里就是在复制粘贴原文的基础上稍稍改动:)
6.
//5.11.6
/* addemup.c -- five kinds of statements */
#include
int main(void) /* finds sum of first 20 integers */
{
int count, sum; /* declaration statement */
int days;
printf("Please enter working days:");
scanf("%d",&days);
getchar();
count = 0; /* assignment statement */
sum = 0; /* ditto */
while (count++ < days) /* while */
sum = sum + count*count; /* statement */
printf("sum = %d\n", sum);/* function statement */
return 0; /* return statement */
}
7.
//5.11.7
#include
void cubic_value(double);
int main(void)
{
double input;
printf("请输入一个双精度浮点值:\n");
scanf("%lf",&input);
cubic_value(input);
return 0;
}
void cubic_value(double x)
{
printf("该数的立方值是%.2lf.",x*x*x);
}
8.
//5.11.8
#include
int main(void)
{
int f_operand,s_operand;
printf("This program computs moduli.\n");
printf("Enter an integer to serve as the second operand:");
scanf("%d",&s_operand);
printf("Now enter the first operand:");
while((scanf("%d",&f_operand) == 1) && f_operand >0 )
{
printf("%d %% %d is %d\n",f_operand,s_operand,f_operand%s_operand);
printf("Enter next number for first operand(<=0 to quit):");
}
printf("Done");
return 0;
}
9.
//5.11.9
#include
void Temperatures(double);
int main(void)
{
double f_temp;
printf("请输入温度(华氏度)(q退出):");
while((scanf("%lf",&f_temp) == 1) )
{
getchar();
Temperatures(f_temp);
printf("\n请输入温度(华氏度)(q退出):");
}
printf("再见");
return 0;
}
void Temperatures(double f_temp)
{
const float k_to_s = 273.16;
const float s_to_f = 32.0;
double c_temp = 5.0/9.0*(f_temp - s_to_f);
printf("输入的 %.2lf 华氏度,是 %.2lf 摄氏度,是 %.2lf 开氏度\n",f_temp,c_temp,c_temp+k_to_s);
}