1 / 10

多重選擇 ---switch

多重選擇 ---switch. 適用於多選一 可減少 if 與 else 配對混淆的錯誤. switch 敘述 (switch statement). 根據括號內運算式的結果 , 來找 case 後面的選擇值. switch ( 運算式 ) { case 選擇值 1 : 敘述主體 1; break; …….. case 選擇值 n : 敘述主體 2; break; d efault : /* 若運算式的結果不等於 1~n, 就執行此部份* / 敘述主體 ; }. 有無 break 的差異. expr.

Download Presentation

多重選擇 ---switch

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. 多重選擇---switch 適用於多選一 可減少if 與 else配對混淆的錯誤.

  2. switch 敘述(switch statement) 根據括號內運算式的結果,來找case後面的選擇值 switch ( 運算式 ) { case選擇值 1: 敘述主體1; break; …….. case選擇值 n: 敘述主體2; break; default :/*若運算式的結果不等於1~n,就執行此部份*/ 敘述主體; } 有無break的差異

  3. expr 選擇值2 default 選擇值1 選擇值n … 敘述主體2 敘述主體 敘述主體1 敘述主體n break break break 其它敘述 Switch簡略流程圖

  4. #include<stdio.h> #include<stdlib.h> int main(void) { int a,b,output; char oper; printf("please input a expression(like 3+2):"); scanf("%d %c %d",&a,&oper,&b); switch(oper) { case '+': output=a+b; break; /*計算a+b*/ case '-': output=a-b; break; /*計算a-b*/ case '*': output=a*b; break; /*計算a*b*/ case '/': output=a/b; break; /*計算a/b*/ default: printf("you type anon-standard expression!!\n"); } printf("the calculated result:%d\n",output); system(“pause"); return 0; } Example 1 程式碼

  5. 開始 輸入運算式 oper / * - default + a/b a*b 非標準的 運算式 a-b a+b break break break break 輸出運算結果 結束 Example A 流程圖

  6. 若將此”break;”拿掉,試試看輸入b會有什麼結果若將此”break;”拿掉,試試看輸入b會有什麼結果 switch 敘述(不加break) ----範例 1 • #include<stdio.h> • int main(void) • { • char grade; • printf("Input grade : "); • scanf("%c", &grade); • switch (grade) /*檢查 grade 值,做多重選擇*/ • { • case 'a': • case 'A': printf("Excellent!\n"); break; /*輸入a或A時*/ • case 'b': • case 'B': printf("Good!\n"); break; /*輸入b或B時*/ • case 'c': • case 'C': printf("Be study hard!\n"); break; /*輸入c或C時*/ • default: printf("Failed\n"); /*輸入其他字元時*/ • } • system("pause"); • return 0; } Ch2_1_1.c

  7. switch 敘述(switch statement) 範例2: 題庫[A-19] • 設電力公司的電費計算方式分成三類: • 家庭用電:100度以下,每度2.5元;101~300度,每度3.3元; • 301度(含)以上每度4.2元。 • 工業用電:其本費為每一契約馬力150元,實際用電費每度1.9元。 • 營業用電:0~300度,每度6元;301度(含)以上每度6.8元。 • 輸入用電類別及使用度數後,計算其應繳電費為何?

  8. 範例2流程圖 T

  9. #include<stdio.h> int main(void) { int T; /* 用電類別 */ float Deg; /* 用電度數 */ float C; /* 工業用電契約馬力 */ float Fee; /* 電費 */ float TypeA1=2.5; float TypeA2=3.3; float TypeA3=4.2; float TypeB2=1.9; float TypeB1=150.0; float TypeC1=6.0; float TypeC2=6.8; printf("1. 家庭用電"); printf("\n"); printf("2. 工業用電"); printf("\n"); printf("3. 營業用電"); printf("\n"); printf("請輸入用電類別(1~3): "); scanf("%d", &T); if (T>=1 && T<=3) { printf("用電度數= "); scanf("%f", &Deg); switch 敘述(switch statement) ---- 範例 2 程式碼(1): Ch2_1_2.c

  10. switch(T) { case 1: if (Deg<=100) Fee = Deg*TypeA1; else if (Deg<=300) Fee = (Deg-100)*TypeA2 + 100*TypeA1; else Fee = (Deg-300)*TypeA3 + 200*TypeA2 + 100*TypeA1; break; case 2: printf("契約馬力= "); scanf("%f", &C); Fee = C*TypeB1 + Deg*TypeB2; break; case 3: if (Deg<=300) Fee = Deg*TypeC1; else Fee = (Deg-300)*TypeC2 + 300*TypeC1; break; } printf("電費共為%f", Fee); printf("\n"); } else { printf("類別錯誤!"); printf("\n"); } system("pause"); return 0; } switch 敘述(switch statement) ----範例 2 程式碼(2):

More Related