C++ program to perform arithmetic operations of two complex numbers using operator overloading

Saturday, March 13th, 2010

BINARY OPERATOR

AIM:
A program to perform simple arithmetic operations of two complex numbers using operator overloading.

ALGORITHAM:

• Start the process
• Get the complex value a.real and a.image
• Check while ((ch=getchar())!=’q’)
o True : execute switch(ch)
o Case ‘a’:Then
Compute c<-a+b, Print c.real and c.imag
o Case ‘s’: Then
Compute c<-a-b, Print c.real and c.imag
o Case ‘m’: Then
Compute c<-a*b, Print c.real and c.imag
o Case ‘d’: Then
Compute c<-a/b, Print c.real and c.imag
o End of switch
• End of while
• Stop the process

PROGRAM

#include<iostream.h>
#include<conio.h>
#include<string.h>
struct complex
{
float real;
float imag;
};
complex operator + (complex a,complex b);
complex operator - (complex a,complex b);
complex operator * (complex a,complex b);
complex operator / (complex a,complex b);
 
void main()
{
complex a,b,c;
int ch;
void menu(void);clrscr();
cout<<"Enter the first complex no:";
cin>>a.real>>a.imag;
cout<<"Enter the second complex no:";
cin>>b.real>>b.imag;
menu();
while ((ch = getchar()) != 'q')
{
switch(ch)
{
case 'a':c =a + b;
cout<<"Addition of 2 no’s";
cout<<c.real<<"+i"<<c.imag;
break;
case 's':c=a-b;
cout<<"Substraction of 2 no’s";
cout<<c.real<<"i"<<c.imag;
break;
case 'm':c=a*b;
cout<<"Multiplication of 2 no’s";
cout<<c.real<<"i"<<c.imag;
break;
 				case 'd':c=a/b;
cout<<"Division of 2 no’s";
cout<<c.real<<"i"<<c.imag;
break;
 			}
 		}
 	}
 	void menu()
{
cout<<"complex no: operators";
cout<<"a->addition";
cout<<"s->substraction";
cout<<"m->multiplication";
cout<<"d->division";
cout<<"q->quit";
cout<<"options please";
}
complex operator -(struct complex a, struct complex b)
{
complex c;
c.real=a.real-b.real;
c.imag=a.imag-b.imag;
return(c);
}
complex operator *(struct complex a, struct complex b)
{
complex c;
c.real=((a.real*b.real)-(a.imag*b.imag));
c.imag=((a.real*b.imag)+(a.imag*b.real));
return(c);
}
complex operator +(struct complex a,struct complex b)
{
complex c;
c.real=a.real+b.real;
c.imag=a.imag+b.imag;
return(c);
}
complex operator /(struct complex a, struct complex b)
{
complex c;
float temp;
temp=((b.real*b.real)+(b.imag*b.imag));
c.real=((a.real*b.real)+(a.imag*b.imag))/temp;
  		 return(c);
}

OUTPUT

Enter the first complex no: 1,1
Enter the second complex no: 2,2

Addition of 2 no’s : 3+I3

Avatar of Ranjith

Author Name :
Ranjith

Total : 12 Comments


12 Responses to “C++ program to perform arithmetic operations of two complex numbers using operator overloading”

  1. sam says:

    thanks very much!i realy needed it

  2. toto says:

    Thank for share. But I have a problem with / complex number. Please check your code. I think your multiply code and / code not consitent. * code is right, but / is wrong. I think the / code is :

    temp = ((b.real*b.real)+(b.imag*b.imag));
    c.real = ((a.real*b.real) – (a.imag*b.imag))/temp; //this is from your * operation
    c.imag = ((a.real*b.imag) + (a.imag*b.real))/temp; //this is from your * operation

    Am I wrong?
    thank for share

  3. anujeet kaur says:

    thanks for the help but i still have some problem in it

  4. geethika says:

    it is very useful for clg students.thanks to ranjith sir for displaying it

  5. sudha says:

    it is very useful for computer students for program purpose.

  6. maryam says:

    why i got problem .. it said that
    \proposal.cpp(1): fatal error C1083: Cannot open include file: ‘iostream.h’: No such file or directory

    ?? please help ^^

  7. harika bai says:

    u can open directory and u can change the drive C to drive D try this once i will ask my sir and send u again

  8. maryam says:

    can not get it :(

  9. aiswarya says:

    too lengthy program

  10. Robert L. Smith says:

    The above program for division of complex numbers will fail for very small or very large real or imaginary parts of the denominator because of overflow or underflow of the parts. About 50 years ago I showed a simple method that works in almost all cases where the result can be represented in a computer. The trick is to break the problem into two parts, depending on whether the absolute value of real part of the denominator is greater of less than the absolute value of the imaginary part of the denominator. Consider Q = (a + ib)/(c + id). Case 1: If abs(c) > than abs(d), then note that the denominator can be written (c + id) = c*(1 + ir) where r = d/c. Now instead of multiplying both the numerator and denominator by the complex conjugate of (c + id), just multiply both by the complex conjugate of (1 + ir). Note that the absolute value of r is less than 1. Note that the new denominator will become c*(1 + ir)*(1 – ir) = c*(1 + r*r) = c*(1 + r*d/c) = c + d*r). The quotient is:

    q = (a + ib) / (c + id) = ((a + ib)*(1 – ir)) / (c*(1 + ir)*(1 – id/c))

    = ((a + b*r) + i(b – a*r)) / (c*(1 + r*d/c))

    = ((a + b*r)/(c + r*d)) + i((b – a*r)/(c + r*d))

    Note that the numerator and denominator no longer involve squares of possibly large or small quantities.

    Case 2: If abs(c) <= abs(d) then just reverse things a little bit so that the denominator (c + id) = d*(r + i) where r = c/d. In this case the absolute value of r is less than or equal to 1. In computing the quotient, we multiply the numerator and denominator by (r – i). The quotient can now be computed as follows:

    q = (a + ib) / (c + id) = ((a + ib)*(r – i)) / (d*(r + i)*(c/d – i))

    = ((a*r + b) + i(b*r – a)) / (c*r + d)

    = ((a*r + b) / (d + c*r)) + i((-a + b*r) / (d + c*r))

    I might mention that there are a few obscure cases where this method can be improved, again by breaking it down to two more sub-cases. My original paper appears in the Journal of the ACM.

  11. guru says:

    totally wrong

Leave a Reply

Question and Answer
C/C++ Unix & Linux Wordpress
Source codes
C C++ Java

Free email signup

Email: