• 复数类的运算


    根据以下代码段完善 ?? 处内容及程序内容,以实现规定的输出。

    class Complex
    {
    	public:
    		Complex(double r=0, double i=0):real(r), imag(i){	}
    		Complex operator+( ?? ) const;//重载双目运算符'+'
    		Complex operator-=( ?? ); //重载双目运算符'-='
    		friend Complex operator-( ?? ) const;//重载双目运算符'-'
    		void Display() const;
    	private:
    		double real;
    		double imag;
    };
    
    void Complex::Display() const
    {
    	cout << "(" << real << ", " << imag << ")" << endl;
    }
    
    int main()
    {
    	double r, m;
    	cin >> r >> m;
    	Complex c1(r, m);
    	cin >> r >> m;
    	Complex c2(r, m);
    	Complex c3 = c1+c2;
    	c3.Display();
    	c3 = c1-c2;
    	c3.Display();
    	c3 -= c1;
    	c3.Display();
    	return 0;
    }
    

      

     1 #include<iostream>
     2 using namespace std;
     3 
     4 class Complex
     5 {
     6     public:
     7         Complex(double r=0,double i=0):real(r), imag(i){}
     8         Complex operator+(Complex &) const;//重载双目运算符'+'
     9         Complex operator-=(Complex &); //重载双目运算符'-='
    10         friend  const Complex operator-(Complex &,Complex &);//重载双目运算符'-'
    11         void Display() const;
    12     private:
    13         double real;
    14         double imag;
    15 };
    16 
    17 Complex Complex::operator + (Complex &C) const
    18 {
    19     return Complex(real+C.real,imag+C.imag);        //重载 + 返回值为相加之后的结果
    20 }
    21 
    22 Complex Complex::operator-=(Complex &C)
    23 {
    24     real=real-C.real;
    25     imag=imag-C.imag;
    26     return Complex(real,imag);          //重载 -= 返回值为减后结果,且对象本身数值也发生变化
    27 }
    28 
    29 Complex const operator - (Complex &C1,Complex &C2)  //重载 - 返回值为相减后的结果
    30 {
    31     return Complex(C1.real-C2.real,C1.imag-C2.imag);
    32 }
    33 
    34 void Complex::Display() const
    35 {
    36     cout << "(" << real << ", " << imag << ")" << endl;
    37 }
    38 
    39 int main()
    40 {
    41     double r, m;
    42     cin >> r >> m;
    43     Complex c1(r, m);
    44     cin >> r >> m;
    45     Complex c2(r, m);
    46     Complex c3 = c1+c2;
    47     c3.Display();
    48     c3 = c1-c2;
    49     c3.Display();
    50     c3 -= c1;
    51     c3.Display();
    52     return 0;
    53 }
  • 相关阅读:
    AtCoder Beginner Contest 157
    AtCoder Beginner Contest 158 BCD
    1046 A^B Mod C
    AtCoder Beginner Contest 150
    Aizu
    【建图】【思维】拓扑排序建图+序列自动机——swerc Gym
    【思维】单调栈——cf1366E
    【数学】gcd数论——cf1366D
    【思维】cf 1365 DEF
    【思维】构造——ICPC NEAU B
  • 原文地址:https://www.cnblogs.com/wzzdeblog/p/10722786.html
Copyright © 2020-2023  润新知