C++は一つの複素クラスを実現する

18655 ワード

要求
1つの複素クラスComplexを実現する.Complexクラスは、2つのdoubleタイプのメンバーrealおよびimageを含み、それぞれ複数の実部および虚部を表す.Complexクラスに対して、そのストリーム抽出、ストリーム挿入演算子、および加算減算4つの演算演算演算子を再ロードします.
リロードストリーム抽出演算子>>は、以下のフォーマットの演算子(2つの数値の間にアプリケーションを空に区切る)を読み取ることができ、1つ目の数値を複数の実部として、2つ目の数値を複数の虚部として保存する.
 -1.1 2.0
 +0 -4.5

リロードストリーム挿入演算子<<は、複素数を以下のフォーマット出力には、半
(-1.1+2.0i)
 (0-4.5i)

各複素数は、スペースで区切られた2つの浮動小数点数を含み、1番目の複素数を入力した後、キーを返し、2番目の複素数を入力し続けます.
2つの複素数を出力し、各複素数が1つを占める.複数は、𝞛𝞛かっこで囲まれた(a+bi)の形式である.注意全カッコは出力できません.
サンプル負け
-1.1 2.0
 0 -4.5 

サンプル出力
(-1.1+2i) (0-4.5i)
(-1.1-2.5i)
(-1.1+6.5i)
(9+4.95i)
(-0.444444-0.244444i)

ヒント
なお、複素数の4則演算は以下のように定義される.
  • 加算法則:(a+b i)+(c+d i)=(a+c)+(b+d)i(a+bi)+(c+di)=(a+c)+(b+d)i(a+bi)+(c+di)=(a+c)+(b+d)i
  • 減算法則:(a+b i)−(c+d i)=(a−c)+(b−d)i(a+bi)−(c+di)=(a−c)+(b−d)i(a+bi)−(c+di)=(a−c)+(b−d)i
  • 乗算法則:(a+b i)× ( c + d i ) = ( a c − b d ) + ( b c + a d ) i (a + bi) × (c + di) = (ac − bd) + (bc + ad)i (a+bi)×(c+di)=(ac−bd)+(bc+ad)i
  • 除法法則:(a+b i)÷(c+d i)=[(a c+b d)/(c 2+d 2)/(c 2+d 2)]+[(b c c c−a d)/(c 2+d 2)]i(a+bi)÷(c+di)=[( ac+ bd)/(c^2+d^2)]+[(bc−ad)/(c^2+d^2)/(c^2+d^2)]]i(a+bi)÷(c+di)=[( ac+ bd)/(c 2+d)+[(c 2+d)+[(c 2+d)]+[(((c 2+d)+[(c 2+d)]+[(((c 2+d))+bc−ad)/(c 2+d 2)]i
  • 2つのストリームオペレーション演算子は、Complexクラスの友元関数に再ロードする必要があります.
    また、出力時には、複数の虚部が、例えば、出力が3 1.0であるか否かを判断する必要があり、出力は3+1.0iであるべきである.この方法は、ostreamによって提供されるsetf()関数たとえば、次のコードについて説明します.
    ostream os;
    os.setf(std::ios::showpos);
    os << 12;
    

    出力内容は+12になります.
    前12207;の正号出力をキャンセルしたい場合は、次のことを実行できます.
    os.unsetf(std::ios::showpos);
    

    デフォルトの設定を復元できます(プラス記号は出力されません).
    コード実装
    #include 
    using namespace std;
    
    const double EPISON = 1e-7;
    class Complex
    {
         
    private:
      	double real;
      	double image;
    public:
      	Complex(const Complex& complex) :real{
          complex.real }, image{
          complex.image } {
         
    
      	}
      	Complex(double Real=0, double Image=0) :real{
          Real }, image{
          Image } {
         
    
      	}
      	//TODO
        Complex operator+(const Complex c) {
         
            return Complex(this->real + c.real, this->image + c.image);
        }
        
        Complex operator-(const Complex c) {
         
            return Complex(this->real - c.real, this->image - c.image);
        }
        
        Complex operator*(const Complex c) {
         
            double _real = this->real * c.real - this->image * c.image;
            double _image = this->image * c.real + this->real * c.image;
            return Complex(_real, _image);
        }
        
        Complex operator/(const Complex c) {
         
            double _real = (this->real * c.real + this->image * c.image) / (c.real * c.real + c.image * c.image);
            double _image = (this->image * c.real - this->real * c.image) / (c.real * c.real + c.image * c.image);
            return Complex(_real, _image);
        }
        friend istream &operator>>(istream &in, Complex &c);
        friend ostream &operator<<(ostream &out, const Complex &c);
    };
    
    //  >>
    istream &operator>>(istream &in, Complex &c) {
         
        in >> c.real >> c.image;
        return in;
    }
    
    //  <<
    ostream &operator<<(ostream &out, const Complex &c) {
         
        out << "(";
        //          0
        if (c.real >= EPISON || (c.real < EPISON && c.real > -EPISON)) out.unsetf(std::ios::showpos);
        out << c.real;
        out.setf(std::ios::showpos);
        out << c.image;
        out << "i)";
        return out;
    }
    
    int main() {
         
      	Complex z1, z2;
      	cin >> z1;
      	cin >> z2;
      	cout << z1 << " " << z2 << endl;
      	cout << z1 + z2 << endl;
      	cout << z1 - z2 << endl;
      	cout << z1*z2 << endl;
      	cout << z1 / z2 << endl;
      	return 0;
    }