C++ 实现复数加减运算
要求1采用重载“+”“-”运算符的方式;2定义复数类,分别重载为类成员函数和友元函数的方式实现;3在主函数中分别定义两个复数c1=6+3i;c2=3+6i,计算c1+c2...
要求
1 采用重载“+”“-”运算符的方式;
2定义复数类,分别重载为类成员函数和友元函数的方式实现;
3在主函数中分别定义两个复数c1=6+3i;c2=3+6i,计算c1+c2,c1-c2,输出结果 展开
1 采用重载“+”“-”运算符的方式;
2定义复数类,分别重载为类成员函数和友元函数的方式实现;
3在主函数中分别定义两个复数c1=6+3i;c2=3+6i,计算c1+c2,c1-c2,输出结果 展开
4个回答
2013-11-08
展开全部
#include "iostream.h"
class Complex
{
private:
double real,imag;
public:
Complex(double r,double i)
{
real=r;
imag=i;
}
/*friend Complex operator +(Complex& a,Complex& b)//友元运算符重载
{
double r=a.real+b.real;
double i=a.imag+b.imag;
return Complex(r,i);
}*/
Complex operator +(Complex&b)//类运算符重载
{
double r=real+b.real;
double i=imag+b.imag;
return Complex(r,i);
}
show()
{
cout<<real<<"+"<<imag<<"i";
}
};
void main()
{
Complex c1(6,3),c2(3,6),c=c1+c2;
c1.show();
cout<<"+";
c2.show();
cout<<"=";
c.show();
cout<<endl;
}
以上重载"+",重载"-"以此类似,仅供参考
class Complex
{
private:
double real,imag;
public:
Complex(double r,double i)
{
real=r;
imag=i;
}
/*friend Complex operator +(Complex& a,Complex& b)//友元运算符重载
{
double r=a.real+b.real;
double i=a.imag+b.imag;
return Complex(r,i);
}*/
Complex operator +(Complex&b)//类运算符重载
{
double r=real+b.real;
double i=imag+b.imag;
return Complex(r,i);
}
show()
{
cout<<real<<"+"<<imag<<"i";
}
};
void main()
{
Complex c1(6,3),c2(3,6),c=c1+c2;
c1.show();
cout<<"+";
c2.show();
cout<<"=";
c.show();
cout<<endl;
}
以上重载"+",重载"-"以此类似,仅供参考
2013-11-08
展开全部
给你弄了几行~
你照着写下去吧~
好久没有动手了~
#include <iostream.h>
class Complex{
public:
Complex(double r=0, double v=0):real(r),virt(v){}
friend Complex operator+(Complex a, Complex b);
friend ostream& operator<<(ostream& out, Complex& a);
private:
double real;
double virt;
};
ostream& operator<<(ostream& out, Complex& a){ return out <<a.real <<"+" <<a.virt <<"i\n"; }
Complex operator+(Complex a, Complex b)
{
return Complex(a.real+b.real, a.virt+b.virt);
}
void main()
{
Complex a(2,5), b(7,8), c(0,0);
c = a + b;
cout <<c;
c = 4.1 + a;
cout <<c;
c = b + 5.6;
cout <<c;
}
你照着写下去吧~
好久没有动手了~
#include <iostream.h>
class Complex{
public:
Complex(double r=0, double v=0):real(r),virt(v){}
friend Complex operator+(Complex a, Complex b);
friend ostream& operator<<(ostream& out, Complex& a);
private:
double real;
double virt;
};
ostream& operator<<(ostream& out, Complex& a){ return out <<a.real <<"+" <<a.virt <<"i\n"; }
Complex operator+(Complex a, Complex b)
{
return Complex(a.real+b.real, a.virt+b.virt);
}
void main()
{
Complex a(2,5), b(7,8), c(0,0);
c = a + b;
cout <<c;
c = 4.1 + a;
cout <<c;
c = b + 5.6;
cout <<c;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
class complex
{
public:
complex(double xx = 0.0, double yy = 0.0){x = xx;y = yy;}
complex operator+(complex);
complex operator-(complex);
void display();
private:
double x;
double y;
};
complex complex::operator+(complex b)
{
complex temp;
temp.x = x+b.x;
temp.y = y+b.y;
return temp;
}
complex complex::operator-(complex b)
{
complex temp;
temp.x = x-b.x;
temp.y = y-b.y;
return temp;
}
void complex::display()
{
cout<<"("<<x<<")"<<"+"<<"("<<y<<")"<<"j"<<endl;
}
void main()
{
complex a(1.1, 1.2);
complex b(2.1, 2.2);
complex c;
c = a+b;
cout<<"a = ";
a.display();
cout<<"b = ";
b.display();
cout<<"c = a+b =";
c.display();
c = a-b;
cout<<"c = a-b =";
c.display();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-11-08
展开全部
http://zhidao.baidu.com/question/79347783.html?si=2楼主,这个应该符合你的要求。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询