Hello guys im been tasked to do a series of complex functions. so far im able to complete addition, subtraction and multiplication, but currently stuck at division. Would appreciate any kind soul help on this

class Complex 
{

public:

   Complex( double r, double i ) : re(r), im(i) {}
   Complex operator+( Complex &other );
   Complex operator-( Complex &other );
   Complex operator*( Complex &other );
   Complex operator/( Complex &other );
   void Display( ) 
   {   
	   cout << re << " + " << im <<"i"<< endl; }
private:
   double re, im; //re = Real Number1, im = Imaginary1, other.re = Real2, other.im = Imaginary2
};
int main() 
{
   Complex a = Complex(4, 5 );    //given static value for convenience.Actual case will require user to input values.
   Complex b = Complex( 3, 2 );
   Complex c = Complex( 0,0 );

   c = a/b;
   c.Display();
   system("pause");
}

Division code

int normalize = (other.re * other.re) + (other.im * other.im);//debug denominator
	cout<< " Output: "<< normalize<<"test"; //denominator = 13

	int testing1=((re * other.re) + (im * other.im));
	
	cout<<"Output of first expression: "<<testing1<<" / "<<normalize<< "Confirmed correct"<<endl;  //debug expression 1, 22 is correct answer

	int testing2=((im * other.re) - (re * other.im));
		cout<<"Output of second expression: " <<testing2<<"testing1";
	cout<<" Final Output: "<<testing2<<" / "<<normalize<<"Final test"<<endl;//debug expression 2, +7i is correct answer

  return Complex(((re * other.re) + (im * other.im)), 
    ((im * other.re) - (re * other.im)));   //debug of expression 1 and 2, 22+7i
}

The equation is correct, however the system keeps displaying its float values after i put in the denominator. For now it only displays 22+7i. My question is, how do i make it such that i can make it display 22+7i/13

If you want that then you need to have a fraction class that handles that. So that instead of just displaying its floating values, it can display the numerator and denominator. Thus ultimately, you need to keep a variable for the numerator and denominator. So instead of double re,im you need either Fraction real,imaginary or something like double realNumerator, realDenominator, imaginaryNumerator, ImaginaryDenominator and keep track of each variable. I suggest creating a fraction class that handles those for you. Or just be satisfied with floating numbers.

ah. I was thinking something of modifying the display output instead, rather than create a whole new class for it. Floating numbers are really a no go for the program though.

Do you mind pasting the psuedocode for the fraction class so i can get started on it?

Umm...maybe something like so :

class Fraction{
private:
 int _numerator, _denominator;
public:
 Fraction(int num = 0, int denom = 1): _numerator(num), _denominator(denom){
  //make sure denominator is not 0!
 }
 int numerator()const{return _numerator;}
 int denominator()const{ return _denominator;}

 int numerator(int n){ _numerator = n;}
 int denominator(int d){ /*makre sure d != 0 */ _denominator = 0; }
 string toString()const{
   return _toStr(_numerator) + "/" + _toStr(_denominator);
 }
 void display()const{ cout << _numerator << "/" << _denominator ;}
 float value(){ return float(_numerator)/_denominator; }

private:
 string _toStr(int num)const{
  stringstream ss;
  ss << num;
  return ss.str();
 }
};

The above isn't compiled but you get the idea.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.