首页 > 其他分享 >实验三

实验三

时间:2024-04-27 11:56:01浏览次数:16  
标签:10 int long char 实验 func include

实验任务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, April~";
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);  // 暂停1000ms
22     }
23     
24     return 0; 
25 }
26 
27 // 打印n个空格 
28 void print_spaces(int n) {
29     int i;
30     
31     for(i = 1; i <= n; ++i)
32         printf(" ");
33 }
34 
35 // 打印n行空白行
36 void print_blank_lines(int n) {
37     int i;
38     
39     for(i = 1; i <= n; ++i)
40         printf("\n");
41  } 
42 
43 // 在第line行第col列打印一段文本 
44 void print_text(int line, int col, char text[]) {
45     print_blank_lines(line-1);      // 打印(line-1)行空行 
46     print_spaces(col-1);            // 打印(col-1)列空格
47     printf("%s", text);         // 在第line行、col列输出text中字符串
48 }

程序功能为每隔1秒在随机在第line行第col列打印“hi,April~”,共打印十个。

 

实验任务2

task2_1

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

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

task2_2

 1 // 练习:局部static变量特性
 2 
 3 #include <stdio.h>
 4 int func(int, int);        // 函数声明
 5 
 6 int main() {
 7     int k = 4, m = 1, p1, p2;
 8 
 9     p1 = func(k, m);    // 函数调用
10     p2 = func(k, m);    // 函数调用
11     printf("%d, %d\n", p1, p2);
12 
13     return 0;
14 }
15 
16 // 函数定义
17 int func(int a, int b) {
18     static int m = 0, i = 2;
19 
20     i += m + 1;       
21     m = i + a + b;    
22 
23     return m;
24 }

局部static变量是在编译时赋初值的,在程序运行时它已经具有值,以后每次调用函数时不再重新赋初值而保留上次函数调用结束时的值。

 

实验任务3

 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 long long func(int n)
16 {
17        if(n==0)
18        return 0;
19     else
20        return 2*(func(n-1)+1)-1;
21 } 

 

实验任务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 {
14     int i;
15     double s;
16     
17     for(i=1;i<=m-1;i++)
18        n=n*(n-i);
19        
20     for(i=1;i<m;i++)
21        m=m*(m-i);
22     
23     s=1.0*n/m;
24     
25     return s;
26 }

递归方式

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

 

实验任务5

 1 #include <stdio.h>
 2 #include<math.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 main() 
 7 {
 8     unsigned int n;
 9     while(scanf("%u",&n)!=EOF)
10     {
11         int m;
12         hanoi(n,'A','B','C');
13         m=pow(2,n)-1;
14         printf("\n一共移动了%d次.\n\n",m);
15     }
16     return 0;
17 }
18 
19 void hanoi(unsigned int n,char from,char temp,char to)
20 {
21     if(n==1)
22     {
23         moveplate(n,from,to);
24     }
25     else
26     {
27         hanoi(n-1,from,to,temp);
28         moveplate(n,from,to);
29         hanoi(n-1,temp,from,to);
30     }
31 }
32 void moveplate(unsigned int n,char from,char to)
33 {
34     printf("%u:%c --> %c\n",n,from,to);
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     long ans;
21     long digit,t;
22     ans=0;
23     t=1;
24     
25     while(s){
26         digit=s%10;
27         if(digit%2){
28             ans+=t*digit;
29             t*=10;
30         }
31         s/=10;
32     }
33     return ans;    
34 }

 

标签:10,int,long,char,实验,func,include
From: https://www.cnblogs.com/wxy0105/p/18161890

相关文章

  • 实验一 二手平台原型设计
    墨刀、Axure、Mockplus等原型设计工具优缺点分析:一、墨刀优点:在轻量级的移动端原型制作更加迅速,展示更加方便。缺点:价格较贵,不能画流程图,相对于其他两款功能还不是很全面;应用局限性,专注于app原型设计,在后台和网页稍有乏力;归档能力不足,更倾向于链接、二维码形式输出,不能以文档输......
  • 实验三
    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);//......
  • 实验一———美团APP
    墨刀、Axure、Mockplus等原型设计工具优缺点分析:一、墨刀优点:在轻量级的移动端原型制作更加迅速,展示更加方便。缺点:价格较贵,不能画流程图,相对于其他两款功能还不是很全面;应用局限性,专注于app原型设计,在后台和网页稍有乏力;归档能力不足,更倾向于链接、二维码形式输出,不能以文档输......
  • 实验三
    一、实验题目:软件测试二、实验目的 1、熟悉开发环境下的自动化测试工具;1、利用自动化测试工具进行自动化单元测试。三、实验内容1、选择开发环境,IDEA或PYCHARM任选其一;2、基于所选择的开发环境实现对输入的n个整数进行排序的代码;3、对所编写代码设计测试用例;4、基于所选......
  • 实验3 C语言函数应用编程
    task1.c1#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#include<windows.h>5#defineN8067voidprint_text(intline,intcol,chartext[]);//函数声明8voidprint_spaces(intn);//函数声明9voidpri......
  • 实验三.软件测试
    一、实验题目:软件测试二、实验目的1、熟悉开发环境下的自动化测试工具;1、利用自动化测试工具进行自动化单元测试。三、实验内容1、选择开发环境,IDEA或PYCHARM任选其一;2、基于所选择的开发环境实现对输入的n个整数进行排序的代码;3、对所编写代码设计测试用例;4、基于所选择......
  • 实验3 C语言函数应用编程
    task1#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);//函数声明voidprint_spaces(intn);//函数声明voidprint_blank_lines(intn);//函数声明intmain(){ intline,col,i; chartext[N]=......
  • 实验三
    一、实验题目:软件测试二、实验目的1、熟悉开发环境下的自动化测试工具;2、利用自动化测试工具进行自动化单元测试。三、实验内容1、选择开发环境,IDEA或PYCHARM任选其一;2、基于所选择的开发环境实现对输入的n个整数进行排序的代码;3、对所编写代码设计测试用例;4、基于所选择......
  • 实验3
    1#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#include<windows.h>5#defineN8067voidprint_text(intline,intcol,chartext[]);89voidprint_spaces(intn);//函数声明1011voidprint_blank_li......
  • 实验3
    task1#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);voidprint_spaces(intn);voidprint_blank_lines(intn);intmain(){intline,col,i;chartext[N]="hi,April~";......