实验3 函数应用编程

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

程序的功能:随机在第1-25行第1-79l列打印“hi,April”重复上述操作10次。

 

2.实验任务2

task2_1.c

 1 #include<stdio.h>
 2 long long fac(int n);//函数声明
 3 
 4 int main()
 5 {
 6     int i,n;
 7 
 8     printf("Enter n:");
 9     scanf("%d",&n);
10     for(i=1;i<=n;++i)
11         printf("%d!=%lld\n",i,fac(i));
12     return 0;
13 }
14 
15 //函数定义
16 long long fac(int n)
17 {
18     static long long p=1;
19     //printf("p=%lld\n",p);
20     p=p*n;
21     return p;
22 }

运行结果:

 

task2_1.c增加一行代码 即printf("p=%lld\n',p);

 1 #include<stdio.h>
 2 long long fac(int n);//函数声明
 3 
 4 int main()
 5 {
 6     int i,n;
 7 
 8     printf("Enter n:");
 9     scanf("%d",&n);
10     for(i=1;i<=n;++i)
11         printf("%d!=%lld\n",i,fac(i));
12     return 0;
13 }
14 
15 //函数定义
16 long long fac(int n)
17 {
18     static long long p=1;
19     printf("p=%lld\n",p);
20     p=p*n;
21     return p;
22 }

运行结果:

task2_2.c

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

运行结果:

static变量的特性:
(1)只赋初值1次。

(2)静态局部变量:函数退出时,变量始终存在,但不能被其他函数使用;当再次进入该函数时,将使用上次的结果,其他与局部变量一样。

 

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

运行结果:

 

4.实验任务4

task4.c

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

运行结果:

 

5.实验任务5

task5_1.c

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

运行结果:

 

task5_2.c

 1 #include<stdio.h>
 2 double mypow(int x,int y);//函数声明
 3 
 4 int main()
 5 {
 6     int x,y;
 7     double ans;
 8 
 9     while(scanf("%d%d",&x,&y)!=EOF)
10     {
11         ans=mypow(x,y);//函数调用
12         printf("%d的%d的次方:%g\n\n",x,y,ans);//g:按e、f格式中较短的一种输出
13     }
14     return 0;
15 }
16 //函数定义
17 double mypow(int x,int y)
18 {
19     if(y==0)
20         return 1.0;
21     if(y>0)
22         return 1.0*x*mypow(x,y-1);
23     if(y<0)
24         return 1.0/(mypow(x,-y-1)*x);
25 }

运行结果:

 

6.实验任务6

task6.c

 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 int count=0;
 6 int main()
 7 {
 8     unsigned int n;
 9     while(scanf("%u",&n)!=EOF)
10     {
11         hanoi(n,'A','B','C');
12         printf("一共移动了%d次\n",count);
13         count=0;
14         system("pause");
15     }
16     return 0;
17 }
18 void hanoi(unsigned int n,char from,char temp,char to)
19 {
20     if(n==1)
21         moveplate(n,from,to);
22     else
23     {
24         hanoi(n-1,from,to ,temp);
25         moveplate(n,from,to);
26         hanoi(n-1,temp,from,to);
27     }
28 }
29 void moveplate(unsigned int n,char from,char to)
30 {
31     printf("%u:%c-->%c\n",n,from,to);
32     count+=1;
33 }

运行结果:

 

7.实验任务7

task7.c

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<math.h>
 4 int isprime(int n);
 5 int main()
 6 {
 7     int n,i;
 8     while(scanf("%d",&n)!=EOF)
 9     {
10         for(i=2;i<=n/2;i++)
11             {if(isprime(i)&&isprime(n-i)&&(i!=1)&&((n-i)!=1))
12                 printf("%d=%d+%d\n",n,i,n-i);
13             }
14     }
15 }
16 int isprime(int n)
17 {
18     int k;
19     for(k=2;k<=sqrt(1.0*n);k++)
20         if(n%k==0)
21             return 0;
22     return 1;
23 }

运行结果:

 

8.实验任务8

task8.c

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

运行结果:

 

posted @ 2023-04-01 13:35  202113020120张艳  阅读(25)  评论(0编辑  收藏  举报