230330实验三

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

代码实现功能:随机地在某行某列打印字符串,之后再打印n个空格和n行空白行,循环十次,实现类似弹幕的效果

实验2

2.1

// 利用局部static变量的特性,计算阶乘
#include <stdio.h>
long long fac(int n); // 函数声明
int main() {
    int i, n;
    printf("Enter n: ");
    scanf_s("%d", &n);
    for (i = 1; i <= n; ++i)
        printf("%d! = %lld\n", i, fac(i));
    return 0;
}
// 函数定义
long long fac(int n) {
    static long long p = 1;
    printf("p = %lld\n", p);
    p = p * n;
    return p;
}

P作用是存储上一个阶乘的结果.

 

2.2

// 练习:局部static变量特性
#include <stdio.h>
int func(int, int); // 函数声明
int main() {
    int k = 4, m = 1, p1, p2;
    p1 = func(k, m); // 函数调用
    p2 = func(k, m); // 函数调用
    printf("%d, %d\n", p1, p2);
    return 0;
}
// 函数定义
int func(int a, int b) {
    static int m = 0, i = 2;
    i += m + 1;//i=3 i=8+1+3=12
    m = i + a + b;//m=3+a+b=8=p1 m=12+4+1=17
    return m;
}

static变量特性:在调用函数结束时,变量的值依然存在而不是消失,再次调用函数时,变量将以上一次调用结束时的值参与运算而非重新赋值.

实验3

 1 #include <stdio.h>
 2 long long func(int n); // 函数声明
 3 int main() {
 4     int n;
 5     long long f;
 6     while (scanf_s("%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 {
14     long long sum = 1;
15     for (int i = 1; i <= n; i++)
16     {
17         sum = 2*sum;
18     }
19     return sum - 1;
20 }

 

实验4

#include <stdio.h>
int func(int n, int m);
int main() {
    int n, m;
    while (scanf_s("%d%d", &n, &m) != EOF)
        printf("n = %d, m = %d, ans = %d\n", n, m, func(n, m));
    return 0;
}
int func(int n, int m) {
    int ans;
    if (n < m)
        return 0;
    else if (n == m || m == 0)
        return 1;
    else {
        return  func(n - 1, m - 1) + func(n - 1, m);
    }
}

 

实验5

5.1

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

 

5.2

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

 

实验6

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

 

实验7

 1 #include <stdio.h>
 2 #include <math.h>
 3 
 4 int is_prime(int n);    //函数声明,判断素数 
 5 
 6 int main()
 7 {
 8     int even;
 9     even = 1;
10     for (even = 1; even < 21; even++)
11     {
12         for (int i = 1; i <= even / 2; i += 2)
13             if (is_prime(i) && is_prime(even - i))
14                 printf("%d=%d+%d\n", even, i, even - i);
15         
16     }
17     return 0;
18 }
19 
20 // 判断素数,若是返回1,否则返回0 
21 int is_prime(int n)
22 {
23     if (n == 1)
24         return 0;
25     int temp = (int)sqrt(n);
26     for (int i = 2; i <= temp; i++)
27         if (n % i == 0)
28             return 0;
29     return 1;
30 }

 

实验8

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

 

posted @ 2023-03-30 20:25  Orangpetofi  阅读(53)  评论(0)    收藏  举报