From ff10f994778ef716155cf4dbea4939a3ea029e8b Mon Sep 17 00:00:00 2001 From: flykhan Date: Tue, 11 Jul 2023 19:52:53 +0800 Subject: [PATCH] =?UTF-8?q?day7=20coding:=20=E5=8F=98=E9=87=8F=E5=AD=98?= =?UTF-8?q?=E5=82=A8=E3=80=81=E9=A2=84=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day7/d1.c | 15 +++++++++++++++ day7/d10/a.c | 11 +++++++++++ day7/d10/a.h | 4 ++++ day7/d10/main.c | 9 +++++++++ day7/d11.c | 11 +++++++++++ day7/d12.c | 12 ++++++++++++ day7/d13.c | 10 ++++++++++ day7/d14.c | 8 ++++++++ day7/d15.c | 16 ++++++++++++++++ day7/d16.c | 18 ++++++++++++++++++ day7/d17.c | 19 +++++++++++++++++++ day7/d18.c | 16 ++++++++++++++++ day7/d2.c | 11 +++++++++++ day7/d3.c | 19 +++++++++++++++++++ day7/d4.c | 19 +++++++++++++++++++ day7/d5.c | 21 +++++++++++++++++++++ day7/d6.c | 19 +++++++++++++++++++ day7/d7.c | 12 ++++++++++++ day7/d8.c | 19 +++++++++++++++++++ day7/d9/help.c | 13 +++++++++++++ day7/d9/help.h | 7 +++++++ day7/d9/main.c | 13 +++++++++++++ 22 files changed, 302 insertions(+) create mode 100644 day7/d1.c create mode 100644 day7/d10/a.c create mode 100644 day7/d10/a.h create mode 100644 day7/d10/main.c create mode 100644 day7/d11.c create mode 100644 day7/d12.c create mode 100644 day7/d13.c create mode 100644 day7/d14.c create mode 100644 day7/d15.c create mode 100644 day7/d16.c create mode 100644 day7/d17.c create mode 100644 day7/d18.c create mode 100644 day7/d2.c create mode 100644 day7/d3.c create mode 100644 day7/d4.c create mode 100644 day7/d5.c create mode 100644 day7/d6.c create mode 100644 day7/d7.c create mode 100644 day7/d8.c create mode 100644 day7/d9/help.c create mode 100644 day7/d9/help.h create mode 100644 day7/d9/main.c diff --git a/day7/d1.c b/day7/d1.c new file mode 100644 index 0000000..ba383d0 --- /dev/null +++ b/day7/d1.c @@ -0,0 +1,15 @@ +#include + +void show() +{ + int x = 10; // x 只在 show() 函数中有效 + printf("show x = %d\n", x); +} + +int main(int argc, char const *argv[]) +{ + int x = 100; + show(); // show 函数内的变量则会自动释放 + printf("main x = %d\n", x); // x 是 main 局部定义的 x + return 0; +} \ No newline at end of file diff --git a/day7/d10/a.c b/day7/d10/a.c new file mode 100644 index 0000000..66d3ac8 --- /dev/null +++ b/day7/d10/a.c @@ -0,0 +1,11 @@ +#include "a.h" + +int sum(int a, int b) +{ + return a + b; +} + +int sub(int a, int b) +{ + return a - b; +} \ No newline at end of file diff --git a/day7/d10/a.h b/day7/d10/a.h new file mode 100644 index 0000000..b25bb49 --- /dev/null +++ b/day7/d10/a.h @@ -0,0 +1,4 @@ +#include + +extern int sum(int, int); +extern int sub(int, int); \ No newline at end of file diff --git a/day7/d10/main.c b/day7/d10/main.c new file mode 100644 index 0000000..bda8878 --- /dev/null +++ b/day7/d10/main.c @@ -0,0 +1,9 @@ +#include "./a.h" + +int main(int argc, char const *argv[]) +{ + int ret = sum(100, 200); + printf("100 + 200 = %d\n", ret); + printf("ret - 60 = %d\n", sub(ret, 60)); + return 0; +} \ No newline at end of file diff --git a/day7/d11.c b/day7/d11.c new file mode 100644 index 0000000..17ef43f --- /dev/null +++ b/day7/d11.c @@ -0,0 +1,11 @@ +#include + +int main() +{ + extern int m; + { + int m; + printf("%d\n", m / 0); // 除 0 编译测试 + } + return 0; +} \ No newline at end of file diff --git a/day7/d12.c b/day7/d12.c new file mode 100644 index 0000000..0a37809 --- /dev/null +++ b/day7/d12.c @@ -0,0 +1,12 @@ +#include + +#define PI 3.14 + +int main() +{ + float radiu; + printf("半径(π=%.2f): ", PI); + scanf("%f", &radiu); + printf("面积: %.2f\n", PI * radiu * radiu); + return 0; +} \ No newline at end of file diff --git a/day7/d13.c b/day7/d13.c new file mode 100644 index 0000000..3f5bc96 --- /dev/null +++ b/day7/d13.c @@ -0,0 +1,10 @@ +#include + +#define SUM(a, b) a + b +int main() +{ + int x, y; + scanf("%d,%d", &x, &y); + printf("%d+%d=%d\n", x, y, SUM(x, y)); + return 0; +} \ No newline at end of file diff --git a/day7/d14.c b/day7/d14.c new file mode 100644 index 0000000..88516f4 --- /dev/null +++ b/day7/d14.c @@ -0,0 +1,8 @@ +#include +#define M(a, b) (a) * (b) + +int main(int argc, char const *argv[]) +{ + printf("%d*%d=%d\n", 2 + 5, 6 - 3, M(2 + 5, 6 - 3)); + return 0; +} diff --git a/day7/d15.c b/day7/d15.c new file mode 100644 index 0000000..aaf9ad7 --- /dev/null +++ b/day7/d15.c @@ -0,0 +1,16 @@ +#include + +#define __DEBUG__ + +int main() +{ +#ifdef __DEBUG__ + printf("------debug------\n"); +#endif + + int a = 10, b = 20; + int ret = a + b; + printf("%d+%d=%d\n", a, b, ret); + + return 0; +} \ No newline at end of file diff --git a/day7/d16.c b/day7/d16.c new file mode 100644 index 0000000..5bb3564 --- /dev/null +++ b/day7/d16.c @@ -0,0 +1,18 @@ +#include + +#define __DEBUG__ + +int main() +{ +#ifdef __DEBUG__ // 如果定义了 __DEBUG__ 宏,则执行下面代码 + printf("------debug------\n"); +#else // 如果没有定义 __DEBUG__ 宏,则执行下面的程序 + printf("------v1.0------\n"); +#endif + + int a = 10, b = 20; + int ret = a + b; + printf("%d+%d=%d\n", a, b, ret); + + return 0; +} \ No newline at end of file diff --git a/day7/d17.c b/day7/d17.c new file mode 100644 index 0000000..e8d34c8 --- /dev/null +++ b/day7/d17.c @@ -0,0 +1,19 @@ +#include + +#define __DEBUG__ + +int main() +{ +#ifndef __DEBUG__ // 如果没有定义了 __DEBUG__ 宏,则执行下面代码 + printf("------v1.0------\n "); +#else // 如果定义了 __DEBUG__ 宏,则执行下面的程序 + printf(" ------debug-- ----\n "); + +#endif + + int a = 10, b = 20; + int ret = a + b; + printf("%d+%d=%d\n", a, b, ret); + + return 0; +} \ No newline at end of file diff --git a/day7/d18.c b/day7/d18.c new file mode 100644 index 0000000..907e885 --- /dev/null +++ b/day7/d18.c @@ -0,0 +1,16 @@ +#include +#define DEBUG 0 +// #define DEBUG 1 + +int main(int argc, char const *argv[]) +{ + int a, b; + scanf("%d,%d", &a, &b); + +#if DEBUG + printf("%d*%d=%d\n", a, b, a * b); +#else + printf("%d+%d=%d\n", a, b, a + b); +#endif + return 0; +} diff --git a/day7/d2.c b/day7/d2.c new file mode 100644 index 0000000..94a1d52 --- /dev/null +++ b/day7/d2.c @@ -0,0 +1,11 @@ +#include + +int x; // 全局变量,未初始化,存在静态全局区中 +static int y; // 全局静态变量 + +int main(int argc, char const *argv[]) +{ + int x = 10; // x 是 main 函数的局部变量,存在栈中 + printf("x = %d\n", x); // 局部变量 + return 0; +} \ No newline at end of file diff --git a/day7/d3.c b/day7/d3.c new file mode 100644 index 0000000..03ba2c0 --- /dev/null +++ b/day7/d3.c @@ -0,0 +1,19 @@ + +// 使用全局变量计算100以内累加和。 +#include + +int total = 0; // 全局变量在整个 + +void add(int n) +{ + total += n; // 将局部变量 n 的值累加到 total 全局变量 +} + +int main(int argc, char const *argv[]) +{ + for (int i = 1; i <= 100; i++) + add(i); + + printf("100 以内的整数和: %d\n", total); + return 0; +} \ No newline at end of file diff --git a/day7/d4.c b/day7/d4.c new file mode 100644 index 0000000..0096fa1 --- /dev/null +++ b/day7/d4.c @@ -0,0 +1,19 @@ +// 使用全局变量计算100以内累加和。 +#include + +void add(int n) +{ + static int total = 0; // 只定义一次静态变量 + total += n; // 将局部变量 n 的值累加到 total 全局变量 + + printf("total = %d\n", total); +} + +int main(int argc, char const *argv[]) +{ + for (int i = 1; i <= 100; i++) + add(i); + + // printf("100 以内的整数和: %d\n", total); + return 0; +} \ No newline at end of file diff --git a/day7/d5.c b/day7/d5.c new file mode 100644 index 0000000..3d7066b --- /dev/null +++ b/day7/d5.c @@ -0,0 +1,21 @@ +// 使用全局变量计算100以内累加和。 +#include + +int add(int n) +{ + static int total = 0; // 只定义一次静态变量 + total += n; // 将局部变量 n 的值累加到 total 全局变量 + + // printf("total = %d\n", total); + return total; +} + +int main(int argc, char const *argv[]) +{ + int sum; + for (int i = 1; i <= 100; i++) + sum = add(i); + + printf("100 以内的整数和: %d\n", sum); + return 0; +} \ No newline at end of file diff --git a/day7/d6.c b/day7/d6.c new file mode 100644 index 0000000..dcb3e71 --- /dev/null +++ b/day7/d6.c @@ -0,0 +1,19 @@ +#include + +static int totalScore; +int totalPersons; + +void count(int a, int b, int c) +{ + totalScore += a + b + c; + totalPersons++; +} + +int main() +{ + printf("全局变量值: %d, %d\n", totalPersons, totalScore); + count(90, 100, 89); + count(95, 90, 79); + printf("平均成绩: %.2f\n", totalScore * 1.0 / totalPersons); + return 0; +} \ No newline at end of file diff --git a/day7/d7.c b/day7/d7.c new file mode 100644 index 0000000..bec9a6a --- /dev/null +++ b/day7/d7.c @@ -0,0 +1,12 @@ +#include + +void sum(int a, int b) +{ + int ret = a + b; + printf("%d + %d = %d\n", a, b, ret); +} + +int main(int argc, char const *argv[]) +{ + return 0; +} \ No newline at end of file diff --git a/day7/d8.c b/day7/d8.c new file mode 100644 index 0000000..b3f9a3c --- /dev/null +++ b/day7/d8.c @@ -0,0 +1,19 @@ +#include + +void watch_n(int i) +{ + // 监听 watch_n 函数每调 10 次,打印 i 的结果 + static int cnt; + if (++cnt % 10 == 0) + { + printf("%d ", i); + } +} + +int main(int argc, char const *argv[]) +{ + for (int i = 0; i < 100; i++) + watch_n(i); + + return 0; +} \ No newline at end of file diff --git a/day7/d9/help.c b/day7/d9/help.c new file mode 100644 index 0000000..a60cf2f --- /dev/null +++ b/day7/d9/help.c @@ -0,0 +1,13 @@ +#include "help.h" + +// 内部函数 +static void _show_sum(int a, int b, int c) +{ + printf("%d + %d = %d\n", a, b, c); +} + +// 全局函数 +void twoNumAdd(int a, int b) +{ + _show_sum(a, b, a + b); +} \ No newline at end of file diff --git a/day7/d9/help.h b/day7/d9/help.h new file mode 100644 index 0000000..9919706 --- /dev/null +++ b/day7/d9/help.h @@ -0,0 +1,7 @@ +#include + +// 内部函数 +static void _show_sum(int a, int b, int c); + +// 全局函数 +void twoNumAdd(int a, int b); \ No newline at end of file diff --git a/day7/d9/main.c b/day7/d9/main.c new file mode 100644 index 0000000..eb3d654 --- /dev/null +++ b/day7/d9/main.c @@ -0,0 +1,13 @@ +#include "help.h" + +int main() +{ + for (int i = 1; i < 3; i++) + { + for (int j = 1; j <= i; j++) + { + twoNumAdd(i, j); // 可以调用 help.c 的外部函数 + // _show_sum(i, j, i + j); // 不能调用 static 修饰的内部函数 + } + } +} \ No newline at end of file