math.h和ctype.h头文件里几个常用函数(c语言)

一、应用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

  • 8
    点赞
  • 65
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值