首页 > 其他分享 >实验三

实验三

时间:2023-04-02 12:44:26浏览次数:25  
标签:return int long 实验 func printf include

1.实验任务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     {
13         int line,col,i;
14         char text[N] = "hi, April~";
15     
16         srand(time(0));
17 
18         for(i = 1;i <= 10;++i)
19         {
20             line = rand() % 25;
21             col = rand() % 80;
22             print_text(line,col,text);
23             Sleep(1000);
24         }
25 
26     return 0;
27 
28     }
29 
30 
31 void print_spaces(int n)
32 {
33     int i;
34 
35     for(i = 1;i<=n; ++i)
36         printf(" ");
37 }
38 
39 
40 void print_blank_lines(int n)
41 {
42     int i;
43 
44     for(i = 1;i <= n;++i)
45         printf("\n");
46 }
47 
48 
49 void print_text(int line, int col, char text[])
50 {
51     print_blank_lines(line-1);
52     print_spaces(col-1);
53     printf("%s",text);
54 }

在屏幕上二维坐标点随机生成“hi,April~”,单个语句长度为8个字符,N为80,总共生成十个语句。

2.实验任务2

task2_1.c

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 long long fac(int n);
 4 
 5 int main()
 6 {
 7     int i,n;
 8 
 9     printf("Enter n: ");
10     scanf("%d",&n);
11     for(i = 1;i<=n; ++i)
12         printf("%d! = %lld\n",i,fac(i));
13 
14     system("pause");
15     return 0;
16 }
17 
18 
19 long long fac(int n)
20 {
21     static long long p = 1;
22 
23     p = p * n;
24 
25     return p;
26 }

task2_2.c

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 
 4 int func(int, int );
 5 
 6 int main()
 7 {
 8     int k = 4,m = 1,p1,p2;
 9 
10     p1 = func(k,m);
11     p2 = func(k,m);
12     printf("%d, %d\n",p1,p2);
13 
14     system("pause");
15     return 0;
16 }
17 
18 
19 int func(int a, int b)
20 {
21     static int m = 0,i =2;
22 
23     i = i + m + 1;
24     m = i + a + b;
25 
26     return m;
27 }

局部static变量的特性:数据存储在静态存储区,函数退出时变量始终存在,再次进入该函数时,将不再重新赋值,直接使用上一次的结果。

3.实验任务3

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 
 4 long long func(int n);
 5 
 6 int main()
 7 {
 8     int n;
 9     long long f;
10 
11     while(scanf("%d",&n)!=EOF)
12     {
13         f = func(n);
14         printf("n = %d,f = %lld\n",n,f);
15     }
16 
17     system("pause");
18     return 0;
19 }
20 
21 
22 long long func(int n)
23 {
24     int i=1;
25     long long p=1;
26     for(;i<=n;i++)
27     {
28         p = p * 2;
29 
30     }
31         return p-1;
32 }

4.实验任务4

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

5.实验任务5

task5_1.c

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 double mypow(int x,int y);
 4 
 5 int main()
 6 {
 7     int x,y;
 8     double ans;
 9 
10     while(scanf("%d%d",&x,&y)!=EOF)
11     {
12         ans = mypow(x,y);
13         printf("%d的%d次方:%g\n\n",x,y,ans);
14     }
15 
16     system("pause");
17     return 0;
18 }
19 
20 double mypow(int x,int y)
21 {
22     double s = 1;
23     int i = 1;
24     if(y>=0)
25         {for(;i<=y;i++)
26             {s = s * x;}
27         }
28     if(y<0)
29     {for(;i<=abs(y);i++)
30         {s = s / x;}
31     }
32 
33     return s;
34 }

task5_2.c

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

6.实验任务6

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<math.h>
 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     int m;
10     while(scanf("%u",&n)!=EOF)
11 
12         {
13             m = pow(2,n);
14             hanoi(n,'A','B','C');
15             printf("一共移动了%d次\n\n",m-1);
16         }
17 
18     system("pause");
19     return 0;
20 }
21 void hanoi(unsigned int n,char from,char temp,char to)
22 {
23     if(n==1)
24         moveplate(n,from,to);
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 }

7.实验任务7

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<math.h>
 4 int is_prime(int n);
 5 int main()
 6 {    
 7     int n,i,p = 1,q,p_flag = 1,q_flag = 1;
 8     for(n=2;n<=20;n++)
 9     {    if(n%2 == 1)
10             continue;
11         if(n>2&&n%2 == 0)
12             {    p = 1;
13                 do
14                 {    
15                     p = p + 1;
16                     q = n - p;
17                     p_flag = is_prime(p);
18                     q_flag = is_prime(q);
19                 }while(p_flag * q_flag == 0);
20                 printf("%d = %d + %d\n",n,p,q);
21             }        
22     }
23     system("pause");
24     return 0;
25 }
26 
27 int is_prime(int n)
28 {
29     int i;
30     for(i=2;i<=sqrt(n);++i)
31     {
32         if(n%i==0)
33             break;
34     }
35     if(n>1&&i>sqrt(n))
36         return 1;
37     else
38         return 0;
39 
40 }

8.实验任务8

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 long long func(long s);
 4 int main()
 5 {
 6 
 7     long s,t;
 8 
 9     printf("Enter a numner:");
10     while(scanf("%ld",&s)!=EOF)
11     {
12         t = func(s);
13         printf("new number is: %ld\n\n",t);
14         printf("Enter a number:");
15     }
16 
17     system("pause");
18     return 0;
19 }
20 
21 long long func(long s)
22 {
23     int d;
24     long s1 = 1;
25     long m = 0;
26     while(s>0)
27     {
28         d = s % 10;
29         if(d%2)
30         {
31             m = s1 * d + m;
32             s1 = s1 * 10;
33         }
34         s = s / 10;
35     }
36     return m;
37 }

 

标签:return,int,long,实验,func,printf,include
From: https://www.cnblogs.com/cxj114-514/p/17280250.html

相关文章

  • 202031607221-王彦润 实验一 软件工程准备—博客园技巧与博客首秀
    1、项目和内容简介项目内容班级博客链接2023年春软件工程本次作业要求链接实验一我的课程学习目标注册博客园和Github账号,学习使用博客园,了解Github的基本操作本次作业在哪些方面帮我实现学习目标1.初步了解博客园软件和Github的基本操作;初步了解学......
  • 202031603143-郭思彤 实验一 软件工程准备-对软件工程的初步了解
    项目内容班级博客链接20级卓越班本次作业要求链接实验一软件工程准备我的课程学习目标1.深入使用博客园进行学习2.了解Github的基本操作3.拓展关于软件工程的知识本次作业在哪些方面帮我实现学习目标1.学会了使用markdown编辑器的基本操作2.通过提问......
  • 202031607128-张政文 实验一 软件工程准备
    1、项目和内容简介项目内容班级博客链接2023年春软件工程(2020级计算机科学与技术)(西北师范大学-计算机科学与工程学院)本次作业要求链接实验一软件工程准备我的课程学习目标注册博客园和Github账号,学习使用博客园,了解Github的基本操作。本次作业在哪些......
  • 202031607230-王格 实验一 软件工程准备--构建之法与博客首秀
    实验一软件工程准备项目内容班级博客链接2023年春软件工程本次作业要求链接实验一软件工程准备我的课程学习目标1.学习博客园软件开发者学习社区使用技巧和经验。2.了解Github工具的基本操作3.阅读《现代软件工程—构建之法》,深入了解什么是软件工程......
  • 实验一 软件工程准备
    1、项目和内容简介 项目内容班级博客链接https://edu.cnblogs.com/campus/xbsf/2020CSSE本次作业要求链接https://edu.cnblogs.com/campus/xbsf/2020CSSE/homework/12938我的课程学习目标注册博客园和Github账号,学习使用博客园,了解Github的基本操作。本次作......
  • 实验三
    实验三task.1 程序源代码#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);//函数声明voidprint_spaces(intn);//函数声明voidprint_blank_lines(i......
  • 实验三 函数
    实验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);//函数声明intma......
  • 三月三十日第三次实验
    实验一:#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,c......
  • 202031607327-杨辽辽 实验一 软件工程准备 初步了解软件工程
    202031607327-杨辽辽实验一软件工程准备初步了解软件工程项目内容班级博客链接20级卓越班本次作业要求链接实验一软件工程准备我的课程学习目标1.学习博客园软件开发者学习社区使用技巧和经验;2.了解Github的基本操作;本次作业在哪些方面帮我实现学习......
  • 实验2
    实验目的1.知道Python中字符串的表示,熟练使用索引、切片、常见字符串操作2.知道Python中列表的表示,熟练使用索引、切片、常见列表操作,能正确使用列表推导式3.针对具体问题场景,能够灵活、组合使用字符串、列表、控制语句编程解决实际问题实验准备实验前,请练习/复习以下内容:4......