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

发布时间 2023-10-31 15:34:38作者: Sunria

1.实验任务1

task1源代码:

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

task1运行截图:

功能:随机空行/列打印“hi,november”,并停顿1秒,产生动画效果。

 

2.实验任务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 
16     system("pause");
17     return 0;
18 }
19 
20 // 函数定义
21 long long fac(int n) {
22     static long long p = 1;
23 
24     p = p * n;
25 
26     return p;
27 }

task2_1运行截图:

在25行添加【printf("p=%lld\n",p);】,运行截图:

 

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 
14     system("pause");
15     return 0;
16 }
17 
18 // 函数定义
19 int func(int a, int b) {
20     static int m = 0, i = 2;
21 
22     i += m + 1;
23     m = i + a + b;
24 
25     return m;
26 }

task2_2运行截图:

总结static变量特性:static静态局部变量,使变量的生命周期与程序生命周期相同,程序结束时才销毁。离开定义它的函数后不能使用,但再次调用定义它的函数时,又可以继续使用,而且保留了前次被调用后留下的值。

 

3.实验任务3

task3源代码:

 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("pause");
14     return 0;
15 }
16 
17 // 函数定义
18 long long func(int n){
19     long long m;
20     if(n>=2){
21         m=(func(n-1)+1)*2-1;
22     }
23     else
24         m=1;
25 
26     return m;}

task3运行代码:

 

4.实验任务4

task4_1源代码:

 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     
11     system("pause");
12     return 0;
13 }
14 
15 // 函数定义
16 int func(int n,int m){
17     int i,s;
18     int a=1,b=1;
19     for(i=n;i>=(n-m+1);--i)
20         b=b*i;
21     for(i=1;i<=m;++i)
22         a=a*i;
23     s=b/a;
24     return s;}

task4_1运行截图:

task4_2源代码:

 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     
11     system("pause");
12     return 0;
13 }
14 
15 // 函数定义
16 int func(int n,int m){
17     int s;
18     if(m==n)
19         s=1;
20     else if(m>n)
21         s=0;
22     else if(m==0)
23         s=1;
24     else if(m==1)
25         s=n;
26     else
27         s=func(n-1,m)+func(n-1,m-1);
28     return s;}

task4_2运行截图:

 

 

5.实验任务5

task5源代码:

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

task5运行截图:

 

6.实验任务6

task6源代码:

 1 long func(long s);//函数声明
 2 
 3 int main()
 4 {
 5     long s,t;
 6 
 7     printf("Enter a number:");
 8     while (scanf("%ld",&s)!=EOF){
 9         t=func(s);
10         printf("new number is:%ld\n\n",t);
11         printf("Enter a number:");
12     }
13     system("pause");
14     return 0;
15 }
16 
17 long func(long s)
18 {
19     int a,b;
20     int i=1,c=0;
21     do
22     {
23         a=s%10;
24         s=s/10;
25         b=a%2;
26         if(b!=0)
27         {
28             c=a*i+c;
29             i*=10;
30         }
31     }while(s>=1);
32 
33     return c;
34 }

task6运行截图:

 

7.实验任务7

task7源代码:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int main()
 5 {
 6     //从47开始,平方、立方才能用到10个数字    
 7     int t,a,b,c,d,e,m,n,p,q,z;
 8     for(t=47;t<100;++t)
 9     {
10         b=t*t*t;
11         m=b%10;
12         b=b/10;
13         n=b%10;
14         if(m!=n){
15             b=b/10;
16             p=b%10;
17             if(p!=n&&p!=m){
18                 b=b/10;
19                 q=b%10;
20                 if(q!=m&&q!=n&&q!=p){
21                     b=b/10;
22                     z=b%10;
23                     if(z!=m&&z!=n&&z!=p&&z!=q){
24                         b=b/10;
25                         if(b!=m&&b!=n&&b!=p&&b!=q&&b!=z){
26                             a=t*t;
27                             c=a%10;
28                             a=a/10;
29                             d=a%10;
30                             if(c!=d&&c!=m&&c!=n&&c!=p&&c!=q&&c!=z&&c!=b){
31                                 a=a/10;    
32                                 e=a%10;
33                                 if(e!=c&&e!=d&&e!=d&&e!=m&&e!=n&&e!=p&&e!=q&&e!=z&&e!=b){
34                                     a=a/10;
35                                     if(a!=c&&a!=d&&a!=e&&a!=m&&a!=n&&a!=p&&a!=q&&a!=z&&a!=b){
36                                         printf("找到数字%d\n",t);}}}}}}}}
37     }
38                                     
39     system("pause");
40     return 0;
41 }

task7运行截图:

 

总结:歪瑞古德!