求大神 编写c++复数计算器源代码
要求1设计复数计算器可以重复进行+-*除和两个复数比较运算2用cout和cin输出和输入复数3设计复数类complex设计重载复数+-*除《》设计复数相加减乘除比较的函数...
要求 1设计复数计算器可以重复进行+ - * 除和两个复数比较运算
2用cout和cin输出和输入复数
3设计复数类 complex
设计重载复数 + - * 除 《 》
设计复数相加减乘除 比较的函数 展开
2用cout和cin输出和输入复数
3设计复数类 complex
设计重载复数 + - * 除 《 》
设计复数相加减乘除 比较的函数 展开
1个回答
展开全部
#include<iostream>
using namespace std;
class Complex{
double real;
double imag;
public:
Complex(double r=0,double i=0)
{
real=r;
imag=i;
}
Complex operator+(const Complex& a)
{
Complex b;
b.real=real+a.real;
b.imag=imag+a.imag;
return b;
}
Complex operator-(const Complex& a)
{
Complex b;
b.real=real-a.real;
b.imag=imag-a.imag;
return b;
}
Complex operator*(const Complex& a)
{
Complex b;
b.real=real*a.real-imag*a.imag;
b.imag=real*a.imag+imag*a.real;
return b;
}
Complex operator/(const Complex& a)
{
Complex b,c;
c.real=a.real;
c.imag=-a.imag;
b.real=c.real*a.real-c.imag*a.imag;
b.imag=c.real*b.imag+c.imag*a.real;
return b;
}
friend ostream& operator<<(ostream& os,const Complex& a)
{
if(a.imag >0)
os<<a.real<<"+"<<a.imag<<"i"<<endl;
else
os<<a.real<<a.imag<<"i"<<endl;
return os;
}
friend istream& operator>>(istream&is,Complex& a)
{
is>>a.real;
is>>a.imag;
return is;
}
};
void main()
{
Complex a,b;
Complex c;
cin>>a>>b;
cout<<a<<endl<<b<<endl;
c=a*b;
cout<<c<<endl;
}
复数不能比较大小!
希望回答能帮到你,哪里不清楚再问。
如果能帮到你,望采纳!
using namespace std;
class Complex{
double real;
double imag;
public:
Complex(double r=0,double i=0)
{
real=r;
imag=i;
}
Complex operator+(const Complex& a)
{
Complex b;
b.real=real+a.real;
b.imag=imag+a.imag;
return b;
}
Complex operator-(const Complex& a)
{
Complex b;
b.real=real-a.real;
b.imag=imag-a.imag;
return b;
}
Complex operator*(const Complex& a)
{
Complex b;
b.real=real*a.real-imag*a.imag;
b.imag=real*a.imag+imag*a.real;
return b;
}
Complex operator/(const Complex& a)
{
Complex b,c;
c.real=a.real;
c.imag=-a.imag;
b.real=c.real*a.real-c.imag*a.imag;
b.imag=c.real*b.imag+c.imag*a.real;
return b;
}
friend ostream& operator<<(ostream& os,const Complex& a)
{
if(a.imag >0)
os<<a.real<<"+"<<a.imag<<"i"<<endl;
else
os<<a.real<<a.imag<<"i"<<endl;
return os;
}
friend istream& operator>>(istream&is,Complex& a)
{
is>>a.real;
is>>a.imag;
return is;
}
};
void main()
{
Complex a,b;
Complex c;
cin>>a>>b;
cout<<a<<endl<<b<<endl;
c=a*b;
cout<<c<<endl;
}
复数不能比较大小!
希望回答能帮到你,哪里不清楚再问。
如果能帮到你,望采纳!
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询