C语言——库函数

常用的函数

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

1 数学函数

c 复制代码
#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 时间函数

可以做游戏用

c 复制代码
#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 随机函数

通常结合时间函数来做

c 复制代码
#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 可增长函数

c 复制代码
#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 其他头文件

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

c 复制代码
#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;
}
相关推荐
侃侃_天下16 小时前
最终的信号类
开发语言·c++·算法
echoarts16 小时前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Aomnitrix16 小时前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
每天回答3个问题17 小时前
UE5C++编译遇到MSB3073
开发语言·c++·ue5
伍哥的传说17 小时前
Vite Plugin PWA – 零配置构建现代渐进式Web应用
开发语言·前端·javascript·web app·pwa·service worker·workbox
小莞尔17 小时前
【51单片机】【protues仿真】基于51单片机的篮球计时计分器系统
c语言·stm32·单片机·嵌入式硬件·51单片机
小莞尔18 小时前
【51单片机】【protues仿真】 基于51单片机八路抢答器系统
c语言·开发语言·单片机·嵌入式硬件·51单片机
liujing1023292918 小时前
Day03_刷题niuke20250915
c语言
我是菜鸟0713号18 小时前
Qt 中 OPC UA 通讯实战
开发语言·qt
JCBP_18 小时前
QT(4)
开发语言·汇编·c++·qt·算法