📄 pc3_9.cpp
字号:
// Chapter 3 of C++ How to Program
// Programming Challenge 9 (Solution)
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
using std::fixed;
#include <iomanip>
using std::setw;
using std::setprecision;
#include <cmath>
double calculateCharges( double );
int main()
{
double hour; // hours parked for each car
double currentCharge; // current parking charge
double totalCharges = 0.0; // total charges
double totalHours = 0.0; // total hours
cout << "Enter the hours parked for three cars: ";
for ( int i = 1; i <= 3; i++ ) {
cin >> hour;
totalHours += hour;
if ( i == 1 ) {
cout << setw( 5 ) << "Car" << setw( 15 ) << "Hours"
<< setw( 15 ) << "Charge\n";
} // end if
totalCharges += ( currentCharge =
calculateCharges( hour ) );
cout << fixed << setw( 3 ) << i << setw( 17 )
<< setprecision( 1 ) << hour
<< setw( 15 ) << setprecision( 2 )
<< currentCharge << "\n";
} // end for
cout << setw( 7 ) << "TOTAL" << setw( 13 ) << setprecision( 1 )
<< totalHours << setw( 15 ) << setprecision( 2 )
<< totalCharges << endl;
return 0;
} // end main
// calculate charges for hours parked
double calculateCharges( double hours )
{
double charge;
if ( hours < 3.0 )
charge = 2.0;
else if ( hours < 19.0 )
charge = 2.0 + .5 * ceil( hours - 3.0 );
else
charge = 10.0;
return charge;
} // end function calculateCharges
/**************************************************************************
* (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice *
* Hall. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -