#include <stdio.h>
typedef struct complex
{
int real,imag;
}comp;
void create(comp *x,int real,int imag);
void print(comp x);
void add(comp x,comp y,comp *z);
void subtract(comp x,comp y,comp *z);
void multiply(comp x,comp y,comp *z);
int main()
{
comp x,y,z;
create(&x,1,3);
create(&y,2,-2);
print(x);
print(y);
add(x,y,&z);
print(z);
subtract(x,y,&z);
print(z);
multiply(x,y,&z);
print(z);
return 0;
}
void create(comp *x,int real,int imag)
{
x -> real = real;
x-> imag = imag;
}
void print(comp x)
{
if(x.real != 0)
{
if(x.imag > 0)
{
if(x.imag != 1)
printf("%d + %di",x.real,x.imag);
else
printf("%d + i",x.real);
}
else if(x,imag < 0)
{
if(x.imag != -1)
printf("%d - %di",x.real,x.imag);
else
printf("%d - i",x.real);
}
else
printf("%d",x.real);
}
else
{
if(x.imag > 0)
{
if(x.imag != 1)
printf("%di",x.imag);
else
printf("i");
}
else if(x.imag < 0)
{
if(x.imag != -1)
printf("%di",x.imag);
else
printf("-i");
}
else
printf("0");
}
printf("\n");
}
void add(comp x,comp y,comp *z)
{
z -> real = x.real + y.real;
z -> imag = x.imag + y.imag;
}
void subtract(comp x,comp y,comp *z)
{
z -> real = x.real - y.real;
z -> imag = x.imag - y.imag;
}
void multiply(comp x,comp y,comp *z)
{
z -> real = x.real * y.real - x.imag * y.imag;
z -> imag = x.real * y.imag + x.imag * y.real;
}