📄 carpayment.cpp
字号:
// Tutorial 20: CarPayment.cpp
// Calculate different billing plans for a car loan.
#include <iostream> // required to perform C++ stream I/O
#include <iomanip> // required for parameterized stream manipulators
#include <cmath> // required to use the C++ math library functions
#include <string> // required to access string functions
using namespace std; // for accessing C++ Standard Library members
// function prototypes
double calculateMonthlyPayment( double, int, int );
void processData( int, int, double );
// function main begins program execution
int main()
{
// define variables
int price; // price of the car
int downPayment; // the down payment
double interest; // the annual interest rate
// prompt user for and input the car price, the
// down payment and the annual interest rate
cout << "\nEnter the car price: ";
cin >> price; // get the car price
cout << "Enter the down payment: ";
cin >> downPayment; // get the down payment
cout << "Enter the annual interest rate: ";
cin >> interest; // get the annual interest rate
// create table of options over different periods of time
processData( price, downPayment, interest);
return 0; // indicate that program ended successfully
} // end function main
// function calculates payments for each time interval
void processData( int price, int downPayment, double interest )
{
// calculate loan amount and monthly interest
int loanAmount = price - downPayment;
double monthlyInterest = interest / 1200;
// set precision for output values
cout << fixed << setprecision( 2 );
// add header to output
cout << endl << left << setw( 10 ) << "Months"
<< "Monthly Payments" << endl;
// calculate and display payments
for ( int years = 2; years <= 5; years++ )
{
// convert payment period to months
int months = 12 * years;
// get monthlyPayment
double monthlyPayment = calculateMonthlyPayment(
monthlyInterest, months, loanAmount );
// insert result into output stream
cout << left << setw( 10 ) << months
<< "$" << monthlyPayment << endl;
} // end for
} // end function processData
// calculate monthlyPayment
double calculateMonthlyPayment( double monthlyInterest,
int months, int loanAmount )
{
double base = pow( 1 + monthlyInterest, months );
return loanAmount * monthlyInterest / ( 1 - ( 1 / base ) );
} // end function calculateMonthlyPayment
/**************************************************************************
* (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. 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 + -