Assignment Instructions

  1. Edit, compile, and run the following program on the UH UNIX shell:
    1. Create a class called Complex for performing arithmetic with complex numbers.
    2. Complex numbers have the form a + bi, where a is the real part, b is the imaginary part, and i is the square root of negative one.
    3. The data members, real and imaginary, should be data type double.
    4. Write a constructor with two parameters, copy constructor, add member function, subtract member function, divide member function, multiply member function, and destructor.
    5. Use these formulas:
      1. Complex add(const Complex &) : (a + bi) + (c + di) = (a + c) + (b + d)i
      2. Complex subtract(const Complex &) : (a + bi) - (c + di) = (a - c) + (b - d)i
      3. Complex multiply(const Complex &) : (a + bi) * (c + di) = (a*c - b*d) + (b*c + a*d)i
      4. Complex divide(const Complex &) : (a + bi) / (c + di) = (a*c + b*d)/(c*c + d*d) + (b*c - a*d)/(c*c + d*d)i
    6. In the driver program (main() function), create at least three (3) complex numbers and test all of the constructors, math member functions, and destructor for class Complex.
    7. Email your makefile and C program to the instructor, or show it to the instructor in class.
  2. For the submission guidelines and the grading guidelines, see the syllabus.

Example I/O

Test the constructor with two parameters.
Complex number c1 is: (1.1+2.3i)

Test the copy constructor.
Complex number c2 is: (1.1+2.3i)

Test the add() member function.
Destructor for: (2.2 + 4.6i)
(1.1+2.3i) + (1.1+2.3i) = (2.2+4.6i)

Test the subtract() member function:
Destructor for: (0 + 0i)
(1.1+2.3i) - (1.1+2.3i) = (0+0i)

Test the multiply() member function:
Destructor for: (-4.08 + 5.06i)
(1.1+2.3i) * (1.1+2.3i) = (-4.08+5.06i)

Test the divide member function:
Destructor for: (1 + 0i)
(1.1+2.3i) / (1.1+2.3i) = (1+0i)

Destructor for: (1 + 0i)
Destructor for: (1.1 + 2.3i)
Destructor for: (1.1 + 2.3i)



Click to validate the HTML code

Click to validate the CSS code