// Complex number calculations

#include <iostream.h>
#include <math.h>
#include <time.h>
#include <iomanip.h>
#include <stdlib.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
               float getRealPart();
	       float getImagPart();
	       void setRealPart(float);
	       void setImagPart(float);

	private:
            
	      float real;  // Real part of the number
              float imag;	 // Imaginary part of the number     
};

// 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()
{
	real = rand()%100;
	imag = rand()%100;
}

// 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;
}

// Main Function:

int main()
{
        complex *array1;
        complex *array2;
        complex result;
	Menu mainMenu;
	int option,index1,index2;
	int arraysize;
	float absolute;

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

        cout << "\nEnter the size of the arrays:";
        cin >> arraysize;	

        array1 = new complex[arraysize];  
        array2 = new complex[arraysize];

        for (int i = 0; i<arraysize; i++)
	{
		array1[i].readComplex();
		array2[i].readComplex();
	}	
	
	do
	{
		option = mainMenu.displayMenu();

                switch(option)  // Do the operation that the user wants to do
		{
			case 1: // Addition
                               cout << "\nEnter the index number for the first array[1-" << arraysize <<"]:"; 
			       cin >> index1; 
                               index1--;  // First index number is 0

                               cout << "Enter the index number for the second array[1-" << arraysize <<"]:";; 
			       cin >> index2; 
                               index2--;  // First index number is 0
			       
                               cout << "\nThe number you have chosen in the array #1 is:";
			       array1[index1].print();
                               cout << "The number you have chosen in the array #2 is:";
			       array2[index2].print();
                               
			       result = array1[index1].add(array2[index2]); 
		               
		               cout << "\nThe result is: ";
			       result.print();	       
			       
			       break;  

			case 2: // Subtraction

                               cout << "\nEnter the index number for the first array[1-" << arraysize <<"]:"; 
			       cin >> index1; 
                               index1--;  // First index number is 0

                               cout << "Enter the index number for the second array[1-" << arraysize <<"]:";; 
			       cin >> index2; 
                               index2--;  // First index number is 0
			       
                               cout << "\nThe number you have chosen in the array #1 is:";
			       array1[index1].print();
                               cout << "The number you have chosen in the array #2 is:";
			       array2[index2].print();
                               
			       result = array1[index1].sub(array2[index2]); 
		               
		               cout << "\nThe result is: ";
			       result.print();	       
			       
			      break;
			case 3:  // Multiplication

                               cout << "\nEnter the index number for the first array[1-" << arraysize <<"]:"; 
			       cin >> index1; 
                               index1--;  // First index number is 0

                               cout << "Enter the index number for the second array[1-" << arraysize <<"]:";; 
			       cin >> index2; 
                               index2--;  // First index number is 0
			       
                               cout << "\nThe number you have chosen in the array #1 is:";
			       array1[index1].print();
                               cout << "The number you have chosen in the array #2 is:";
			       array2[index2].print();
                               
			       result = array1[index1].multiply(array2[index2]); 
		               
		               cout << "\nThe result is: ";
			       result.print();	       
			       
			      break;
			case 4: // Division
               
                               cout << "\nEnter the index number for the first array[1-" << arraysize <<"]:"; 
			       cin >> index1; 
                               index1--;  // First index number is 0

                               cout << "Enter the index number for the second array[1-" << arraysize <<"]:";; 
			       cin >> index2; 
                               index2--;  // First index number is 0
			       
                               cout << "\nThe number you have chosen in the array #1 is:";
			       array1[index1].print();
                               cout << "The number you have chosen in the array #2 is:";
			       array2[index2].print();
                               
			       result = array1[index1].div(array2[index2]); 
		               
		               cout << "\nThe result is: ";
			       result.print();	       
			       
			       
			      break;
                
		
			 case 5: // Absolute Value Foundation 
               
                               cout << "\nEnter the index number for the array[1-" << arraysize <<"]:"; 
			       cin >> index1; 
                               index1--;  // First index number is 0
                              
                               cout << "\nThe number you have chosen in the array is:";
			       array1[index1].print();  // The number is from the first array
			      
			       absolute = array1[index1].absoluteValue();
			       cout << "\nThe absolute value of the complex number is: ";
			       cout << absolute; 
			       
			       break;	

                  }
	
	} while (option != 6);

        return 0;
}




