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

发布时间 2023-11-05 20:10:22作者: 侯岩宏

实验任务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 {
13     int line,col,i;
14     char text[N]="hi,November~";
15     
16     srand(time(0));//以当前系统时间作为随机种子
17     
18     for(i=1;i<=10;++i){
19         line=rand()%25;
20         col=rand()%80;
21         print_text(line,col,text);
22         Sleep(1000);//暂停1000s 
23     } 
24     
25     return 0;
26  } 
27  
28  //打印n个空格
29  void print_spaces(int n){
30      int i;
31      
32      for(i=1;i<=n;++i)
33         printf(" ");
34  } 
35  
36  //打印n行空白行
37  void print_blank_lines(int n){
38      int i;
39      
40      for(i=1;i<=n;++i)
41         printf("\n");
42  } 
43  
44  //在第line行第col列打印一段文本
45  void print_text(int line,int col,char text[]){
46      print_blank_lines(line-1);//打印(line-1)行空行 
47      print_spaces(col-1);//打印(col-1)列空格 
48      printf("%s",text);//在第line行、col列输出text中字符串 
49  } 

运行截图:

实验任务2

task2-1.c

源代码:

 1 //利用局部static变量的特性,计算阶乘
 2 
 3 #include <stdio.h>
 4 long long fac(int n);//函数声明
 5 
 6 int main()
 7 {
 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     
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-2.c

源代码:

 1 //练习:局部static变量特性
 2 
 3 #include <stdio.h>
 4 int func(int,int);//函数声明
 5 
 6 int main()
 7 {
 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     return 0; 
15  } 
16  
17  //函数定义
18 int func(int a,int b)
19 {
20     static int m=0,i=2;
21     
22     i+=m+1;
23     m=i+a+b;
24     
25     return m;
26  } 

运行截图:

实验任务3

源代码:

 1 #include <stdio.h>
 2 long long func(int n);  //函数声明 
 3 
 4 int main()   
 5 {
 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     
14     return 0;
15 }
16 
17 //函数定义
18 int long long func(int n){
19      if(n==0){
20          return 0;
21      }else{
22          return 2*func(n-1)+1;
23          
24      }
25 } 

运行截图:

实验任务4

task 4-1

源代码:

 1 #include <stdio.h>
 2 int func(int n,int m);
 3 
 4 int main()
 5 {
 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     
12     return 0;
13 }
14 
15 int func(int n,int m)
16 {
17     if(n<m){
18         return 0;
19     }else if(m==0){
20         return 1;
21     }else{
22         return func(n-1,m)+func(n-1,m-1);
23     }
24 }

运行截图:

 task 4-2

源代码:

 1 #include <stdio.h>
 2 int func(int n,int m);
 3 
 4 int main()
 5 {
 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     
12     return 0;
13 }
14 int func(int n,int m)
15 {
16     long long f=1;
17     for(int i=1;i<=m;i++){
18         f=f*(n-i+1)/i;
19     }   
20     
21     return f;
22 }

运行截图:

实验任务5

源代码:

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

运行截图:

实验任务6

源代码:

 1 #include <stdio.h>
 2 #include <math.h>
 3 long func(long s);
 4 
 5 int main()
 6 {
 7     long s,t;
 8     
 9     printf("Enter a number:");
10     while(scanf("%ld",&s)!=EOF){
11         t=func(s);
12         printf("new number is:%ld\n\n",t);
13         printf("Enter a number:");
14     }
15     
16     return 0;
17 }
18 
19 long func(long s)
20 {
21     int n,m;
22     long long k=0,d=0;
23     
24     while(s>0){
25         m=s%10;
26         if(m%2==0)
27            printf(" ");
28         else
29            k=k*10+m;
30            s=s/10;
31     }
32     
33     while(k>0){
34         n=k%10;
35         d=d*10+n;
36         k=k/10;
37     }
38     
39     return d;
40  } 

运行截图: