• No se han encontrado resultados

An expression in C++ can have tabs and spaces in it to make it more readable. For example, the following two expressions are the same, but the second is easier to read:

x=10/y*(127/x);

x = 10 / y * (127/x);

Parentheses increase the precedence of the operations contained within them, just like in algebra. Use of redundant or additional parentheses will not cause errors or slow down the execution of the

expression. You are encouraged to use parentheses to make clear the exact order of evaluation, both for yourself and for others who may have to figure out your program later. For example, which of the following two expressions is easier to read?

29

C++ A Beginner’s Guide by Herbert Schildt

x = y/3-34*temp+127;

x = (y/3) - (34*temp) + 127;

Project 2-3 Compute the Regular Payments on a Loan

In this project, you will create a program that computes the regular payments on a loan, such as a car loan. Given the principal, the length of time, number of payments per year, and the interest rate, the program will compute the payment. Since this is a financial calculation, you will need to use

floating-point data types for the computations. Since double is the most commonly used floating-point type, we will use it in this project. This project also demonstrates another C++ library function: pow( ). To compute the payments, you will use the following formula:

where IntRate specifies the interest rate, Principal contains the starting balance, PayPerYear specifies the number of payments per year, and NumYears specifies the length of the loan in years.

Notice that in the denominator of the formula, you must raise one value to the power of another. To do this, you will use pow( ). Here is how you will call it:

result = pow(base, exp);

pow( ) returns the value of base raised to the exp power. The arguments to pow( ) are double values, and pow( ) returns a value of type double.

Step by Step

1. Create a new file called RegPay.cpp.

2. Here are the variables that will be used by the program:

double Principal; // original principal

double IntRate; // interest rate, such as 0.075 double PayPerYear; // number of payments per year double NumYears; // number of years

double Payment; // the regular payment

double numer, denom; // temporary work variables double b, e; // base and exponent for call to pow()

Notice how each variable declaration is followed by a comment that describes its use. This helps anyone reading your program understand the purpose of each variable. Although we won’t include

30

C++ A Beginner’s Guide by Herbert Schildt such detailed comments for most of the short programs in this book, it is a good practice to follow as your programs become longer and more complicated.

3. Add the following lines of code, which input the loan information:

cout << "Enter principal: "; cin >> Principal;

cout << "Enter interest rate (i.e., 0.075): "; cin >> IntRate;

cout << "Enter number of payments per year: "; cin >> PayPerYear;

cout << "Enter number of years: "; cin >> NumYears;

4. Add the lines that perform the financial calculation:

numer = IntRate * Principal / PayPerYear; e = -(PayPerYear * NumYears);

b = (IntRate / PayPerYear) + 1;

denom = 1 - pow(b, e); Payment = numer / denom;

5. Finish the program by outputting the regular payment, as shown here:

cout << "Payment is " << Payment;

6. Here is the entire RegPay.cpp program listing:

/*

Project 2-3

Compute the regular payments for a loan. Call this file RegPay.cpp

*/

#include <iostream> #include <cmath> using namespace std; int main() {

double Principal; // original principal

double IntRate; // interest rate, such as 0.075 double PayPerYear; // number of payments per year double NumYears; // number of years

31

C++ A Beginner’s Guide by Herbert Schildt

double Payment; // the regular payment

double numer, denom; // temporary work variables double b, e; // base and exponent for call to pow()

cout << "Enter principal: "; cin >> Principal;

cout << "Enter interest rate (i.e., 0.075): "; cin >> IntRate;

cout << "Enter number of payments per year: "; cin >> PayPerYear;

cout << "Enter number of years: "; cin >> NumYears;

numer = IntRate * Principal / PayPerYear; e = -(PayPerYear * NumYears);

b = (IntRate / PayPerYear) + 1; denom = 1 - pow(b, e);

Payment = numer / denom;

cout << "Payment is " << Payment;

return 0; }

Here is a sample run:

Enter principal: 10000

Enter interest rate (i.e., 0.075): 0.075 Enter number of payments per year: 12 Enter number of years: 5

Payment is 200.379

7. On your own, have the program display the total amount of interest paid over the life of the loan.

1. What type of integers are supported by C++? 2. By default, what type is 12.2?

32

C++ A Beginner’s Guide by Herbert Schildt 3. What values can a bool variable have?

4. What is the long integer data type?

5. What escape sequence produces a tab? What escape sequence rings the bell? 6. A string is surrounded by double quotes. True or false?

7. What are the hexadecimal digits?

8. Show the general form for initializing a variable when it is declared. 9. What does the % do? Can it be used on floating-point values?

10. Explain the difference between the prefix and postfix forms of the increment operator. 11. Which of the following are logical operators in C++?

a. && b. ## c. || d. $$ e. ! 12. How can x = x + 12; be rewritten? 13. What is a cast?

1

C++ A Beginner’s Guide by Herbert Schildt

Module3

Program Control