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

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

时间:2023-10-31 11:22:49浏览次数:35  
标签:10 函数 int 编程 long C语言 func printf include

一,实验目的

二,实验准备

三,实验内容

1,实验任务1

task1.c

 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,November";
15 
16     srand(time(0));//以当前系统时间作为随机种子
17 
18     for(i=1;i<=1;++i)
19     {
20         line = rand()%25;
21         col = rand()%80;
22         print_text(line,col,text);
23         Sleep(1000);//暂停1000ms
24     }
25     return 0;
26 }
27 
28 //打印n个空格
29 void print_spaces(int n)
30 {
31     int i;
32 
33     for(i=1;i<=n;++i)
34         printf(" ");
35 }
36 
37 //打印n个空格
38 void print_blank_lines(int n)
39 {
40     int i;
41 
42     for(i=1;i<=n;++i)
43         printf("\n");
44 }
45 
46 //在第line行第col列打印一段文本
47 void print_text(int line,int col,char text[])
48 {
49     print_blank_lines(line-1);
50     print_spaces(col-1);
51     printf("%s",text);
52 }

在随机位置生成文本"hi,November"。

2,实验任务2

task2_1.c

 1 //利用局部static变量的特性,计算阶乘
 2 
 3 #include<stdio.h>
 4 long long fac(int n);//函数声明
 5 
 6 int main()
 7 {
 8     int i,n;
 9 
10     printf("Enter n:");
11     scanf("%d",&n);
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 {
21     static long long p = 1;
22 
23     p = p*n;
24 
25     return p;
26 }

 

 1 //利用局部static变量的特性,计算阶乘
 2 
 3 #include<stdio.h>
 4 long long fac(int n);//函数声明
 5 
 6 int main()
 7 {
 8     int i,n;
 9 
10     printf("Enter n:");
11     scanf("%d",&n);
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 {
21     static long long p = 1;
22     printf("p=%lld\n",p);
23     p = p*n;
24 
25     return p;
26 }

task2_2.c

p1=8,p2=17.

 1 //练习:局部static变量特性
 2 
 3 #include<stdio.h>
 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     return 0;
15 }
16 
17 //函数定义
18 int func(int a,int b)
19 {
20     static int m=0,i=2;
21 
22     i+=m+1;
23     m=i+a+b;
24     return m;
25 }

 static修饰的变量会发生变化。

3,实验任务3

task3.c

 

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

 

 

4,实验任务4

task4.1.c

 

 1 #include<stdio.h>
 2 int func(int n,int m);
 3 
 4 int main()
 5 {
 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    
11     return 0;
12 }
13 
14 int func(int n,int m)
15 {
16     int i,s;
17     int a=1,b=1,c=1;
18     for(i=1;i<=n;++i)
19         a*=i;
20     for(i=1;i<=m;++i)
21         b*=i;
22     for(i=1;i<=n-m;++i)
23         c*=i;
24     s=a/(b*c);
25     return s
26 }

 

 task4.2.c

 

 1 #include<stdio.h>
 2 int func(int n,int m);
 3 
 4 int main()
 5 {
 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    
11     return 0;
12 }
13 
14 int func(int n,int m)
15 {
16     int ans;
17     if(n<m)
18         return 0;
19     if(n==0)
20        return 0;
21     if(m==0)
22        return 1;
23     else if(m==1)
24        return n;
25     else
26        ans=func(n-1,m)+func(n-1,m-1);
27     return ans;
28 }

5,实验任务5

task5.c

 

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

6,实验任务6

task6.c

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

 

7,实验任务7

 task7.c

 

 1 #include<stdio.h>
 2 
 3 int main()
 4 {
 5     int A,B;
 6     int i=0,j=0;
 7     int s=1,n=0,m=0;
 8     while(s!=0)
 9     {
10         int a[10],b[10];
11         A=s*s;
12         B=s*s*s;
13         while(A!=0)
14         {
15             a[i]=A%10;
16             A=A/10;
17             ++i;
18             ++n;
19         }
20         while(B!=0)
21         {
22             b[j]=B%10;
23             B=B/10;
24             ++j;
25             ++m;
26         }
27         if(n+m==10)
28         {
29             int count=0;
30             for(i=0;i<n;++i)
31             {
32                 for(j=0;j<m;++j)
33                 {
34                     if(a[i]==b[j])
35                         count++;
36                 }
37             }
38             if(count==0)
39             {
40                 printf("%d",s);
41                 return 0;
42             }
43             else
44             {
45                 ++s;
46                 continue;
47             }
48         }
49         else
50         {
51             ++s;
52             continue;
53         }
54     }
55     return 0;
56 }

 

四,实验结论

见上

五,实验总结

 

标签:10,函数,int,编程,long,C语言,func,printf,include
From: https://www.cnblogs.com/lze0389/p/17798087.html

相关文章

  • 响应式编程
    响应式编程(ReactiveProgramming)什么是ReactorReactor是一个用于JVM的完全非阻塞的响应式编程框架,具备高效的需求管理(即对“背压(backpressure)”的控制)能力。它与Java8函数式API直接集成,比如CompletableFuture,Stream,以及Duration。它提供了异步序列APIFlux(用于[N]个......
  • python url 网址链接写函数()括号里不能访问显示403
    说明一则奇怪问题。同一个url网址链接,写到函数里就访问失败,写到变量里就可以正常访问。一、文件名test_url.pydefr_http(url): response=requests.get(url,headers=u_headers) print(f"response.status_code={response.status_code}")二、url直接写到调用的函数中,......
  • C# Socket网络编程
    入门级C#Socket编程实现只要知道要通信的两台主机的IP地址和进程的端口号,然后可以用Socket让这两个进程进行通信。在本机上运行服务端和客户端,ip为127.0.0.1,使用端口9050(0~1023的端口号通常用于一些比较知名的网络服务和应用,普通网络应用程序则应该使用1024以上的端口号,以避免......
  • 欧拉函数 & 欧拉定理
    欧拉函数互质:对于\(\foralla,b\in\mathbb{N}\),若\(a,b\)的最大公因数为\(1\),则称\(a,b\)互质。欧拉函数:即$\varphi(N)$,表示从\(1\)到\(N\)中与\(N\)互质的数的个数。在算术基本定理中,任何一个大于\(1\)的整数都可以唯一分解为有限个质数的乘积,......
  • 无涯教程-C语言 - 输入(Input)&输出(Output)
     C编程提供了一组内置函数来读取给定的输入,并根据需要将其输入到程序中。getchar()和putchar()函数intgetchar(void)  - 函数从屏幕读取下一个可用字符,并将其作为整数返回。intputchar(intc)  - 函数将传递的字符放在屏幕上,并返回相同的字符。#include<stdi......
  • 实验3 C语言函数应用编程
    实验任务11#include<stdio.h>2#include<math.h>3#include<stdlib.h>4#include<time.h>5#include<windows.h>6#defineN807voidprint_text(intline,intcol,chartext[]);8voidprint_spaces(intn);9voidpr......
  • 重要的函数及方法
    (个人解题方法,可能有更高效的)输入方法input():都是以字符串的形式输入1、单行单个数据输入a=eval(input())#一般我习惯使用input()搭配eval()使用,也可直接将eval换成已经知道要转化类型的类型名#eg:a=int(input())补:eval()用法:将以字符串输入的格式转化为原本数据......
  • 无涯教程-C语言 - 结构(Struct)
    数组允许定义变量的类型,这些变量可以容纳相同种类的多个数据项。同样,结构是C中可用的另一种用户定义的数据类型,它允许组合不同种类的数据项。结构Struct用于表示记录,假设您想定义图书馆中的书籍,您可能需要定义跟踪有关每本书的以下属性-标题title作者author主题subject图......
  • 【Azure Function App】如何修改Azure函数应用的默认页面呢?
    问题描述当在Azure中创建了一个函数应用(FunctionApp)后,访问默认URL会得到一个默认的页面。是否有办法修改这个默认页面呢?  问题解答在之前的博文中,介绍了修改AppService的默认页面。1:【Azure应用服务】AppService默认页面暴露Tomcat版本信息,存在安全风险 :https://www.cnbl......
  • C语言的system("pause")是什么
    ......