C++期末穩過複習無敵知識點1(輸入輸出類、呼叫數學函式類)
一、輸入輸出類
(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 >
範例:
![在這裡插入圖片描述](https://img.796t.com/res/2020/12-20/16/6afbc6e1a80def0eaa154751532ce11e.png)
![在這裡插入圖片描述](https://img.796t.com/res/2020/12-20/16/70781bc855bd2c87a84c4e5552e92029.png)
如果說要是以特殊符號佔位,下面就以補 @為例,用特殊符號佔位就需要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必須為整數!