?? 11-2.txt
字號:
/* 范例:11-2 標志 */
#include <iostream.h>
void main(void)
{
int a = 68, b = 38, c = 6;
cout.setf(ios::showpos); // 當 >=0 顯示'+'號
/* 對齊控制 */
cout << "/* 對齊控制: */ " <<endl;
cout.width(10); // 設(shè)定寬度為10
cout.setf(ios::left);
cout << a << "\n";
cout.unsetf(ios::left); // #1.1 清除同一組靠左對齊設(shè)定
cout.setf(ios::right); // #1.2 設(shè)定同一組靠右對齊設(shè)定
cout.width(10);
cout << b << endl;
cout.unsetf(ios::adjustfield); // #2 取消對齊
cout.width(10);
cout << c << "\n\n";
/* 布爾 */
cout << "/* 布爾: */" << endl;
cout << "布爾常數(shù) true = " << true << endl;
cout.setf(ios::boolalpha);
cout << "設(shè)定 boolalpha 后,布爾常數(shù) true = " << true << endl;
// -----------------------------------------------------------
float f = 5.680;
int k = 24;
cout.setf(ios::showpoint); // 顯示小數(shù)點
cout.setf(ios::showpos); // 當 >=0 顯示'+'號
cout.precision(3); // 設(shè)定浮點數(shù)精確度
cout.setf(ios::showbase); // 完全顯示幾進制(0x,0)
cout.setf(ios::uppercase); // 轉(zhuǎn)換16進位數(shù)值時,A~F以大寫顯示
/* 進位制控制 */
cout << "/* 進位制控制: */" << endl;
cout.setf(ios::hex, ios::basefield); // #3 設(shè)定以16進制顯示
cout << "十進制 k = 24 十六進制 k = " << k << endl;
cout.setf(ios::oct, ios::basefield); // 設(shè)定以8進制顯示
cout << "十進制 k = 24 八進位 k = " << k << endl;
cout.setf(ios::dec, ios::basefield); // 設(shè)定以10進制顯示
/* 浮點數(shù)顯示控制 */
cout << "/* 浮點數(shù)顯示控制: */" << endl;
cout.precision(3); // 設(shè)定精度有效3位
cout.setf(ios::fixed); // 設(shè)定以fixed符號輸出
cout << f << endl; // +5.680
cout.setf(ios::scientific,ios::floatfield); // 以科學記號表示
cout << f << endl; // +5.680E+00
/* 使用fill()成員函數(shù) */
cout << "使用fill()成員函數(shù)" << endl;
cout.fill('#'); // 設(shè)定輸出寬度內(nèi)空白填入字符'#'
cout.width(10); // # 4
cout << 24 << endl;
cout.fill('\0'); // # 5
cout.width(10);
cout << 24 << endl;
getchar();
}
程序執(zhí)行結(jié)果:
/* 對齊控制: */
+68
+38
+6
/* 布爾: */
布爾常數(shù) true = 1
設(shè)定 boolalpha 后,布爾常數(shù) true = true
/* 進位制控制: */
十進制 k = 24 十六進制 k = 0X18
十進制 k = 24 八進位 k = 030
/* 浮點數(shù)顯示控制: */
+5.680
+5.680E+00
使用fill()成員函數(shù)
#######+24
+24
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -