1 / 26

上週上機課:

上週上機課:. 讓使用者輸入五個整數, 然後印出最大與最小數 分別為何。 我們來構思一下,如何寫這個程式!!. while (or for). 先定一最大值與一最小值. 輸入 k. if. 有. 比較 k 有沒有比最大值大,或比最小值小. 改變最大值或最小值. 沒有. 再修改成先問使用者想輸入多少個數字,然後印出最大與最小值!. #include &lt;stdio.h&gt; int main() { int s,l,input,k=0; printf(&quot; 請輸入五個整數 <br>&quot;); printf(&quot; 第 1 個是: &quot;);

suchi
Télécharger la présentation

上週上機課:

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 上週上機課: • 讓使用者輸入五個整數, 然後印出最大與最小數 分別為何。 我們來構思一下,如何寫這個程式!!

  2. while (or for) 先定一最大值與一最小值 輸入 k if 有 比較 k有沒有比最大值大,或比最小值小 改變最大值或最小值 沒有

  3. 再修改成先問使用者想輸入多少個數字,然後印出最大與最小值!再修改成先問使用者想輸入多少個數字,然後印出最大與最小值! #include <stdio.h> int main() { int s,l,input,k=0; printf("請輸入五個整數\n"); printf("第1個是:"); scanf("%d", &input); s=l=input; while(++k <= 4){ printf("第%d個是:", k+1); scanf("%d", &input); if(input <= s) s = input; if(input >= l) l = input; } printf("最大的是%d,最小的是%d\n",l,s ); return 0; }

  4. 加入一個變數j(想輸入的個數),再稍稍修正程式。加入一個變數j(想輸入的個數),再稍稍修正程式。 int s,l,input,k=0, j; printf("請問您想輸入幾個整數\n"); scanf("%d",&j); printf("第1個是:"); scanf("%d", &input); s=l=input; while(++k <= j-1){ printf("第%d個是:", k+1); scanf("%d", &input); if(input <= s) s = input; if(input >= l) l = input; }

  5. * ** *** **** ***** ****** ******* ******** ********* ********** 想法: (1)用A迴圈10圈來印10列 (2)第i列就印i個*   (印i個就是跑i圈: 要用到B迴圈) (3)印完一列後要換列 利用迴圈印出右列圖形: 想想要怎麼辦得到? (當然請用迴圈的方式)

  6. 把while都改成for? #include <stdio.h> int main() { int k, j=0; while(++j <= 10){ k=0; while(++k <= j){ printf("*"); } printf("\n"); } return 0; } int k, j; for(j=1; j <= 10; j++){ for(k=1; k <= j; k++){ printf("*"); } printf("\n"); }

  7. 4.7 switch 多重選擇敘述式 • switch • 需要檢驗一個可能很多情況的變數或運算式時 • 通式: • 由一串的 case標籤與 default狀況(不一定有)組成 switch ( value ){ case '1': actions case '2': actions default: actions } • break:立即結束敘述式

  8. 看個實例: #include <stdio.h> int main() { int grade; int aCount = 0, bCount = 0, cCount = 0, dCount = 0, fCount = 0; printf( “輸入成績的等級\n" ); printf( “結束輸入時按Ctrl+z\n" ); while ( ( grade = getchar() ) != EOF ) { switch ( grade ) { /* 在 while裡用switch */ case ‘A’: case ‘a’: /* 如果輸入為大寫 A or 小寫 a */ ++aCount; break;

  9. case ‘B’: case ‘b’: /* 如果輸入為大寫 B or 小寫 b */ ++bCount; break; case ‘C’: case ‘c’: /* 如果輸入為大寫 C or 小寫 c */ ++cCount; break; case ‘D’: case ‘d’: /* 如果輸入為大寫 D or 小寫 d */ ++dCount; break; case ‘F’: case ‘f’: /* 如果輸入為大寫 F or 小寫 f */ ++fCount; break; case ‘\n’: case‘ ’:   /* 忽略這個輸入 */ break;

  10. default: /* 輸入其他字元時 */ printf( “錯誤輸入" ); printf( “請再輸入一次\n" ); break; } } printf( "\nTotals for each letter grade are:\n" ); printf( "A: %d\n", aCount ); printf( "B: %d\n", bCount ); printf( "C: %d\n", cCount ); printf( "D: %d\n", dCount ); printf( "F: %d\n", fCount ); return 0; }

  11. 注意!! 舉例來說: printf(“The character (%c) has the value %d.”, ‘a’, ‘a’ ); • The character (a) has the value 97. • 在 ACII 字元集中,97代表了小寫 ‘a’ 。 • getchar() : 從鍵盤讀進一個字元,我們可以視需要將一個字元視為一個整數或一個字元。 • a=b=c=0 : 程式讀取數個等號的順序為:  c=0=> b=c=0=>a=b=0。 • EOF : “end of file” 在 <stdio.h>裡的定義一般為 (-1),或是 windows 系統裡按 (ctrl-z)。

  12. 4.8 do…while重複敘述式 • do…while 重複敘述式 • 跟while 敘述式有點像 • 迴圈本體執行後才檢驗 • 所以,迴圈至少會執行一次 • 通式 • do { • statement; • } while ( condition );

  13. 4.8 do…while重複敘述式 • 舉例來看 (令 counter = 1): • do { • printf( "%d ", counter ); • } while (++counter <= 10); • 從 1 印到 10

  14. 把while都改成do…while? #include <stdio.h> int main() { int k, j=0; while(++j <= 10){ k=0; while(++k <= j){ printf("*"); } printf("\n"); } return 0; } int k, j=0; do{ k=0; do{ printf("*"); } while(++k <= j); printf("\n"); } while(++j <= 10); 這兩個印出來會有點不一樣喔!猜猜哪裡不同?

  15. 4.9 break和continue敘述式 • break • 可以在 while,for,do…while,switch 中立即跳出敘述式來。 • 程式接著由跳出後的第一個敘述式接下去 • break 通常都用在 • 可以早點跳出一個迴圈 • 跳過switch敘述式剩下的部分

  16. 印出:1 2 3 4Broke out of loop at x == 5 #include <stdio.h> int main() { int x; for ( x = 1; x <= 10; x++ ) { if ( x == 5 ) break; /* break 當 x == 5 */ printf( "%d ", x ); } printf( "\nBroke out of loop at x == %d\n", x ); return 0; }

  17. 4.9 break和continue敘述式 • continue • 在while,for 或 do…while敘述式執行時,跳過剩下的動作直接執行下一次迴圈 • 在while和 do…while時 • 迴圈繼續條件會在 continue之後立刻檢驗。 • 在for時 • 會先執行遞增動作,然後才檢驗繼續條件。

  18. 印出:1 2 3 4 6 7 8 9 10Used continue to skip printing the value #include <stdio.h> int main() { int x; for ( x = 1; x <= 10; x++ ) { if ( x == 5 ) continue; printf( "%d ", x ); } printf( "\nUsed continue to skip printing the value 5\n" ); return 0; }

  19. 4.10 邏輯運算子 • && ( 邏輯 AND ) • 兩者皆真才為真 • || ( 邏輯 OR ) • 一者為真即為真 • ! ( 邏輯 NOT or 邏輯否定) • 反轉真偽的情況 • 一元運算子,非二元運算子 • 於多重檢驗的情況下很有用 運算式 結果 真 && 偽     偽真 || 偽     真 !偽 真

  20. 4.11 對於相等運算子(==) 和指定運算子(=) 常見的混淆 • Dangerous error • 不會出現語法錯誤 • 可以產生值的任何運算式都可以使用在任何敘述式的判斷裡;非零為真,零則偽。 • 舉例說明,使用 ==時: if ( payCode == 4 ) printf( "You get a bonus!\n" ); • 這程式的意思是:檢驗paycode,如果等於4就可以得到bonus。

  21. 4.11 對於相等運算子(==) 和指定運算子(=) 常見的混淆 • 例子中的 == 如果換成了 =: if ( payCode = 4 ) printf( "You get a bonus!\n" ); • 這樣會定義 paycode 等於 4 • 因為 4 非零,所以運算式為真,也就表示無論paycode原來是多少,都會有bonus • 這是邏輯上的錯誤(得不到想要的答案),但是語法上也沒錯。

  22. 4.11 對於相等運算子(==) 和指定運算子(=) 常見的混淆 • lvalues(左邊數值) • 可以被放在左邊 • 左邊放的都是可以改變數值的數,即變數 • x = 4; • rvalues(右邊數值) • 只能出現在一個等式的右邊 • 就好像一個常數 • 不能寫成 4 = x 而必須寫成 x = 4 • 左邊數值可以當作右邊數值,但是反之不然 • y = x;

  23. 4.12 結構化程式設計總整理 • 結構化的程式 • 比較容易被瞭解,除錯與修改 • 結構化程式的規則 • 由程式的格式與特性導出的規則 • 只使用 單一入口/單一出口 的零件 • 規則: • 從最簡單的流程圖著手 • 任何矩形(動作)都可以用兩個連續的矩形(動作)來取代 • 任何矩形(動作)可由任何的控制敘述式取代 (循序, if,if/else,switch,while,do/while or for) • 規則 2 與 3 可以隨你喜歡運用

  24. 4.12 結構化程式設計總整理 • 任何程式都可以被分解成三種控制格式: • 循序 • 選擇 –if,if/else或 switch • 重複 –while,do…while或 for • 只能用兩種方法來結合他們 • 巢式 (規則 3) • 堆疊式 (規則 2) • 任何選擇格式其實都可以用 if 敘述式來取代,而任何 重複格式都可以用 while 敘述式來取代。

  25. 3/23 上機課 #include <stdio.h> int main() { int k, j; for(j=1; j <= 10; j++){ for(k=1; k <= j; k++){ printf("*"); } printf("\n"); } return 0; } * ** *** **** ***** ****** ******* ******** ********* **********

  26. 提示:(C),(D)必須以適當的空格當作開頭 ********** ********* ******** ******* ****** ***** **** *** ** * (B) ********** ********* ******** ******* ****** ***** **** *** ** * (C) * ** *** **** ***** ****** ******* ******** ********* ********** (D)

More Related