silverbullet4869

实验3 C语言函数应用编程

实验任务1

复制代码
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 #include <windows.h>
 5 #define N 80
 6 
 7 void print_text(int line, int col, char text[]);  // 函数声明 
 8 void print_spaces(int n);  // 函数声明 
 9 void print_blank_lines(int n); // 函数声明 
10 
11 int main() {
12     int line, col, i;
13     char text[N] = "hi, April~";
14     
15     srand(time(0)); // 以当前系统时间作为随机种子
16     
17     for(i = 1; i <= 10; ++i) {
18         line = rand() % 25;
19         col =  rand() % 80;
20         print_text(line, col, text);
21         Sleep(1000);  // 暂停1000ms
22     }
23     system("pause");
24     return 0; 
25 }
26 
27 // 打印n个空格 
28 void print_spaces(int n) {
29     int i;
30     
31     for(i = 1; i <= n; ++i)
32         printf(" ");
33 }
34 
35 // 打印n行空白行
36 void print_blank_lines(int n) {
37     int i;
38     
39     for(i = 1; i <= n; ++i)
40         printf("\n");
41  } 
42 
43 // 在第line行第col列打印一段文本 
44 void print_text(int line, int col, char text[]) {
45     print_blank_lines(line-1);      // 打印(line-1)行空行 
46     print_spaces(col-1);            // 打印(col-1)列空格
47     printf("%s", text);         // 在第line行、col列输出text中字符串
48 }
复制代码

             

 

程序实现功能为每隔1s在第line行第col列打印文本“hi,April~”,共生成十段文本(由主函数中循环控制),每段文本输出由print_text函数实现,其中line和col由随机数生成,print_spaces函数和print_blank_lines函数起定位作用,分别负责打印n个空格和n行空白行。

实验任务2

task2_1

复制代码
 1 // 利用局部static变量的特性,计算阶乘
 2 
 3 #include <stdio.h>
 4 #include<stdlib.h>
 5 long long fac(int n); // 函数声明
 6 
 7 int main() {
 8     int i, n;
 9 
10     printf("Enter n: ");
11     scanf("%d", &n);
12 
13     for (i = 1; i <= n; ++i)
14         printf("%d! = %lld\n", i, fac(i));
15     system("pause");
16     return 0;
17 }
18 
19 // 函数定义
20 long long fac(int n) {
21     static long long p = 1;
22 
23     p = p * n;
24     return p;
25 }
复制代码

复制代码
 1 // 利用局部static变量的特性,计算阶乘
 2 
 3 #include <stdio.h>
 4 #include<stdlib.h>
 5 long long fac(int n); // 函数声明
 6 
 7 int main() {
 8     int i, n;
 9 
10     printf("Enter n: ");
11     scanf("%d", &n);
12 
13     for (i = 1; i <= n; ++i)
14         printf("%d! = %lld\n", i, fac(i));
15     system("pause");
16     return 0;
17 }
18 
19 // 函数定义
20 long long fac(int n) {
21     static long long p = 1;
22     printf("p=%11d\n",p);
23     p = p * n;
24     return p;
25 }
复制代码

task2_2

复制代码
 1 // 练习:局部static变量特性
 2 
 3 #include <stdio.h>
 4 #include<stdlib.h>
 5 int func(int, int);        // 函数声明
 6 
 7 int main() {
 8     int k = 4, m = 1, p1, p2;
 9 
10     p1 = func(k, m);    // 函数调用
11     p2 = func(k, m);    // 函数调用
12     printf("%d, %d\n", p1, p2);
13     system("pause");
14     return 0;
15 }
16 
17 // 函数定义
18 int func(int a, int b) {
19     static int m = 0, i = 2;
20 
21     i += m + 1;
22     m = i + a + b;
23 
24     return m;
25 }
复制代码

 局部static变量在程序运行的整个生命周期中只会被分配一次地址内存,其值保持不变,如task2_2中,m和i的值在函数调用的过程中保持不变,始终保持上一次调用结束时的状态。

实验任务3

复制代码
 1 #include <stdio.h>
 2 #include<stdlib.h>
 3 long long func(int n); // 函数声明
 4 
 5 int main() {
 6     int n;
 7     long long f;
 8 
 9     while (scanf("%d", &n) != EOF) {
10         f = func(n); // 函数调用
11         printf("n = %d, f = %lld\n", n, f);
12     }
13     system("pasue");
14     return 0;
15 }
16 
17 // 函数定义
18 long long func(int n)
19 {
20     long long f;
21     if(n==1)
22     {
23         f=1;
24     }
25     else
26     {
27         f=2*func(n-1)+1;
28     }
29     return f;
30 }
复制代码

实验任务4

迭代方式

复制代码
 1 #include <stdio.h>
 2 #include<stdlib.h>
 3 int func(int n, int m);
 4 
 5 int main() {
 6     int n, m;
 7 
 8     while(scanf("%d%d", &n, &m) != EOF)
 9         printf("n = %d, m = %d, ans = %d\n", n, m, func(n, m));
10     system("pause");
11     return 0;
12 }
13 
14 // 函数定义
15 int func(int n,int m)
16 {
17     int i,s=1,k=1;
18     if(m>n)
19     {
20         return 0;
21     }
22     else if(m==0&&n==0)
23     {
24         return 1;
25     }
26     else
27     {
28         for(i=1;i<=m;i++)
29         {
30             s*=n-i+1;
31             k*=i;
32         }
33         return s/k;
34     }
35 }
复制代码

递归方式

复制代码
 1 #include <stdio.h>
 2 #include<stdlib.h>
 3 int func(int n, int m);
 4 
 5 int main() {
 6     int n, m;
 7 
 8     while(scanf("%d%d", &n, &m) != EOF)
 9         printf("n = %d, m = %d, ans = %d\n", n, m, func(n, m));
10     system("pause");
11     return 0;
12 }
13 
14 // 函数定义
15 int func(int n,int m)
16 {
17     if(m>n)
18     {
19         return 0;
20     }
21     else if(m==n||m==0)
22     {
23         return 1;
24     }
25     else
26     {
27         int i;
28         i=func(n-1,m)+func(n-1,m-1);
29         return i;
30     }
31 }
复制代码

实验任务5

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

实验任务6

复制代码
 1 #include <stdio.h>
 2 #include<stdlib.h>
 3 #include <math.h>
 4 long func(long s);   // 函数声明
 5 
 6 int main() {
 7 
 8     long s, t;
 9 
10     printf("Enter a number: ");
11     while (scanf("%ld", &s) != EOF) {
12         t = func(s); // 函数调用
13         printf("new number is: %ld\n\n", t);
14         printf("Enter a number: ");
15     }
16     system("pause");
17     return 0;
18 }
19 
20 // 函数定义
21 long func(long s)
22 {
23     long long m=0,n=1,k;
24     while (s>0) 
25     {
26         k = s%10; 
27 
28         if (k%2!=0) {
29             m=k*n+m;
30             n*=10;
31         }
32         s/=10;
33     }
34 
35     return m;
36 }
复制代码

实验任务7

 

复制代码
 1 #include <stdio.h>
 2 #include<stdlib.h>
 3 int main() 
 4 {
 5     int num,i,k,m,n,s;
 6     for (num=10;num<=99;num++) 
 7     {
 8         int square[10]={0}; 
 9         int cube[10]={0};   
10         m=num*num;
11         n=num*num*num;
12         s=1;
13 
14         while(m>0)
15         {
16             k=m%10;
17             square[k]++;
18             m/=10;
19         }
20 
21 
22         while(n>0) 
23         {
24             k=n%10;
25             cube[k]++;
26             n/=10;
27         }
28 
29         for(i=0;i<10;i++)
30         {
31             if (square[i]+cube[i]!=1)
32             {
33                 s=0;
34                 break;
35             }
36         }
37 
38         if(s)
39         {
40             printf("找到的数字为:%d\n",num);
41             break;
42         }
43     }
44     system("pause");
45     return 0;
46 }
复制代码

实验总结

  1. 逐渐熟悉递归函数的应用,之后需要进一步加强练习,尝试将一些迭代问题用递归思想解决;
  2. 敲代码过程中最大困难还是将实际问题抽象成数学模型解决,面对复杂问题容易找不到出发点,也还需要更多练习。

posted on   SilverBullet4869  阅读(54)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示