首页 > 编程语言 >实验3 c语言函数应用编程

实验3 c语言函数应用编程

时间:2023-10-31 19:35:57浏览次数:24  
标签:10 函数 int 编程 long char 实验 func include

task1

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

1 结果

task 2

2 源代码

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

 

2结果

task2_2

源代码

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

 

结果

 task3_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     }
14 
15     return 0;
16 }
17 long long func(int n){
18     int i;
19     long long p = 1;
20     if(n==0)
21         p = 0;
22     else
23         p = func(n-1)*2+1;
24     return p;
25 
26 }

 

结果

task 3_4

源代码

 迭代

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

 

结果

递归

 

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

 task3_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 int i =0;
 6 int main(){
 7     unsigned int n;
 8     
 9     
10     while(scanf("%u",&n)!=EOF)
11     {
12         hanoi(n,'A','B','C');
13         printf("一共移动了%d次",i);
14         i=0;
15         
16         
17     }
18     system("pause");
19      return 0;
20 
21 }
22 void hanoi(unsigned int n ,char from,char temp,char to)
23 {
24     
25     if(n==1){
26         moveplate(n,from,to);
27        
28     }
29     else
30     {
31         hanoi(n-1,from,to,temp);
32         
33         moveplate(n,from,to);
34         hanoi(n-1,temp,from,to);
35         
36     }
37 }
38 void moveplate(unsigned int n,char from,char to)
39 {
40     
41     printf("%u:%c-->%c\n",n,from,to);
42     i++;
43 }

 task3_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 numberm is:%ld\n\n",t);
13         printf("Enter a number:");
14     
15     
16     }
17 
18     return 0;
19 }
20 long func(long s){
21     int n,m;
22     long long k=0,d=0;
23     
24     while(s>0){
25         m=s%10;
26         if(m%2==0)
27             printf(" ");
28         else
29             k=k*10+m;
30         s=s/10;
31 
32     }
33     while(k>0){
34         n=k%10;
35         d=d*10+n;
36         k=k/10;
37     
38     
39     
40     }
41     return d;
42 
43 }

 

结果

 

标签:10,函数,int,编程,long,char,实验,func,include
From: https://www.cnblogs.com/meng36/p/17798205.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);//函数声明int......
  • 05. UDP编程
    一、什么是UDP协议  相对于TCP协议,UDP协议则是面向无连接的协议。使用UDP协议时,不需要建立连接,只需要知道对象的IP地址和端口号,就可以直接发数据包。但是,数据无法保证一定到达。虽然用UDP传输数据不可靠,但它的优点是比TCP协议的速度快。对于不要求可靠到达的数据而......
  • 学编程如何赚钱?翔知盈
    在技术公司工作 。当你掌握了编程技能,你可以在一些技术公司中找到工作,例如Google、Microsoft、IBM等,这些公司通常提供高薪和良好的福利。做游戏开发者 。如果你对游戏开发感兴趣,你可以尝试开发自己的游戏,并在Steam、App Store等平台上销售或提供付费服务。做网站开发者 。网......
  • 无代码:让编程不再有门槛
    随着数字化时代的到来,一种全新的开发方式正在崛起,它就是无代码开发。通过简单的拖拽和配置,无代码平台-kepler能让没有编程经验的人,快速构建出功能强大的应用程序,从而改变了传统软件开发的模式。Kepler无代码是一种可视化编程工具,让用户通过拖拽组件和配置参数等方式,快速构建应用程......
  • Kotlin: 高阶函数与Lambda表达式 (转)
    Kotlin:高阶函数与Lambda表达式(转)本文转自:https://rengwuxian.com/kotlin-lambda/看到一篇写得不错的关于kotlin高阶函数与Lambda的博文,特转载之。1.Kotlin的高阶函数Kotlin很方便,但有时候也让人头疼,而且越方便的地方越让人头疼,比如Lambda表达式。很多人因为Lamb......
  • 【Python微信机器人】第三篇:使用ctypes调用进程函数和读取内存结构体
    目录修整目前的系列目录(后面会根据实际情况变动):在windows11上编译python将python注入到其他进程并运行注入Python并使用ctypes主动调用进程内的函数和读取内存结构体使用汇编引擎调用进程内的任意函数利用beaengine反汇编引擎的c接口写一个pyd库,用于实现inlinehook利用......
  • 01_jQuery 核心函数和选择器
    目录一、jQuery概述1.1、什么是框架?1.2、主流的前端框架(UI/JS)框架1.2.1、JS框架库1.2.2、UI框架1.2.3、框架和类库的区别1.3、什么是jQuery1.4、jQuery的特点1.5、jQuery的优缺点1.6、为什么要使用jQuery二、jQuery入门2.1、jQuery下载和引入2.1.1、jQuery的下载2.1.2、jQuery的......
  • 使用函数的选择法排序
    本题要求实现一个用选择法对整数数组进行简单排序的函数。函数接口定义:voidsort(inta[],intn);其中a是待排序的数组,n是数组a中元素的个数。该函数用选择法将数组a中的元素按升序排列,结果仍然在数组a中。裁判测试程序样例:#include<stdio.h>#defineMAXN10voids......
  • 实验三
    #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()......
  • 使用EMR+DLF+OSS-HDFS进行数据湖分析(阿里云实验)
    实验地址:https://developer.aliyun.com/adc/scenario/exp/f7cf565798e34710acf483ba56e6ebf6hadoopfs操作oss#上传文件hadoopfs-putlogtail.shoss://u-5stubb6d.cn-shanghai.oss-dls.aliyuncs.com/#新建目录hadoopfs-mkdiross://u-5stubb6d.cn-shanghai.oss-dls.aliyu......