实验3

task1.c

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 #include <windows.h>
 5 #define N 80
 6 void print_text(int line, int col, char text[]);
 7 void print_blank_lines(int n); 
 8 int main() {
 9     int line, col, i;
10     char text[N] = "hi, April~";
11     srand(time(0)); 
12     for (i = 1; i <= 10; ++i) {
13         line = rand() % 25;
14         col = rand() % 80;
15         print_text(line, col, text);
16         Sleep(1000); 
17     }
18     return 0;
19 }
20 void print_spaces(int n) {
21     int i;
22     for (i = 1; i <= n; ++i)
23         printf(" ");
24 }
25 void print_blank_lines(int n) {
26     int i;
27     for (i = 1; i <= n; ++i)
28         printf("\n");
29 }
30 void print_text(int line, int col, char text[]) {
31     print_blank_lines(line - 1);
32     print_spaces(col - 1);
33     printf("%s", text); 
34 }

 

代码作用:

以系统时间为随机种子生成随机数,打印随机空格和换行,并打印字符串,进行十次

 

 

task2_1.c

 1 #include <stdio.h>
 2 long long fac(int n);
 3 int main() {
 4     int i, n;
 5     printf("Enter n: ");
 6     scanf_s("%d", &n);
 7     for (i = 1; i <= n; ++i)
 8         printf("%d! = %lld\n", i, fac(i));
 9     return 0;
10 }
11 long long fac(int n) {
12     static long long p = 1;
13     p = p * n;
14     return p;
15 }

 

 

task2_2.c

 1 #include <stdio.h>
 2 int func(int, int); // 函数声明
 3 int main() {
 4 int k = 4, m = 1, p1, p2;
 5 p1 = func(k, m); // 函数调用
 6 p2 = func(k, m); // 函数调用
 7 printf("%d, %d\n", p1, p2);
 8 return 0;
 9 }
10 // 函数定义
11 int func(int a, int b) {
12 static int m = 0, i = 2;
13 i += m + 1;
14 m = i + a + b;
15 return m;
16 }

 运行结果与我判断一致

 

总结局部static变量特点:

1.局部性,该变量只能作用于定义其的函数或复合语句中,只有在函数内部有效,离开函数就不能再被使用

2.静态存储,该变量在程序运行期间有系统分配固定的存储空间,当再次进入函数时,使用上次的结果,即具有继承性

 

 

task3.c

 

 1 #include <stdio.h>
 2 long long func(int n); // 函数声明
 3 int main() {
 4     int n;
 5     long long f;
 6     while (scanf_s("%d", &n) != EOF) {
 7         f = func(n); // 函数调用
 8         printf("n = %d, f = %lld\n", n, f);
 9     }
10     return 0;
11 }
12 long long func(int n) {
13     long long f;
14     if (n == 1) f = 1;
15     if (n >=2) {
16         f= 2 * func(n - 1) + 1;
17     }
18     return f;
19 }

 

task4.c

 

 1 #include <stdio.h>
 2 int func(int n, int m);
 3 int main() {
 4     int n, m;
 5     while (scanf_s("%d%d", &n, &m) != EOF)
 6         printf("n = %d, m = %d, ans = %d\n", n, m, func(n, m));
 7     return 0;
 8 }
 9 int func(int n, int m) {
10     int ans;
11     if (n == m) ans = 1;
12     else if (m == 1) ans = n;
13     else if (n == 0) ans = 0;
14     else
15         ans = func(n - 1, m) + func(n - 1, m - 1);
16     return ans;
17 }

 

 

task5_1.c

 

 1 #include <stdio.h>
 2 double mypow(int x, int y); // 函数声明
 3 int main() {
 4     int x, y;
 5     double ans;
 6     while (scanf_s("%d%d", &x, &y) != EOF) {
 7         ans = mypow(x, y); // 函数调用
 8         printf("%d的%d次方: %g\n\n", x, y, ans);
 9     }
10     return 0;
11 }
12 double mypow(int x, int y) {
13     int i ,j;
14     double ans=1.0;
15     if (y >= 0)
16     {
17         for (i = 1; i <= y; i++)
18             ans = ans * x;
19     }
20     else
21     {
22         for (i = 1; i <= ( - y); i++)
23             ans = ans / x;
24     }
25     return ans;
26 }

 

 

task5_2.c

 

 1 #include <stdio.h>
 2 double mypow(int x, int y); // 函数声明
 3 int main() {
 4     int x, y;
 5     double ans;
 6     while (scanf_s("%d%d", &x, &y) != EOF) {
 7         ans = mypow(x, y); // 函数调用
 8         printf("%d的%d次方: %g\n\n", x, y, ans);
 9     }
10     return 0;
11 }
12 double mypow(int x, int y) {
13     double ans;
14     if (y == 0) ans = 1;
15     else if (y > 0) ans = x * mypow(x, y - 1);
16     else ans = 1.0 / mypow(x, (-y));
17     return ans;
18 }

 

 

 

task6.c

 

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 void hanoi(unsigned int n, char from, char temp, char to);
 5 void moveplate(unsigned int n, char from, char to);
 6 int t = 0;
 7 int main() {
 8     unsigned int n;
 9     while ((scanf_s("%u", &n)) != 0)
10 
11     {
12         hanoi(n, 'A', 'B', 'C');
13         printf("\n\n一共移动了%d次", t);
14     }
15 
16     
17     system("pause");
18 
19     return 0;
20 }
21 
22 void hanoi(unsigned int n, char from, char temp, char to) {
23     if (n == 1)
24         moveplate(n, from, to);
25     else {
26         hanoi(n - 1, from, to, temp);
27         moveplate(n, from, to);
28         hanoi(n - 1, temp, from, to);
29     }
30     
31     
32 }
33 
34 void moveplate(unsigned int n, char from, char to) {
35     printf("%u: %c --> %c\n", n, from, to);
36     t = t + 1;
37 }

 

 

tsak7.c

 

 

 1 #include <stdio.h>
 2 int is_prime(int );
 3 int main() {
 4     int i, j;
 5 
 6     for (j = 4; j <= 20; j += 2)
 7     {
 8         for (i = 2; i <= j / 2; i++)
 9         {
10             if (is_prime(i) && is_prime(j - i))
11             {
12                 printf("%d=%d+%d\n", j, i, j - i);
13                 break;
14             }
15         }    
16     }
17     return 0;
18 
19 }
20 int is_prime(int n) {
21     int i = 2, t;
22 
23     for (; n % i != 0 && i <= n; )
24     {
25         i++;
26     }
27     if (n == i)
28         t = 1;
29 
30     else
31         t = 0;
32 
33     return (t);
34 
35 
36 
37         
38 }

 

 

 

task8.c

 1 #include <stdio.h>
 2 #include <math.h>
 3 long func(long s);
 4 int main() {
 5     long s, t;
 6     printf("Enter a number: ");
 7     while (scanf_s("%ld", &s) != EOF) {
 8         t = func(s); // 函数调用
 9         printf("new number is: %ld\n\n", t);
10         printf("Enter a number: ");
11     }
12     return 0;
13 }
14 long func(long s) {
15     int t, n=0, i = 0;
16     while (s != 0)
17     {
18         t = s % 10;
19         if (t % 2 != 0)
20         {
21             n = n+ pow(10, i) * t;
22             i++;
23         }
24         
25         s= s/ 10;
26         
27 
28     }
29     return n;
30     
31 }

 

posted @ 2023-03-30 23:44  shaobky  阅读(24)  评论(0编辑  收藏  举报