#include <iostream> #include <iomanip> #include <cmath> using namespace std; int main() { // boolalpha 可以讓 bool 值按字符串輸出 cout<<"Before operating: "<<true<<boolalpha<<endl;
cout<<"After operating: "<<true<<noboolalpha<<endl; // showbase 顯示進制,uppercase 在十六進制打印 0X、在科學記數法打印 E // oct 打印八進制,hex 打印十六進制,dec 打印十進制 ,也可以用 setbase(base) 來設置進制 int num = 0; cout<<"Enter the num: "; cin>>num; cout<<showbase<<uppercase<<"Default: "<<num<<endl; cout<<"Octal: "<<oct<<num<<endl; cout<<"Hex: "<<hex<<num<<endl; cout<<"Decimal: "<<dec<<num<<noshowbase<<nouppercase<<endl; // 我們可以使用 cout.precision(size) 或 setprecision(size) 來設置精度
// 即幾位數字,通過 cout.precision() 返回當前精度值 cout<<"Precision: "<<cout.precision()<<", Value: "<<sqrt(2.0)<<endl; cout.precision(12); cout<<"Precision: "<<cout.precision()<<", Value: "<<sqrt(2.0)<<endl; cout<<setprecision(3); cout<<"Precision: "<<cout.precision()<<", Value: "<<sqrt(2.0)<<endl; // showpoint 對浮點值總是顯示小數點,showpos 對非負數顯示 + cout<<"Before operating the num is: "<<2.0<<endl;
cout<<showpoint<<showpos<<"After operating: "<<2.0<<noshowpoint<<noshowpos<<endl; // sciencetific 用科學計數法顯示浮點數,acos 為反三角函數,在頭文件 cmath 里 cout<<scientific<<"Scientific: "<<acos(-1)<<endl; // setw(width) 設置寬度為 width 個字符,right 為右對齊,left 為左對齊,setfill(ch) 用 ch 來填充 cout<<setw(20)<<setfill('*')<<left<<"I LOVE U"<<endl<<setw(20)<<right<<"I LOVE U TOO";
cout<<endl; // unitbuf 每次輸出操作都刷新緩沖區,對應nounitbuf;skipws 輸入運算符跳過空白符,對應 noskip cout<<unitbuf; cin>>noskipws; cout<<"Enter the chars or enter the '!' to end it: "<<endl; char ch; while (cin>>ch && ch != '!') cout<<ch; cout<<nounitbuf; cin>>skipws; system("pause"); return 0; }