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. Overload the following operators: >> (for cin), << (for cout), +, -, *, /, ==, and !=.
    5. Since we are overloading the >> operator (for cin), we need to test this operator by asking the user to input complex numbers. See the example output below, where the program is asking the user to "Enter a Complex number in the form 'a+bi'". In the main() function, prompt the user to enter two complex numbers. Your >> operator definition should be able to read a double, read the character '+', read another double, and then read the character 'i'. In other words, it should be able to read a complex number in this format: a+bi.
    6. For the overloaded operator functions for == and !=, the return type should be bool, and return value should be true or false. Here is an example of the bool data type: bool.cpp
    7. Use these formulas:
      1. add: (a + bi) + (c + di) = (a + c) + (b + d)i
      2. subtract: (a + bi) - (c + di) = (a - c) + (b - d)i
      3. multiply: (a + bi) * (c + di) = (a*c - b*d) + (b*c + a*d)i
      4. divide: (a + bi) / (c + di) = (a*c + b*d)/(c*c + d*d) + (b*c - a*d)/(c*c + d*d)i
    8. In the driver program (main() function), create at least three (3) complex numbers and test all of the overloaded operators for class Complex.
    9. Do NOT use the print() method to output complex numbers. Instead, use the overloaded cout<< method.
    10. 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

Enter a Complex number in the form 'a+bi': 3+4i
Enter another Complex number in the form 'a+bi': 3+5i
Test the + operator:
3+4i + 3+5i = 6+9i

Test the - operator:
3+4i - 3+5i = 0-1i

Test the * operator:
3+4i * 3+5i = -11+27i

Test the / operator:
3+4i / 3+5i = 0.852941-0.0882353i

Test the == operator:
3+4i == 3+5i = false

Test the != operator:
3+4i != 3+5i = true

Click to validate the HTML code

Click to validate the CSS code