Script started on Tue Sep 28 15:08:12 1999 strauss[3:08pm] [~/Class/cisc181/examples/]> cat 10-parking-charges.cc // Program calculates the parking charge for 3 cars when // given the input of number of hours they were parked // A table is printed // Exercise 3.12 from D&D #include #include #include // this program deals with dollar amounts as integers // the following in-line functions pull out various components // of money represented that way inline int dollarsplace(int money) { return money / 100; } inline int tencentplace(int money) { return (money % 100)/10; } inline int onecentplace(int money) { return money % 10; } int calculate_charges(float); main () { float hc1, hc2, hc3; int result1, result2, result3; // these integers are intended to hold money amounts // initialize cout << "Enter the number of hours for Customer 1: "; cin >> hc1; cout << "Enter the number of hours for Customer 2: "; cin >> hc2; cout << "Enter the number of hours for Customer 3: "; cin >> hc3; // calculate result1 = calculate_charges(hc1); result2 = calculate_charges(hc2); result3 = calculate_charges(hc3); cout << endl; // print table cout.setf(ios::fixed | ios::showpoint); cout << setw(10) << "Car"; cout << setw(10) << "Hours" << setw(10) << "Charge" << endl; cout << setw(10) << "1"; cout << setw(10) << setprecision(1) << hc1 << setw(10) << dollarsplace(result1) << "." << tencentplace(result1) << onecentplace(result1) << endl; cout << setw(10) << "2"; cout << setw(10) << setprecision(1) << hc2 << setw(10) << dollarsplace(result2) << "." << tencentplace(result2) << onecentplace(result2) << endl; cout << setw(10) << "3" << setw(10) << setprecision(1) << hc3 << setw(10) << dollarsplace(result3) << "." << tencentplace(result3) << onecentplace(result3) << endl; cout << setw(10) << "TOTAL" << setw(10) << setprecision(1) << (hc1 + hc2 + hc3) << setw(10) << dollarsplace(result1 + result2 + result3) << "." << tencentplace(result1 + result2 + result3) << onecentplace(result1 + result2 + result3) << endl; return 0; } int calculate_charges(float hours) { int charge; if (hours <= 3) return 200; else { charge = 200 + (50 * (ceil(hours) - 3)); } if (charge <= 1000) return charge; else return 1000; } strauss[3:08pm] [~/Class/cisc181/examples/]> CC -compat 10-parking-charges.cc strauss[3:08pm] [~/Class/cisc181/examples/]> a.out Enter the number of hours for Customer 1: 7 Enter the number of hours for Customer 2: 12 Enter the number of hours for Customer 3: 3 Car Hours Charge 1 7.0 4.00 2 12.0 6.50 3 3.0 2.00 TOTAL 22.0 12.50 strauss[3:08pm] [~/Class/cisc181/examples/]> a.out Enter the number of hours for Customer 1: 1 Enter the number of hours for Customer 2: 22 Enter the number of hours for Customer 3: 6 Car Hours Charge 1 1.0 2.00 2 22.0 10.00 3 6.0 3.50 TOTAL 29.0 15.50 strauss[3:09pm] [~/Class/cisc181/examples/]> exit script done on Tue Sep 28 15:09:20 1999