Delphi VJ VB VC 討論 聯繫 首頁
MS VC 參考文件

 運算子
 輸出與輸入
 流程控制
 陣列
 指標應用
 建立函數
 參數傳遞
 動態配置記憶體
 類別物件
 建構子與解建構子
 this 指標
 例外處理
 資料流
 過載運算子
 fsteram 類別

作品介紹

免費下載

廠商軟體

setf資料流的控制

格式旗標

//小誌的資料格式旗標設定範例
#include <iostream.h>
void main() //主程式開始
{
double d = 24.546;
int n = 24;

cout.setf(ios::fixed);
//設定fixed旗標,使浮點數以fixed方式顯示
cout << "固定格式顯示浮點數...\n" << d << endl;
cout.unsetf(ios::fixed);
//清除fixed旗標
cout.setf(ios::scientific); //設定scientific旗標
cout << "指數型式顯示浮點數...\n" << d << endl;
cout.setf(ios::showpos);
//設定showpos旗標
cout << "在正整數前方加上正號...\n";
cout << d << endl;
cout.setf(ios::oct);
//設定oct旗標,以八進位制顯示數字
cout << "以八進制顯示...\n" << n << endl;
cout.setf(ios::hex, ios::basefield);

//清除進位制控制旗標,然後設定hex旗標以十六進位制顯示數字
cout << "以16進制顯示...\n" << n << endl;
}
//主程式結束

參數設定:setf(參數1,參數2)
取消設定;unsetf(參數1,參數2)

ios::ocx 整數以八進制顯示 ios::left 靠左切齊
ios::dec 整數以十進制顯示 ios::right 靠右切齊
ios::hex 整數以十六進制顯示 ios::fixed 以固定格式顯示浮點數
ios::showpos 在正整數前顯示加號 ios::scientific 以指數格式顯示浮點數

計算子

//小誌的資料流計算子控制範例
#include <iostream.h>
#include <iomanip.h> //載入iomanip標頭檔

void main() //主程式開始
{
double d = 24.5467865;
int n = 24;
cout << "使用計算子 oct , setw() and setiosflags...\n";
cout << oct << setw(10) << setiosflags(ios::right) << n << endl;

//使用setw設定欄位寬度為10,並以setiosflags設定輸出方式為靠右對齊
cout << "使用計算子 setprecision and resetiosflags...\n";
cout << setiosflags(ios::scientific) << setprecision(4) << d << endl;

//設定浮點數以科學記號輸出,並精確度為小數點後4位
cout << resetiosflags(ios::scientific) << d << endl;
//清除浮點數以科學記號輸出的設定
} //主程式結束

ocx 轉換為八進制 ends 插入\0作為字串的結尾
dec 轉換為十進制 setw(n) 設定欄位寬度為n
hex 轉換為十六進制 setprecision(n) 設定精準度為n
ws 忽略輸入的空白 setiosflags(flag) 設定flag旗標
endl 插入新行並更新輸出資料流 resetiosflags(flag) 清除flag旗標

函數

//小誌的資料流函數範例
#include <iostream.h>

void main() //主程式開始
{
double d = 24.5467865;
int n = 24;
cout << "使用資料流函數 [width and setf()]...\n";
cout.width(10);
//使用width設定欄位寬度為10
cout.setf(ios::oct | ios::right); //設定oct與right旗標
cout << n << endl;
cout << "使用資料流函數 [precision]...\n";
cout.precision(4);
//設定精確度為小數點後4位
cout.setf(ios::scientific);
cout << d << endl;
cout.unsetf(ios::scientific);
//清除浮點數以科學記號輸出的設定
cout << d << endl;
}
//主程式結束

p=precsion 傳回目前設定的精準度
precsion(n) 設定浮點數顯示的精準度為n
w=width() 傳回目前設定的欄位寬度
width(n) 設定欄位寬度為n
setf(flag) 設定格式旗標
unsetf(flag) 清除格式旗標

資料流的偵錯

//小誌的資料流偵錯範例
#include <iostream.h>

void main() //主程式開始
{
int i;
cout << "Please input a integer :\n";
cin >> i;
//讀取一個整數
//輸出資料流錯誤檢查函數的檢查結果

cout << "good() = " << cin.good() << endl;
cout << "eof() = " << cin.eof() << endl;
cout << "fail() = " << cin.fail() << endl;
cout << "bad() = " << cin.bad() << endl << endl;
cin.clear();
//重設錯誤狀態位元
cout << "Reset error state ...\n";
cout << "good() = " << cin.good() << endl;
cout << "eof() = " << cin.eof() << endl;
cout << "fail() = " << cin.fail() << endl;
cout << "bad() = " << cin.bad() << endl;
}
//主程式結束

good() 資料流正確傳回非零值
bad() 資料流發生嚴重錯誤時傳回非零值
fail() 資料流發生可恢復錯誤或嚴重錯誤時傳回非零值
eof() 到達資料流尾端傳回非零值
clear() 未傳入錯誤狀態位元,則清除所有錯誤狀態位元
clear(錯誤狀態位元) 傳入某一狀態位元,則設定該錯誤狀態位元


本網頁由
昱得資訊工作室製作(C) Copyright Valor-Studio since in 1993

 

本網站全部內容係屬【昱得資訊工作室】版權所有,轉載必究,
非經正式書面同意,不得將全部或部分內容,以任何方式轉載於其他網站或用於任何商業行為