//Windows Turbo C 编译器下编译成功
//Visual C++编译器下编译成功
//主要是改了class和printf,应该符合要求
//其他任何问题,追问。。。。。
#include
#include
struct Complex{
float real,imag;
};
void Init( struct Complex * num)
{
num->real=0;
num->imag=0;
}
void SetCom( struct Complex * num, float r,float i)
{
num->real=r;
num->imag=i;
}
void Print(struct Complex * C)
{
if(C->real==0)
if(C->imag==0)
printf( "%f\n", C->real);
else
printf( "%fi\n", C->imag);
else
if(C->imag<0)
printf( "%f+%fi\n", C->real, C->imag);
else if(C->imag==0)
printf( "%f\n", C->real);
else
printf( "%f+%fi\n", C->real, C->imag);
}
struct Complex * Add(struct Complex * C1,struct Complex * C2)
{
struct Complex * C= ( struct Complex *)malloc( sizeof( struct Complex));
C->real=C1->real+C2->real;
C->imag=C1->imag+C2->imag;
Print( C);
return C;
}
struct Complex * DEC(struct Complex * C1,struct Complex * C2)
{
struct Complex * C = ( struct Complex *)malloc( sizeof( struct Complex));
C->real=C1->real-C2->real;
C->imag=C1->imag-C2->imag;
Print( C);
return C;
}
struct Complex * MUL(struct Complex * C1,struct Complex * C2)
{
struct Complex * C= ( struct Complex *)malloc( sizeof( struct Complex));
C->real=C1->real*C2->real-C1->imag*C2->imag;
C->imag=C1->real*C2->imag+C1->imag*C2->real;
Print( C);
return C;
}
struct Complex * DIV(struct Complex * C1,struct Complex * C2)
{
struct Complex * C= ( struct Complex *)malloc( sizeof( struct Complex));
C->real=(C1->real*C2->real+C1->imag*C2->imag)/(C2->real*C2->real+C2->imag*C2->imag);
C->imag=(C1->imag*C2->real-C1->real*C2->imag)/(C2->real*C2->real+C2->imag*C2->imag);
Print( C);
return C;
}
int main()
{
struct Complex * C1, * C2, * C;
float rx,iy;
C1 = ( struct Complex *)malloc( sizeof( struct Complex));
C2 = ( struct Complex *)malloc( sizeof( struct Complex));
C = ( struct Complex *)malloc( sizeof( struct Complex));
printf( "输入第一个复数的\n实部 虚部\n");
scanf( "%f %f", &rx, &iy);
SetCom( C1,rx,iy);
printf( "输入第二个复数的\n实部 虚部\n");
scanf( "%f %f", &rx, &iy);
SetCom( C2, rx,iy);
printf( "相加:\n");
C = Add(C1,C2);
printf( "相减:\n");
C = DEC(C1,C2);
printf( "相乘:\n");
C = MUL(C1,C2);
printf( "相除:\n");
C = DIV(C1,C2);
return( 0);
}