OJ第三批——Problem J:C++复数运算符重载(+与<<)

问题及代码:

Problem J: C++复数运算符重载(+与<<)

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 755   Solved: 466
[ Submit][ Status][ Web Board]

Description

定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算与输出操作。
(1)参加运算的两个运算量可以都是类对象,也可以其中有一个是实数,顺序任意。例如,c1+c2,d+c1,c1+d均合法(设d为实数,c1,c2为复数)。
(2)输出的算数,在复数两端加上括号,实部和虚部均保留两位小数,如(8.23+2.00i)、(7.45-3.40i)、(-3.25+4.13i)等。
编写程序,分别求两个复数之和、整数和复数之和,并且输出。

请在下面的程序段基础上完成设计:
#include <iostream>
#include <iomanip>
using namespace std;

class Complex
{
public:
Complex():real(0),imag(0) {}
Complex(double r,double i):real(r),imag(i) {}
Complex operator+(Complex &);
Complex operator+(double &);
friend Complex operator+(double&,Complex &);
friend ostream& operator << (ostream& output, const Complex& c);
private:
double real;
double imag;
};

//将程序需要的其他成份写在下面,只提交begin到end部分的代码
//******************** begin ********************


//********************* end ********************
int main()
{
//测试复数加复数
double real,imag;
cin>>real>>imag;
Complex c1(real,imag);
cin>>real>>imag;
Complex c2(real,imag);
Complex c3=c1+c2;
cout<<"c1+c2=";
cout<<c3;

//测试复数加实数
double d;
cin>>real>>imag;
cin>>d;
c3=Complex(real,imag)+d;
cout<<"c1+d=";
cout<<c3;

//测试实数加复数
cin>>d;
cin>>real>>imag;
c1=Complex(real,imag);
c3=d+c1;
cout<<"d+c1=";
cout<<c3;

return 0;
}

Input

一个复数的实部和虚部,另一个复数的实部和虚部 
一个复数的实部和虚部,一个实数
一个实数,一个复数的实部和虚部

Output

两个复数之和、复数和实数之和,实数和复数之和。

Sample Input

3 4 5 -10
3 4 5
5 3 4

Sample Output

c1+c2=(8.00-6.00i)
c1+d=(8.00+4.00i)
d+c1=(8.00+4.00i)

HINT

只提交自己定义的函数部分,控制输出小数点后两位,用 setiosflags(ios::fixed);和setprecision(2);控制


 

 

#include <iostream>
#include <iomanip>
using namespace std;

class Complex
{
public:
    Complex():real(0),imag(0) {}
    Complex(double r,double i):real(r),imag(i) {}
    Complex operator+(Complex &);
    Complex operator+(double &);
    friend Complex operator+(double&,Complex &);
    friend ostream& operator << (ostream& output, const Complex& c);
private:
    double real;
    double imag;
};

//将程序需要的其他成份写在下面,只提交begin到end部分的代码
//******************** begin ********************
Complex Complex::operator+(Complex &c1)
{
    return Complex((real+c1.real),(imag+c1.imag));
}

Complex Complex::operator+(double &d)
{
    return Complex((real+d),imag);
}

Complex operator+(double &d1,Complex &c2)
{
    return Complex((d1+c2.real),c2.imag);
}

ostream & operator << (ostream & output,const Complex &c)
{
    output<<"("<<setiosflags(ios::fixed)<<setprecision(2)<<c.real;
    if(c.imag>=0)
        output<<"+";
    output<<setiosflags(ios::fixed)<<setprecision(2)<<c.imag<<"i)"<<endl;
    return output;
}
//********************* end ********************
int main()
{
//测试复数加复数
    double real,imag;
    cin>>real>>imag;
    Complex c1(real,imag);
    cin>>real>>imag;
    Complex c2(real,imag);
    Complex c3=c1+c2;
    cout<<"c1+c2=";
    cout<<c3;

//测试复数加实数
    double d;
    cin>>real>>imag;
    cin>>d;
    c3=Complex(real,imag)+d;
    cout<<"c1+d=";
    cout<<c3;

//测试实数加复数
    cin>>d;
    cin>>real>>imag;
    c1=Complex(real,imag);
    c3=d+c1;
    cout<<"d+c1=";
    cout<<c3;

    return 0;
}


 

运行结果:


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include<iostream> #include<cmath> using namespace std; class Complex { public: Complex(double r = 0.0, double m = 0.0) :real(r), magic(m) { cout << "Complex instructor!" << endl; } ~Complex() { cout << "Complex destructor!" << endl; } void display() const { if (real == 0) cout << magic << "i" << endl; else { if (magic > 0) cout << real << "+" << magic << "i" << endl; else if (magic < 0) cout << real << magic << "i" << endl; else cout << real << endl; } } Complex operator +(const Complex& c2) const; Complex operator -(const Complex& c2) const; Complex operator *(const Complex& c2) const; Complex operator /(const Complex& c2) const; friend Complex operator + (const double& d, const Complex& c); friend Complex operator - (const double& d, const Complex& c); friend Complex operator + (const Complex& c, const double& d); friend Complex operator - (const Complex& c, const double& d); friend ostream& operator << (ostream& out, const Complex& c);//operator"<<" friend Complex operator / (const double& d, const Complex& c); friend Complex operator * (const Complex& c, const double& d); friend Complex operator / (const Complex& c, const double& d); private: double real, magic; }; Complex Complex::operator +(const Complex& c2) const { return Complex(real + c2.real, magic + c2.magic);//copy } Complex Complex::operator -(const Complex& c2) const { return Complex(real - c2.real, magic - c2.magic);//copy } Complex Complex::operator *(const Complex& c2) const { return Complex(real * c2.real - magic * c2.magic, c2.real * magic + real*c2.magic);//copy } Complex Complex::operator /(const Complex& c2) const { double k = pow(c2.real, 2) + pow(c2.magic, 2); return Complex((real * c2.real + magic * c2.magic)/k, (c2.real * magic - real * c2.magic)/k);//copy } Complex operator + (const double& d, const Complex& c) { return Complex(d + c.real, c.magic);//copy } Complex operator - (const double& d, const Complex& c) { return Complex(d - c.real, -c.magic);//copy } Complex operator + (const Complex& c, const double& d) { return Complex(c.real + d, c.magic);//copy } Complex operator / (const double& d, const Complex& c) { double k = pow(c.real, 2) + pow(c.magic, 2); return Complex(d * c.real / k, - d * c.magic / k);//copy } Complex operator - (const Complex& c, const double& d) { return Complex(c.real - d, c.magic);//copy } Complex operator * (const Complex& c, const double& d) { return Complex(c.real * d, c.magic * d);//copy } Complex operator / (const Complex& c, const double& d) { return Complex(c.real / d, c.magic / d);//copy } ostream& operator<<(ostream& out, const Complex& c) { if (c.real == 0) cout << c.magic << "i" << endl; else { if (c.magic > 0) cout << c.real << "+" << c.magic << "i" << endl; else if (c.magic < 0) cout << c.real << c.magic << "i" << endl; else cout << c.real << endl; } return out; } int main() { Complex C1(5, 4), C2(7,2), C3; cout << "C1=" << C1 << endl; cout << "C2=" << C2 << endl; C3 = C1 + C2; cout << "C3=" << C3 << endl; C3 = C1 - C2; cout << "C3=" << C3 << endl; C3 = C1 * C2; cout << "C3=" << C3 << endl; C3 = C1 / C2; cout << "C3=" << C3 << endl; double d; cin >> d; C3 = d + C1; cout << "C3=" << C3 << endl; C3 = d - C1; cout << "C3=" << C3 << endl; C3 = C1 + d; cout << "C3=" << C3 << endl; C3 = C1 - d; cout << "C3=" << C3 << endl; C3 = C1 * d; cout << "C3=" << C3 << endl; C3 = C1 / d; cout << "C3=" << C3 << endl; C3 = d / C1; cout << "C3=" << C3 << endl; return 0; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值