#include<iostream.h>
#include<conio.h>
struct complex
{
float real;
float image;
};
void display(struct complex c);
int main()
{
struct complex x,y,z;
void add(struct complex a, struct complex b);
void sub(struct complex a, struct complex b);
void mul(struct complex a, struct complex b);
char opr;
clrscr();
cout<<"Enter the first complex no.(first real part and then imaginary part):"<<endl;
cin>>x.real>>x.image;
cout<<"Enter the second complex no.(first real part and then imaginary part):"<<endl;
cin>>y.real>>y.image;
cout<<"Enter the operator(+, -, *) :";cin>>opr;
switch(opr)
{
case '+' :
{
cout<<endl<<"Complex number addition"<<endl;
add(x,y);
break;
}
case '-' :
{
cout<<endl<<"Complex number subtraction"<<endl;
sub(x,y);
break;
}
case '*' :
{
cout<<endl<<"Complex number multiplication"<<endl;
mul(x,y);
break;
}
default : cout<<"Invalid operator" ;
};
getch();
return 0;
}
void add(struct complex a, struct complex b)
{
struct complex z;
z.real = a.real+b.real;
z.image = a.image+b.image;
display(z);
}
void sub(struct complex a, struct complex b)
{ struct complex z;
z.real = a.real - b.real;
z.image = a.image - b.image;
display(z);
}
void mul(struct complex a, struct complex b)
{
struct complex z;
z.real = a.real* b.real - a.image*b.image;
z.image = a.real*b.image + a.image*b.real;
display(z);
}
void display(struct complex c)
{
if(c.image>=0)
cout<<c.real<<"+i"<<c.image;
else
cout<<c.real<<"-i"<<c.image;
}
|
|

| Program to perform arithmetic operations (+,-,*) on given compare numbers using structures. |
|
| |
|
|
|
|