[C语言刷题] chapter05
原文发布在 https://www.qarks.top/2020-02-16/chapter05-ep/
今天我们来看看C Primer Plus的课后习题。
## 第一题
### 题目
写一个程序,将原本用分钟表示的时间转换成用分钟和小时来表示,要求要用到常量,while循环,当输入小于或等于0时终止程序。
### 分析
小时可以以min/60得到,分钟取余
### 答案
先在这里声明,我写的答案全是我本人写的,本人想的,有什么问题或疑问可以在评论区指出。
```c
/* pe01.c -- convert min to hour and min */ #include <stdio.h>
#define MPH 60 // define the symbolic constant for 60
int main(void) {
int min = 0, hour = 0, ms = 0;
printf("Enter minutes(0 to end):\n");
scanf("%d",min);
while(min > 0) {
hour = min / MPH;
ms = min % MPH;
printf("%d minutes convert to hour and minute is %d hour(s) %d minute(s).\n", min, hour, ms);
printf("Enter minutes(0 to end):\n"); scanf("%d", &min);
}
return 0;
}
```
## 第二题
### 题目
写一个程序,要求输入一个整数,输出从该整数到大于该整数10的所有整数,并用空格符号(空格,制表等)间隔。
### 分析
可以使用while(第五章没学for)。
### 答案
```c
/* pe02.c -- print all integer in the range */
#include <stdio.h>
int main(void) {
int i, d;
printf("Enter a integer:\n"); // ask for an integer
scanf("%d",&i);
d = i;
while(d <= i+10) {
printf("%d ", d);
d++;
}
printf("\n");
return 0;
}
```
## 第三题
### 题目
写一个程序,让用户输入天数,之后将天数转换成用周数和天数表示。
### 分析
同第一题
### 答案
```c
/* pe03.c -- converts days to weeks and days */
#include <stdio.h>
#define DTW 7
int main(void) {
int week, day, ds;
scanf("%d", &day); week = day / DTW;
ds = day % DTW;
printf("%d days are %d weeks, %d days\n", day, week, ds);
return 0;
}
```
## 第四题
### 题目
写一个程序,让用户输入一个用厘米表示的身高,转换成英制单位>
一起输出,允许小数厘米,英寸。
例: 168.0 cm = 5 feet, 6.4 inches
### 分析
首先我们要知道 一英寸是2.54厘米,一英尺是12英寸。
### 答案
```c
/* pe04.c -- height convertion */
#include <stdio.h>
#define CTE 2.54
int main(void) {
int feet; float cheight, inches, temp;
printf("Enter a height in centimeters: ");
scanf("%f", &cheight);
while(cheight > 0) { temp = cheight / CTE;
feet = temp / 12;
inches = temp - 12*feet;
printf("%.1f cm = %d feet, %.1f inches\n", cheight, feet, inches);
printf("Enter a height in centimeters(<=0 to quit): ");
scanf("%f", &cheight);
}
printf("bye!");
return 0;
}
```
## 第五题
### 题目
改造addemup.c
### 分析
说白了,就是要加个scanf。
### 答案
```c
/* pe05.c -- modify the addemup.c */
#include <stdio.h>
int main(void) /* finds sum of first 20 integers */
{
int count, sum;/* declaration statement */
int n;
printf("How far do you want to caculate: ");
scanf("%d", &n);
count = 0; /* assignment statement */
sum = 0; /* ditto */
while(count++ < n) /* while */
sum = sum + count; /* statement */
printf("sum = %d\n", sum); /* function statement */
return 0; /* return statememt */
}
```
## 第六题
### 题目
改造第五题,改成平方相加。
### 答案
直接上答案了
```c
/* pe06.c -- modify pe05.c */
#include <stdio.h>
int main(void) /* finds sum of first 20 integers */
{
int count, sum; /* declaration statement */
int n;
printf("How far do you want to caculate: ");
scanf("%d", &n);
count = 0; /* assignment statement */
sum = 0; /* ditto */
while(count++ < n) /* while */
sum = sum + count * count; /* statement */
printf("sum = %d\n", sum); /* function statement */
return 0; /* return statememt */
}}
```
## 第七题
### 题目
写一个程序,用函数算立方,并输出。
### 分析
简单分析可知cube()需要一个double形参并且为void类型。
### 答案
```c
/* pe07.c -- use function to caculate the cube */
#include <stdio.h>
void cube(double);
int main(void) {
double d;
printf("Enter a number: ");
scanf("%lf", &d);
cube(d);
return 0;
}
void cube(double d) {
double res = d * d * d;
printf("The cube of %f is %f\n", d, res);
}
```
## 第八题
### 题目
写一个程序来取余,输入两个数,第一个数作第二个算子,第二个做第一个算子,之后输入的都为第一个算子,第二个算子不变,输入的数小于等于零则退出。
### 答案
这种题目比较直接,以后就不会写分析了
```c
/* pe08.c -- used to caculate the modulus */
#include <stdio.h>
int main(void) {
int sec, fir;
printf("This program computes moduli.\n");
printf("Enter an integer to serve as the second operand: ");
scanf("%d", &sec);
printf("Now enter the first operand: ");
scanf("%d", &fir);
while(fir > 0) {
printf("%d %% %d is %d\n", fir, sec, fir%sec);
printf("Enter next number for first operand(<=0 to quit): ");
scanf("%d", &fir);
}
printf("Done!\n");
return 0;
}
```
## 第九题
### 题目
温度转换,输入华氏摄氏度,用函数(自定义)Temperatures(),来输出摄氏度和开尔文温度。请用温度标准命名。
### 分析
Celsius = 5.0 / 9.0\* (Fahrenheit - 32.0)
Kelvin = Celsius + 273.16
### 答案
```c
/* pe09.c -- converts temperature */
#include <stdio.h>
const double U = 5.0;
const double D = 9.0;
const double M = 32.0;
const double CK = 273.16;
void Temperatures(double fahrenheit);
int main(void) {
double fahrenheit;
printf("Enter the temperature: "); while(scanf("%lf", &fahrenheit) == 1) {
Temperatures(fahrenheit);
printf("Enter the temperature(enter nonnumeric to quit): ");
}
printf("Bye!\n");
return 0;
}
void Temperatures(double fahrenheit) {
double celsius, kelvin;
celsius = U/D*(fahrenheit-M);
kelvin = celsius + CK;
printf("Temperature in Fahrenheit is %.2f℉, in Celsius is %.2f℃, in Kelvin is %.2fK\n", fahrenheit, celsius, kelvin);
}
```
好了,今天的教程就到这里了,谢谢大家阅读。