c++常用库函数 2 常用数学函数 3 其他常用函数

明天就要数据结构实验考试了,这两天疯狂复习总结一下C++常用的库函数,以便到时候能飘过……

2 常用数学函数

头文件 #include < math> 或者 #include <math.h>

函数原型 功能 返回值
int abs(int x) 求整数x的绝对值 绝对值
double fabs(double x) 求实数x的绝对值 绝对值
long labs(long x) 求长整数x的绝对值 绝对值
double sin(double x) 计算sin(x)的值 计算结果
double cos(double x) 计算cos(x)的值 计算结果
double tan(double x) 计算tan(x)的值 计算结果
double asin(double x) 计算arsin(x)的值 计算结果
double acos(double x) 计算arcos(x)的值 计算结果
double atan(double x) 计算artan(x)的值 计算结果
double log(double x) 计算ln(x)的值 计算结果
double log10(double x) 计算以10为底x的对数的值 计算结果
double sqrt(double x) 计算x的平方值 计算结果
double pow(double x, double y) 求x的y次幂的值 计算结果

3 其他常用函数

头文件#include < stdlib> 或者 #include <stdlib.h>

函数原型 功能 返回值
double atof(const char *s) 将s所指向的字符串转换成实数 实数值
int atoi(const char *s) 将s所指向的字符串转换成整数值 整数值
long atol(const char *s) 将s所指的字符串转换成长整数 长整数值
int rand(void) 产生一个随机整数 随机整数
max(a, b) 求两个数中的大数 大数
min(a, b) 求两个数中的小数 小数

头文件#include < complex> 或者 #include <complex.h>

实例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <complex>
#include <math.h>
using namespace std;
int main()
{
// 复数类对象定义
cout << "复数类对象定义" << endl;
double r = 1.0;
double x = 1.0;
complex<double> c1;
complex<double> c2(1,1);
complex<double> c3(r,x);
complex<double> c4 = 2.0;
complex<double> c5 = c4 + complex<double>(2,1);

cout << "c1 = " << c1 << endl;
cout << "c2 = " << c2 << endl;
cout << "c3 = " << c3 << endl;
cout << "c4 = " << c4 << endl;
cout << "c5 = " << c5 << endl << endl;

// 笛卡尔坐标系和极坐标系下有关函数
cout << "笛卡尔坐标系和极坐标系下有关函数" << endl;
cout << "c5实部real:" << c5.real() << endl;
cout << "c5虚部imag:" << c5.imag() << endl;
cout << "c5模值abs:" << abs(c5) << endl;
cout << "c5模值平方norm:" << norm(c5) << endl;
cout << "c5幅角arg:" << arg(c5) << endl;
cout << "c5共轭复数conj:" << conj(c5) << endl;
complex<double> z = polar(1.0, 3.14/6);
cout << "复数极坐标定义polar:" << z << endl << endl;

// 运算符重载,四则运算
cout << "运算符重载,四则运算" << endl;
cout << "c2 + c5 = " << c2 + c5 << endl;
cout << "c2 - c5 = " << c2 - c5 << endl;
cout << "c2 * c5 = " << c2 * c5 << endl;
cout << "c2 / c5 = " << c2 / c5 << endl << endl;

system("pause");
return 0;
}

输出结果

复数及其运算


转载请注明来源,欢迎对文章中的引用来源进行考证,指出任何有错误或不够清晰的表达,可以邮件至 [email protected]