EXAMPLE 1.15 Integer Operators
Read QR Code ISO/IEC18004 In NoneUsing Barcode Control SDK for Software Control to generate, create, read, scan barcode image in Software applications.
QR Generator In NoneUsing Barcode encoder for Software Control to generate, create QR Code image in Software applications.
This program illustrates the use of the six arithmetic operators:
Read QR Code JIS X 0510 In NoneUsing Barcode reader for Software Control to read, scan read, scan image in Software applications.
Making QR Code JIS X 0510 In Visual C#.NETUsing Barcode generation for .NET framework Control to generate, create Quick Response Code image in VS .NET applications.
#include <iostream.h> // Tests arithmetic operators: main0 { int m = 38, n = 5; tout << m << ' + fl << n << 1 = ' << (m + n) << endl; tout << m << ' - u << n CC fl = n CC (m - n) << endl; tout << cc n << ' = 'I << (-n) << endl; tout << m << ' * ' << n << ' = ' << (m * n) << endl; tout << m << ' / ' << n << ' = ' << (m / n) << endl; tout << m << ' % ' << n << ' = ' << (m % n) << endl; return 0; 1
Printing QR-Code In .NETUsing Barcode creator for ASP.NET Control to generate, create QR Code 2d barcode image in ASP.NET applications.
Generate QR Code In .NETUsing Barcode maker for .NET framework Control to generate, create QR Code ISO/IEC18004 image in .NET applications.
II -
QR Code JIS X 0510 Generator In Visual Basic .NETUsing Barcode maker for .NET framework Control to generate, create QR Code image in .NET applications.
EAN 13 Creator In NoneUsing Barcode maker for Software Control to generate, create EAN / UCC - 13 image in Software applications.
5 5 5 5 5 5
Bar Code Creation In NoneUsing Barcode printer for Software Control to generate, create barcode image in Software applications.
Generating Barcode In NoneUsing Barcode drawer for Software Control to generate, create barcode image in Software applications.
r = z = = =r
USS-128 Encoder In NoneUsing Barcode maker for Software Control to generate, create GS1-128 image in Software applications.
UPC-A Supplement 2 Drawer In NoneUsing Barcode generator for Software Control to generate, create UCC - 12 image in Software applications.
38 38
I-2/5 Creator In NoneUsing Barcode generation for Software Control to generate, create USS ITF 2/5 image in Software applications.
Making Bar Code In JavaUsing Barcode printer for Android Control to generate, create barcode image in Android applications.
38 38
Make Code 3/9 In NoneUsing Barcode encoder for Font Control to generate, create Code 39 image in Font applications.
UPC Symbol Generation In NoneUsing Barcode drawer for Microsoft Excel Control to generate, create GS1 - 12 image in Microsoft Excel applications.
43 3-3
UCC.EAN - 128 Generator In NoneUsing Barcode drawer for Font Control to generate, create UCC - 12 image in Font applications.
Printing USS-128 In JavaUsing Barcode printer for Java Control to generate, create EAN / UCC - 13 image in Java applications.
-5 I$0 7
Drawing Code 128 Code Set C In .NET FrameworkUsing Barcode maker for VS .NET Control to generate, create Code 128 image in .NET framework applications.
EAN13 Recognizer In Visual C#.NETUsing Barcode reader for .NET framework Control to read, scan read, scan image in Visual Studio .NET applications.
* i %
Note that 3 8 / 5 = 7 a n d 3 8 % 5 = 3. These two operations together provide complete information about the ordinary division of 38 by 5: 38 + 5 = 7.6. The resulting integer part is 35+5 = 7, and the fractional part is 3+5 = 0.6. The integer quotient 7 and the integer remainder 3 can be recombined with the dividend 38 and the divisor 5 in the following relation: 7 x 5 + 3 = 3 8 .
The integer quotient and remainder operators are more complicated if the integers are not positive. Of course, the divisor should never be zero. But if either m or n is negative, then m/n and m%n may give different results on different machines. The only requirement is that
q*n + r == m
where q = m/n and r = m%n. For example, -14 divided by 5 is -2.8. For the integer quotient, this could be rounded to -3 or to -2. If your computer rounds the quotient g to -3, then the integer remainder r will be 1. But if your computer rounds g to -2, then r will be -4.
INTRODUCTION TO PROGRAMMING IN C++
[CHAP. 1
EXAMPLE 1.16 Division with Negative Integers
This program is used to determine how the computer handles the division of negative integers:
#include ciostream.h> // Tests quotient and main0 int m = -14, tout << "rn = tout << '92 = tout CC "q = tout CC 'r = tout << q*n << r << return 0; remainder operators:
n = 5, q = m/n, r = m%n; ' C-C m CC endl; u -CC n C-C endl; " << q CC endl; ' << r C-C endl; + r = 1 << 1 ( << q << ")*('I << n CC ") + " = ' << q*n + r CC ' = ' -CC m CC endl;
m = -14 n a5 q s -2 IT = -4 cpn + r .= (-2] (5) + -4 t= -24 == 44
. : . . : . :
This shows the same results both from a UNIX workstation using a Motorola 68040 processor and from a DOS PC using an Intel Pentium processor.
1.14 OPERATOR PRECEDENCE AND ASSOCIATIVITY
C++ has a rich repertoire of operators. (Appendix C lists all 55 of them.) Since an expression may include several operators, it is important to know in what order the evaluations of the operators occurs. We are already familiar with the precedence of ordinary arithmetic operators: the *,
/, and % operators have higher precedence than the + and - operators; i.e., they are evaluated first. For example,
42 - 3*5
is evaluated as
42 (3*5) = 42 - 15 = 27
Moreover, all the arithmetic operators have higher precedence than the assignment and output operators. For example, the statement
n = 42 - 3*5;
will assign the value 27 to n. First the operator * is invoked to evaluate 3 * 5, then the operator - is invoked to evaluate 4 2 - 15, and then the operator = is invoked to assign 2 7 to n.