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

发布时间 2023-10-31 11:20:22作者: Regec

一,实验目的

二,实验准备

三,实验内容

1,实验任务1

task1.c

 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<=1;++i)
19     {
20         line = rand()%25;
21         col = rand()%80;
22         print_text(line,col,text);
23         Sleep(1000);//暂停1000ms
24     }
25     return 0;
26 }
27 
28 //打印n个空格
29 void print_spaces(int n)
30 {
31     int i;
32 
33     for(i=1;i<=n;++i)
34         printf(" ");
35 }
36 
37 //打印n个空格
38 void print_blank_lines(int n)
39 {
40     int i;
41 
42     for(i=1;i<=n;++i)
43         printf("\n");
44 }
45 
46 //在第line行第col列打印一段文本
47 void print_text(int line,int col,char text[])
48 {
49     print_blank_lines(line-1);
50     print_spaces(col-1);
51     printf("%s",text);
52 }

在随机位置生成文本"hi,November"。

2,实验任务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     for(i=1;i<=n;++i)
13         printf("%d!=%lld\n",i,fac(i));
14 
15     return 0;
16 }
17 
18 //函数定义
19 long long fac(int n)
20 {
21     static long long p = 1;
22 
23     p = p*n;
24 
25     return p;
26 }

 

 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     for(i=1;i<=n;++i)
13         printf("%d!=%lld\n",i,fac(i));
14 
15     return 0;
16 }
17 
18 //函数定义
19 long long fac(int n)
20 {
21     static long long p = 1;
22     printf("p=%lld\n",p);
23     p = p*n;
24 
25     return p;
26 }

task2_2.c

p1=8,p2=17.

 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     return m;
25 }

 static修饰的变量会发生变化。

3,实验任务3

task3.c

 

 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     {
11         f=func(n);//函数调用
12         printf("n=%d,f=%lld\n",n,f);
13     }
14 
15     return 0;
16 }
17 long long func(int n)
18 {
19     long long int ans;
20     if(n==0)
21         ans=0;
22     else
23         ans=2*(func(n-1)+1)-1;
24     return ans;
25 }

 

 

4,实验任务4

task4.1.c

 

 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     return 0;
12 }
13 
14 int func(int n,int m)
15 {
16     int i,s;
17     int a=1,b=1,c=1;
18     for(i=1;i<=n;++i)
19         a*=i;
20     for(i=1;i<=m;++i)
21         b*=i;
22     for(i=1;i<=n-m;++i)
23         c*=i;
24     s=a/(b*c);
25     return s
26 }

 

 task4.2.c

 

 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     return 0;
12 }
13 
14 int func(int n,int m)
15 {
16     int ans;
17     if(n<m)
18         return 0;
19     if(n==0)
20        return 0;
21     if(m==0)
22        return 1;
23     else if(m==1)
24        return n;
25     else
26        ans=func(n-1,m)+func(n-1,m-1);
27     return ans;
28 }

5,实验任务5

task5.c

 

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

6,实验任务6

task6.c

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

 

7,实验任务7

 task7.c

 

 1 #include<stdio.h>
 2 
 3 int main()
 4 {
 5     int A,B;
 6     int i=0,j=0;
 7     int s=1,n=0,m=0;
 8     while(s!=0)
 9     {
10         int a[10],b[10];
11         A=s*s;
12         B=s*s*s;
13         while(A!=0)
14         {
15             a[i]=A%10;
16             A=A/10;
17             ++i;
18             ++n;
19         }
20         while(B!=0)
21         {
22             b[j]=B%10;
23             B=B/10;
24             ++j;
25             ++m;
26         }
27         if(n+m==10)
28         {
29             int count=0;
30             for(i=0;i<n;++i)
31             {
32                 for(j=0;j<m;++j)
33                 {
34                     if(a[i]==b[j])
35                         count++;
36                 }
37             }
38             if(count==0)
39             {
40                 printf("%d",s);
41                 return 0;
42             }
43             else
44             {
45                 ++s;
46                 continue;
47             }
48         }
49         else
50         {
51             ++s;
52             continue;
53         }
54     }
55     return 0;
56 }

 

四,实验结论

见上

五,实验总结