Sie sind auf Seite 1von 18

Chapter 4: Selection Structures: if and switch Statements

Problem Solving & Program Design in C


Sixth Edition By Jeri R. Hanly & Elliot B. Koffman

Addison Wesley is an imprint of

2010 Pearson Addison-Wesley. All rights reserved.

Figure 4.1 Evaluation Tree and Step-byStep Evaluation for !flag || (y + z >= x - z)

1-2 2010 Pearson Addison-Wesley. All rights reserved.


1-2

Figure 4.2 Range of True Values for min <= x && x <= max

1-3 2010 Pearson Addison-Wesley. All rights reserved.


1-3

Figure 4.3 Range of True Values for z > x || x > y

1-4 2010 Pearson Addison-Wesley. All rights reserved.


1-4

Figure 4.4 Flowcharts of if Statements with (a) Two Alternatives and (b) One Alternative

1-5 2010 Pearson Addison-Wesley. All rights reserved.


1-5

Figure 4.5 if Statement to Order x and y

1. 2.

3.
4. 5.

i f (x > y) { temp = x , x = y; y = temp ;


}

/* /* /* /*

Switch x and y Store ol d x in Store old y in Store old x in

*/ temp */ x */ y */

1-6 2010 Pearson Addison-Wesley. All rights reserved.


1-6

Figure 4.6 Structure Chart for Water Bill Problem

1-7 2010 Pearson Addison-Wesley. All rights reserved.


1-7

Figure 4.7 Program for Water Bill Problem

1-8 2010 Pearson Addison-Wesley. All rights reserved.


1-8

Figure 4.7 Program for Water Bill Problem (contd)

1-9 2010 Pearson Addison-Wesley. All rights reserved.


1-9

Figure 4.7 Program for Water Bill Problem (contd)

1-10 2010 Pearson Addison-Wesley. All rights reserved.


1-10

Figure 4.7 Program for Water Bill Problem (contd)

121. { 122. 123. 124. 125. 126. 127. }

if ( late_charge> 0.0) { printf("\nBill includes $ %.2f late charge", late_charge); printf(" on unpaid balance of $ %. 2f\n", unpaid);
}

printf (" \nTotal due = $ %. 2f\n", bill);

1-11 2010 Pearson Addison-Wesley. All rights reserved.


1-11

Figure 4.8 Sample Run of Water Bill Program


This program figures a water bill based on the demand charge ($35.00) and a $1.10 per 1000 gallons use charge. A $2.00 surcharge is added to accounts with an unpaid ba lance. Enter unpaid balance, previous and current meter readings on separate lines after the prompts . Press <return> or <enter> after typing each number . Enter unpaid balance> $ 71.50 Enter p revious meter reading> 4198 Enter current meter reading> 4238 Bill includes $2 . 00 late charge on unpaid balance of $71.50 Total due = $152 . 50

1-12 2010 Pearson Addison-Wesley. All rights reserved.


1-12

Figure 4.9 Function comp_use_charge Revised

1. 2. 3. 4. 5. 6. 7. 8. 9.

/*

* Computes use charge with conservation requirements * Pre: previous, current, and use_last_year are defined.
*/

double comp use_charge(int previous, int current, int use_last_year) { int used; /* gallons of water used (in thousands) double use_charge; /* charge for actual water use

*/ */
(colltillll ed )

1-13 2010 Pearson Addison-Wesley. All rights reserved.


1-13

Figure 4.9 Function comp_use_charge Revised (contd)


10. 11. 12.
used = current - previous; if (used <= CONSERV_RATE / 100.0 * use_last_year) { /* conservation guidelines met */ use_charge = used * PER_1000_CHG; } else { printf("Use charge is at %.2f times" OVERUSE_CHG_RATE); printf("normal rate since use of\n"); printf(" %d units exceeds % d percent ", used, CONSERV_RATE); printf("of last year's %d-unit use.\n", use_last_year); use_charge = used * OVERUSE CHG RATE * PER_1000_CHG;
}

13.
14. 15. 16.

17.
18. 19. 20. 21. 22. 23.

return (use_charge);
}

1-14 2010 Pearson Addison-Wesley. All rights reserved.


1-14

Figure 4.10 Function comp_tax

/*

2. 3. 4. 5. 6. 7. 8.
9. 10. 11.

* 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;
i f (salary < 0 . 0) tax = -1. 0 ;

12.
13.

(continu ed)

1-15 2010 Pearson Addison-Wesley. All rights reserved.


1-15

Figure 4.10 Function comp_tax (contd)

14. 15. 16. 17. 18. 19. 20. 21 . 22. 23. 24. 25. 26. 27. 28.

else if (sa l ary < 15000 . 00) tax = 0 . 15 * salary; else if (salary < 30000 . 00) tax = (salary - 15000 . 00) else if (salary < 50000.00) tax = (salary - 30000 . 00) else if (salary < 80000 . 00) tax = (salary 50000.00) else if (salary <= 150000.00) tax = (salary - 80000 . 00) else tax = -1. 0;

/* first range /* second range * 0.18 + 2250.00 ; /* third range * 0.22 + 5400.00; / * fourth range * 0.27 + 11000.00 ; /* fifth range * 0.33 + 21600.00;

*/ */ */ */ */

return (tax) ;
}

1-16 2010 Pearson Addison-Wesley. All rights reserved.


1-16

Figure 4.11 Flowchart of Road Sign Decision Process

1-17 2010 Pearson Addison-Wesley. All rights reserved.


1-17

Figure 4.12 Example of a switch Statement with Type char Case Labels

1-18 2010 Pearson Addison-Wesley. All rights reserved.


1-18

Das könnte Ihnen auch gefallen