首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C 語言判斷語句實戰大全:if-else/switch 八大經典案例(閏年/機票/計算器/限行/稅收/銀行/評分)

     編輯:關於C語言

C 語言判斷語句實戰大全

判斷語句是程序邏輯的分水嶺。本文精選 8 個涵蓋生活、商業、行政場景的經典案例,系統演示 if-else 多重分支與 switch-case 多分支的實戰應用、邊界處理、輸入驗證與輸出格式化技巧。

案例 1:年月判斷天數(雙版本對比)

版本 A:if-else 區間判斷

int year, month;
scanf("%d%d", &year, &month);
if (month < 1 || month > 12) printf("月份錯誤\n");
else if (year < 0) printf("年份錯誤\n");
else if (month == 2) {
    if ((year%4==0 && year%100!=0) || year%400==0) printf("%d年%d月29天\n", year, month);
    else printf("%d年%d月28天\n", year, month);
}
else if (month==1||month==3||month==5||month==7||month==8||month==10||month==12)
    printf("%d年%d月31天\n", year, month);
else printf("%d年%d月30天\n", year, month);

版本 B:switch-case 多分支(推薦)

switch (month) {
    case 4: case 6: case 9: case 11: printf("30天\n"); break;
    case 2:
        if ((year%4==0 && year%100!=0) || year%400==0) printf("29天\n");
        else printf("28天\n");
        break;
    default: printf("31天\n");  // 1,3,5,7,8,10,12
}

案例 2:機票價格計算(旺季/淡季 × 艙位)

int month, cabin; const double base = 1200;
scanf("%d%d", &month, &cabin);
if (month >= 9 && month <= 12) {        // 旺季
    if (cabin == 1) printf("頭等艙9.5折: %.2f\n", base*0.95);
    else if (cabin == 2) printf("經濟艙8.5折: %.2f\n", base*0.85);
} else {                                 // 淡季
    if (cabin == 1) printf("頭等艙5折: %.2f\n", base*0.5);
    else if (cabin == 2) printf("經濟艙2折: %.2f\n", base*0.2);
}

案例 3:四則運算計算器(switch + 除零保護)

double a, b, result; char op;
scanf("%lf %c %lf", &a, &op, &b);
switch (op) {
    case '+': result = a+b; break;
    case '-': result = a-b; break;
    case '*': result = a*b; break;
    case '/': 
        if (b == 0) { printf("除數不能為0\n"); return 0; }
        result = a/b; break;
    default: printf("運算符錯誤\n"); return 0;
}
printf("%.2f %c %.2f = %.2f\n", a, op, b, result);

案例 4:車輛限行判斷(星期 × 尾號)

int weekday, tail;
scanf("%d%d", &weekday, &tail);
int restricted = 0;
switch (weekday) {
    case 1: restricted = (tail==2 || tail==7); break;
    case 2: restricted = (tail==3 || tail==8); break;
    case 3: restricted = (tail==4 || tail==9); break;
    case 4: restricted = (tail==5 || tail==0); break;
    case 5: case 6: case 7: restricted = 0; break;  // 假設周末不限行
}
if (restricted) printf("今天限行,尾號%d不能上路\n", tail);
else printf("今天不限行\n");

案例 5:字母大小比較(ASCII 碼)

char c1, c2;
scanf(" %c %c", &c1, &c2);  // 空格跳過換行
if (c1 > c2) printf("較大: %c\n", c1);
else if (c1 < c2) printf("較大: %c\n", c2);
else printf("相等\n");

案例 6:三數求最大值

double a, b, c, max;
scanf("%lf%lf%lf", &a, &b, &c);
max = a;
if (b > max) max = b;
if (c > max) max = c;
printf("最大值: %.4f\n", max);

案例 7:金額換算面額張數(貪心算法)

long amount;
scanf("%ld", &amount);
long denominations[] = {100, 50, 20, 10, 5, 1};
int counts[6] = {0};
for (int i = 0; i < 6; i++) {
    counts[i] = amount / denominations[i];
    amount %= denominations[i];
}
printf("面額: 100  50  20  10  5  1\n");
printf("張數: ");
for (int i = 0; i < 6; i++) printf("%-5d", counts[i]);
printf("\n");

案例 8:鐵路行李托運費用(分段計價)

double weight, fee;
scanf("%lf", &weight);
if (weight <= 0) printf("重量錯誤\n");
else if (weight <= 50) fee = weight * 0.15;
else fee = 50*0.15 + (weight-50)*0.10;
printf("托運費: %.2f 元\n", fee);

案例 9:分段函數求值

double x, y;
scanf("%lf", &x);
if (x < 1) y = x;
else if (x < 10) y = 2*x - 1;
else y = 3*x - 11;
printf("y = %.3f\n", y);

案例 10:個人所得稅計算(速算扣除數法)

double salary, taxable, tax = 0;
scanf("%lf", &salary);
taxable = salary - 300 - 3500;  // 扣除費用+起征點
if (taxable > 0) {
    if (taxable <= 1500) tax = taxable * 0.03;
    else if (taxable <= 4500) tax = taxable * 0.10 - 105;
    else if (taxable <= 9000) tax = taxable * 0.20 - 555;
    else if (taxable <= 35000) tax = taxable * 0.25 - 1005;
    else if (taxable <= 55000) tax = taxable * 0.30 - 2755;
    else if (taxable <= 80000) tax = taxable * 0.35 - 5505;
    else tax = taxable * 0.45 - 13505;
    printf("個稅: %.2f\n", tax);
} else printf("免稅\n");

案例 11:企業利潤提成(累進制)

double profit, bonus;
scanf("%lf", &profit);
if (profit <= 10) bonus = profit * 0.10;
else if (profit <= 20) bonus = 1.0 + (profit-10)*0.075;
else if (profit <= 40) bonus = 1.0 + 0.75 + (profit-20)*0.05;
else if (profit <= 60) bonus = 1.0 + 0.75 + 1.0 + (profit-40)*0.03;
else if (profit <= 100) bonus = 1.0 + 0.75 + 1.0 + 0.6 + (profit-60)*0.015;
else bonus = 1.0 + 0.75 + 1.0 + 0.6 + 0.6 + (profit-100)*0.01;
printf("提成: %.4f 萬\n", bonus);

案例 12:銀行存款利息(活期/定期三種)

double principal; char type; int kind;
scanf("%lf %c", &principal, &type);
if (type=='h'||type=='H') printf("活期: %.2f\n", principal*0.0035);
else if (type=='d'||type=='D') {
    scanf("%d", &kind);
    switch (kind) {
        case 1: printf("整存整取: %.2f\n", principal*0.0325); break;
        case 2: printf("零存整取: %.2f\n", principal*0.0285); break;
        case 3: printf("定活兩便: %.2f\n", principal*0.0325*0.6); break;
        default: printf("定期類型錯誤\n");
    }
} else printf("類型錯誤\n");

案例 13:實習生綜合評分(工齡+學歷+資格)

double years; int edu, qual, score = 0;
scanf("%lf%d%d", &years, &edu, &qual);
if (years == 0) score += 0;
else if (years < 1) score += 1;
else if (years < 3) score += 2;
else if (years < 7) score += 3;
else score += 4;

switch (edu) { case 1: score+=1; break; case 2: score+=2; break; case 3: score+=3; break; }
switch (qual) { case 1: score+=0; break; case 2: score+=1; break; case 3: score+=2; break; case 4: score+=3; break; }
printf("總分: %d\n", score);

核心技巧總結

  1. 邊界值測試:每個分支邊界(如 1500、4500)必須構造測試用例。
  2. 輸入清洗:scanf(" %c", &c) 空格跳過空白;數值輸入檢查返回值。
  3. 常量命名:稅率、扣除數、利率定義為 const 或宏,避免魔術數字。
  4. 分支排序:高頻/小範圍條件前置,減少平均判斷次數。
  5. 貫穿利用:case 1: case 2: ... 合併同邏輯分支。
  6. 格式化輸出:%.2f 保留兩位小數,%-5d 左對齊對齊表格。

這 13 個案例覆蓋了條件判斷、多分支選擇、分段函數、累進計算、輸入驗證、格式化輸出等核心技能,反覆練習並嘗試修改邊界條件、擴展功能,即可建立堅實的判斷邏輯編程直覺。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved