面向对象程序设计上机练习十(运算符重载)

来源:互联网 发布:网络教育一对一策划书 编辑:程序博客网 时间:2024/06/05 18:11

面向对象程序设计上机练习十(运算符重载)

Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic

Problem Description

定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。参加运算的两个运算量可以都是类对象,也可以其中有一个是整数,顺序任意。例如:c1+c2、i+c1、c1+i均合法。(其中i是整数,c1、c2是复数),编程实现求2个复数之和、整数与复数之和。

Input

输入有三行:第1行是第1个复数c1的实部和虚部,以空格分开。第2行是第2个复数c2的实部和虚部,以空格分开。第3行是1个整数i的值。

Output

输出有三行:
第1行是2个复数c1和c2的和,显示方式:实部+虚部i
第2行是第1个复数c1加i的值,显示方式:实部+虚部i 
第3行是i加第1个复数c1的值,显示方式:实部+虚部i

Example Input

2 33 510

Example Output

5+8i12+3i12+3i

Hint


#include<bits/stdc++.h>using namespace std;class Complex{private:    double real, imag;public:    Complex(double r = 0.0, double i = 0.0)    {        real = r;        imag = i;    }    Complex operator+(Complex &c2);    Complex operator+(int c);    void showComplex()    {        cout<<real<<"+"<<imag<<"i"<<endl;    }};Complex Complex::operator+(Complex &c2){    return Complex(real + c2.real, imag + c2.imag);}Complex Complex::operator+(int c){    return Complex(real + c, imag);}int main(){    int a, b, c;    cin>>a>>b;    Complex c1(a, b);    cin>>a>>b;    Complex c2(a, b);    cin>>c;    Complex c3 = c1 + c2;    c3.showComplex();    c3 = c1 + c;    c3.showComplex();    c3.showComplex();    return 0;}



#include <iostream>  #include <bits/stdc++.h>  using namespace std;  class Complex  {  private:      double real,imag;  public:      Complex(double a=0,double b=0)      {          real=a;          imag=b;      }      Complex operator +(Complex &a)      {          return Complex(a.real+real,a.imag+imag);      }      Complex operator +(int a)      {          return Complex(a+real,imag);      }      friend ostream &operator<<(ostream &o,Complex a)      {          o<<a.real;          if(a.imag>0)o<<"+";          if(a.imag)o<<a.imag<<"i";          o<<endl;          return o;      }      friend Complex operator + (int a,Complex &b);      };  Complex operator + (int a,Complex &b)  {      return Complex(a+b.real,b.imag);  }    int main()  {      int a,b;      scanf("%d%d",&a,&b);      Complex c1(a,b);      scanf("%d%d",&a,&b);      Complex c2(a,b);      scanf("%d",&a);      cout<<c1+c2<<c1+a<<a+c1<<endl;      return 0;  }  


#include<iostream>  using namespace std;  class Complex  {  private:      double real;double imag;      public:      Complex(){real=0;imag=0;}      Complex(double r,double i)      {          real=r;imag=i;      }      void get()      {          cin>>real>>imag;      }      Complex operator + (Complex &c1)      {      Complex c;      c.real=real+c1.real;      c.imag=imag+c1.imag;      return c;      }      Complex operator + (int &i)      {          return Complex(real+i,imag);      }      friend Complex operator + (int &i,Complex &c2);      friend ostream &operator << (ostream &out,Complex &c3);  };  Complex operator + (int &i,Complex &c2)  {      Complex cc;      cc.real=i+c2.real;      cc.imag=c2.imag;      return cc;  }  ostream &operator <<(ostream &out,Complex &c3)  {      if(c3.imag>0)      out<<c3.real<<"+"<<c3.imag<<"i"<<endl;      else out<<c3.real<<c3.imag<<"i"<<endl;      return out;  }  int main()  {      Complex c1,c2,h,x,y;      int i;      c1.get();      c2.get();      cin>>i;      h=c1+c2;      x=c1+i;      y=i+c1;      cout<<h<<x<<y;      return 0;  } 


阅读全文
0 0
原创粉丝点击