34、C语言常用库函数-数学函数类

数学函数(原型声明所在头文件为math.hstdlib.hstring.hfloat.h 

int abs(int i) 返回整型参数i的绝对值 

double cabs(structcomplex znum) 返回复数znum的绝对值 

double fabs(doublex) 返回双精度参数x的绝对值 

long labs(long n) 返回长整型参数n的绝对值 

double exp(doublex) 返回指数函数ex的值 

double frexp(double value,int*eptr) 返回value=x*2n中x的值,分配得来的n存贮在eptr中 

doubleldexp(double value,int exp); 返回value*2exp的值 

double log(doublex) 返回logex的值 

doublelog10(double x) 返回log10x的值 

double pow(doublex,double y) 返回xy的值 

double pow10(intp) 返回10p的值 

double sqrt(doublex) 返回x的开方 

double acos(doublex) 返回x的反余弦cos-1(x)值,x为弧度 

double asin(doublex) 返回x的反正弦sin-1(x)值,x为弧度 

double atan(doublex) 返回x的反正切tan-1(x)值,x为弧度 

double atan2(doubley,double x) 返回y/x的反正切tan-1(x)值,y的x为弧度 

double cos(doublex) 返回x的余弦cos(x)值,x为弧度 

double sin(doublex) 返回x的正弦sin(x)值,x为弧度 

double tan(doublex) 返回x的正切tan(x)值,x为弧度 

double cosh(doublex) 返回x的双曲余弦cosh(x)值,x为弧度 

double sinh(doublex) 返回x的双曲正弦sinh(x)值,x为弧度 

double tanh(doublex) 返回x的双曲正切tanh(x)值,x为弧度 

doublehypot(double x,double y) 返回直角三角形斜边的长度(z), 

x和y为直角边的长度,z2=x2+y2 

double ceil(doublex) 返回不小于x的最小整数 

doublefloor(double x) 返回不大于x的最大整数 

voidsrand(unsigned seed) 初始化随机数发生器 

int rand() 产生一个随机数并返回这个数 

double poly(doublex,int n,double c[])从参数产生一个多项式 

double modf(double value,double*iptr)将双精度数value分解成尾数和阶,iptr返回整数部分,函数返回小数部分:fraction = modf(number, &integer);

double fmod(doublex,double y) 返回x/y的余数 

double atof(char*nptr) 将字符串nptr转换成浮点数并返回这个浮点数

double atoi(char*nptr) 将字符串nptr转换成整数并返回这个整数

double atol(char*nptr) 将字符串nptr转换成长整数并返回这个整数

char *ecvt(doublevalue,int ndigit,int *decpt,int *sign) 

将浮点数value转换成字符串并返回该字符串 

char *fcvt(doublevalue,int ndigit,int *decpt,int *sign) 

将浮点数value转换成字符串并返回该字符串 

char *gcvt(doublevalue,int ndigit,char *buf) 

将数value转换成字符串并存于buf中,并返回buf的指针 

char*ultoa(unsigned long value,char *string,int radix) 

将无符号整型数value转换成字符串并返回该字符串,radix为转换时所用基数 

char *ltoa(longvalue,char *string,int radix) 

将长整型数value转换成字符串并返回该字符串,radix为转换时所用基数 

char *itoa(intvalue,char *string,int radix) 

将整数value转换成字符串存入string,radix为转换时所用基数 

double atof(char*nptr) 将字符串nptr转换成双精度数,并返回这个数,错误返回0 

int atoi(char*nptr) 将字符串nptr转换成整型数, 并返回这个数,错误返回0 

long atol(char*nptr) 将字符串nptr转换成长整型数,并返回这个数,错误返回0 

double strtod(char*str,char **endptr)将字符串str转换成双精度数,并返回这个数, 

long strtol(char *str,char**endptr,int base)将字符串str转换成长整型数, 并返回这个数。

int matherr(structexception *e) 用户修改数学错误返回信息函数(没有必要使用) 

double_matherr(_mexcep why,char *fun,double *arg1p, double *arg2p,double retval) 

用户修改数学错误返回信息函数(没有必要使用) 

unsigned int_clear87() 清除浮点状态字并返回原来的浮点状态 

void _fpreset() 重新初使化浮点数学程序包 

unsigned int_status87() 返回浮点状态字

举个例子:

itoa函数及atoi函数

C语言提供了几个标准库函数,可以将任意类型(整型、长整型、浮点型等)的数字转换为字符串。以下是用itoa()函数将整数转 换为字符串的 一个例子:

# include <stdio.h>

# include <stdlib.h>

void main (void)

{

int num = 100;

char str[25];

itoa(num, str, 10);

printf("The number 'num' is %d and thestring 'str' is %s. \n" ,

num, str);

}

itoa()函数有3个参数:第一个参数是要转换的数字,第二个参数是要写入转换结果的目标字符串,第三个参数是转移数字时所用的基数。在上例中,转换基数为10。10:十进制;2:二进制...

itoa并不是一个标准的C函数,它是Windows特有的,如果要写跨平台的程序,请用sprintf。 

是Windows平台下扩展的,标准库中有sprintf,功能比这个更强,用法跟printf类似: 

char str[255]; 

sprintf(str, "%x", 100); //将100转为16进制表示的字符串。 

itoa() 将整型值转换为字符串

itoa() 将长整型值转换为字符串

ultoa() 将无符号长整型值转换为字符串

1、atoi把字符串转换成整型数

#include <ctype.h>

#include <stdio.h>

int atoi (char s[]);

int main(void )

{   

char s[100];

gets(s);

printf("integer=%d\n",atoi(s));

return 0;

}

int atoi (char s[])

{

int i,n,sign;

for(i=0;isspace(s[i]);i++)//跳过空白符     ;

sign=(s[i]=='-')?-1:1;

if(s[i]=='+'||s[i]==' -')//跳过符号

      i++;

for(n=0;isdigit(s[i]);i++)

      n=10*n+(s[i]-'0');//将数字字符转换成整形数字

return sign *n;

}

2、itoa把一整数转换为字符串

#include <ctype.h>

#include <stdio.h>

void itoa (int n,char s[]);

//atoi 函数:将s转换为整形数

int main(void )

{   

int n;

char s[100];

printf("Input n:\n");

scanf("%d",&n);

printf("the string : \n");

itoa (n,s);

return 0;

}

void itoa (int n,char s[])

{

int i,j,sign;

if((sign=n)<0)//记录符号

      n=-n;//使n成为正数

        i=0;

do{

  s[i++]=n%10+'0';//取下一个数字

}while ((n/=10)>0);//删除该数字

if(sign<0)

      s[i++]='-';

s[i]='\0';

for(j=i;j>=0;j--)//生成的数字是逆序的,所以要逆序输出

      printf("%c",s[j]);

}

发布了278 篇原创文章 · 获赞 31 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/hopegrace/article/details/104533176