/* * Example of multiple-alternative decision of nested if * Hanly and Koffman, 5e, Figure 4.10, page 184 */ #include /* function prototype */ double comp_tax(double salary); /* Main function */ int main(void) { double salary, tax; printf("Input your annual salary >$ "); scanf("%lf",&salary); tax = comp_tax(salary); if (tax != -1.0) printf("your tax is $%.2f", tax); else printf("your salary is outside the table ranged"); return 0; } /* * Function definition: Computes the tax due based on a tax table. * Pre : salary is defined. * Post: Returns the tax due for 0.0 <= salary <= 150,000.00; * returns -1.0 if salary is outside the table range. */ double comp_tax(double salary) { double tax; if (salary < 0.0) tax = -1.0; else if (salary < 15000.00) /* first range */ tax = 0.15 * salary; else if (salary < 30000.00) /* second range */ tax = (salary - 15000.00) * 0.18 + 2250.00; else if (salary < 50000.00) /* third range */ tax = (salary - 30000.00) * 0.22 + 5400.00; else if (salary < 80000.00) /* fourth range */ tax = (salary - 50000.00) * 0.27 + 11000.00; else if (salary <= 150000.00) /* fifth range */ tax = (salary - 80000.00) * 0.33 + 21600.00; else tax = -1.0; return (tax); }