C语言——库函数

news2025/2/7 4:47:16

常用的函数

https://cplusplus.com/reference/ 没事儿多看看

1 数学函数

#include <math.h>
#include <stdio.h>
int main() 
{

	printf("%lf\n", sqrt(4));//开平方根——>double类型
	printf("%lf\n", pow(2, 10));//求几次方的——>double类型
	printf("%d\n", abs(-1));//整数绝对值
	printf("%lf\n", fabs(-1.11));//浮点数的绝对值
    //三角函数...
    //对数函数...

	return 0;
}

2 时间函数

可以做游戏用

#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include <windows.h>
void test_time_type() 
{
	clock_t t1;	//其实就是long类型
	time_t t2;	//int64类型
	size_t t3;			
	struct tm t4;//右击查看定义就能知道详细
    /*struct tm
    {
    int tm_sec;   // seconds after the minute - [0, 60] including leap second
    int tm_min;   // minutes after the hour - [0, 59]
    int tm_hour;  // hours since midnight - [0, 23]
    int tm_mday;  // day of the month - [1, 31]
    int tm_mon;   // months since January - [0, 11]
    int tm_year;  // years since 1900——>从1900年开始
    int tm_wday;  // days since Sunday - [0, 6]
    int tm_yday;  // days since January 1 - [0, 365]
    int tm_isdst; // daylight savings time flag
	};*/
}
	

//1 获取当前时间
void get_cur_time() 
{
	//1.使用time函数获取时间戳 
	time_t t = time(NULL);
	//2.直接用ctime转换为字符串打印出来
	puts(ctime(&t));
	//3.时间戳转换为struct tm 自己去访问年月日时分秒
	struct tm* pt = localtime(&t);
	printf("%d年%d月%d日\n", pt->tm_year + 1900, pt->tm_mon + 1, pt->tm_mday);
	printf("%02d:%02d:%02d\n", pt->tm_hour, pt->tm_min, pt->tm_sec);
	//4.struct tm* 转换为字符串,内部有很多转换函数
	puts(asctime(pt));
	//5.格式化字符串
	char buffer[50] = "";
	strftime(buffer,50, "%Y-%m-%d %H:%M:%S", pt);
	puts(buffer);
	strftime(buffer, 50, "%F %T", pt);
	puts(buffer);
}

-----------------------------------------------------------------------------------------

// 2 !!!挺有用的,可以测自己写的算法运行时间间隔
void test_clock() 
{
	clock_t begin = clock();
    
	for (int i = 0; i < 1000000; i++) 
	{
		int a = i + 1;
		int b = i + a;
		int c = a + b + i;
	}
	clock_t end = clock();
	printf("duration:%ld", end - begin);
}

-----------------------------------------------------------------------------------------

//3 !!!定时器——>和sleep函数(会影响程序主循环)有区别的
bool on_timer(int duration, int id) 
{
	static int start_time[20] = { 0 };//静态变量,初始化代码只执行一次。普通变量每次都执行
	if (start_time[id] == 0) 
	{
		start_time[id] = clock();
	}
	int end_time = clock();
	if (end_time - start_time[id] >= duration) 
	{
		start_time[id] = end_time;
		return true;
	}
	return false;
}

-----------------------------------------------------------------------------------------


//4 记录时间
void count_time(int duration) 
{
	time_t start_time, end_time;
	int  old_time = 0;
	start_time = time(NULL);
	while (1) 
	{
		end_time = time(NULL);
		int etime = (int)difftime(end_time, start_time);
		if (old_time != etime) 
		{
			printf("过去了 %d 秒\n", etime);
		}
		old_time = etime;
		if(etime>=duration)
		{
			break;
		}
	}
}


-----------------------------------------------------------------------------------------

int main() 
{

	//get_cur_time();
	//test_clock();
	count_time(10);
	//while (true) 
	//{
	//	printf("11111111\n");
	//	//Sleep(2000);
	//	if(on_timer(2000,0))
	//		printf("22222222\n");
	//}
	return 0;
}

3 随机函数

通常结合时间函数来做

#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>

//1 rand 和 srand
void test_rand() 
{
	
	//rand()——>伪随机数
	//srand(100);//生成的随机树的最小值是100,随机函数种子,但也是伪随机数,可以与时间建立联系,就可以					 真正随机了
    //srand((unsigned int)time(NULL));
	printf("%d\n", rand());
}

-----------------------------------------------------------------------------------------

//2 设置随机数范围
void set_rand() 
{
	//1.取余数  n%x  [0,x-1]
	printf("取余固定范围:%d\n", rand() % 10);
	//2.n%x+a   [a,x-1+a];
	printf("最小值限定:%d\n", rand() % 10 + 5);
	//3.某一个数字的整数倍 
	printf("整数倍:%d\n", rand() % 100 * 10);
}

-----------------------------------------------------------------------------------------

//与定时器结合(下面场景用)
bool on_timer(int duration, int id)
{
	static int start_time[20] = { 0 };
	if (start_time[id] == 0)
	{
		start_time[id] = clock();
	}
	int end_time = clock();
	if (end_time - start_time[id] >= duration)
	{
		start_time[id] = end_time;
		return true;
	}
	return false;
}

-----------------------------------------------------------------------------------------

//3 随机数两种应用场景
void  test_use_rand() 
{	
    
    //抽奖
	int count = 1;
	while (1) 
	{
		int result = rand() % 1000;
		printf("第%03d次抽奖结果:", count);
		if (count>=100&&result == 0) //最少抽100次
		{
			printf("特等奖\n");
			break;
		}
		else if (result >= 10 && result < 20) 
		{
			printf("一等奖\n");
		}
		else if (result >= 100 && result <= 199) 
		{
			printf("二等奖\n");
		}
		else if (result >= 300 && result <= 500) 
		{
			printf("三等奖\n");
		}
		else 
		{
			printf("安慰奖\n");
		}
		count++;
	}
    
    
    //游戏
	int direct = 0;
	while (1) 
	{
		printf("怪物:");
        //用上定时器
		if (on_timer(1000, 0)) 
		{
			direct = rand() % 4;
		}
		switch (direct) 
		{
		case 0:
			printf("往上走!\n");
			break;
		case 1:
			printf("往下走\n");
			break;
		case 2:
			printf("往左走\n");
			break;
		case 3:
			printf("往右走\n");
			break;
		}
	}

}

int main() 
{
	
	test_rand();
	set_rand();
	test_use_rand();
	return 0;
}

4 可增长函数

#include <stdarg.h>
#include <stdio.h>
/*
va_list
va_start
va_arg
va_end
...缺省符
*/

int sum(unsigned int count, int arg1, ...) 
{
    //创建参数列表,初始化
	int result = arg1;
	va_list start;
	va_start(start, arg1);

	for (int i = 1; i < count; i++) 
	{
		result += va_arg(start, int);
	}
	va_end(start);
	return result;
}

-----------------------------------------------------------------------------------------

//模拟自己写一个printf函数
//为什么要用'%d'之类的格式控制字符
void my_printf(const char* str, ...) 
{	
    //做2种数据的解析
	int inum = 0;
	double dnum = 1.0;
    
	va_list start;
	va_start(start, str);
	while (*str)
	{
		if (*str == '%') 
		{
			str++;
			switch (*str) 
			{
			case 'd':
				inum = va_arg(start, int);
				printf("%d", inum);
				break;
			case 'f':
				dnum = va_arg(start, double);
				printf("%f", dnum);
				break;
			}
		}
		else 
		{
			printf("%c", *str);
		}
		str++;
	}
}

int main() 
{
	printf("sd\n");
	printf("%d\n", sum(2, 1,2));
	printf("%d\n", sum(3, 1,2,3));
	printf("%d\n", sum(4, 1,2,3,4));
	my_printf("整数 %d\t,小数%f", 12, 1.11f);
	return 0;
}

5 其他头文件

在这里插入图片描述

有的函数很简单,可以自己封装

#include<ctype.h>//检查字符各种功能
#include <stdlib.h>//字符串转换,伪随机,动态内存,搜索,qsort....
#include <stdio.h>//输入输出,文件操作
#include <stdbool.h>//true和false
#include <iso646.h>//逻辑运算符用单词表示
#include <limits.h>//里面有最大最小正整数之类的宏
#include <string.h>//字符串操作

bool is_digit(char num) 
{
	//return num >= '0' && num <= '9';
	return num >= '0' and num <= '9';
}

//qsort函数
int compare(const void* a, const void* b)
{
	return (*(int*)a - *(int*)b);
}


int main() 
{
	isdigit('1');
    
	printf("%d\n", INT_MAX);
	printf("%d\n", INT_MIN);
    
  ----------------------------------------------------------------------------
    //qsort函数 
	int values[] = { 40, 10, 100, 90, 20, 25 };
	int n;
	qsort(values, 6, sizeof(int), compare);
	for (n = 0; n < 6; n++)
		printf("%d ", values[n]);
   ----------------------------------------------------------------------------
    //分割字符串
	char str[] = "- This, a sample string.";
	char* pch;
	printf("Splitting string \"%s\" into tokens:\n", str);
	pch = strtok(str, " ,.-");
	while (pch != NULL)
	{
		printf("%s\n", pch);
		pch = strtok(NULL, " ,.-");
	}

	return 0;
}

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

树莓派远程连接方法

1、树莓派端 在树莓派终端&#xff08;Ctrl键alt键T键&#xff09;输入&#xff1a; ipconfig 查看树莓派的IP&#xff0c;记住这个IP号&#xff0c;都是192.168开头的 注意&#xff0c;这里远程连接需要树莓派和电脑在同一网络之下才可以 2、电脑端 我们在电脑上面下载 M…

qt QProxyStyle详解

1、概述 QProxyStyle是Qt框架中QStyle类的一个子类&#xff0c;它提供了一种代理机制&#xff0c;允许开发者在不直接修改现有样式&#xff08;QStyle&#xff09;实现的情况下&#xff0c;对样式行为进行定制或扩展。通过继承QProxyStyle&#xff0c;开发者可以重写其虚方法&…

TCP三次握手与四次挥手(TCP重传机制,2MSL)超详细!!!计算机网络

本篇是关于3次握手和四次挥手的详细解释~ 如果对你有帮助&#xff0c;请点个免费的赞吧&#xff0c;谢谢汪。&#xff08;点个关注也可以&#xff01;&#xff09; 如果以下内容需要补充和修改&#xff0c;请大家在评论区多多交流~。 目录 1. TCP头部&#xff1a; 2. 三次握手…

智能化业务校验框架:动态设计与应用实践

目录 一、业务背景 二、配置内容展示 三、商品动态配置内容展示 &#xff08;一&#xff09;商品spu校验信息数据 &#xff08;二&#xff09;商品sku校验信息数据 &#xff08;三&#xff09;组包商品校验信息数据 &#xff08;四&#xff09;商品数据校验数据持有者 &…

蓝桥杯每日真题 - 第23天

题目&#xff1a;&#xff08;直线&#xff09; 题目描述&#xff08;12届 C&C B组C题&#xff09; 解题思路&#xff1a; 题目理解: 在平面直角坐标系中&#xff0c;从给定的点集中确定唯一的直线。 两点确定一条直线&#xff0c;判断两条直线是否相同&#xff0c;可通过…

设计模式---建造者模式

建造者模式 一种创建型设计模式&#xff0c;它允许你一步一步地构建复杂对象。通过使用建造者模式&#xff0c;你可以将对象的构建过程与其表示分离&#xff0c;使得同样的构建过程可以创建不同的表示。说白点就是&#xff0c;解决了构造函数创建对象的问题。 适用于那种构造函…

【北京迅为】iTOP-4412全能版使用手册-第二十章 搭建和测试NFS服务器

iTOP-4412全能版采用四核Cortex-A9&#xff0c;主频为1.4GHz-1.6GHz&#xff0c;配备S5M8767 电源管理&#xff0c;集成USB HUB,选用高品质板对板连接器稳定可靠&#xff0c;大厂生产&#xff0c;做工精良。接口一应俱全&#xff0c;开发更简单,搭载全网通4G、支持WIFI、蓝牙、…

Python小黄人

文章目录 系列目录写在前面完整代码代码分析1. 初始化部分2. 身体部分3. 右眼睛部分4. 左眼睛部分5. 嘴巴部分6. 裤子部分7. 脚部部分8. 手部部分9. 头顶装饰部分10. 代码总结 写在后面 系列目录 序号直达链接爱心系列1Python制作一个无法拒绝的表白界面2Python满屏飘字表白代码…

操作系统 | 学习笔记 | 王道 | 2.2处理机调度

2.2 处理机调度 文章目录 2.2 处理机调度2.2.1 调度的概念2.2.2 调度的目标2.2.3 调度的实现2.2.4 典型的调度算法错题总结&#xff1a; 2.2.1 调度的概念 调度的基本概念 处理机调度是对处理机进行分配&#xff0c;即从就绪队列中按照一定的算法&#xff08;公平、高效的原则&…

算法与数据结构(1)

一&#xff1a;数据结构概论 数据结构分为初阶数据结构&#xff08;主要由C语言实现&#xff09;和高阶数据结构&#xff08;由C实现&#xff09; 初阶数据结构当中&#xff0c;我们会学到顺序表、链表、栈和队列、二叉树、常见排序算法等内容。 高阶数据结构当中&#xff0…

【Android】EventBus的使用及源码分析

文章目录 介绍优点基本用法线程模式POSTINGMAINMAIN_ORDEREDBACKGROUNDASYNC 黏性事件 源码注册getDefault()registerfindSubscriberMethods小结 postpostStickyunregister 介绍 优点 简化组件之间的通信 解耦事件发送者和接收者在 Activity、Fragment 和后台线程中表现良好避…

深度学习笔记——生成对抗网络GAN

本文详细介绍早期生成式AI的代表性模型&#xff1a;生成对抗网络GAN。 文章目录 一、基本结构生成器判别器 二、损失函数判别器生成器交替优化目标函数 三、GAN 的训练过程训练流程概述训练流程步骤1. 初始化参数和超参数2. 定义损失函数3. 训练过程的迭代判别器训练步骤生成器…

成都睿明智科技有限公司抖音电商服务的新引擎

在这个短视频风起云涌的时代&#xff0c;抖音不仅成为了人们休闲娱乐的首选&#xff0c;更是商家们竞相角逐的电商新蓝海。在这片充满机遇与挑战的海域中&#xff0c;成都睿明智科技有限公司如同一艘装备精良的航船&#xff0c;引领着众多企业向抖音电商的深水区进发。今天&…

51c视觉~YOLO~合集4

我自己的原文哦~ https://blog.51cto.com/whaosoft/12512597 1、Yolo8 1.1、检测PCB元件 技术世界正在以惊人的速度发展&#xff0c;而这种转变的核心是一个革命性的工具 — 计算机视觉。它最有趣的应用之一是电子印刷电路板 &#xff08;PCB&#xff09; 的检测和分析。本文…

Jenkins的使用

文章目录 一、Jenkins是什么\有什么用\与GitLab的对比二、Jenkins的安装与配置Jenkins的安装方式在Linux上安装Jenkins&#xff1a;在Windows上安装Jenkins&#xff1a;配置Jenkins&#xff1a; &#xff08;可选&#xff09;配置启动用户为root&#xff08;一定要是root吗??…

图论入门教程:GTM173 Graph Theory

这是本图论的入门教材&#xff0c;Graph Theory Fifth Edition&#xff0c;隶属于著名的GTM系列&#xff0c;作者是Reinhard Diestel。这是本对新人友好的教材&#xff0c;之前本科上离散数学的课时&#xff0c;因为涉及到图论&#xff0c;而学校的课堂又太水让我心生不满&…

QT5 Creator (Mingw编译器) 调用VS2019 (阿里云 oss C++库) 报错的解决方法

方法就是不要用VS2019编译&#xff0c;要用MINgw32编译。 编译命令如下&#xff1a; cmake -G "MinGW Makefiles" ^-DCMAKE_MAKE_PROGRAMD:\qt\Tools\mingw810_32\bin\mingw32-make.exe ^-DCMAKE_C_COMPILERD:\qt\Tools\mingw810_32\bin\gcc.exe ^-DCMAKE_CXX_COMP…

反向传播、梯度下降与学习率:深度学习中的优化艺术

目录 反向传播&#xff1a;神经网络的学习机制 梯度下降&#xff1a;优化算法的基石 学习率&#xff1a;平衡速度与稳定性的关键 学习率的调整策略 固定学习率 学习率衰减 自适应学习率 梯度消失与梯度爆炸 结语 在深度学习的领域中&#xff0c;构建一个有效的神经网络…

论文笔记(五十九)A survey of robot manipulation in contact

A survey of robot manipulation in contact 文章概括摘要1. 引言解释柔顺性控制的概念&#xff1a;应用实例&#xff1a; 2. 需要接触操控的任务2.1 环境塑造2.2 工件对齐2.3 关节运动2.4 双臂接触操控 3. 接触操控中的控制3.1 力控制3.2 阻抗控制3.3 顺应控制 4. 接触操控中的…

881.救生艇

目录 题目过程 题目 给定数组 people 。people[i]表示第 i 个人的体重 &#xff0c;船的数量不限&#xff0c;每艘船可以承载的最大重量为 limit。 每艘船最多可同时载两人&#xff0c;但条件是这些人的重量之和最多为 limit。 返回 承载所有人所需的最小船数 。 过程 cla…