一、应用math函数时需要在代码开头添加该头文件,才能应用
#include <stdio.h>
#include <math.h>
1、sqrt,主要用于对数据开平方处理
#include <stdio.h>
#include <math.h>
int main()
{
double a=9.0;
printf("%lf",sqrt(a));
return 0;
}
//输出结果为3.000000
注:sqrt只支持double和float类型,输出表达时注意类型转化
若%lf改为%d就会出现错误
printf("%d",sqrt(a));
//输出结果为0
2、三角函数(sin、cos、tan)
就是熟悉的函数计算,注意输入时为弧度制,不是角度制
#include <stdio.h>
#include <math.h>
int main()
{
printf("%f",sin(3.1415926/2));
return 0;
}
//输出结果为1.000000
里面3.1415926就是π,结果也就是sin(2/π)的答案cos和tan同理,将里面sin替换即可
3、pow(x,y),用于求x的y次方
#include <stdio.h>
#include <math.h>
int main()
{
printf("%f",pow(2,3));
return 0;
}
//输出结果为8.000000
4、cbrt(x):x的立方根
#include <stdio.h>
#include <math.h>
int main()
{
printf("%f",cbrt(27));
return 0;
}
输出结果为3.00000
5、abs:求x的绝对值
#include <stdio.h>
#include <math.h>
int main()
{
int a=-1234;
printf("%d",abs(a));
return 0;
}
//输出结果为1234
6、ceil:对x进行取整
#include <stdio.h>
#include <math.h>
int main()
{
float a=4.6;
printf("%f",ceil(a));
return 0;
}
//输出结果为5.000000
注:该函数并不是四舍五入,而是直接进一位,例如输入4.2,结果还是5。printf时不能用%d,因为这个函数的返回类型是float类型的
7、esp(x):用来计算以e为底数的x次方值
#include <stdio.h>
#include <math.h>
int main()
{
printf("%f",exp(1));
return 0;
}
//输出结果为2.718282
二、应用ctype函数时需要在代码开头添加该头文件,才能应用
#include <stdio.h>
#include <ctype.h>
1、isalpha:用于判断参数是否是字母字符,若是则返回非零值,否则返回零值
#include <stdio.h>
#include <ctype.h>
int main()
{
printf("%d",isalpha('b'));
}
//输出结果不是0
2、isdigit:用于判断参数是否是0~9的阿拉伯数字,如果是返回非0数字,否则返回0
#include <stdio.h>
#include <ctype.h>
int main()
{
printf("%d",isdigit('5'));
}
//输出结果不是0
3、toupper和tolower:第一个为小写字母转化为大写字母,第二个为大写字母转化为小写字母,若不是则不转化,直接输出参数
#include <stdio.h>
#include <ctype.h>
int main()
{
printf("%c",toupper('a'));
}
//输出结果为A
#include <stdio.h>
#include <ctype.h>
int main()
{
printf("%c",tolower('A'));
}
//输出结果为a