一、输入输出类
(1)精确到小数点后多少位:
#include < iomanip> //不要忘了头文件
//第一种写法
cout<<setiosflags(ios::fixed)<<setprecision(2);
//第二种写法
cout.setf(ios::fixed);
cout<<setprecision(2);
//第三种写法
cout<<fixed<<setprecision(2);
要保留n位小数即setprecision(n)
(2)宽度控制/左右对齐
使用setw(n)函数 ,记住头文件#include< iomanip >
setw(n)函数 是C++中关于在输出操作中使用的字段宽度设置函数,其中n表示字段宽度
范例:
如果说要是以特殊符号占位,下面就以补 @为例,用特殊符号占位就需要setfill(‘x’)—这里的’x’指的是字符,代码如下:
setw()默认的是输出右对齐,而当你想要左对齐时,便需要另外添加setiosflags(ios::left)来实现输出左对齐。
(3)任意整数拿到各个数位的值
#include <iostream>
using namespace std;
void sb(int n)
{
if(n>9)
{
sb(n/10); //每次调用时将输入的数最后一位去掉
cout<<n%10<<" "; //每次调用都打印出第一位
}
else
cout<<n<<" ";//直接打印小于10的所有数
}
int main()
{
int n=0;
cin>>n;
sb(n);
cout<<endl;
return 0;
}
二、调用数学函数类
1.开平方
double sqrt(double x);
2.求常数e的x次方
double exp(double x);
3.求x的y次方
double pow(double x, double y);
4.求对数ln(x)
double log(double x);
求对数lg(x)
double log10(double x);
其他用换底公式
5.求x绝对值
int abs(x);
long int abs(long int x);
double fabs(double x);
6.三角函数
求正弦
double sin(double x);
求余弦
double cos(double x);
求正切
double tan(double x);
反正切
double atan(double x);
7.取整函数
向上取整
double ceil(double x);
向下取整
double floor(double x);
8.产生随机数 0~32767
int rand(void);
9.科学计数法:
AeB:其中,A为实数,表示A*(10^B)。注意:B必须为整数!