⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 9-2-1.cpp

📁 学习c++的ppt
💻 CPP
字号:
#include <iostream.h>
class Complex  {
    private:
        double  real, image;    // 复数的实部和虚部
    public:
        Complex()  { real = image = 0.0; }
        Complex(double  r)  {real = r; image = 0.0; }
        Complex(double r, double i)  
        {  real = r; image = i; };
        Complex operator + (const Complex &c);
        Complex operator - (const Complex &c);
        Complex operator * (const Complex &c);
		Complex operator / (const Complex &c);
        Complex operator - ();
        friend  void  print(const Complex &c);
};
inline  Complex Complex::operator + (const Complex &c)
{  return Complex(real + c.real, image + c.image);
}
inline  Complex Complex::operator - (const Complex &c)
{  return Complex(real - c.real, image - c.image);
}
inline  Complex Complex::operator * (const Complex &c)
{  return Complex(real * c.real - image * c.image,
                                 real * c.image + image * c.real);
}
inline  Complex Complex::operator / (const Complex &c)
{  return Complex((real * c.real +image * c.image) /
                                 (c.real * c.real + c.image * c.image),
                                 (image * c.real + real * c.image) /
                                 (c.real * c.real + c.image * c.image));
}
inline  Complex Complex::operator - ()
{  return Complex( -real, -image);
}
void print(const Complex &c)
{   if(c.image < 0) cout <<c.real<<" - "<< -c.image<<" i" ;
    else cout <<c.real<<" + "<<c.image<<" i" ;
    cout << endl;
}
void main()
{  Complex c1(2.5),  c2(3.6, -1.2), c3;
   c3 = c1 + c2;
   cout << "c1+c2 = ";    print(c3);
   c3 = c1 - c2;
   cout << "c1-c2 = ";      print(c3);
   c3 = c1 * c2;
   cout << "c1*c2 = ";     print(c3);
   c3 = c1 / c2;
   cout << "c1/c2 = ";      print(c3);
   c3 = -c2;
   cout << " - c2 = ";         print(c3);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -