首页 > 其他分享 >实验3

实验3

时间:2023-03-30 23:57:21浏览次数:37  
标签:int long char 实验 func ans include

task1.c

 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_blank_lines(int n); 
 8 int main() {
 9     int line, col, i;
10     char text[N] = "hi, April~";
11     srand(time(0)); 
12     for (i = 1; i <= 10; ++i) {
13         line = rand() % 25;
14         col = rand() % 80;
15         print_text(line, col, text);
16         Sleep(1000); 
17     }
18     return 0;
19 }
20 void print_spaces(int n) {
21     int i;
22     for (i = 1; i <= n; ++i)
23         printf(" ");
24 }
25 void print_blank_lines(int n) {
26     int i;
27     for (i = 1; i <= n; ++i)
28         printf("\n");
29 }
30 void print_text(int line, int col, char text[]) {
31     print_blank_lines(line - 1);
32     print_spaces(col - 1);
33     printf("%s", text); 
34 }

 

代码作用:

以系统时间为随机种子生成随机数,打印随机空格和换行,并打印字符串,进行十次

 

 

task2_1.c

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

 

 

task2_2.c

 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 printf("%d, %d\n", p1, p2);
 8 return 0;
 9 }
10 // 函数定义
11 int func(int a, int b) {
12 static int m = 0, i = 2;
13 i += m + 1;
14 m = i + a + b;
15 return m;
16 }

 运行结果与我判断一致

 

总结局部static变量特点:

1.局部性,该变量只能作用于定义其的函数或复合语句中,只有在函数内部有效,离开函数就不能再被使用

2.静态存储,该变量在程序运行期间有系统分配固定的存储空间,当再次进入函数时,使用上次的结果,即具有继承性

 

 

task3.c

 

 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     long long f;
14     if (n == 1) f = 1;
15     if (n >=2) {
16         f= 2 * func(n - 1) + 1;
17     }
18     return f;
19 }

 

task4.c

 

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

 

 

task5_1.c

 

 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     int i ,j;
14     double ans=1.0;
15     if (y >= 0)
16     {
17         for (i = 1; i <= y; i++)
18             ans = ans * x;
19     }
20     else
21     {
22         for (i = 1; i <= ( - y); i++)
23             ans = ans / x;
24     }
25     return ans;
26 }

 

 

task5_2.c

 

 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     double ans;
14     if (y == 0) ans = 1;
15     else if (y > 0) ans = x * mypow(x, y - 1);
16     else ans = 1.0 / mypow(x, (-y));
17     return ans;
18 }

 

 

 

task6.c

 

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 void hanoi(unsigned int n, char from, char temp, char to);
 5 void moveplate(unsigned int n, char from, char to);
 6 int t = 0;
 7 int main() {
 8     unsigned int n;
 9     while ((scanf_s("%u", &n)) != 0)
10 
11     {
12         hanoi(n, 'A', 'B', 'C');
13         printf("\n\n一共移动了%d次", t);
14     }
15 
16     
17     system("pause");
18 
19     return 0;
20 }
21 
22 void hanoi(unsigned int n, char from, char temp, char to) {
23     if (n == 1)
24         moveplate(n, from, to);
25     else {
26         hanoi(n - 1, from, to, temp);
27         moveplate(n, from, to);
28         hanoi(n - 1, temp, from, to);
29     }
30     
31     
32 }
33 
34 void moveplate(unsigned int n, char from, char to) {
35     printf("%u: %c --> %c\n", n, from, to);
36     t = t + 1;
37 }

 

 

tsak7.c

 

 

 1 #include <stdio.h>
 2 int is_prime(int );
 3 int main() {
 4     int i, j;
 5 
 6     for (j = 4; j <= 20; j += 2)
 7     {
 8         for (i = 2; i <= j / 2; i++)
 9         {
10             if (is_prime(i) && is_prime(j - i))
11             {
12                 printf("%d=%d+%d\n", j, i, j - i);
13                 break;
14             }
15         }    
16     }
17     return 0;
18 
19 }
20 int is_prime(int n) {
21     int i = 2, t;
22 
23     for (; n % i != 0 && i <= n; )
24     {
25         i++;
26     }
27     if (n == i)
28         t = 1;
29 
30     else
31         t = 0;
32 
33     return (t);
34 
35 
36 
37         
38 }

 

 

 

task8.c

 1 #include <stdio.h>
 2 #include <math.h>
 3 long func(long s);
 4 int main() {
 5     long s, t;
 6     printf("Enter a number: ");
 7     while (scanf_s("%ld", &s) != EOF) {
 8         t = func(s); // 函数调用
 9         printf("new number is: %ld\n\n", t);
10         printf("Enter a number: ");
11     }
12     return 0;
13 }
14 long func(long s) {
15     int t, n=0, i = 0;
16     while (s != 0)
17     {
18         t = s % 10;
19         if (t % 2 != 0)
20         {
21             n = n+ pow(10, i) * t;
22             i++;
23         }
24         
25         s= s/ 10;
26         
27 
28     }
29     return n;
30     
31 }

 

标签:int,long,char,实验,func,ans,include
From: https://www.cnblogs.com/shaobky/p/17273542.html

相关文章

  • 实验3
    实验任务1实验代码#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);voidprint_spaces(intn);voidprint_blank_lines(intn);intmain(){ intlin......
  • 实验3
    task1程序源码#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);voidprint_spaces(intn);voidprint_blank_lines(intn);intmain(){ intline,co......
  • ChCore—实验 3:进程与线程、异常处理 部分记录
    思考题1:内核从完成必要的初始化到用户态程序的过程是怎么样的?尝试描述一下调用关系。内核启动到用户程序启动的流程:main├──uart_init├──mm_init├──arch_interrupt_init├──create_root_thread│├──create_root_cap_group│├──__create_......
  • 实验3
    task1#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);//函数声明voidprint_spaces(intn);//函数声明voidprint_blank_lines(intn);//函数......
  • 实验3
    实验任务一:#include<time.h>#include<stdio.h>#include<stdlib.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);voidprint_spaces(intn);voidprint_blank_lines(intn);intmain(){intline,co......
  • 230330实验三
    实验1 1#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#include<windows.h>5#defineN806voidprint_text(intline,intcol,chartext[]);//函数声明7voidprint_spaces(intn);//函数声明8voidprint_blank_l......
  • Exp3-免杀原理 实验报告—20201229赵斌
    免杀原理与实践基础问题回答Q1:杀软是如何检测出恶意代码的?(1)基于特征码的检测特征码是一段或多段数据,如果一个可执行文件(或其他运行的库、脚本等)包含这样的数据则被认为是恶意代码。AV软件厂商要做的就是尽量搜集最全的、最新的特征码库。所以杀毒软件的更新很重要。过时的特......
  • 实验三
    实验三实验任务1实验代码#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);voidprint_spaces(intn);voidprint_blank_lines(intn);intmain(){in......
  • 博客首发Cisco实验
    第一步(划分VLAN):  主机方:     1.根据划分好的子网表格填写主机IP、子网掩码、默认网关。  2.添加给每个部门划分的VLAN,给VLAN配置IP、子网掩码:    interfacevlan70      ipaddress192.168.2.226255.255.255.240      n......
  • Python计算机视觉基础实验3-显著性检测(HC&FC)
    一、实验基础图像显著性检测图像的显著性是指对于给定一副真实世界中的场景图像,人们会自动地识别出感兴趣区域,忽略掉不感兴趣的区域,即将注意力集中在图像中某些显著的部分区域。图像的注意预测,也称视觉显著性检测,指通过智能算法模拟人的视觉系统特点,预测人类的视觉凝视点(就是全神贯......