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

发布时间 2023-11-03 17:11:50作者: 孙雨婷

1、实验任务1

源代码

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 #include <windows.h>
 5 #define N 80
 6 void print_text(int line, int col, char text[]); // 函数声明
 7 void print_spaces(int n); // 函数声明
 8 void print_blank_lines(int n); // 函数声明
 9 int main() {
10     int line, col, i;
11     char text[N] = "hi, November~";
12     
13     srand(time(0)); // 以当前系统时间作为随机种子
14     
15     for(i = 1; i <= 10; ++i) {
16         line = rand() % 25;
17         col = rand() % 80;
18         print_text(line, col, text);
19         Sleep(1000); // 暂停1000ms
20     }
21     return 0;
22 }
23 // 打印n个空格
24 void print_spaces(int n) {
25     int i;
26     
27     for(i = 1; i <= n; ++i)
28         printf(" ");
29 }
30 
31 // 打印n行空白行
32 void print_blank_lines(int n) {
33     int i;
34     
35     for(i = 1; i <= n; ++i)
36     printf("\n");
37 }
38 
39 // 在第line行第col列打印一段文本
40 void print_text(int line, int col, char text[]) {
41     print_blank_lines(line-1); // 打印(line-1)行空行
42     print_spaces(col-1); // 打印(col-1)列空格
43     printf("%s", text); // 在第line行、col列输出text中字符串
44 }

运行结果截图

实验结论:字符串的随机出现

2、实验任务2

源代码

 1 // 利用局部static变量的特性,计算阶乘
 2 #include <stdio.h>
 3 long long fac(int n); // 函数声明
 4 int main() {
 5     int i, n;
 6     printf("Enter n: ");
 7     scanf("%d", &n);
 8     for (i = 1; i <= n; ++i)
 9         printf("%d! = %lld\n", i, fac(i));
10     return 0;
11 }
12 // 函数定义
13 long long fac(int n) {
14     static long long p = 1;
15     printf("p=%lld\n",p);
16     p = p * n;
17     return p;
18 }

运行结果截图

源代码

 1 // 练习:局部static变量特性
 2 #include <stdio.h>
 3 int func(int, int); // 函数声明
 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     printf("%d%d\n",m,i);
17     i += m + 1;
18     m = i + a + b;
19     return m;
20 }

运行结果截图

局部static变量特性:static变量可以使变量的访问范围控制在函数内,并且保存变量值至下一次调用,所占内存在程序运行结束时释放。即下一次调用时变量初始化值为上一次调用的最终值。

3、实验任务3

源代码

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

运行结果截图

4、实验任务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 int func(int n,int m){
13     int i,j;
14     int up=1,down=1;
15     for(i=0;i<=m-1;i++)
16         up*=n-i;
17     for(j=0;j<=m-1;j++)
18         down*=m-j;
19     return up/down;
20 }

运行结果截图

源代码

 

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

 

5、实验任务5

源代码

 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 long long func(int n);
 6 
 7 int main() {
 8     unsigned int n;
 9     while(scanf("%u",&n)!=EOF){/*输入盘子数目*/
10         hanoi(n,'A','B','C');
11         printf("一共移动了%lld次\n",func(n));
12     }
13         system ("pause");
14         return 0;
15 
16 }
17 void hanoi(unsigned int n,char from,char temp,char to){
18     int i=0;
19     i++;
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     int i;
31     printf("%u:%c-->%c\n",n,from,to);
32 }
33  long long func(int n){
34     if(n==0)
35        return 0;
36     else
37         return (func(n-1)+1)*2-1;
38 }

运行结果截图

6、实验任务6

源代码

#include <stdio.h>
#include <math.h>
#define N 10000
long func(long s);   // 函数声明
  
int main() {
  
    long s, t;
  
    printf("Enter a number: ");
    while (scanf("%ld", &s) != EOF) {
         t=func(s);
        printf("new number is: %ld\n\n", t);
        printf("Enter a number: ");
    }
 
    return 0;
 }
 long func(long s){
    int length=0,k=0,m=0,t=0,j=1;
    k=s;
    while(s){
         s=s/10;
         length++;
    }
    s=k;
    int i;
    for(i=1;i<=length;i++)
    {
        m=s%10;
        s=(s-m)/10;
        if(m%2!=0){
            t+=m*j;
            j*=10;
        }
        else
            continue;
    }
    return t; 
 }
 

运行结果截图

7、实验任务7

源代码

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 
 4 int square(int n) ;
 5 int cube(int n);
 6 int bubble_sort(int a[]);
 7 
 8 int main()
 9 {
10     int n=1,sq,cu,a[10],i;
11     while(1){
12     sq=square(n);
13     cu=cube(n);
14     i=0;
15     while(sq!=0){
16         a[i]=sq%10;
17         sq=sq/10;
18         i++;
19     }
20     while(cu!=0){
21         
22         a[i]=cu%10;
23         cu=cu/10;
24         i++;
25     }
26     bubble_sort(a);
27     if(a[0]==0&&a[1]==1&&a[2]==2&&a[3]==3&&a[4]==4&&a[5]==5&&a[6]==6&&a[7]==7&&a[8]==8&&a[9]==9)
28         break;
29     n++;
30 }
31     printf("%d\n",n);
32     
33     system("pause");
34     return 0;    
35 }
36 int square(int n){
37     n=n*n;
38     return n;
39 }
40 int cube(int n){
41     n=n*n*n;
42     return n;
43 }
44 int bubble_sort(int a[10]){
45     int i,j,t;
46     for(i=0;i<9;i++)
47         for(j=0;j<9-i;j++)
48             if(a[j]>a[j+1])
49             {
50                 t=a[j];
51                 a[j]=a[j+1];
52                 a[j+1]=t;
53             } 
54 }

运行结果截图