当前位置:编程学习 > C/C++ >>

C++问题 急求

C++问题 急求 复数的减法乘法和除法运算符重载函数是怎样定义的(分别写出成员函数和友元函数形式)
答案:#include<iostream.h>
class complex{
public:
 complex(double r=0.0,double i=0.0)//构造函数
 complex operator +(complex c);//重载“+”运算符
 complex operator -(complex c);//重载“-”运算符
 complex operator *(complex c);//重载“*”运算符
 friend complex operator/(complex &c1,complex &c2);//友元函数重载“/”运算符
 friend int operator ==(complex &c1,complex &c2);//友元函数重载“==”运算符
 friend int operator !=(complex &c1,complex &c2);//友元函数重载“!=”运算符
 friend complex operator++(complex &);//友元函数重载“++”后缀运算符(先赋值后自加)
 complex operator++();//成员函数重载“++”前缀运算符(先自加后赋值)
 void disp();//最后输出
private:
 double real;
 double imag;
};
complex::complex(double r,double i)
{real=r;imag=i;}
complex complex::operator+(complex c)
{
complex w;
w.real=real+c.real;
w.imag=imag+c.imag;
return w;
}
complex complex::operator-(complex c)
{complex w;
w.real=real-c.real;
w.imag=imag-c.imag;
return w;
}
complex complex::operator*(complex c)
{
complex w;
w.real=real*c.real-imag*c.imag;
w.imag=real*c.imag+imag*c.real;
return w;
}
complex operator/(complex &c1,complex &c2)
{
complex w;
double t;
t=1/(c2.real*c2.real+c2.imag*c2.imag);
w.real=(c1.real*c2.real+c1.imag*c2.imag)*t;
w.imag=(c2.real*c1.imag-c1.real*c2.imag)*t;
return w;
}
int operator ==(complex &c1,complex &c2)
{if(c1.real==c2.real&&c1.imag==c2.imag)
 cout<<"the two complex is same:"<<endl;
 return 0;
}
int operator !=(complex &c1,complex &c2)
{
if(c1.imag!=c2.imag||c1.real!=c2.real)
cout<<"the two complex is different:"<<endl;
return 0;
}
complex complex::operator++()
{
++real;
++imag;
return *this;
}
complex operator++(complex &c)
{
c.real++;
c.imag++;
return c;
}
void complex::disp()
{
if(real!=0||imag==0)
cout<<real;
if(imag!=0)
{
if(imag>0)
cout<<"+";
if(imag<0)
cout<<"-";
cout<<imag<<"i";
}
}
int main()
{
double x,y,z,w;
complex*p1complex=NULL;
complex*p2complex=NULL;
complex*p3complex=new complex;
complex*p4complex=new complex;
complex*p5complex=new complex;
complex*p6complex=new complex;
cout<<"please input the first complex:"<<endl;
cin>>x>>y;
p1complex=new complex(x,y);
cout<<"please input the second complex:"<<endl;
cin>>z>>w;
p2complex=new complex(z,w);
*p3complex=*p1complex+*p2complex;
*p4complex=*p1complex-*p2complex;
*p5complex=*p1complex* *p2complex;
*p6complex=*p1complex/ *p2complex;
cout<<"the first complex is:";
p1complex->disp();
cout<<endl;
cout<<"the second complex is:";
p2complex->disp();
cout<<endl;
cout<<"the add is:";
p3complex->disp();
cout<<endl;
cout<<"the subtraction is:";
p4complex->disp();
cout<<endl;
cout<<"the multiplication";
p5complex->disp();
cout<<endl;
cout<<"the divison is:";
p6complex->disp();
cout<<endl;
cout<<"the add self before is:";
p1complex->operator ++();
p1complex->disp();
cout<<endl;
cout<<"the add self later is:";
operator ++(*p1complex);
p1complex->disp();
cout<<endl;
operator==(*p1complex,*p2complex);
operator!=(*p1complex,*p2complex);
delete p1complex;
delete p2complex;
return 0;
}

纯手工制作,手打,复制者手贱。楼主请采纳,不懂加qq1319443281为您详解。


#include<iostream>usingnamespacestd; classPoint{public: //ERROR*********found********* Point(intx=0,inty=0)
:x_(x),y_(y){} //ERROR*********found********* voidmove(intxOff,intyOff) { x_ =xOff; y_ =yOff; }
voidprint()const { cout<<"Thepointis("<<x_<<','<<y_<<')'<<endl; }private: intx_,y_; }; voidfun(Point*p){ //ERROR*********found********* p->print();} intmain(){ Pointp1,p2(2,1); p1.print(); p2.move(1,4); fun(

要定义一个复数的类   我记的课本上有的   照那个写就可以了

友元类  声明为  friend     马上要睡觉了  也不能帮你写了

你看看吧!之前写的,简单的实现:

#include <iostream>
using namespace std;

class complex
{
public:
 complex(){};
 complex(double r, double i)
 {
  real = r, imag = i;
 }
 complex operator +(const complex &c);
 complex operator -(const complex &c);
 complex operator *(const complex &c);
 complex operator /(const complex &c);
 friend void print(const complex &c);
private:
 double real, imag;
};

complex complex::operator +(const complex &c)
{
 return complex(real + c.real, imag + c.imag);
}

complex complex::operator -(const complex &c)
{
 return complex(real - c.real, imag - c.imag);
}

complex complex::operator *(const complex &c)
{
 return complex(real * c.real - imag * c.imag, real * c.imag + imag * c.real);
}

complex complex::operator /(const complex &c)
{
 return complex((real * c.real + imag + c.imag) / (c.real * c.real + c.imag * c.imag),
 (imag * c.real - real * c.imag) / (c.real * c.real + c.imag * c.imag));
}

void print(const complex &c)
{
 if(c.imag<0)
  cout<<c.real<<c.imag<<'i';
 else
  cout<<c.real<<'+'<<c.imag<<'i';
}

void main()
{
 complex c1(6.0, 5.0), c2(3.0, -2.0), c3;
 c3 = c1 + c2;
 cout<<"\n c1 + c2 = ";
 print(c3);
 
 c3 = c1 - c2;
 cout<<"\n c1 - c2 = ";
 print(c3);
 
 c3 = c1 * c2;
 cout<<"\n c1 * c2 = ";
 print(c3);
 
 c3 = c1 / c2; 
 cout<<"\n c1 / c2 = ";
 print(c3);
 
 c3 = (c1+c2) * (c1-c2) * c2/c1;
 cout<<"\n ( c1 + c2 ) * ( c1 - c2 ) * c2 / c1 = ";
 print(c3);
 cout<<endl;
}
附上运行截图:

上一个:怎么样用visual c++编写窗口化的程序?
下一个:用C++编写计算器

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,