This section describes the basic input/output, arithmetic, comparison, and assignment operators. #include <complex.h> class complex { public:
};
| |||||
The operators have their conventional precedences. In the following descriptions for operators, x, y, and z are variables of class complex. Arithmetic operators: z = x + y Returns a complex which is the arithmetic sum of complex numbers x and y. z = -x Returns a complex which is the arithmetic negation of complex number x. z = x - y Returns a complex which is the arithmetic difference of complex numbers x and y. z = x * y Returns a complex which is the arithmetic product of complex numbers x and y. z = x / y Returns a complex which is the arithmetic quotient of complex numbers x and y. Comparison operators: x == y Returns a non-zero integer if complex number x is equal to complex number y; x != y Returns a non-zero integer if complex number x is not equal to complex number y; Assignment operators: x += y Complex number x is assigned the value of the arithmetic sum of itself and complexnumber y. x -= y Complex number x is assigned the value of the arithmetic difference of itself and complex number y. x *= y Complex number x is assigned the value of the arithmetic product of itself and complex number y. x /= y Complex number x is assigned the value of the arithmetic quotient of itself and complex number y. WarningThe assignment operators do not produce a value that can be used in an expression.In other words, the following construction is syntactically invalid:
The following lines, by contrast:
are valid. Input/output operators: Output and input of complex numbers may be performed using the << and >> operators, respectively. Output format: ( real, imag) Input format:
| |||||
EXAMPLE | The following program defines the complex numbers c and d, divides d by c, and then prints the values of c and d: #include <iostream.h> #include <complex.h> main() { complex c,d; d = complex(10.0, 11.0); c = complex (2.0, 2.0); while (norm(c) < norm(d)) { d /= c; cout << c << " " <<d << "\n"; } return 0; } The result of executing the program is: ( 2, 2) ( 5.25, 0.25) ( 2, 2) ( 1.375, -1.25) % CCM0998 CPU time used: 0.0009 seconds | ||||
SEE ALSO | |||||