計算機概論與程式設計 Introduction to Computers and Programming, 交大電機工程學系 溫宏斌老師有課程影音與講義可下載使用,課本(電子書)在意見回饋裡最後的流言可以找到。
Array(陣列)的補充範例
字串的使用:#include <iostream> #include <string> #include <algorithm> using namespace std; int main(){ /*
如果用三個變數儲存國名,後面要使用就只能用 swtich ...casechar name[] = "曹操"; // in C string name = "曹操"; // in C++
string nation0="魏"; string nation1="蜀"; string nation2="吳"; */ string nations[3]={"魏","蜀","吳"}; string names[3][4]= { {"曹操","曹丕","曹植","司馬懿"}, {"劉備","關羽","張飛","諸葛亮"}, {"孫權","周瑜","魯肅","陸遜"} }; int ages[3][4]= { {61,40,37,39}, {45,43,42,28}, {27,35,38,25} }; int i=0,j=0; cout << "names["<< i << "][" << j <<"] is " << names[i][j] << endl;; cout << endl; string search_for ="魯肅"; for (int i=0;i<3;i++){ for (int j=0;j<4;j++){ if (names[i][j]==search_for){ cout << search_for << "is at names["<<i<<","<<j<<"]" << endl; cout << search_for << "is" << nations[i] << endl; /* cout << search_for << "is"; switch(i){ case 0: cout << nation0; break; case 1: cout << nation1; break; case 2: cout << nation2; break; } cout << endl;; */ } } } cout << endl; // find oldest //cout << *max_element(ages[0],ages[0]+4); int oldests[3]= {0}; for (int i=0;i<3;i++) oldests[i]= *max_element(ages[i],ages[i]+4); int oldest = *max_element(oldests,oldests+3); cout << "最老的人的年齡是:" << oldest << endl; for (int i=0;i<3;i++){ for (int j=0;j<4;j++){ if (ages[i][j]==oldest){ cout << "oddest is " << names[i][j] << endl; } } } cout << endl; system("pause"); return 0; }
沒有留言:
張貼留言