C primer plus第6版第5章プログラミング練習解答
5088 ワード
5.1
5.2
5.3
5.4
5.5
5.6
5.7
5.8
5.9
// ,
// #define const 60 const
// while , 0
#include
int main(void)
{
const int CHANGENUM = 60;
int time, hours, minutes; //time: hours: minutes:
printf(" ( ): ");
scanf("%d", &time);
while (time > 0)
{
hours = time / CHANGENUM;
minutes = time % CHANGENUM;
printf("%d %d %d
", time, hours, minutes);
printf(" ( ): ");
scanf("%d", &time);
}
printf(" !");
return 0;
5.2
/* , ,
10 。 、 */
#include
int main(void)
{
int num;
printf("Please enter a number: ");
scanf("%d", &num);
for (int i = 0; i < 10; ++i)
printf("%d\t", ++num);
return 0;
}
5.3
//
//
//while
#include
int main(void)
{
int days; //
int week, day; //
printf("Please enter the days number.(enter a number < 0 to quit): ");
scanf("%d", &days);
while (days > 0)
{
week = days / 7;
day = days % 7;
printf("%d days are %d weeks, %d days.
", days, week, day);
printf("Please enter the days number again.(enter a number < 0 to quit):");
scanf("%d", &days);
}
printf("Job Done!");
return 0;
}
5.4
// , (cm),
// ,
// ,
#include
#define CMTOFEET 0.032808 //1 =0.032808 feet
#define CMTOINCH 0.3937008 //1 =0.3937008 feet
#define FEETOINCH 12 //1 feet = 12 inch
int main(void)
{
float height; //
int feet; //
float inch; //
printf("Please enter your height in cm: ");
scanf("%f", &height);
while (height > 0)
{
feet = height * CMTOFEET;
inch = (height * CMTOINCH) - (FEETOINCH*feet);
printf("%.1f cm = %d feet, %.1f inches
", height, feet, inch);
printf("Enter a height in centimeters (<=0 to quit): ");
scanf("%f", &height);
}
printf("Job Done!");
return 0;
}
5.5
/* n , n $n*/
#include
int main(void) //
{
int count, sum;
//count = 0;
//sum = 0;
int n;
printf("Please enter the day: ");
scanf("%d", &n);
while (n > 0)
{
count = 0;
sum = 0;
while (count++ < n)
sum = sum + count;
printf("sum = %d
", sum);
printf("Please enter the day again(enter <=0 to quit:");
scanf("%d", &n);
}
printf("Job Down!");
return 0;
}
5.6
/* n , n $n*/
// n $n*n
#include
int main(void) //
{
int count, sum;
//count = 0;
//sum = 0;
int n;
printf("Please enter the day: ");
scanf("%d", &n);
while (n > 0)
{
count = 0;
sum = 0;
while (count++ < n)
sum = sum + count * count;
printf("sum = %d
", sum);
printf("Please enter the day again(enter <=0 to quit:");
scanf("%d", &n);
}
printf("Job Down!");
return 0;
}
5.7
// double ,
//
#include
double cube(double a);//
int main(void)
{
double num;
printf("Please enter a number: ");
scanf("%lf", &num);
double cube_num = cube(num);
printf("The cube of %.2lf is %.2lf.", num, cube_num);
return 0;
}
double cube(double a)
{
return a * a * a; //
}
5.8
// ,
// , 。
// ,
#include
int main(void)
{
int one, two, moduli;
printf("This program computers moduli.
");
printf("Enter an integer to serve as the second operand: ");
scanf("%d", &two);
printf("Now enter the first operand: ");
scanf("%d", &one);
while (one > 0)
{
moduli = one % two;
printf("%d %% %d is %d.
", one, two, moduli);
printf("Enter next number for first operand (<= 0 to quit): ");
scanf("%d", &one);
}
printf("Job Done!");
return 0;
}
5.9
// , double ,
// ,
#include
void Temperatures(double a);
int main(void)
{
double ft; //
printf("Please enter a temperature number( ): ");
while (scanf("%lf", &ft) == 1)
{
Temperatures(ft);
printf("Please enter the next number: ( q to quit ) : ");
}
printf("Job Done!");
return 0;
}
void Temperatures(double a)
{
double ct; //
double kt; //
ct = 5.0 / 9.0 * (a - 32.0);
kt = ct + 273.16;
printf("%.2f = %.2f = %.2f
", a, ct, kt);
}