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
Description :
This is the one stop educational site for all Electronic and Computer students. If you want to learn something new then we are here to help. We work on Microcontroller projects, Basic Electronics, Digital electronics, Computer projects and also in basic c/c++ programs.
#Home #Sitemap #Submit #Terms of Use
Copyright©2011 electrofriends.com All Rights Reserved
Contact:info@electrofriends.com | Powered by Dhyeya
thanks very much!i realy needed it
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
thanks for the help but i still have some problem in it
it is very useful for clg students.thanks to ranjith sir for displaying it
it is very useful for computer students for program purpose.
good
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 ^^
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
can not get it