#include<stdio.h>
#include<stdlib.h>
typedef struct Complex
{
float real;
float image;
}Complex;
Complex CreatComplex(float a,float b);
Complex AddComplex(Complex c1,Complex c2);
Complex Subtract_C(Complex c1,Complex c2);
Complex Multiple_C(Complex c1,Complex c2);
Complex Divide_C(Complex c1,Complex c2);
void Print_C(Complex c);
Complex CreatComplex(float a,float b)
{
Complex c;
c.real=a;
c.image=b;
return c;
}
Complex AddComplex(Complex c1,Complex c2)
{
Complex sum;
sum.real=c1.real+c2.real ;
sum.image=c1.image+c2.image ;
return sum;
}
Complex Subtract_C(Complex c1,Complex c2)
{
Complex Sub;
Sub.real=c1.real-c2.real ;
Sub.image=c1.image-c2.image ;
return Sub;
}
Complex Multiple_C(Complex c1,Complex c2)
{
Complex product;
product.real=c1.real*c2.real-c1.image*c2.image;
product.image=c1.real*c2.image+c1.image*c2.real;
return product;
}
Complex Divide_C(Complex c1,Complex c2)
{
Complex quotient;
quotient.real=(c1.real*c2.real+c1.image*c2.image)/(c2.real*c2.real+c2.image*c2.image);
quotient.image=(c1.image*c2.real-c1.real*c2.image)/(c2.real*c2.real+c2.image*c2.image);
return quotient;
}
void Print_C(Complex c)
{
if(c.image==0.0)
printf("%5.2f\n",c.real);
else
if(c.real==0.0)
printf("%5.2fi\n",c.image);
else
printf("%5.2f+%5.2fi\n",c.real,c.image);
}
real_C(Complex c)
{ printf("%5.2f\n",c.real);
return c.real;}
image_C(Complex c)
{printf("%5.2fi\n",c.image);return c.image;}
void Open()
{
printf("*****************************\n");
printf(" complex \n");
printf(" my name:viky\n");
printf("*****************************\n");
printf("make a choce,add or mul\n");
printf("0.exit\n");
printf("1.stand for input+\n");
printf("2.stand for input-\n");
printf("3.stand for input*\n");
printf("4.stand for input/\n");
printf("5.get real part\n");
printf("6.get image part\n");
}
void main()
{Complex z1,z2,sum,Sub,product,quotient;
float e1,e2;
char sym;
int f=-1;
Open();
while(f!=0)
{
scanf("%d",&f);
getchar();
switch(f)
{ case 0:
return;
case 1:
printf("Input z1 like: 3.0+2.1i or -3.0+-2.1i\nz1=");
scanf("%f+%fi",&e1,&e2);
z1=CreatComplex(e1,e2);
printf("Input z2 like: 3.0+2.1i or -3.0+-2.1i\nIf you want to let z1 divide z2, don't input z2=0.0+0.0i!\nz2=");
scanf("%f+%fi",&e1,&e2);
z2=CreatComplex(e1,e2); sum=AddComplex(z1,z2);Print_C(sum);f=-1;Open();break;
case 2:
printf("Input z1 like: 3.0+2.1i or -3.0+-2.1i\nz1=");
scanf("%f+%fi",&e1,&e2);
z1=CreatComplex(e1,e2);
printf("Input z2 like: 3.0+2.1i or -3.0+-2.1i\nIf you want to let z1 divide z2, don't input z2=0.0+0.0i!\nz2=");
scanf("%f+%fi",&e1,&e2);
z2=CreatComplex(e1,e2); Sub=Subtract_C(z1,z2);Print_C(Sub);f=-1;Open();break;
case 3:
printf("Input z1 like: 3.0+2.1i or -3.0+-2.1i\nz1=");
scanf("%f+%fi",&e1,&e2);
z1=CreatComplex(e1,e2);
printf("Input z2 like: 3.0+2.1i or -3.0+-2.1i\nIf you want to let z1 divide z2, don't input z2=0.0+0.0i!\nz2=");
scanf("%f+%fi",&e1,&e2);
z2=CreatComplex(e1,e2);product=Multiple_C(z1,z2);Print_C(product);f=-1;Open();break;
case 4:
printf("Input z1 like: 3.0+2.1i or -3.0+-2.1i\nz1=");
scanf("%f+%fi",&e1,&e2);
z1=CreatComplex(e1,e2);
printf("Input z2 like: 3.0+2.1i or -3.0+-2.1i\nIf you want to let z1 divide z2, don't input z2=0.0+0.0i!\nz2=");
scanf("%f+%fi",&e1,&e2);
z2=CreatComplex(e1,e2);quotient=Divide_C(z1,z2);Print_C(quotient);f=-1;Open();break;
case 5:printf("Input z1 like: 3.0+2.1i or -3.0+-2.1i\nz1=");
scanf("%f+%fi",&e1,&e2);
z1=CreatComplex(e1,e2);
z1.real=real_C(z1);f=-1;Open();break;
case 6:printf("Input z1 like: 3.0+2.1i or -3.0+-2.1i\nz1=");
scanf("%f+%fi",&e1,&e2);
z1=CreatComplex(e1,e2);
z1.image=image_C(z1);f=-1;Open();break;
default: printf("error symbol!!");f=-1;Open();
}
}
}