将以下C++程序改成C程序,要求有数据结构的内容,实现复数的四则运算,编译无错。

#include<iostream.h>classComplex{public:Complex(){real=0;imag=0;}voidSetCom(floatr,fl... #include<iostream.h>
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);
}
展开
 我来答
iamagoodguy
2012-06-12 · TA获得超过840个赞
知道小有建树答主
回答量:175
采纳率:100%
帮助的人:94.1万
展开全部

//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);

}

推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式