您的位置:首页 > 编程语言 > C语言/C++

C语言中一些不常用函数

2015-05-11 09:43 169 查看
time

这个用的最多就是随机数的时候,现在用一用其他特性.

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

int main()
{
time_t sec;       ///typedef long time_t
struct tm * curTime;

sec = time(NULL);           ///获取时间,从1970.1.1到现在的秒数,也可以写成 time(&sec);
curTime = localtime(&sec);  ///把sec转换为当地时间,存于时间结构体curTime中,注意返回值是struct tm* 类型的

printf("asctime(curTime): %s\n", asctime (curTime));
///asctime(struct tm*)函数将curTime转化成标准ASCII时间格式

printf("ctime(curTime):   %s\n", ctime (&sec));
///ctime(time_t*)直接将sec转换成标准时间格式

printf("%d-%d-%d %d:%d:%d\n", 1900+curTime->tm_year, 1+curTime->tm_mon,
curTime->tm_mday, curTime->tm_hour, curTime->tm_min, curTime->tm_sec);
///手动输出struct tm中的字段,其中月份加1,年份要加1900,
///sec记录的是1970到现在的秒数,为什么是加1900呢???

printf("\nsec = %ld\nmktime(curTime) = %ld\n", sec, mktime(curTime));
///time_t mktime(struct tm*)将curTime反向转化成1970到现在的秒数
return 0;
}


字符转换/数学公式

#include <stdlib.h>


把字符串转化成浮点数:double atof( const char *str );

或 double strtod( const char *start, char **end );

把字符串转化成整型数:int atoi( const char *str );

长整型:long atol( const char *str );

二分查找:

void bsearch(const void *key, const void *buf, size_t num, size_t size, int (*compare)(const void , const void *));

#include <math.h>


以十为底的对数函数:double log10(double x);

以e(=2.71828)为底:double log(double x);

反正切函数:double atan(double x); //返回值范围(-PI/2,PI/2)

double atan2(double y, double x); //返回点(x,y)与原点的连线和x轴正方向的夹角(弧度制),范围(-PI,PI]

说明: 1.特别注意:参数的顺序是y,x,不是x,y.

2.结果为正表示从x 轴逆时针旋转的角度,结果为负表示从x 轴顺时针旋转的角度.

3.atan2(y, x) 与 atan(y/x) 返回值的范围不同.

4.当x=0,y=0时返回错误.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: