![](https://iknow-base.cdn.bcebos.com/lxb/notice.png)
将以下C++程序改成C程序,要求有数据结构的内容,实现复数的四则运算,编译无错。
class Complex
{
public:
Complex()
{
real=0;
imag=0;
}
void SetCom(float r,float i)
{
real=r;
imag=i;
}
void Print(Complex C)
{
if(C.real==0)
if(C.imag==0)cout<<C.real<<endl;
else cout<<C.imag<<"i"<<endl;
else
if(C.imag<0)cout<<C.real<<C.imag<<"i"<<endl;
else if(C.imag==0)cout<<C.real<<endl;
else cout<<C.real<<"+"<<C.imag<<"i"<<endl;
}
void Add(Complex C1,Complex C2)
{
real=C1.real+C2.real;
imag=C1.imag+C2.imag;
Print(*this);
}
void DEC(Complex C1,Complex C2)
{
real=C1.real-C2.real;
imag=C1.imag-C2.imag;
Print(*this);
}
void MUL(Complex C1,Complex C2)
{
real=C1.real*C2.real-C1.imag*C2.imag;
imag=C1.real*C2.imag+C1.imag*C2.real;
Print(*this);
}
void DIV(Complex C1,Complex C2)
{
real=(C1.real*C2.real+C1.imag*C2.imag)/(C2.real*C2.real+C2.imag*C2.imag);
imag=(C1.imag*C2.real-C1.real*C2.imag)/(C2.real*C2.real+C2.imag*C2.imag);
Print(*this);
}
private:
float real,imag;
};
void main()
{
Complex C1,C2,C;
float rx,iy;
cout<<"输入第一个复数的\n实部 虚部\n";
cin>>rx>>iy;
C1.SetCom(rx,iy);
cout<<"输入第二个复数的\n实部 虚部\n";
cin>>rx>>iy;
C2.SetCom(rx,iy);
cout<<"相加:\n";
C.Add(C1,C2);
cout<<"相减:\n";
C.DEC(C1,C2);
cout<<"相乘:\n";
C.MUL(C1,C2);
cout<<"相除:\n";
C.DIV(C1,C2);
} 展开
//Windows Turbo C 编译器下编译成功
//Visual C++编译器下编译成功
//主要是改了class和printf,应该符合要求
//其他任何问题,追问。。。。。
#include<stdio.h>
#include<malloc.h>
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);
}