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

实验任务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     int line,col,i;
13     char text[N] = "hi, November~";
14     
15     srand(time(0));
16     
17     for(i = 1; i <= 10; ++i){
18         line = rand() % 25;
19         col = rand() % 80;
20         print_text(line,col,text);
21         sleep(1000);
22     }
23     
24     return 0;
25 }
26 
27 void print_spaces(int n){
28     int i;
29     
30     for(i = 1;i <= n; ++i)
31         printf(" ")
32 }
33 
34 void print_blank_lines(int n){
35     int i;
36     
37     for(i = 1; i <= n; ++i)
38         print("\n")
39 }
40 
41 void print_text(int line,int col,char text[]){
42     print_blank_lines(line-1);
43     print_spaces(col-1);
44     printf("%s",text);
45 }

 

运行结果

 功能

在屏幕中随机一个位置生成hi, November,重复执行十次

实验任务2

源代码task2_1.c

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

运行结果

 

源代码task2_1.c(改)

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

运行结果

 源代码task2_2.c

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

运行结果

 一致

实验任务3

源代码task3.c

 1 #include<stdio.h>
 2 long long func(int n);
 3 
 4 int main(){
 5     int n;
 6     long long f;
 7     
 8     while(scanf("%d",&n) != EOF){
 9         f = func(n);
10         printf("n = %d, f = %lld\n",n,f);
11     }
12     
13     return 0;
14 }
15 
16 long long func(int n){
17     int i;
18     long long s = 1;
19     
20     
21     for(i = 1; i <= n; ++i)
22         s = s * 2;
23     
24     return s - 1;
25 }

运行结果

 实验任务4

源代码task4_1.c(迭代)

 1 #include<stdio.h>
 2 int func(int n, int m);
 3 
 4 int main(){
 5     int n, m;
 6     
 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 a, int b){
14     int s = 1;
15     int t = 1;
16     int ans,i,c;
17     
18     c = b;
19     
20     if(a < b)
21         return 0;
22     
23     else if(b == 0 || b == a)
24         return 1;
25         
26     else{
27         for(i = 1;i <= b; ++i){
28         s = s * a;
29         t = t * c;
30         a--;
31         c--;
32         ans = s / t;
33         }
34     
35         return ans;
36     }
37 }

运行结果

 源代码task4_2.c(递归)

 1 #include<stdio.h>
 2 int func(int n, int m);
 3 
 4 int main(){
 5     int n, m;
 6     
 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 a, int b){
14     int s = 1;
15     int t = 1;
16     int ans,i,c;
17     int x;
18     
19     c = b;
20     
21     if(a < b)
22         return 0;
23     
24     else if(b == 0 || b == a)
25         return 1;
26         
27     else{
28         x= func(a-1,b) + func(a-1,b-1);
29         return x;
30     }
31 }

运行结果

 实验任务5

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

运行结果

 实验任务6

源代码task6.c

 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 long func(long s){
19     int m, n;
20     n = 0;
21     while(s != 0){
22         m = s%10;
23         if(m % 2 != 0)
24         n = 10 * n + m;
25         s /= 10;
26     }
27     int a, b;
28     b = 0;
29     while(n != 0){
30         a = n % 10;
31         b = 10 * b + a;
32         n /= 10;
33         
34     }
35     return b;
36 }

运行结果

 

posted @ 2023-10-30 17:26  臣子民  阅读(30)  评论(0编辑  收藏  举报