double pro;
scanf("%f", pro); // 兩個錯誤
scanf("%lf", &pro);
int main() {
double pro;
printf("請輸入利潤: ");
if (scanf("%lf", &pro) == 1) { // 檢查返回值確保讀取成功
count(pro);
} else {
printf("輸入無效\\n");
}
return 0;
}
原代碼用陣列累加分段稅率,可改用結構體數組 + 循環,更清晰易維護:
struct Bracket { double limit; double rate; };
Bracket brackets[] = {
{100000, 0.10}, {200000, 0.075}, {400000, 0.05},
{600000, 0.03}, {1000000, 0.015}, {DBL_MAX, 0.01}
};
double bonus = 0, remaining = profit;
for (auto &b : brackets) {
double taxable = fmin(remaining, b.limit);
bonus += taxable * b.rate;
remaining -= taxable;
if (remaining <= 0) break;
}