文档介绍:Part II
Using C++ Operators
C++ By
EXAMPLE8
Using C++ Math
Operators and
Precedence
If you are dreading this chapter because you don’t like math—relax,
C++ does all your math for you! It is a misconception that you have
to be good at math to understand how to puters. In
fact, programming practice assumes the opposite is true! Your
computer is your “slave,” to follow your instructions, and to do all
the calculations for you. This chapter explains how C++ computes
by introducing you to
♦ Primary math operators
♦ Order of operator precedence
♦ Assignment statements
♦ Mixed data type calculations
♦ Type casting
163
Chapter 8 ♦ Using C++ Math Operators and Precedence
Many people who dislike math actually enjoy learning how the
computer handles it. After learning the math operators and a few
simple ways in which C++ uses them, you should fortable
using calculations in your programs. Computers are fast, and they
can perform math operations much faster than you can!
C++’s Primary Math
Operators
A C++ math operator is a symbol used for adding, subtracting,
multiplying, dividing, and other operations. C++ operators are not
always mathematical in nature, but many are. Table lists these
operator symbols and their primary meanings.
Table . C++ primary operators.
Symbol Meaning
* Multiplication
/ Division and Integer Division
% Modulus or Remainder
+ Addition
- Subtraction
Most of these operators work in the familiar way you expect
them to. Multiplication, addition, and subtraction produce the same
results (and the division operator usually does) as those produced
with a calculator. Table illustrates four of these simple operators.
Table . Typical operator results.
Formula Result
4 * 2 8
64 / 4 16
80 - 15 65
12 + 9 21
164
C++ By
EXAMPLE
Table contains examples of binary operations performed with
the four operators. Don’t confuse binary operations with binary
numb