C语言封装函数算法整理

1. 输入两个数,找出两个数的最大公约数和最小公倍数——

#include <stdio.h>

int GetComDivNum(int x, int y)
{
	int i = 0;
	int min = 0;

	min = x < y ? x : y;
	for (i = min; i >= 1; i--)
	{
		if (0 == x % i && 0 == y % i)	
		{
			break;
		}
	}

	return i;
}

int GetComMulNum(int x, int y)
{
	int i = 0;
	int max = 0;

	max = x > y ? x : y;
	for (i = max; i <= x * y; i++)
	{
		if (0 == i % x && 0 == i % y)
		{
			break;
		}
	}

	return i;
}

int main(void)
{
	int num1 = 0;
	int num2 = 0;
	int divnum = 0;
	int mulnum = 0;

	scanf("%d%d", &num1, &num2);

	divnum = GetComDivNum(num1, num2);
	mulnum = GetComMulNum(num1, num2);

	printf("最大公约数:%d\n", divnum);
	printf("最小公倍数:%d\n", mulnum);
	
	return 0;
}

2. 输入年、月、日,获得该年剩余多少天——

#include <stdio.h>

int IsLeepYear(int tmpyear)
{
	if ((0 == tmpyear % 4 && tmpyear % 100 != 0) || (0 == tmpyear % 400))
	{
		return 1;
	}

	return 0;
}

int GetDayOfYear(int tmpyear, int tmpmon, int tmpday)
{
	int i = 0;
	int sum = 0;
  	int a1[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
   	int a2[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    
    if (IsLeepYear(tmpyear))
    {
        for (i = 0; i < tmpmon-1; i++)
        {
            sum += a1[i];
        }
    }else
    {
        for (i = 0; i < tmpmon-1; i++)
        {
            sum += a2[i];
        }
    }
	sum += tmpday;

	return sum;
}

int GetLeftDayOfYear(int tmpyear, int tmpmon, int tmpday)
{
	int sum = 0;
	
	if (IsLeepYear(tmpyear))
	{
		sum = 366;
	}
	else 
	{
		sum = 365;
	}
	
	return sum - GetDayOfYear(tmpyear, tmpmon, tmpday);
}

int main(void)
{
	int year = 0;
	int mon = 0;
	int day = 0;
	int leftday = 0;

	scanf("%d%d%d", &year, &mon, &day);

	leftday = GetLeftDayOfYear(year, mon, day);
	printf("%d年剩余%d天\n", year, leftday);

	return 0;
}

3. 封装函数,以数组传递形式实现strlen、strcpy、strcat、strcmp——

#include <stdio.h>

int Mystrlen(char pstr[])
{
	int i = 0;
	int cnt = 0;

	while (pstr[i] != '\0')
	{
		cnt++;
		i++;
	}

	return cnt;
}

int Mystrcpy(char pdst[], char psrc[])
{
	int i = 0;

	while (psrc[i] != '\0')
	{
		pdst[i] = psrc[i];
		i++;
	}
	pdst[i] = '\0';

	return 0;
}

int Mystrcat(char pstr1[], char pstr2[])
{
	int i = 0;
	int j = 0;

	while (pstr1[i] != '\0')
	{
		i++;
	}

	while (pstr2[j] != '\0')
	{
		pstr1[i] = pstr2[j];
		i++;
		j++;
	}
	pstr1[i] = '\0';

	return 0;
}

int Mystrcmp(char pstr1[], char pstr2[])
{
	int i = 0;

	while (pstr1[i] == pstr2[i] && pstr1[i] != '\0')
	{
		i++;
	}

	return pstr1[i] - pstr2[i];
}

int main(void)
{
	char str[32] = {0};
	char dst[32] = {0};
	int len = 0;
	int ret = 0;

	gets(str);

	len = Mystrlen(str);
	printf("len = %d\n", len);

	Mystrcpy(dst, str);
	printf("dst = %s\n", dst);
	
	Mystrcat(dst, str);
	printf("dst = %s\n", dst);

	ret = Mystrcmp(dst, str);
	printf("ret = %d\n", ret);

	return 0;
}

4. 封装函数,以指针传递形式实现strlen、strcpy、strcat、strcmp

4.1 strlen

#include <stdio.h>

int MyStrlen(char *pstr)
{
	int cnt = 0;

	while (*pstr != '\0')
	{
		cnt++;
		pstr++;
	}

	return cnt;
}

int main(void)
{
	char str[32] = {0};
	int len = 0;

	gets(str);

	len = MyStrlen(str);
	printf("len = %d\n", len);

	return 0;
}

4.2 strcpy

#include <stdio.h>
#include <string.h>

int MyStrcpy(char *pdst, char *psrc)
{
	while (*psrc != '\0')
	{
		*pdst = *psrc;
		psrc++;
		pdst++;
	}

	*pdst = '\0';

	return 0;
}

int main(void)
{
	char src[32] = {0};
	char dst[32] = {0};

	gets(src);

	MyStrcpy(dst, src);
	
	printf("dst = %s\n", dst);

	return 0;
}

4.3 strcat

#include <stdio.h>
#include <string.h>

int MyStrcat(char *pstr1, char *pstr2)
{
	while (*pstr1 != '\0')
	{
		pstr1++;
	}

	while (*pstr2 != '\0')
	{
		*pstr1 = *pstr2;
		pstr1++;
		pstr2++;
	}
	*pstr1 = '\0';

	return 0;
}

int main(void)
{
	char str1[32] = {0};
	char str2[32] = {0};

	gets(str1);
	gets(str2);

	MyStrcat(str1, str2);
	
	printf("str1 = %s, str2 = %s\n", str1, str2);

	return 0;
}

4.4 strcmp

#include <stdio.h>

int MyStrcmp(char *pstr1, char *pstr2)
{
	while (*pstr1 == *pstr2 && *pstr1 != '\0')
	{
		pstr1++;
		pstr2++;
	}

	return *pstr1 - *pstr2;
}

int main(void)
{
	char str1[32] = {0};
	char str2[32] = {0};
	int ret = 0;

	gets(str1);
	gets(str2);

	ret = MyStrcmp(str1, str2);

	printf("ret = %d\n", ret);

	return 0;
}

5. 判断素数——

#include <stdio.h>

int IsPrimayNum(int n)
{
	int i = 0;

	for (i = 2; i < n; i++)
	{
		if (0 == n % i)
		{
			return 0;
		}
	}
	
	return 1;
}

int main(void)
{
	int num = 0;

	scanf("%d", &num);

	if (IsPrimayNum(num))
	{
		printf("Yes!\n");
	}
	else 
	{
		printf("No!\n");
	}

	return 0;
}

6. 封装函数实现斐波那切数列    1 1 2 3 5 8 13 21 34 55 ——

#include <stdio.h>

int Fibo(int num)
{
    if (1 == num || 2 == num)
    {
        return 1;
    }else
    {
        return Fibo(num-1) + Fibo(num-2);
    }
}

int main(void)
{
    int num = 0;
    int fibo = 0;

    scanf("%d", &num);

    fibo = Fibo(num);

    printf("fibo = %d\n", fibo);

    return 0;
}

7. 递归实现汉诺塔——

#include <stdio.h>

int HanNuoTa(int n, char src, char tmp, char dst)
{
	if (1 == n)
	{
		printf("%c -> %c\n", src, dst);
	}
	else 
	{
		HanNuoTa(n-1, src, dst, tmp);
		HanNuoTa(1, src, tmp, dst);
		HanNuoTa(n-1, tmp, src, dst);
	}

	return 0;
}

int main(void)
{
	int n = 0;

	scanf("%d", &n);

	HanNuoTa(n, 'A', 'B', 'C');

	return 0;
}

8. 拆分数字各个位数,并将各个位数求和(除10(/10),再对10求余(%10))——

#include <stdio.h>

int fun(int tmp)
{
	int sum = 0;

	while (tmp != 0)
	{
		sum += tmp % 10;
		tmp /= 10;
	}

	return sum;
}

int main(void)
{
	int num = 0;
	int sum = 0;

	scanf("%d", &num);

	sum = fun(num);

	printf("sum = %d\n", sum);

	return 0;
}

9. 封装一个函数传入数组和n,根据n的值将数组完成转换——
    int a[5] = {1, 2, 3, 4, 5}
        n=3                        n=2
    3 4 5 1 2                4 5 1 2 3 

#include <stdio.h>

int OutputArray(int arrayp[], int tmplen)
{
    int i = 0;

    for (i = 0; i < tmplen; i++)
    {
        printf("%d ", arrayp[i]);
    }
    putchar('\n');

    return 0;
}

int Fun(int arrayp[], int len, int n)
{
    int i = 0;
    int j = 0;
    int tmp = 0;

    for (i = 0; i < n; i++)
    {
        tmp = arrayp[len-1];
        for (j = len-1; j > 0; j--)
        {
            arrayp[j] = arrayp[j-1];
        }
        arrayp[0] = tmp;
    }
}

int main(void)
{
    int str[5] = {1, 2, 3, 4, 5};
    int len = 5;
    int n = 0;
    
    scanf("%d", &n);

    OutputArray(str, len);
    Fun(str, len, n);
    OutputArray(str, len);

    return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/586477.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

手撕spring框架(2)

相关系列 java中spring底层核心原理解析&#xff08;1&#xff09;-CSDN博客 java中spring底层核心原理解析(2)-CSDN博客 手撕spring框架&#xff08;1&#xff09;-CSDN博客 依赖注入原理 依赖注入(Dependency Injection&#xff0c;简称DI)是一种设计模式&#xff0c;它允许我…

DS高阶:图论基础知识

一、图的基本概念及相关名词解释 1.1 图的基本概念 图是比线性表和树更为复杂且抽象的结&#xff0c;和以往所学结构不同的是图是一种表示型的结构&#xff0c;也就是说他更关注的是元素与元素之间的关系。下面进入正题。 图是由顶点集合及顶点间的关系组成的一种数据结构&…

深入浅出DBus-C++:Linux下的高效IPC通信

目录标题 1. DBus简介2. DBus-C的优势3. 安装DBus-C4. 使用DBus-C初始化和连接到DBus定义接口和方法发送和接收信号 5. dbus-cpp 0.9.0 的安装6. 创建一个 DBus 服务7. 客户端的实现8. 编译和运行你的应用9. 瑞芯微&#xff08;Rockchip&#xff09;的 Linux 系统通常会自带 db…

上位机开发PyQt(五)【Qt Designer】

PyQt5提供了一个可视化图形工具Qt Designer&#xff0c;文件名为designer.exe。如果在电脑上找不到&#xff0c;可以用如下命令进行安装&#xff1a; pip install PyQt5-tools 安装完毕后&#xff0c;可在如下目录找到此工具软件&#xff1a; %LOCALAPPDATA%\Programs\Python\…

智能体可靠性的革命性提升,揭秘知识工程领域的参考架构新篇章

引言&#xff1a;知识工程的演变与重要性 知识工程&#xff08;Knowledge Engineering&#xff0c;KE&#xff09;是一个涉及激发、捕获、概念化和形式化知识以用于信息系统的过程。自计算机科学和人工智能&#xff08;AI&#xff09;历史以来&#xff0c;知识工程的工作流程因…

【Web】2024XYCTF题解(全)

目录 ezhttp ezmd5 warm up ezMake ez?Make εZ?мKε? 我是一个复读机 牢牢记住&#xff0c;逝者为大 ezRCE ezPOP ezSerialize ezClass pharme 连连看到底是连连什么看 ezLFI login give me flag baby_unserialize ezhttp 访问./robots.txt 继…

ChatGPT向付费用户推“记忆”功能,可记住用户喜好 | 最新快讯

4月30日消息&#xff0c;人工智能巨头OpenAI宣布&#xff0c;其开发的聊天机器人ChatGPT将在除欧洲和韩国以外的市场全面上线“记忆”功能。这使得聊天机器人能够“记住”ChatGPT Plus付费订阅用户的详细信息&#xff0c;从而提供更个性化的服务。 OpenAI早在今年2月就已经宣布…

无缝对接配电自动化:IEC104转OPC UA网关解决方案

随着水电厂自动化发展的要求&#xff0c;具有一定规模的梯级水电站越来越多&#xff0c;为了实现水电站的无人值班(少人值守)&#xff0c;并考虑到节能控制&#xff0c;电厂采用了集中监控。集中监控关注的是整个电网的安全稳定运行及电压、频率和整个电网的电力需求&#xff0…

C++必修:类与对象(二)

✨✨ 欢迎大家来到贝蒂大讲堂✨✨ &#x1f388;&#x1f388;养成好习惯&#xff0c;先赞后看哦~&#x1f388;&#x1f388; 所属专栏&#xff1a;C学习 贝蒂的主页&#xff1a;Betty’s blog 1. 构造函数 1.1. 定义 构造函数是一个特殊的成员函数&#xff0c;名字与类名相…

软件工程物联网方向嵌入式系统复习笔记--如何控制硬件

5-如何控制硬件 #mermaid-svg-of9KvkxJqwLjSYzH {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-of9KvkxJqwLjSYzH .error-icon{fill:#552222;}#mermaid-svg-of9KvkxJqwLjSYzH .error-text{fill:#552222;stroke:#552…

vue3步骤条带边框点击切换高亮

如果是div使用clip-path: polygon(0% 0%, 92% 0%, 100% 50%, 92% 100%, 0% 100%, 8% 50%);进行裁剪加边框没实现成功。目前这个使用svg完成带边框的。 形状可自行更改path 标签里的 :d“[num ! 1 ? ‘M 0 0 L 160 0 L 176 18 L 160 38 L 0 38 L 15.5 18 Z’ : ‘M 0,0 L 160,0…

Docker: 如何不新建容器 修改运行容器的端口

目录 一、修改容器的映射端口 二、解决方案 三、方案 一、修改容器的映射端口 项目需求修改容器的映射端口 二、解决方案 停止需要修改的容器 修改hostconfig.json文件 重启docker 服务 启动修改容器 三、方案 目前正在运行的容器 宿主机的3000 端口 映射 容器…

2024最新版JavaScript逆向爬虫教程-------基础篇之常用的编码与加密介绍(python和js实现)

目录 一、编码与加密原理1.1 ASCII 编码1.2 详解 Base641.2.1 Base64 的编码过程和计算方法1.2.2 基于编码的反爬虫设计1.2.3 Python自带base64模块实现base64编码解码类封装 1.3 MD5消息摘要算法1.3.1 MD5 介绍1.3.2 Python实现md5以及其他常用消息摘要算法封装 1.4 对称加密与…

【GitHub】github学生认证,在vscode中使用copilot的教程

github学生认证并使用copilot教程 写在最前面一.注册github账号1.1、注册1.2、完善你的profile 二、Github 学生认证注意事项&#xff1a;不完善的说明 三、Copilot四、在 Visual Studio Code 中安装 GitHub Copilot 扩展4.1 安装 Copilot 插件4.2 配置 Copilot 插件&#xff0…

Java设计模式 _结构型模式_组合模式

一、组合模式 1、组合模式 组合模式&#xff08;Composite Pattern&#xff09;是这一种结构型设计模式。又叫部分整体模式。组合模式依据树形结构来组合对象&#xff0c;用来表示部分以及整体层次关系。即&#xff1a;创建了一个包含自己对象组的类&#xff0c;该类提供了修改…

Educational Codeforces Round 165 (Rated for Div. 2)[A~D]

这场签到很快那会rank1400吧&#xff0c;但到c就写不动了&#xff0c;最后排名也是3000 左右&#xff0c;可见很多人其实都不会写dp。快速签到也很重要啊&#xff01;&#xff01; A. Two Friends Problem - A - Codeforces 题目大意&#xff1a; M有n个朋友&#xff0c;编号…

【Java】java实现文件上传和下载(上传到指定路径/数据库/minio)

目录 上传到指定路径 一、代码层级结构 二、文件上传接口 三、使用postman进行测试&#xff1b; MultipartFile接收前端传递的文件&#xff1a;127.0.0.1:8082/path/uploadFile part接收前端传递的文件&#xff1a;127.0.0.1:8082/path/uploadFileByRequest 接收前端传递…

基于大模型的智能案件询问系统

一、数据库层面 1、document表 这个表是用来存储文件信息的。具体字段含义如下&#xff1a; 1. id&#xff1a;文件的唯一标识&#xff0c;整型&#xff0c;自增。 2. name&#xff1a;文件名称&#xff0c;字符串类型&#xff0c;最大长度为255个字符。 3. type&#xff1a…

宠物领养|基于SprinBoot+vue的宠物领养管理系统(源码+数据库+文档)

宠物领养目录 基于Spring Boot的宠物领养系统的设计与实现 一、前言 二、系统设计 三、系统功能设计 1前台 1.1 宠物领养 1.2 宠物认领 1.3 教学视频 2后台 2.1宠物领养管理 2.2 宠物领养审核管理 2.3 宠物认领管理 2.4 宠物认领审核管理 2.5 教学视频管理 四、…

Linux专栏03:使用Xshell远程连接云服务器

博客主页&#xff1a;Duck Bro 博客主页系列专栏&#xff1a;Linux专栏关注博主&#xff0c;后期持续更新系列文章如果有错误感谢请大家批评指出&#xff0c;及时修改感谢大家点赞&#x1f44d;收藏⭐评论✍ 使用Xshell远程连接云服务器 编号&#xff1a;03 文章目录 使用Xsh…
最新文章