struct cmplx
{
float real;
float img;
};
struct cmplx add(struct cmplx x, struct cmplx y)
{
struct cmplx z;
z.real = x.real + y.real;
z.img = x.img + y.img;
return(z);
}
struct cmplx sub(struct cmplx x, struct cmplx y)
{
struct cmplx z;
z.real = x.real - y.real;
z.img = x.img - y.img;
return(z);
}
struct cmplx mul(struct cmplx x, struct cmplx y)
{
struct cmplx z;
z.real = x.real * y.real- x.img * y.img;
z.img = x.real * y.img + x.img * y.real;
return(z);
}
struct cmplx div(struct cmplx x, struct cmplx y)
{
struct cmplx z;
z.real = (x.real*y.real+x.img*y.img)/(y.real*y.real+y.img*y.img);
z.img = (y.real*x.img-y.img*x.real)/(y.real*y.real+y.img*y.img);
return(z);
}
void main()
{
struct cmplx c1,c2,c3;
char ch;
printf("Enter the operator +,-,*,/ : ");
ch = getchar();
printf("\nEnter the real part of fist complex number : ");
scanf("%f",&c1.real);
printf("\nEnter the imaginary part of fist complex number : ");
scanf("%f",&c1.img);
printf("\nEnter the real part of 2nd complex number : ");
scanf("%f",&c2.real);
printf("\nEnter the imaginary part of 2nd complex number : ");
scanf("%f",&c2.img);
switch(ch)
{
case '+':
{
c3=add(c1,c2);
break;
}
case '-':
{
c3=sub(c1,c2);
break;
}
case '*':
{
c3=mul(c1,c2);
break;
}
case '/':
{
c3=div(c1,c2);
break;
}
default:
{
printf("Invalid operator....\a");
break;
}
}
printf("\n\nThe result is %f + %fi",c3.real,c3.img);
}
0 comments:
Post a Comment