141 lines
No EOL
3.5 KiB
Markdown
141 lines
No EOL
3.5 KiB
Markdown
---
|
|
title: 第二次作业(答案)
|
|
mathjax: true
|
|
date: 2020-10-30 10:35:37
|
|
tags:
|
|
---
|
|
|
|
|
|
# P82-6
|
|
|
|
```C
|
|
#include <stdio.h>
|
|
|
|
#define OFFSET 4
|
|
|
|
int main()
|
|
{
|
|
char c1 = 'C',
|
|
c2 = 'h',
|
|
c3 = 'i',
|
|
c4 = 'n',
|
|
c5 = 'a';
|
|
|
|
c1 += OFFSET;
|
|
c2 += OFFSET;
|
|
c3 += OFFSET;
|
|
c4 += OFFSET;
|
|
c5 += OFFSET;
|
|
|
|
/* 使用putchar()输出 */
|
|
putchar(c1);
|
|
putchar(c2);
|
|
putchar(c3);
|
|
putchar(c4);
|
|
putchar(c5);
|
|
|
|
/* 使用printf()输出 */
|
|
|
|
printf("%c%c%c%c%c", c1, c2, c3, c4, c5);
|
|
|
|
return 0;
|
|
}
|
|
```
|
|
|
|
# P82-7
|
|
|
|
圆半径 $r = 1.5$ ,圆柱高 $h = 3$ ,分别用变量`radius`和`height`存储。圆的周长 $P_{\mathrm{circle}} = 2 \pi r$ ,用变量`circle_perimeter`存储;圆的面积 $A_{\mathrm{circle}} = \pi r^{2}$ ,用变量`circle_area`存储;圆球表面积 $A_{\mathrm{sphere}} = 4 \pi r^{2}$ ,用变量`sphere_area`存储;圆球体积 $V_{\mathrm{ball}} = \frac{4}{3} \pi r^{3}$ ,用变量`ball_volume`存储;圆柱体积 $V_{\mathrm{cylinder}} = \pi r^{2} h$ ,用变量`cylinder_volume`存储。
|
|
|
|
在这个程序中用到了`M_PI`,相关内容请见:[Math Constants](https://docs.microsoft.com/en-us/cpp/c-runtime-library/math-constants?view=vs-2019),[19.1 Predefined Mathematical Constants](https://www.gnu.org/software/libc/manual/html_node/Mathematical-Constants.html)。
|
|
|
|
```C
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#define _USE_MATH_DEFINES
|
|
#include <math.h>
|
|
|
|
int main()
|
|
{
|
|
printf("This program calculates the perimeter and area of the circle, ");
|
|
printf("the area and the volume of the ball, and the volume of the cylinder.\n");
|
|
|
|
double radius, height;
|
|
/* 检查用户输入是否合法 */
|
|
printf("Please input the radius: ");
|
|
if (scanf("%lf", &radius) != 1 || radius <= 0) {
|
|
printf("Invalid input!\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
printf("Please input the height: ");
|
|
if (scanf("%lf", &height) != 1 || height <= 0) {
|
|
printf("Invalid input!\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
double circle_perimeter = 2 * M_PI * radius;
|
|
printf("The perimeter of the circle is %.2f\n", circle_perimeter);
|
|
|
|
double circle_area = M_PI * radius * radius;
|
|
printf("The area of the circle is %.2f\n", circle_area);
|
|
|
|
double sphere_area = 4 * M_PI * radius * radius;
|
|
printf("The area of the sphere is %.2f\n", sphere_area);
|
|
|
|
double ball_volume = 4.0 / 3.0 * M_PI * radius * radius * radius;
|
|
printf("The volume of the ball is %.2f\n", ball_volume);
|
|
|
|
double cylinder_volume = M_PI * radius * radius * height;
|
|
printf("The volume of the cylinder is %.2f\n", cylinder_volume);
|
|
|
|
return 0;
|
|
}
|
|
```
|
|
|
|
# P82-8
|
|
|
|
```C
|
|
#include <stdio.h>
|
|
|
|
int main()
|
|
{
|
|
char c1 = getchar();
|
|
char c2 = getchar();
|
|
|
|
putchar(c1);
|
|
putchar(c2);
|
|
printf("%c%c", c1, c2);
|
|
|
|
/* 打印ASCII码 */
|
|
printf("%d%d", c1, c2);
|
|
|
|
return 0;
|
|
}
|
|
```
|
|
|
|
1. 二者皆可。
|
|
2. 可以用`printf("%d", c1);`这种方式来输出它们的ASCII码。
|
|
3. 不可以。两者占用的内存空间不同,表示的数的范围不同。`char`类型是不是有符号类型,视实现而定。
|
|
|
|
# 温度转换
|
|
|
|
```C
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
int main()
|
|
{
|
|
printf("Please input the temperature (in fahrenheit): ");
|
|
|
|
double degree_f;
|
|
if (scanf("%lf", °ree_f) != 1) {
|
|
printf("Invalid input!\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
double degree_c = (degree_f - 32.0) * 5.0 / 9.0;
|
|
|
|
printf("%.1f degree fahrenheit equals to %.1f degree celsius.\n", degree_f, degree_c);
|
|
|
|
return 0;
|
|
}
|
|
``` |