3 1 1 cin include iostream n cin









![3. 3. 1 設定輸入長度setw #include <iomanip> n setw(指定長度) n n 範例 char string[4]; //宣告字串變數string 3. 3. 1 設定輸入長度setw #include <iomanip> n setw(指定長度) n n 範例 char string[4]; //宣告字串變數string](https://slidetodoc.com/presentation_image/43a9d9da401434c42ef2644fb22e2c94/image-10.jpg)






















- Slides: 32
3. 1. 1 輸入函數cin #include <iostream> n cin >> 變數; n n 範例 int length; cin >> length; //宣告整數變數length //將輸入資料存入length
3. 1. 2 多重輸入cin #include <iostream> n cin >> 變數 1 >> 變數 2 >>. . . >> 變數n; n n 範例 int width, height; //宣告變數width與height cin >> width >> height; //分別存入width與height
3. 2. 1 設定輸出長度setw n n n #include <iomanip> setw(指定長度) 範例 int number = 25; cout << '(' << number << ")n"; //輸出(25) cout << '(' << setw(3) << number << ")n"; //輸出( 25) cout << '(' << setw(5) << number << ")n"; //輸出( 25)
3. 2. 2 設定有效數字setprecision n #include <iomanip> setprecision(有效位數) 範例 const double PI = 3. 141592653; cout << setprecision(10) << PI << endl; //輸出 3. 141592653十位 cout << setprecision(8) << PI << endl; //輸出 3. 1415927八位有效 cout << setprecision(6) << PI << endl; //輸出 3. 14159六位有效
3. 2. 3 設定輸出旗號setiosflags n n n #include <iomanip> setiosflags(ios: : 格式旗號); 範例 float number = 35. 7 cout << setprecision(4) << number; //顯示 35. 7 cout << setiosflags(ios: : fixed) << setprecision(2) << number; //顯示 35. 7 cout << setiosflags(ios: : fixed|ios: : showpoint) << setprecision(2) << number; //顯示 35. 70
3. 3. 1 設定輸入長度setw #include <iomanip> n setw(指定長度) n n 範例 char string[4]; //宣告字串變數string cin >> setw(4) >> string; //設定輸入字數並取得輸入
3. 4. 3 轉換資料型態 n 指定型態(資料|變數) n 由小轉大 範例一 int n; short n 1 = 32767; n = int(n 1 + 1); 範例二 int n; unsigned short n 2 = 65535; n = int(n 2 + 1); n n //n 1=32767 //n=32767+1=32768 //n 2=65535 //n=65535+1=65536
3. 4. 3 轉換資料型態 (續) n n n 範例三 int n; short n 3 = -32768; n = int(n 3 - 1); 範例四 int n; unsigned short n 4 = 0; n = int(n 4 - 1); 範例五 int a=5; int b=3; float c = (float(a) / float(b)); //n 3=-32768 //n=-32768 -1=-32769 //n 4=0 //n=0 -1=-1
3. 4. 3 轉換資料型態 (續) n n 由大轉小 範例一 int. Var = 65500; signed short. Var = short(int. Var); 範例二 short. Let = 65; char. Let = char(short. Let); 範例三 float. Num = 70000. 0 f; int. Num = int(float. Num); short. Num = short(float. Num); //int. Var=65500 //short. Var=-36 //short. Let=65 //char. Let='A' //float. Num=70000. 0 //int. Num=70000 //short. Num=4464
3. 6. 1 亂數函數 產生固定亂數 n #include <cstdlib> n int rand(void) n n 範例 cout << rand() << endl; //輸出亂數
3. 6. 1 亂數函數 (續) n n 產生種子亂數 #include <cstdlib> void srand (unsigned 種子數) 範例 unsigned seed; cin >> seed; //輸入種子數 srand(seed); //設定亂數種子數 cout << rand() << endl; //輸出亂數
3. 6. 1 亂數函數 (續) n n 產生隨機亂數 #include <ctime> time(*指標) 範例 void srand (time(NULL)); cout << rand() << endl; //輸出亂數
3. 6. 2 三角函數 n n n #include <cmath> double sin(double 徑度) double cos(double 徑度) double tan(double 徑度) 範例 x = 30 * (3. 14159 / 180); double a = sin(x); double b = cos(x); double c = tan(x); //x = 30 //a = sin(30 ) //b = cos(30 ) //c = tan(30 )
3. 6. 3 指數與對數 n n n #include <cmath> double exp(數值) double log 10(數值) 範例 double a = log(2); double b = log 10(2); double c = exp(2); //a = ln(2) //b = log 10(2) //c = e 2
3. 6. 4 冪次與開方 #include <cmath> n double pow(底數, 冪次) n double sqrt(數值) n n 範例 double a = pow(2, 3); double b = sqrt(3); //a=23 //b=根號 3
3. 6. 5 小數進位與切除小數 n n #include <cmath> double ceil(數值) double floor(數值) 範例 int m 1 = ceil(3. 33) int n 1 = floor(3. 33); int m 2 = ceil(-3. 33) int n 2 = floor(-3. 33); //m 1 = 4 //n 1 = 3 //m 2 = -3 //n 2 = -4
3. 6. 6 取絕對值 #include <cmath> n double fabs (數值) n n 範例 int a = fabs(5. 25); int b = fabs(-3. 75); //a = 5. 25 //b = 3. 75





















