Presentation is loading. Please wait.

Presentation is loading. Please wait.

綠園 2012/11/06. Array 的宣告 整數陣列的宣告 int student[5]; 意義:宣告了 5 個 int 大小的連續空間,名稱 為 student ,沒有預設值,則為系統殘值。 student student[0] student[1] student[4] student[2]

Similar presentations


Presentation on theme: "綠園 2012/11/06. Array 的宣告 整數陣列的宣告 int student[5]; 意義:宣告了 5 個 int 大小的連續空間,名稱 為 student ,沒有預設值,則為系統殘值。 student student[0] student[1] student[4] student[2]"— Presentation transcript:

1 綠園 2012/11/06

2 Array 的宣告 整數陣列的宣告 int student[5]; 意義:宣告了 5 個 int 大小的連續空間,名稱 為 student ,沒有預設值,則為系統殘值。 student student[0] student[1] student[4] student[2] student[3]

3 Array 的宣告 整數陣列的宣告 int student[5]={0}; 意義:宣告了 5 個 int 大小的連續空間,名稱 為 student, 且裏面的值皆預設為 0 。 00000 student student[0] student[1] student[4] student[2] student[3]

4 Array 的宣告 整數陣列的宣告 int student[5]={1, 3, 4, 6, 7}; 意義:宣告了 5 個 int 大小的連續空間,名稱 為 student, 其預設值如下: 13467 student student[0] student[1] student[4] student[2] student[3]

5 Array 的宣告 浮點數陣列的宣告 float num[4]={1.2, 3.2, 4.5}; 意義:宣告了 4 個 float 大小的連續空間,名稱 為 num , 其預設值如下: 1.23.24.5 num num[0]num[1]num[2]num[3] 0

6 Array 的宣告 字元陣列的宣告 — 常用來儲存字串 char name[10]= “John” ; (雙引號) 意義:宣告了 10 個 char 大小的連續空間,名稱 為 name, 其預設值如下: char name[10]={ ‘J’,‘o’,‘h’,‘n’ }; (單引號) 意義:宣告了 10 個 char 大小的連續空間,名稱 為 name, 其預設值如下 J name name[0] ohn name[1]name[2]name[9]name[8] \0 J name name[0] ohn name[1]name[2]name[9]name[8]

7 Array 的宣告 字串陣列的宣告 — 常用來儲存字串 char name[3][10] = { “ John1 ”, “ John2 ”, “ John3 ” } 意義:宣告了 3 組 10 個 char 大小的連續空間,名稱 為 name, 其預設值如下: name[0] John1 John2 John3 name[1] name[2] \0

8 Array 的大小 陣列元素的個數寫在中括號之內,傳統的陣列長度不能 為變數,必須是確定不變的數。 【例一】 const int SIZE = 10; int foo[SIZE]; 【例二】 #define MAX 50; int main() { char data[MAX]; ……… 【例三】 int i; cin >> i; int bar[i]; // 錯誤

9 兩陣列的指定 int i, foo[100], bar[100]; for(i=0; i<100; i++) foo[i]=i+1; // 正確 for(i=0; i<100; i++) bar[i]=foo[i]; // 錯誤 bar = foo;

10 Array 的運用-- Fibonacci #include #include using namespace std; int main() { int f[10]; int i; f[0]=1; f[1]=1; for(i=2; i<10; i++) { f[i] = f[i-1] + f[i-2]; } system(“PAUSE”); return 0; }

11 Array 的運用 -- 找最大值與最小值 請編寫一程式,找出 33, 75, 69, 41, 52, 19 中的最 大值 (max) 與最小值 (min) 。 Hint: (1) 先開一陣列,將這些數值存下。 (2) 最大值: max ,最小值: min 。 (3) 用重覆結構 (for 迴圈 或 while 迴圈)一一比對。 (4) 印出 max 和 min 。

12 【隨堂練習】試設計一程式,將字串陣列中的所有 小寫字母轉換成大寫字母。 #define MAX 50 int main() { char data[MAX]; cout << "Input a string:" ; /* 輸入字串 */ cin.getline(data, MAX); /* 小寫轉換成大寫 */ …… cout <<"\n** After translation **\n"; cout << data << endl; /* 印出陣列的內容 */ system("PAUSE"); return 0; }

13 Array 的運用 - 泡泡排序法 (Bubble Sort) int main() {int list[5]={3, 5, 2, 4, 1}; int i, j, tmp; for(i=4; i>0; i--) for(j=0; j<i; j++) if(list[j] > list[j+1]) // 比較 { tmp = list[j]; // 交換 list[j] = list[j+1]; list[j+1] = tmp; } for(i=0; i<5; i++) // 印出結果 cout << list[i] << ″ ″; system("PAUSE"); return 0; }

14 Array 的運用 - 泡泡排序法 (Bubble Sort) int main() {int list[5]={3, 5, 2, 4, 1}; int i, j, tmp; for(i=4; i>0; i--) for(j=0; j<i; j++) if(list[j] > list[j+1]) // 比較 { tmp = list[j]; // 交換 list[j] = list[j+1]; list[j+1] = tmp; } for(i=0; i<5; i++) // 印出結果 cout << list[i] << ″ ″; system("PAUSE"); return 0; }

15 二維陣列 宣告:資料型態 陣列名稱 [ 列的個數 ][ 行的個數 ] int sale[2][4]={{30,35,26,32}, {33,34,30,29}}; [0][0] 30 [0][1] 35 [0][2] 26 [0][3] 32 [1][0] 33 [1][1] 34 [1][2] 30 [1][3] 29 0123 0 1 sale 【行】【行】 【列】【列】 sale[0] sale[1]

16 二維陣列 — 印出陣列中的資料 int main(void) { int i,j,sum=0; int sale[2][4]={{30,35,26,32},{33,34,30,29}}; for(i=0;i<2;i++) { cout << “ 業務員 ” << (i+1) << “ 的業績分別為: ”; for(j=0;j<4;j++) { cout << sale[i][j] << “ ”; sum+=sale[i][j]; } cout << endl; } cout <<endl <<“ 本年度總銷售量為 ” <<sum <<“ 輛車 ” <<endl; system(“pause”); return 0; }

17 二維陣列 — 印出陣列中的資料 int main(void) { int i,j,sum=0; int sale[2][4]={{30,35,26,32},{33,34,30,29}}; for(i=0;i<2;i++) { cout << “ 業務員 ” << (i+1) << “ 的業績分別為: ”; for(j=0;j<4;j++) { cout << sale[i][j] << “ ”; sum+=sale[i][j]; } cout << endl; } cout <<endl <<“ 本年度總銷售量為 ” <<sum <<“ 輛車 ” <<endl; system(“pause”); return 0; }

18 二維陣列 C++ 允許二維以上的多維陣列不必定義陣列的長度, 但是只有最左邊 ( 第一個 ) 的註標可以省略不定義外, 其它的註標都必須定義其長度。如: int temp[][4]={{30,35,26,32}, {33,34,30,29}, {25,33,29,25}};

19 多維陣列 int a[2][4][3]; a[0][0][0]a[0][0][1]a[0][0][2] a[0][1][0]a[0][1][1]a[0][1][2] a[0][2][0]a[0][2][1]a[0][2][2] a[0][3][0]a[0][3][1]a[0][3][2] 第一維 第二維 第三維 第一維 第二維 第三維

20 綠園 2012/11/06

21 字串的輸入與輸出 使用 cin char str[20]; cin >> str; 字串中不可包含空白字元。 使用 cin.getline( 字串名稱, 最大長度, 字串結束字元 ) ; char str[15]; cin.getline(str,15); 字串結束字元預設為 ‘\n’ ,若不需更改則不必指出該結束字元。 字串中可包含空白字元。 使用 cin.get( 字元變數名稱); char ch; cin.get(ch); 在輸入單一字元的情況下使用。

22 字串陣列元素的引用及存取 int main(void) { int i; char name[3][15]; for(i=0;i<3;i++) // 輸入字串 { cout << “Input student” << i << “\’s name:”; cin.getline(name[i],15); } for(i=0;i<3;i++) // 輸出字串 cout << “name[” << i << “]=” << name[i] << endl; cout << endl; for(i=0;i<3;i++) // 輸出字串位址 { cout << “addr of name[” << i << “]=” << &name[i] << endl; cout << “addr of name[” << i << “][0]=”; cout << (name+i) << endl << endl; } system(“pause”); return 0; }

23 字串陣列的複製 int main(void) { int i,j; char name[3][15]={“David”, “Jane Wang”, “Tom Lee”}; char copystr[3][15]; for(i=0;i<3;i++) { for(j=0;j<15;j++) if(name[i][j] == ‘\0’ break; else copystr[i][j] = name[i][j]; copystr[i][j]=‘\0’; } for(i=0;i<3;i++) cout << “copystr[” << i << “]=” << copystr[i] << endl; system(“pause”); return 0; }

24 字串陣列的複製 int main(void) { int i,j; char name[3][15]={“David”, “Jane Wang”, “Tom Lee”}; char copystr[3][15]; for(i=0;i<3;i++) // 複製字串陣列 { for(j=0;j<15;j++) if(name[i][j] == ‘\0’) // 判斷是否為字串結束字元 break; else copystr[i][j] = name[i][j]; copystr[i][j]=‘\0’; } for(i=0;i<3;i++) // 輸出字串陣列 cout << “copystr[” << i << “]=” << copystr[i] << endl; system(“pause”); return 0; }

25 常用字串處理函數 strlen 字串長度 strlen(string); // 計算 string 字串的長度 strcat 字串連結 strcat(dest,source); // 將 dest 字串加上 source 字串後存回 dest 。 strcpy 字串拷貝 strcpy(dest, source); // 將 source 字串拷貝至 dest strlwr 將字串中的大寫字母轉換小寫 strupr 將字串中的小寫字母轉換大寫 strcmp 字元比較 strcmp(str1, str2); // 根據 ASCII 值的大小比較 str1, str2 ,傳回值分為 // 小於 0 : str1 < str2 // 等於 0 : str1 = str2 // 大於 0 : str1 > str2 需加上標頭檔 #include

26 常用字元處理函數 isalpha 是否為英文字母 isalpha(ch); // 結果為0表示為數字 // 結果為1表示為大寫英文字母 // 結果為2表示為小寫英文字母 isupper 是否為大寫英文字母 isupper(ch); islower 是否為小寫英文字母 islower(ch); toupper 轉換為大寫英文字母 toupper(ch); tolower 轉換為小寫英文字母 tolower(ch); 需加上標頭檔 #include

27 C++ 的字串 C++ 的 string 不同於 int 和 float 等資料型態, std::string 未內建於核心語言中,需從 C++ 程式 庫引入,亦即使用前須先加上標頭檔 #include string first_name; string last_name; string full_name;

28 字串的讀取 string name; // 讀取空白之前之字串 cin >> name; // 讀取換行之前之整行字串 getline(cin, name);

29 字串相加 int main() { string first_name; string last_name; string full_name; cin >> first_name; cin >> last_name; full_name = first_name + “ ” + last_name; cout << “Your name is ” << full_name << endl; return 0; }

30 String 的成員函數 ( 一 ) int main() { string first_name, last_name, full_name; char first_ch, last_ch; int name_length; cin >> first_name; cin >> last_name; full_name = first_name + “ ” + last_name; first_ch = first_name.at(0); name_length = full_name.length(); last_ch = full_name.at(name_length - 1); cout << first_ch << “ ” << last_ch << endl; return 0; }

31 String 的成員函數 ( 二 ) 可以利用 substr 成員函數截取部分字串 string.substr(first, last) // 01234567890123 main_string = “This is a test”; substring = main_string.substr(5,6); 其結果將印出 is 若省略 last , substr 將傳回 first 之後的字串。

32 【作業練習】 一. 修改前面投影片範例,先亂數產生十個整數,再利 用泡泡排序法依小到大排序。 二. 利用二維陣列方式來計算巴斯卡三角形的值。 1 1 1 p[i][j] = 1 2 1 1 3 3 1 1 4 6 4 1 p[i][j] = 1, 當 i=j 或 j=0 時 p[i-1][j-1] + p[i-1][j]


Download ppt "綠園 2012/11/06. Array 的宣告 整數陣列的宣告 int student[5]; 意義:宣告了 5 個 int 大小的連續空間,名稱 為 student ,沒有預設值,則為系統殘值。 student student[0] student[1] student[4] student[2]"

Similar presentations


Ads by Google