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

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

Start exploring endless computing possibilities with your own Raspberry Pi computer and accessories. Perfect for beginners and students.

OUTPUT

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

Addition of 2 no’s : 3+I3


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

  1. 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

    Reply
  2. 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 ^^

    Reply
  3. harika bai

    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

    Reply
  4. Robert L. Smith

    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.

    Reply
  5. On sleep issues in the cafe is bookstore, where students become more willing to stay home line to
    get cramming books on chemistry, civil service, and English literature alongside parents willing to be out
    inside the rain because they buy the latest fashion in notebooks and colored pens because of their primary school children. Therefore,
    when you’ve got accidently visited a Trojan virus program, there is absolutely
    no escape until it fully enters your computer.
    Easy when you know what you are looking for in the product and difficult when you don’t have a system for
    choosing the best Clickbank products.

    Reply

Leave a Reply