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

发布时间 2023-11-04 15:31:42作者: 夏夜#

任务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     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 
35 void print_blank_lines(int n) {
36     int i;
37     
38     for(i = 1; i <= n; ++i)
39         printf("\n");
40  } 
41 
42 
43 void print_text(int line, int col, char text[]) {
44     print_blank_lines(line-1);       
45     print_spaces(col-1);            
46     printf("%s", text);         
47 }

实现了随机生成“hi,November~”在屏幕上的功能。

任务2

 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     for(i=1;i<=n;++i)
10         printf("%d!=%lld\n",i,fac(i));
11         
12         return 0;
13 } 
14 
15 long long fac(int n){
16     static long long p=1;
17     
18     p= p*n;
19     printf("%lld\n",p); 
20     return p;
21 }

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

 

任务3

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

 

任务4

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

 

任务5

 

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 void hano(unsigned int n,char from,char temp,char to);
 4 void move(unsigned int n,char from,char to);
 5 long long func(unsigned int n);
 6 int main()
 7 {
 8     unsigned int n;
 9     scanf("%u",&n);
10     hano(n,'A','B','C');
11     printf("共进行了%lld次",func(n));
12     return 0;
13 }
14 void hano(unsigned int n,char from,char temp,char to){
15     if(n==1){
16         move(n,from,to);
17     }
18     else{
19         hano(n-1,from,to,temp);
20         move(n,from,to);
21         hano(n-1,temp,from,to);
22     }
23 }
24 void move(unsigned int n,char from,char to){
25     printf("%u:%c-->%c\n",n,from,to);
26 }
27 
28 long long func(unsigned int n){
29     long long int ans;
30     if(n==1)
31         ans=1;
32     else
33         ans=2*(func(n-1)+1)-1;
34     return ans;
35 }

 

任务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 long func(long s) {
19     
20 
21     long t=0,ans = 0;
22     int i;
23     while (s > 0) {
24          i = s % 10;
25         if (i % 2 == 1) {
26             ans = ans * 10 + i;
27         }
28         s /= 10;
29     }
30     while(ans>0){
31         t=ans%10+t*10;
32         ans/=10;
33     }