// Complex number calculations

#include <iostream.h>
#include <math.h>

// Class definition for the complex numbers:

class complex
{
	public:
	      
	       complex();  // Constructor
               void readComplex(); // Reading the complex number from the user
               complex add(complex); // Add operation
               complex sub(complex); // Subtraction
	       complex multiply(complex); // Multiplying
	       complex div(complex);      // Division 
               void print();     // Printing the complex number
               float absoluteValue(); // Absolute value foundation

	private:
            
	      float real;  // Real part of the number
              float imag;	 // Imaginary part of the number     
              float getRealPart();
              float getImagPart();
	      void setRealPart(float);
	      void setImagPart(float);

};

// Constructor initializes the real and imaginary part of the number to zero

complex::complex()
{
	real = 0;
	imag = 0;
}

// Setting  the complex number

void complex::readComplex()
{
	cout << "Enter the real part of the complex number: ";
	cin >> real;

	cout << "Enter the imaginary part of the complex number: ";
	cin >> imag;

        cout << "The complex number you entered is: ";  


        if (imag == 0 || imag > 0)
	{	
	    cout << real << '+' << imag << 'i' << endl ;
        }
        
	else
	{	
	    cout << real << imag << 'i' << endl ;
        }

}

// Addition

complex complex::add(complex num)
{
	complex ret;

	ret.setRealPart(num.getRealPart() + real);
        ret.setImagPart(num.getImagPart() + imag);

        return ret;
}

// Subtraction 

complex complex::sub(complex num)
{
       complex ret;
	
       ret.setRealPart(real - num.getRealPart());
       ret.setImagPart(imag - num.getImagPart());
       
       return ret;
}

// Multiplication

complex complex::multiply(complex num)
{
       float oldreal = real; 
       complex ret;

       ret.setRealPart((oldreal*num.getRealPart()) - (imag*num.getImagPart()));
       ret.setImagPart((oldreal*num.getImagPart()) + (imag*num.getRealPart()));

       return ret;
}

// Division

complex complex::div(complex num)
{
       float oldreal = real;
       complex ret;

       ret.setRealPart( ( (real*num.getRealPart()) + (imag*num.getImagPart()) ) / ( (num.getRealPart()*num.getRealPart()) + (num.getImagPart()*num.getImagPart()) ) );

       ret.setImagPart( ( (imag*num.getRealPart()) - (oldreal*num.getImagPart()) ) / ( (num.getRealPart()*num.getRealPart()) + (num.getImagPart()*num.getImagPart()) ) );

       return ret;

}

// Absolute value foundation

float complex::absoluteValue()
{
	float abs;

	abs = sqrt( (real*real) + (imag*imag) );
    
        return abs;

}

// Printing the number

float complex::getRealPart()
{
	return real;
}

float complex::getImagPart()
{
	return imag;
}

void complex::setRealPart(float num)
{
	real = num;
}

void complex::setImagPart(float num)
{
	imag = num;
}

void complex::print()
{
        if (imag == 0 || imag > 0)
	{	
	    cout << real << '+' << imag << 'i' << endl ;
        }
        
	else
	{	
	    cout << real << imag << 'i' << endl ;
        }
}

// Menu displayed to the user:

class Menu 
{
    public:
        int displayMenu();
};

int Menu::displayMenu()
{
	int d1 = 1;

	cout << "\nChoose one of the options from the menu:\n" << endl;

	cout << "[1]. ADDITION\n";
        cout << "[2]. SUBTRACTION\n";
        cout << "[3]. MULTIPLICATION\n";
        cout << "[4]. DIVISION\n";
        cout << "[5]. ABSOLUTE VALUE\n";
       	cout << "[6]. EXIT\n\n";

        
	// Check for the wrong input:
	
	do
	{
	    if( (d1<1) || (d1>6) )
	    {
	       cout << "Wrong Choice!!\n";
            }

            cout << "Enter your choice: ";
            cin >> d1;

        } while (d1<1 || d1>6); 

	return d1;
}

void Addition_Function()
{
                   complex result;
                   complex n1;
                   complex n2;
           
                   cout << "\nFor the first number:\n\n"; 
                   n1.readComplex();                
			      
                   cout << "\nFor the second number:\n\n"; 
		   n2.readComplex();                
				       
                   result = n1.add(n2);
		            
		   cout << "\nThe result is: ";
		   result.print();	       

}

void Subtraction_Function()
{
                   complex result;
                   complex n1;
                   complex n2;

                   cout << "\nFor the first number:\n\n"; 
		   n1.readComplex();                
			      
                   cout << "\nFor the other number to be subtracted:\n\n"; 
		   n2.readComplex();                
				       
                   result = n1.sub(n2);
		               
		   cout << "\nThe result is: ";
		   result.print();	       
}

void Multiply_Function()
{
                   complex result;
                   complex n1;
                   complex n2;

                   cout << "\nFor the first number:\n\n"; 
		   n1.readComplex();                
			      
                   cout << "\nFor the second number:\n\n"; 
		   n2.readComplex();                
				       
                   result = n1.multiply(n2);
		              
		   cout << "\nThe result is: ";
		   result.print();	       

}

void Division_Function()
{
                   complex result;
                   complex n1;
                   complex n2;

                   cout << "\nFor the first number:\n\n"; 
		   n1.readComplex();                
			      
                   cout << "\nFor the other number to be divided:\n\n"; 
		   n2.readComplex();                
				       
                   result = n1.div(n2);
		               
		   cout << "\nThe result is: ";
	           result.print();	       
}
void Absolute_Value_Function()
{
	           float absolute;                   
                   complex n1;

		   n1.readComplex();                
			      
		   absolute = n1.absoluteValue();	        
			      
		   cout << "\nThe absoulate value of the complex number is: ";
	           cout << absolute;
}

// Main Function:

int main()
{

	Menu mainMenu;
	int option;
	float absolute;

        // Display the menu to the user until 'exit' option is selected: 

	do
	{
		option = mainMenu.displayMenu();

                switch(option)  // Do the operation that the user wants to do
		{
			case 1: // Addition
			       Addition_Function();
			       break;

			case 2: // Subtraction
			      Subtraction_Function(); 
			      break;

			case 3:  // Multiplication
                          Multiply_Function(); 	       
			      break;

			case 4: // Division              
                          Division_Function(); 	       			       
			      break;              
		
			case 5: // Absolute Value Foundation 
                          Absolute_Value_Function(); 	       			                      
			      break;
                }	

	} while (option != 6);

        return 0;
}




