Specifications
602 Appendix E
Complex Operation Programs
Complex Operation Programs
Complex Operation Programs
The following section shows sample code fragments that performs addition, subtraction,
multiplication, and division operations. By adding this coding to your program, it can
handle addition, subtraction, multiplication, and division operations on complexes.
Sample Implementation in Visual Basic
The following is a sample program in Visual Basic that performs addition, subtraction,
multiplication, and division operations on complexes. At first, a Type statement is used to
define the complex type name Complex. Then, the Function procedure is used to code
addition, subtraction, multiplication, and division operations as four separate user-defined
functions. The variables used in this program are as follows:
a, b Complexes to be operated
c Variable to which an operation result is assigned
Re, Im Real (Re) and imaginary (Im) components of a complex
These functions can be called from a main program.
Example E-1 Example of Complex Operation Program in Visual Basic
Option Explicit
Public Type Complex
Re As Double
Im As Double
End Type
`
` Adding Complex
`
Public Function complex_add(a As Complex, b As Complex) As Complex
Dim c As Complex
c.Re = a.Re + b.Re
c.Im = a.Im + b.Im
complex_add = c
End Function
`
` Substracting Complex
`
Public Function complex_sub(a As Complex, b As Complex) As Complex
Dim c As Complex
c.Re = a.Re - b.Re
c.Im = a.Im - b.Im
complex_sub = c
End Function
`
` Multiplying Complex
`
Public Function complex_mul(a As Complex, b As Complex) As Complex
Dim c As Complex
c.Re = a.Re * b.Re - a.Im * b.Im
c.Im = a.Re * b.Im + a.Im * b.Re
complex_mul = c
End Function