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

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

时间:2024-04-26 21:55:42浏览次数:19  
标签:10 return 函数 int 编程 long C语言 func include

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     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 }

 

程序实现的功能:在25行,80列的屏幕上,随机生成10个不同位置的hi, April~文本,每一次输出间隔为1s,实现动态输出

task2_1.c

 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 }

task2_2.c

 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 }

理论分析输出结果为8 17

 局部static变量的特性:对变量只进行第一次的初始化,之后保留变量变化后的值

task3.c

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

task4.c

 实验方式1:迭代

 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 
13 // 函数定义
14 int func(int n, int m){
15     int up=1,down=1;
16     int i,j;
17     if(m>n)
18     return 0;
19     for(i=n-m+1;i<=n;i++){
20         up*=i;
21     }
22     for(j=1;j<=m;j++){
23         down*=j;
24     }
25     return up/down;
26 }

实验方式2:递归

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

task5.c

 

 1 #include<stdio.h>
 2 void hanoi(unsigned int n,char from,char temp,char to,int *c);
 3 void moveplate(unsigned nth,char from,char to,int *c);
 4 int main(){
 5     unsigned int n;
 6     while(scanf("%u",&n)!=EOF){
 7         int c=0;
 8         hanoi(n,'A','B','C',&c);
 9         printf("\n一共移动了%d次.\n",c);
10     }
11     return 0;
12 }
13 void hanoi(unsigned int n,char from,char temp,char to,int *c){
14     int count=0;
15     if(n==1)
16         moveplate(n,from,to,c);
17     else{
18         hanoi(n-1,from,to,temp,c);
19         moveplate(n,from,to,c);
20         hanoi(n-1,temp,from,to,c);
21     }
22 }
23 void moveplate(unsigned nth,char from,char to,int *c){
24     printf("%u:%c-->%c\n",nth,from,to,c);
25     (*c)++;
26 }

task6.c

 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 
19 // 函数定义
20 long func(long s){
21     int a,t=1,ans=0;
22     while(s!=0){
23         a=s%10;
24         if(a%2==1){
25             ans+=a*t;
26             t*=10;
27         }
28         s=s/10;
29     }
30     return ans;
31 }

 

实验总结:

1.利用指针在函数里改变主函数的值,来计算次数

标签:10,return,函数,int,编程,long,C语言,func,include
From: https://www.cnblogs.com/qq2055939904/p/18150562

相关文章

  • 自定义单链表队列的基本接口函数(非循环队列)
    单链表构建队列的接口函数/********************************************************************文件名称: 单链表构建队列的接口函数文件作者:[email protected]创建日期:2024/04/26文件功能:对单链表循环队列的增删改查功能的定义注意事项:NoneCop......
  • vue箭头函数、js-for循环、事件修饰符、摁键事件和修饰符、表单控制、完整购物车版本
    【箭头函数】1<!DOCTYPEhtml>2<htmllang="en">3<head>4<metacharset="UTF-8">5<title>Title</title>6<scriptsrc="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">&l......
  • 实验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]=......
  • C语言数据结构:链式队列的创建及其出入队操作
    /**********************************************************************************************************该程序实现链式队列元素的增删改查,目的是提高设计程序的逻辑思维,另外为了提高可移植性,所以链式队列中元素*的数据类型为DataType_t,用户可以根据实际情况修改......
  • 一个生成函数的小结论
    数学能力太弱导致的.求\[[x^n]\frac{1}{\prod_{i=0}^m(1-(u+iv)x)}\]根据EI哥哥的博客\[\def\e{\mathrm{e}}[x^n]\frac{1}{\prod_{i=0}^m(1-(u+iv)x)}=\left[\frac{x^{n+m}}{(n+m)!}\right]\frac{\e^{ux}(\e^{vx}-1)^m}{v^mm!}=\frac{1}{v^mm!}\sum_{k=0}^......
  • SQL窗口分析函数使用详解系列三之偏移量类窗口函数
    1.综述本文以HiveSQL语法进行代码演示。对于其他数据库来说同样也适用,比如SparkSQL,FlinkSQL以及Mysql8,Oracle,SqlServer等传统的关系型数据库。已更新第一类聚合函数类,点击这里阅读①SQL窗口函数系列一之聚合函数类②SQL窗口函数系列二之分组排序窗口函数本节介绍Hive窗口分......
  • 手写bind函数
    今天无事手写一个bind函数//重写bind函数Function.prototype.bindDemo=function(){//arguments可以获取到传的参数//1.先把获取到的数据转换为数组的格式letargs=Array.prototype.slice.call(arguments);//2.获取数组中第一个元素,即this即将指向的数据le......
  • javascript高级编程系列 - 使用fetch发送http请求
    fetch采用模块化设计,api分散在多个对象上(Response对象,Request对象,Header对象),fetch通过数据流(stream对象)处理数据可以分块读取,有利于提高网站性能。发送GET请求fetch函数只传递一个url,默认以get方法发送请求。promisefetch(url).then(response=>response.json()).......
  • day25-索引和函数及存储过程
    1.索引在数据库中索引最核心的作用是:加速查找。例如:在含有300w条数据的表中查询,无索引需要700秒,而利用索引可能仅需1秒。mysql>select*frombigwherepassword="81f98021-6927-433a-8f0d-0f5ac274f96e";+----+---------+---------------+------------------------------......
  • gdb 根据c语言二进制文件进程号查看内部多线程任务
    C语言二进制文件a编译时添加了-g(gdb调试),但是gdba这种方式有时不容易复现一些场景。这时可以先正常启动a,然后根据a的进程号启动gdb调试。#1.找到程序进程号psaux|grepa#2.使用GDB附加到该进程sudogdb-p[PID]#3.使用infothreads命令来列出......