第三次作业题目和答案(未公布)

This commit is contained in:
Zhao Zuohong 2020-10-30 13:59:49 +08:00
commit b01ba87404
102 changed files with 8693 additions and 0 deletions

214
source/_drafts/answer3.md Normal file
View file

@ -0,0 +1,214 @@
---
title: 第三次作业(答案)
tags:
---
# P108-3
写出下面各逻辑表达式的值。设`a = 3``b = 4``c = 5`
| # | 表达式 | 答案 |
| :---: | :------------------------------ | :-- |
| 1 | `a + b > c && b == c` | `0` |
| 2 | `a || b + c && b - c` | `1` |
| 3 | `!(a > b) && !c || 1` | `1` |
| 4 | `!(x = a) && (y = b) && 0` | `0` |
| 5 | `!(a + b) + c - 1 && b + c / 2` | `1` |
# P108-6
```C
#include <stdio.h>
#include <stdlib.h>
int main()
{
double x, y;
printf("This program calculates y: \n");
printf("\n");
printf(" y = x (x < 1)\n");
printf(" 2x - 1 (1 <= x < 10)\n");
printf(" 3x - 11 (x >= 10)\n");
printf("\n");
printf("Please input x:\n");
printf("x = ");
if (scanf("%lf", &x) != 1) {
fprintf(stderr, "Invalid input!\n");
exit(EXIT_FAILURE);
}
if (x < 1) {
y = x;
} else if (x < 10) {
y = 2 * x - 1;
} else {
y = 3 * x - 11;
}
printf("y = %f\n", y);
return 0;
}
```
# P108-9
```C
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
printf("请输入n: \nn = ");
if (scanf("%d", &n) != 1 || n > 99999 || n < 1) {
fprintf(stderr, "非法输入!\n");
exit(EXIT_FAILURE);
}
// 求位数
int tmp = n;
int digits = 5;
for (int i = 0; i < 5; ++i) {
if (tmp == 0) {
digits = i;
break;
}
tmp /= 10;
}
printf("%d有%d位\n", n, digits);
// 输出每一位:
tmp = n;
int digit_1 = tmp % 10;
tmp /= 10;
int digit_2 = tmp % 10;
tmp /= 10;
int digit_3 = tmp % 10;
tmp /= 10;
int digit_4 = tmp % 10;
tmp /= 10;
int digit_5 = tmp;
if (digits >= 1) {
printf("它的个位是%d\n", digit_1);
}
if (digits >= 2) {
printf("它的十位是%d\n", digit_2);
}
if (digits >= 3) {
printf("它的百位是%d\n", digit_3);
}
if (digits >= 4) {
printf("它的千位是%d\n", digit_4);
}
if (digits >= 5) {
printf("它的万位是%d\n", digit_5);
}
// 逆向输出
printf("逆向输出是");
printf("%d", digit_1);
if (digits >= 2) {
printf("%d", digit_2);
}
if (digits >= 3) {
printf("%d", digit_3);
}
if (digits >= 4) {
printf("%d", digit_4);
}
if (digits >= 5) {
printf("%d", digit_5);
}
printf("\n");
return 0;
}
```
# P108-12
```C
#include <stdio.h>
#include <stdlib.h>
int main()
{
double r2 = 1;
double ax = 2, ay = 2,
bx = -2, by = 2,
cx = -2, cy = -2,
dx = 2, dy = -2;
double x, y;
printf("请分别输入所在位置的的x、y坐标:\nx = ");
if (scanf("%lf", &x) != 1) {
fprintf(stderr, "非法输入!\n");
exit(EXIT_FAILURE);
}
printf("y = ");
if (scanf("%lf", &y) != 1) {
fprintf(stderr, "非法输入!\n");
exit(EXIT_FAILURE);
}
double da2 = (x - ax) * (x - ax) + (y - ay) * (y - ay),
db2 = (x - bx) * (x - bx) + (y - by) * (y - by),
dc2 = (x - cx) * (x - cx) + (y - cy) * (y - cy),
dd2 = (x - dx) * (x - dx) + (y - dy) * (y - dy);
if (da2 <= r2 || db2 <= r2 || dc2 <= r2 || dd2 <= r2) {
printf("该点高度是10m\n");
} else {
printf("该点高度是0m\n");
}
return 0;
}
```
# 解一元二次方程
```C
#include <stdio.h>
#include <stdlib.h>
#include <complex.h>
#include <math.h>
int main()
{
double a, b, c;
printf("一元二次方程求根\n");
printf("a x^2 + b x + c = 0\n");
printf("请输入a、b、c:\na = ");
if (scanf("%lf", &a) != 1 || a == 0) {
fprintf(stderr, "非法输入!\n");
exit(EXIT_FAILURE);
}
printf("b = ");
if (scanf("%lf", &b) != 1) {
fprintf(stderr, "非法输入!\n");
exit(EXIT_FAILURE);
}
printf("c = ");
if (scanf("%lf", &c) != 1) {
fprintf(stderr, "非法输入!\n");
exit(EXIT_FAILURE);
}
double delta = b * b - 4 * a * c;
if (delta == 0) {
printf("x1 = x2 = %f\n", -b / 2 / a);
} else if (delta > 0) {
double x1 = (-b + sqrt(delta)) / 2 / a;
double x2 = (-b - sqrt(delta)) / 2 / a;
printf("x1 = %f\nx2 = %f\n", x1, x2);
} else {
double complex x1 = (-b + csqrt(delta)) / 2 / a;
double complex x2 = (-b - csqrt(delta)) / 2 / a;
printf("x1 = %f %+fi\nx2 = %f %+fi\n", creal(x1), cimag(x1), creal(x2), cimag(x2));
}
return 0;
}
```

46
source/_posts/answer1.md Normal file
View file

@ -0,0 +1,46 @@
---
title: 第一次作业(答案)
date: 2020-10-30 09:53:13
tags:
---
# 第一题
写出下列程序运行的结果:
```
sizeof: 4, a = 61, b = 62
sizeof: 1, c1 = a, c2 = b
sizeof: 4, d = 3.56 , e = -6.78
sizeof: 8, f = 3157.890121 , g = 0.123456789000
sizeof: 4, m = 50000, n = -60000
sizeof: 4, p = 32768, q = 4000
```
有些同学可能遇到警告:`"printf": 格式字符串"%d"需要类型"int"的参数,但可变参数 1 拥有了类型"size_t"`,这是因为`sizeof()`的类型是`size_t`而非`int`,在`printf()`中与`%d`的类型不匹配。可以尝试`%zu`(C99标准及以后)或`%lu`(ANSI C标准)。
# 第二题
```
9, 11, 9, 10
```
# 第三题
| # | 表达式 | 变量的值 | 结果 |
| :---: | :------------------------------------- | :--------------------------------- | :---- |
| 1 | `3.5 + 1 / 2 + 56 % 10` | | `9.5` |
| 2 | `(a++ * 1 / 3)` | 设`a = 2` | `0` |
| 3 | `x + a % 3 * (int)(x + y) % 2 / 4` | 设`x = 2.5, a = 7, y = 4.7` | `2.5` |
| 4 | `(float)(a + b) / 2 + (int)x % (int)y` | 设a` = 2, b = 3, x = 3.5, y = 2.5` | `3.5` |
| 5 | `x = (x = ++y, x + 5, x / 5)` | 设`x = 3, y = 4` | `1` |
# 第五题
写出下面表达式运算后a的值,设原来a = 12。
1. `24`
2. `60`
3. `0`
4. `0`

141
source/_posts/answer2.md Normal file
View file

@ -0,0 +1,141 @@
---
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", &degree_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;
}
```

View file

@ -0,0 +1,47 @@
---
title: 资源下载
date: 2020-10-29 20:58:43
tags:
---
# Code::Blocks
网站:[http://www.codeblocks.org/](http://www.codeblocks.org/)
下载:[codeblocks-20.03mingw-setup.exe](/~zhaozuohong/resources/codeblocks-20.03mingw-setup.exe)
## 汉化相关
1. 下载中文语言文件[zh_CN_LC_MESSAGES_codeblocks.mo](/~zhaozuohong/resources/zh_CN_LC_MESSAGES_codeblocks.mo);
2. 在`C:\Program Files\Codeblocks\share\Codeblocks\`下新建`locale`文件夹,再在`locale`文件夹中新建`zh_CN`文件夹;
3. 将`zh_CN_LC_MESSAGES_codeblocks.mo`文件放到刚才创建的`zh_CN`文件夹中;
4. 打开Code::Blocks,点击菜单`Settings` - `Enviroments...`,在弹出的设置窗口中,点击左侧的`View`,选中`Internalization (will take place after restart)`,并在右侧的下拉列表中选择`Chinese (Simplified)`
5. 重启Code::Blocks,汉化完成。
# 课件
[第1章 计算机及程序设计基础知识.ppt](/~zhaozuohong/resources/Chapter1.ppt)
[第2章 C语言概述.ppt](/~zhaozuohong/resources/Chapter2.ppt)
[第3章 数据类型、运算符与表达式.ppt](/~zhaozuohong/resources/Chapter3.ppt)
[第4章 顺序结构程序设计.ppt](/~zhaozuohong/resources/Chapter4.ppt)
[第5章 选择结构程序设计.ppt](/~zhaozuohong/resources/Chapter5.ppt)
[第6章 循环结构程序设计.ppt](/~zhaozuohong/resources/Chapter6.ppt)
[第7章 数组.ppt](/~zhaozuohong/resources/Chapter7.ppt)
# 上机讲解
[第一次上机讲解.pptx](/~zhaozuohong/resources/Experiment1.pptx)
[第二次上机讲解.pptx](/~zhaozuohong/resources/Experiment2.pptx)
# 其它参考资料
[C语言程序设计习题选编.pdf](/~zhaozuohong/resources/C-experiments.ppt)

23
source/_posts/homework.md Normal file
View file

@ -0,0 +1,23 @@
---
title: 作业与答案
top: true
date: 2020-10-30 09:39:53
tags:
---
# 第一次作业
* [题目](/~zhaozuohong/homework1/)
* [答案](/~zhaozuohong/answer1)
# 第二次作业
* [题目](/~zhaozuohong/homework2/)
* [答案](/~zhaozuohong/answer2)
# 第三次作业
* [题目](/~zhaozuohong/homework3/)
* *答案暂未开放*
<!-- * [答案](/~zhaozuohong/answer3) -->

View file

@ -0,0 +1,75 @@
---
title: 第一次作业(题目)
date: 2020-10-30 09:47:21
tags:
---
# 第一题
写出下列程序运行的结果:
```C
#include <stdio.h>
int main()
{
int a, b;
char c1, c2;
float d, e;
double f, g;
long m, n;
unsigned int p, q;
a = 61; b = 62;
c1 = 'a'; c2 = 'b';
d = 3.56; e = -6.78;
f = 3157.890121; g = 0.123456789;
m = 50000; n = -60000;
p = 32768; q = 4000;
printf("sizeof: %d, a = %d, b = %d\n", sizeof(int), a, b);
printf("sizeof: %d, c1 = %c, c2 = %c\n", sizeof(char), c1, c2);
printf("sizeof: %d, d = %-6.2f, e = %-6.2f\n", sizeof(float), d, e);
printf("sizeof: %d, f = %-15.6f, g = %-15.12f\n", sizeof(double), f, g);
printf("sizeof: %d, m = %ld, n = %ld\n", sizeof(long), m, n);
printf("sizeof: %d, p = %u, q = %u\n", sizeof(unsigned), p, q);
return 0;
}
```
# 第二题
写出下列程序的运行结果:
```C
#include <stdio.h>
int main()
{
int i, j, m, n;
i = 8; j = 10;
m = ++i; n = j++;
printf("%d, %d, %d, %d\n", i, j, m, n);
return 0;
}
```
# 第三题
求下面算术表达式的值:
| # | 表达式 | 变量的值 |
| :---: | :------------------------------------- | :--------------------------------- |
| 1 | `3.5 + 1 / 2 + 56 % 10` | |
| 2 | `(a++ * 1 / 3)` | 设`a = 2` |
| 3 | `x + a % 3 * (int)(x + y) % 2 / 4` | 设`x = 2.5, a = 7, y = 4.7` |
| 4 | `(float)(a + b) / 2 + (int)x % (int)y` | 设a` = 2, b = 3, x = 3.5, y = 2.5` |
| 5 | `x = (x = ++y, x + 5, x / 5)` | 设`x = 3, y = 4` |
# 第五题
写出下面表达式运算后`a`的值,设原来`a = 12`
1. `a += a`
2. `a *= 2 + 3`
3. `a /= a + a`
4. `a += a -= a *= a`

View file

@ -0,0 +1,39 @@
---
title: 第二次作业(题目)
mathjax: true
date: 2020-10-30 10:20:37
tags:
---
# 作业要求
* 纸质作业:把代码写在作业本上,周五(10月23日)上课时提交。
* 上机内容:输入、调试代码。
# P82-6
请编程序将`"China"`译成密码,密码规律是,用原来的字母后面第4个字母代替原来的字母。例如,字母`A`后面第4个字母是`E`,用`E`代替`A`。因此,`"China"`应译为`"Glmre"`。请编一程序,用赋初值的方法使`c1``c2``c3``c4``c5`这5个变量的值分别为`'C'``'h'``'i'``'n'``'a'`,经过运算,使`c1``c2``c3``c4``c5`分别变为`'G'``'l'``'m'``'r'``'e'`,分别用`putchar()`函数和`printf()`函数输出这5个字符。
# P82-7
设圆半径 $r = 1.5$ ,圆柱高 $h = 3$ ,求圆周长、圆面积、圆球表面积、圆球体积、圆柱体积,用`scanf()`输入数据,输出计算结果,输出时要求有文字说明,取小数点后2位数字,请编程序。
# P82-8
编程序,用`getchar()`函数读入两个字符给`c1``c2`,然后分别用`putchar()`函数和`printf()`函数输出这两个字符。思考以下问题:
1. 变量`c1``c2`应定义为字符型、整型还是二者皆可?
2. 要求输出`c1``c2`值的ASCII码,应如何处理?用`putchar()`函数还是`printf()`函数?
3. 整型变量与字符变量是否在任何情况下都可以互相代替?如:`char c1, c2;``int c1, c2;`是否无条件地等价?
# 温度转换
编程实现华氏温度到摄氏温度的转换。
提示:
$$
\begin{equation*}
T_{\mathrm{\left ({}^{o}C \right )}} = \left ( T_{\mathrm{\left ({}^{o}F \right )}} - 32 \right ) \times \frac{5}{9}
\end{equation*}
$$

View file

@ -0,0 +1,54 @@
---
title: 第三次作业(题目)
mathjax: true
date: 2020-10-30 12:33:44
tags:
---
# P108-3
写出下面各逻辑表达式的值。设`a = 3``b = 4``c = 5`
| # | 表达式 |
| :---: | :------------------------------ |
| 1 | `a + b > c && b == c` |
| 2 | `a || b + c && b = c` |
| 3 | `!(a > b) && !c || 1` |
| 4 | `!(x = a) && (y = b) && 0` |
| 5 | `!(a + b) + c - 1 && b + c / 2` |
# P108-6
有一个函数:
$$
\begin{equation*}
y = \left \{
\begin{array}{ll}
x & (x < 1) \\
2 x - 1 & (1 \leq x < 10 ) \\
3 x - 11 & (x \geq 10)
\end{array}
\right.
\end{equation*}
$$
写程序,输入 $x$ 的值,输出 $y$ 相应的值。
# P108-9
给出一个不多于5位的正整数,要求:
1. 求出它是几位数;
2. 分别输出每一位数字;
3. 按逆序输出各数字,例如原数为 $321$ ,应输出 $123$ 。
# P108-12
有4个圆塔,圆心分别为 $(2,2)$ 、 $(-2,2)$ 、 $(-2,-2)$ 、 $(2,-2)$ ,圆半径为 $1$ ,如下图所示。这4个塔的高度为 $10\mathrm{m}$ ,塔以外无建筑物。今输入任一点的坐标,求该点的建筑高度(塔外的高度为 $0$ )。
{% asset_img P108-12.png This is an image %}
# 解一元二次方程
要求:包括虚根。

Binary file not shown.

After

Width:  |  Height:  |  Size: 378 KiB