运算

算术运算

-用处 单独放在一个操作符前面 表示正负号反转

赋值运算

a = a + 5 等效于 a+=5

类型转换

C++转换示例

1
2
3
double d;
cin >> d;
cout << (int)d << endl;

常见数学函数

先引入头文件#include <cmath>或者#include <math.h>

返回的类型 函数名(参数的类型) //例如乘方函数

double pow(double,double)//第一个参数底数,第二个参数指数

float pow(float,float)//函数名相同但类型不同,称之为函数重载

1
2
3
4
5
6
7
8
9
10
cout << pow(2,3) << endl; //乘方
cout << sqrt(2) << endl; //开根号
cout << abs(-1) << endl; //绝对值
cout << fmod(10, 3.3) << endl; //浮点数取模
cout << ceil (3.1) << endl;//浮点数向上取整
cout << floor(2.9) << endl;//浮点数向下取整
cout << round(1.49) <<endl;//四舍五入
cout << cbrt(27) <<endl;//立方根
cout <<hypot(3,4) <<endl;//勾股定理
//可到cmath相关文档进行查阅

注意

同是int类型得到结果会舍去小数部分 例如 1 / 2 得到结果 0

应用 1.0 / 2 = 0.5 使用浮点数得到结果

逻辑相关运算

见C基础笔记