Complex Complex::multiply(Complex comp2) { // Following formula (a + ib ) * ( c + id ) = ( a * c - b * d ) + i( a * d + b * c ) // Create Complex object called "temp" Complex temp; // Set temp data member "a" to (this->get_a() * comp2.get_a()) - (this->get_b() * comp2.get_b()) temp.set_a((this->get_a() * comp2.get_a()) - (this->get_b() * comp2.get_b())); // Set temp data member "b" to ((this->get_a() * comp2.get_b()) + (this->get_b() * comp2.get_a())) temp.set_b((this->get_a() * comp2.get_b()) + (this->get_b() * comp2.get_a())); // Return resulting Complex object "temp" return temp; }
// function to add 2 complex numbers Complex Complex::add(Complex comp2) { // Following formula ( a + ib ) + ( c + id ) = ( a + c ) + i( b + d ) // Create Complex object called "temp" Complex temp; // Set temp data member "a" to sum of local "a" and parameter comp2's "a" temp.set_a((this->get_a()) + (comp2.get_a())); // Set temp data member "b" to sum of local "b" and parameter comp2's "b" temp.set_b((this->get_b()) + (comp2.get_b())); // Return resulting Complex object "temp" return temp; }
Complex Complex::subtract(Complex comp2) { // Following formula ( a + ib ) - ( c + id ) = ( a - c ) + i( b - d ) // Create Complex object called "temp" Complex temp; // Set temp data member "a" to difference of local "a" and parameter comp2's "a" temp.set_a((this->get_a()) - (comp2.get_a())); // Set temp data member "b" to difference local "b" and parameter comp2's "b" temp.set_b((this->get_b()) - (comp2.get_b())); // Return resulting Complex object "temp" return temp; }
Complex Complex::divide(Complex comp2) { /* Following formula a + ib ( a * c + b * d ) i( c * b - a * d ) ------ = ----------------- + ------------------- c + id c^2 + d^2 c^2 + d^2 */ // Create Complex object called "temp" Complex temp; // Set temp data member "a" to ((a*c) + (b*d)) / (c^2 + d^2) temp.set_a((this->get_a()*comp2.get_a() + this->get_b()*comp2.get_b()) / (comp2.get_a()*comp2.get_a() + comp2.get_b()*comp2.get_b())); // Set temp data member "b" to ((c*b) - (a*d)) / (c^2 + d^2) temp.set_b((comp2.get_a()*this->get_b() - this->get_a()*comp2.get_b()) / (comp2.get_a()*comp2.get_a() + comp2.get_b()*comp2.get_b())); return temp; }