flags fixed setiosflags ios fixed ios fixedflags the

  • Slides: 44
Download presentation

旗標 (flags) 格式操縱子 fixed 也可以寫成 setiosflags (ios: : fixed) 這裹ios: : fixed這類表示式稱為旗標(flags) 。 運算子「:

旗標 (flags) 格式操縱子 fixed 也可以寫成 setiosflags (ios: : fixed) 這裹ios: : fixed這類表示式稱為旗標(flags) 。 運算子「: : 」稱為範圍運算子 (the scope operator), 或範圍確認運算子 (the scope resolution operator)。 使用這個語法時,using宣告必需寫成 using std: : setiosflags; using std: : ios; 而不是 using std: : fixed; 8

格式操縱子的實例 表 13. 1. 2 Manipulator 9 輸 cout << setw(2) << 3 ;

格式操縱子的實例 表 13. 1. 2 Manipulator 9 輸 cout << setw(2) << 3 ; << endl cout << setw(5) << 158 ; << endl cout << setw(5) << 69. 72 ; << endl cout << setw(5) << left << endl ; << 876 出 | 3| | 158| |69. 72| cout << setw(6) << fixed << setprecision(2) << 133. 456 << endl ; |876 | |133. 46|

範例程式 檔案 Format_1. cpp 12 // Format_1. cpp #include <iostream> #include <iomanip> using namespace

範例程式 檔案 Format_1. cpp 12 // Format_1. cpp #include <iostream> #include <iomanip> using namespace std; int main() { cout << '|' << setw(2) << 3 cout << '|' << setw(5) << 158 cout << '|' << setw(5) << 69. 72 cout << setiosflags(ios: : left) << '|' << setw(5) << 876 cout << setiosflags(ios: : fixed) << setprecision(2) << '|' << setw(6) << 133. 456 return 0; } << '|' << endl ; << '|‘ << endl ; << '|' << endl ;

操作結果 | 3| | 158| |69. 72| |876 | |133. 46| 13

操作結果 | 3| | 158| |69. 72| |876 | |133. 46| 13

範例程式 檔案 Format_2. cpp 16 // Format_2. cpp #include <iostream> #include <iomanip> using namespace

範例程式 檔案 Format_2. cpp 16 // Format_2. cpp #include <iostream> #include <iomanip> using namespace std; // --- 主程式 ---------------int main() { cout << hex << '|' << 14 << '|' << endl; cout << scientific << setprecision(2) << '|' << setw(5) << 46218. 542 << '|' << endl; cout << setprecision (4) << fixed << showpoint << '|' << setw(13) << 64. 7766 << '|' << endl ; }

操作結果 |e| |4. 62 e+04| | 64. 7766| 17

操作結果 |e| |4. 62 e+04| | 64. 7766| 17

13. 2 輸出格式設定間的交互作用 表 13. 2 旗標 (不指定) t 1 t 2 setprecision(5) t

13. 2 輸出格式設定間的交互作用 表 13. 2 旗標 (不指定) t 1 t 2 setprecision(5) t 1 t 2 253 1. 23457 e-01 2. 5 e+02 1. 25 e-0 2. 5 e+02 1. 23 e-01 253. 00 1. 23457 e-01 showpoint fixed 253. 00 0. 13346 showpoint scientific 2. 53 e+02 1. 23 e-01 2. 53000 e+02 showopint 18 setprecision(2) 253. 00000 1. 23457 e-01

範例程式 檔案 Format_3_Short. cpp (獲 得表 13. 2) // Format_3_Short. cpp #include <iostream> #include

範例程式 檔案 Format_3_Short. cpp (獲 得表 13. 2) // Format_3_Short. cpp #include <iostream> #include <iomanip> using namespace std; // --- 主程式 ----------------int main() { double t 1=253. 0; double t 2=0. 123456789; cout 20 << " (1)The value of t 1 is : " << std: : setprecision(2) << t 1 << endl; << " (2)The value of t 1 is : "

<< std: : setprecision(3) << std: : showpoint << t 1 << endl; cout

<< std: : setprecision(3) << std: : showpoint << t 1 << endl; cout << " (3)The value of t 1 is : " << std: : setprecision(5) << std: : showpoint << std: : fixed << t 1 << endl; cout << " (4)The value of t 1 is : " << std: : setprecision(5) << std: : showpoint << std: : scientific << t 1 << endl; return 0; } 21

操作結果 (1) The value of t 1 is : (2) The value of t

操作結果 (1) The value of t 1 is : (2) The value of t 1 is : (3) The value of t 1 is : (4) The value of t 1 is : 22 2. 5 e+02 253. 00000 2. 53000 e+02

三種格式設定語法之比較 (以下三組敘述 的效果完全相同) (3 -1) (1) 在Format_1. cpp中的語法 cout << setiosflags (ios: : fixed||ios:

三種格式設定語法之比較 (以下三組敘述 的效果完全相同) (3 -1) (1) 在Format_1. cpp中的語法 cout << setiosflags (ios: : fixed||ios: : showpoint) << setprecision (2) << t 1 << endl; cout << setiosflags (ios: : hex) << 26; 23

前置處理指令之後的 using指令 (1) 要配合: #include <iomanip> using std: : cout; using std: : endl;

前置處理指令之後的 using指令 (1) 要配合: #include <iomanip> using std: : cout; using std: : endl; using std: : setiosflags; using std: : ios; using std: : setprecision; 24

三種格式設定語法之比較 (3 -2) (2) 在Format_2. cpp中的語法 cout << fixed << showpoint << setprecision (2)

三種格式設定語法之比較 (3 -2) (2) 在Format_2. cpp中的語法 cout << fixed << showpoint << setprecision (2) << t 1 <<endl; cout << hex << 26; 25

前置處理指令之後的 using指令 (2) 要配合: #include <iomanip> using std: : cout; using std: : endl;

前置處理指令之後的 using指令 (2) 要配合: #include <iomanip> using std: : cout; using std: : endl; using std: : fixed; using std: : showpoint; using std: : setprecision; using std: : hex; 26

三種格式設定語法之比較 (3 -3) (2) 在Format_3. cpp中的語法 cout << std: : fixed << std: :

三種格式設定語法之比較 (3 -3) (2) 在Format_3. cpp中的語法 cout << std: : fixed << std: : showpoint << std: : setprecision (2) << t 1 << endl; cout << std: : hex << 26; 27

前置處理指令之後的 using指令 (3) 要配合 (最簡短): #include <iomanip> using std: : cout; using std: :

前置處理指令之後的 using指令 (3) 要配合 (最簡短): #include <iomanip> using std: : cout; using std: : endl; 28

範例程式 檔案 Save. Matrix. cpp // Save. Matrix. cpp #include <iostream> #include <iomanip> #include

範例程式 檔案 Save. Matrix. cpp // Save. Matrix. cpp #include <iostream> #include <iomanip> #include <fstream> using namespace std; const int M = 4; const int N = 5; // --- 函數 Rec. Matrix() 的宣告 ----------void Rec. Matrix (char *, double [][N], int); // --- 主程式 -------------------int main () { double Matrix[M][N]; 31

char *File. Name = "Record. txt"; for (int i = 0; i < M;

char *File. Name = "Record. txt"; for (int i = 0; i < M; i++) for(int j = 0; j < N ; j++) Matrix[i][j]= (i+j*j+0. 5)/(i+j+2); char Ch; cout << "你要將矩陣存在 " << File. Name << " 中嗎? (Y/N)" << endl; cin >> Ch; if (Ch == 'Y' || Ch == 'y') Rec. Matrix (File. Name, Matrix, M, N); else cout << "沒有存檔. " << endl; return 0; 32 }

// --- 函數 Rec. Matrix() 的定義 -----------void Rec. Matrix (char *File. Name. Out, double

// --- 函數 Rec. Matrix() 的定義 -----------void Rec. Matrix (char *File. Name. Out, double A[][N], int M, int N) { ofstream File. Output; File. Output. open( File. Name. Out, ios: : out); if (!File. Output) {cout << "檔案: " << File. Name. Out << " 存檔失敗!" << endl; exit(1); } File. Output << setprecision(4) << right << showpoint << fixed; 33

for (int i = 0; i < M; i++) { File. Output << "第

for (int i = 0; i < M; i++) { File. Output << "第 " << i+1 << " 列:"; for(int j = 0; j < N ; j++) File. Output << setw(8) << A[i][j] << " " ; File. Output << endl; } File. Output. close(); cout << "成功存於檔案 " << File. Name. Out << " 內. " << endl; } 34

操作結果 (執行Save. Matrix. exe在顯示器 上會看到下列訊息) 你要將矩陣存在 Record. txt 中嗎? (Y/N) y 成功存於檔案Record. txt內。 檔案Record.

操作結果 (執行Save. Matrix. exe在顯示器 上會看到下列訊息) 你要將矩陣存在 Record. txt 中嗎? (Y/N) y 成功存於檔案Record. txt內。 檔案Record. txt的內容是: 第 1 列: 0. 2500 0. 5000 1. 1250 1. 9000 第 2 列: 0. 5000 0. 6250 1. 1000 1. 7500 第 3 列: 0. 6250 0. 7000 1. 0833 1. 6429 第 4 列: 0. 7000 0. 7500 1. 0714 1. 5625 35 2. 7500 2. 5000 2. 3125 2. 1667

範例程式 檔案 Matrix. Row. cpp 38 // Matrix. Row. cpp #include <iostream> #include <iomanip>

範例程式 檔案 Matrix. Row. cpp 38 // Matrix. Row. cpp #include <iostream> #include <iomanip> #include <fstream> using namespace std; // --- 函數的宣告 ------------------void Show. Matrix (double A[][N]); void Show. Vector (double A[]); void Pick. Row(double A[][N], double B[], int S); void Set. Col(double A[][N], double B[], int S); // --- 主程式 -------------------int main () { const int M = 4;

39 const int N = 5; double M 2 D[M][N]; double Pick. V [N];

39 const int N = 5; double M 2 D[M][N]; double Pick. V [N]; double Data[] = { 1. 3, 4. 5, 8. 32, 45. 9, 0. 4}; int PRow = 1; int SRow = 2; for (int i = 0; i < M; i++) for(int j = 0; j < N ; j++) M 2 D[i][j]= (i+j*j+0. 5)/(i+j+2); cout << "原來的矩陣是 :n"; Show. Matrix (M 2 D); cout << "原來的向量 Data 是 :n"; Show. Vector (Data); cout << "將矩陣的第 " << PRow+1 << " 列取出成向量 Pick. V:n";

Pick. Row(M 2 D, Pick. V, PRow); Show. Vector (Pick. V); cout << "將矩陣的第

Pick. Row(M 2 D, Pick. V, PRow); Show. Vector (Pick. V); cout << "將矩陣的第 " << SRow+1 << " 列以向量 Data 取代後, n矩陣成為:n"; Set. Col(M 2 D, Data, SRow); Show. Matrix (M 2 D); return 0; } 40

41 // --- 函數 Show. Matrix() 的定義 -----------void Show. Matrix (double A[][N]) { cout

41 // --- 函數 Show. Matrix() 的定義 -----------void Show. Matrix (double A[][N]) { cout << setprecision(4) << right << showpoint << fixed; for (int i = 0; i < M; i++) { cout << "第 " << i+1 << " 列:"; for(int j = 0; j < N ; j++) cout << setw(8) << A[i][j] << " " ; cout << endl; }

// --- 函數 Show. Vector () 的定義 ----------void Show. Vector (double A[]) { cout

// --- 函數 Show. Vector () 的定義 ----------void Show. Vector (double A[]) { cout << setprecision(4) << right << showpoint << fixed; cout << "向量 : "; for(int j = 0; j < N ; j++) cout << setw(8) << A[j] << " " ; cout << endl; } 42

// --- 函數 Pick. Row() 的定義 ------------void Pick. Row(double A[][N], double B[], int S)

// --- 函數 Pick. Row() 的定義 ------------void Pick. Row(double A[][N], double B[], int S) { for (int i=0; i < N; i++) B[i]= A[S][i]; return ; } 43 // --- 函數 Set. Col() 的定義 -------------void Set. Col(double A[][N], double B[], int S) { for(int j = 0; j < N ; j++) A[S][j]= B[j]; return; }

操作結果 44 原來的矩陣是: 第 1 列: 0. 2500 0. 5000 1. 1250 1. 9000

操作結果 44 原來的矩陣是: 第 1 列: 0. 2500 0. 5000 1. 1250 1. 9000 2. 7500 第 2 列: 0. 5000 0. 6250 1. 1000 1. 7500 2. 5000 第 3 列: 0. 6250 0. 7000 1. 0833 1. 6429 2. 3125 第 4 列: 0. 7000 0. 7500 1. 0714 1. 5625 2. 1667 原來的向量 Data 是: 向量 : 1. 3000 4. 5000 8. 3200 45. 9000 0. 4000 將矩陣的第 2 列取出成向量 Pick. V: 向量 : 0. 5000 0. 6250 1. 1000 1. 7500 2. 5000 將矩陣的第 3 列以向量 Data 取代後, 矩陣成為: 第 1 列: 0. 2500 0. 5000 1. 1250 1. 9000 2. 7500 第 2 列: 0. 5000 0. 6250 1. 1000 1. 7500 2. 5000 第 3 列: 1. 3000 4. 5000 8. 3200 45. 9000 0. 4000 第 4 列: 0. 7000 0. 7500 1. 0714 1. 5625 2. 1667