实验3 函数

四、实验结论
1. 实验任务1
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 #include <windows.h>
 5 #define N 80
 6 
 7 void printText(int line,int col,char text[]);
 8 void printSpaces(int n);
 9 void printBlankLines(int n);
10 
11 int main()
12 {
13     int line,col,i;
14     char text[N]="hi,May~";
15     
16     srand(time(0));
17     
18     for(i=1;i<=10;++i)
19     {
20         line=rand()%25;
21         col=rand()%80;
22         printText(line,col,text);
23         Sleep(1000);
24     }
25     
26     return 0;
27 }
28 
29 //打印n个空格 
30 void printSpaces(int n)
31 {
32     int i;
33         
34     for(i=1;i<=n;++i)
35     printf(" ");
36 }
37  
38 //打印n行空白行   
39 void printBlankLines(int n)
40 {
41     int i;
42         
43     for(i=1;i<=n;++i)
44     printf("\n");
45 }
46 
47 //在第line行第col列打印一段文本    
48 void printText(int line,int col,char text[])
49 {
50     printBlankLines(line-1);
51     printSpaces(col-1);
52     printf("%s",text);
53 }

功能:打印十个hi,May~,行数在0~24内随机取直,列数在0~79内随机取值(有点像那种动态文字屏保)

 

2. 实验任务2

task2_1

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

task2_2

 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 }

  实验运行结果与我理论分析得到的结果一致
 
总结局部static变量的特性
(1)它占据⼀个永久性的存储单元。随着⽂件的存在⽽存在。
(2)静态局部变量是在编译时赋初值,在程序执⾏期间,⼀旦存储单元中的值改变,就不会再执⾏赋初值的语句。未赋初值的变量其值为0。
 
3. 实验任务3
 1 //提供n的数值,返回该函数计算式的结果
 2 #include <stdio.h>
 3 long long fun(int n); 
 4 int main()
 5 {
 6     int n;
 7     long long f;
 8     while (scanf("%d", &n) != EOF)
 9     {
10         f = fun(n); 
11         printf("n = %d, f = %lld\n", n, f);
12     }
13  
14         return 0;
15 }
16     
17 long long fun(int n)
18 {   
19     if(n==1)
20     return 1;
21     else
22     return fun(n-1)*2+1; 
23 }

4. 实验任务4
 1 //hanoi塔问题 
 2 #include<stdio.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 i;
 6 int main()
 7 {
 8     unsigned int n;
 9     while(scanf("%u",&n)!=EOF)             //输入盘子数目 
10     {
11        i=0;          
12        hanoi(n,'A','B','C');
13        printf("\n一共移动了%d次\n\n",i);
14     }
15     return 0; 
16  } 
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);      //n-1个盘子从A以C为中转移到B上 
25          moveplate(n,from,to);         //将盘子n从A移到C上 
26          hanoi(n-1,temp,from,to);      //将n-1个盘子从B以A为中转移到C上 
27      }
28  }
29  
30  void moveplate(unsigned int n,char from,char to)
31  {
32      i++;
33      printf("%u:%c-->%c\n",n,from,to);
34   }

5. 实验任务5
 1 //验证哥德巴赫猜想 
 2 #include<stdio.h>
 3 
 4 int is_Prime(int n);
 5 int main()
 6 { 
 7   int n,p,q,flag=0,mark=0;
 8   for(n=4;n<=20;n+=2)
 9   {
10       for(p=2;p<=n;p++)
11       {
12           
13         q=n-p;
14         flag=is_Prime(p);
15         mark=is_Prime(q);
16  
17     if(flag==1&&mark==1)
18     {
19         printf("%d = %d + %d \n",n,p,q);
20         break;
21     }
22 
23     }
24   }
25   return 0;
26 }
27 int is_Prime(int n)
28 {    
29     int i;
30     for(i=2;i<n-1;i++)
31     { 
32        if(n%i==0)
33         break;
34     }
35 
36     if(i>=n-1)
37         return 1;
38     else
39         return 0;
40 
41 }

6. 实验任务6
 1 //取出长整型数的奇数位构成新数 
 2 #include<stdio.h>
 3 long fun(long s);
 4 int main()
 5 {
 6     long s, t;
 7     printf("enter a number:");
 8     while (scanf("%ld", &s) != EOF)
 9     {
10         t = fun(s);
11         printf("new number is: %1d\n\n", t);
12         printf("enter a number:");
13     }
14     return 0;
15 }
16 long fun(long s)
17 {
18     int k,t=0,m=1;
19     while (s != 0)
20     {
21         k = s % 10;
22         if (k % 2 == 1)
23         {
24             t = t + k * m;
25             m = 10 * m;
26         }
27         s = s / 10;
28     }
29     return t;
30 }

 

 

posted @ 2022-04-21 11:12  liuxianer  阅读(35)  评论(0)    收藏  举报