C++練習問題Complex Number(Eden)

9770 ワード

Question:
Create a class called Complex for performing arithmetic with complex numbers.
Complex numbers have the form
realPart + imaginaryPart * i
    where i is √-1

Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in case no initializers are provided.
Provide public member functions that perform the following tasks:
a) Adding two Complex numbers: The real parts are added together and the imaginary parts are added together.
b) Subtracting two Complex numbers: The real part of the right operand is subtracted from the real part of the left operand and the imaginary part of the right operand is subtracted from the imaginary part of the left operand.
c) Printing Complex numbers in the form (a, b) where a is the real part and b is the imaginary part.
Hint:

Complex::Complex( double real, double imaginary ) {
 setComplexNumber( real, imaginary );
 } // end Complex constructor

Complex Complex::add( const Complex &right ) {
 /* Write a statement to return a Complex object. Add
  the realPart of right to the realPart of this Complex
  object and add the imaginaryPart of right to the
  imaginaryPart of this Complex object */
} // end function add

Complex Complex::subtract( const Complex &right ) {
 /* Write a statement to return a Complex object. Subtract
  the realPart of right from the realPart of this Complex
  object and subtract the imaginaryPart of right from
  the imaginaryPart of this Complex object */
} // end function subtract

void Complex::printComplex() {
 cout << '(' << realPart << ", " << imaginaryPart << ')';
} // end function printComplex

void Complex::setComplexNumber( double rp, double ip ) {
 realPart = rp;
 imaginaryPart = ip;
} // end function setComplexNumber

Sample input:
1 2 3 4 5 6 7 8
Sample output:
(1, 2) + (3, 4) = (4, 6) (5, 6) - (7, 8) = (-2, -2)
main.cpp
#include 
using std::cin;
using std::cout;
using std::endl;

#include "Complex.hpp"

int main() {
    int n[8];
    cin >> n[0] >> n[1] >> n[2] >> n[3];
    cin >> n[4] >> n[5] >> n[6] >> n[7];
    Complex a(n[0], n[1]), b(n[2], n[3]), c;
    // create three Complex objects

    a.printComplex();  // output object a
    cout << " + ";
    b.printComplex();  // output object b
    cout << " = ";
    c = a.add(b);
    // invoke add function and assign to object c
    c.printComplex();  // output object c

    cout << '
'
; a.setComplexNumber(n[4], n[5]); // reset realPart and b.setComplexNumber(n[6], n[7]); // and imaginaryPart a.printComplex(); // output object a cout << " - "; b.printComplex(); // output object b cout << " = "; c = a.subtract(b); // invoke add function and assign to object c c.printComplex(); // output object c cout << endl; } // end main

Complex.hpp*
using namespace std;
class Complex {

    public :
    Complex(double ,double);  //          
    /*       ,    ,              */
    Complex()      //         
    {
         realPart = 0.0;
         imaginaryPart = 0.0;
       };
    void printComplex();
    void setComplexNumber( double, double );
    Complex add(const Complex &);
    Complex subtract(const Complex &);

    private :
    double realPart;
    double imaginaryPart;

 };

Complex.cpp*
#include "Complex.hpp"
#include 
using namespace std;
using std::cout;
using std::endl;

Complex::Complex( double real, double imaginary ) {
 setComplexNumber( real, imaginary );
 }

Complex Complex::add( const Complex &right ) {
 realPart += right.realPart;
 imaginaryPart += right.imaginaryPart;
 return *this;
}

Complex Complex::subtract( const Complex &right ) {
 realPart -= right.realPart;
 imaginaryPart -= right.imaginaryPart;
 return *this;
} 

void Complex::printComplex() {
 cout << '(' << realPart << ", " << imaginaryPart << ')';
} 

void Complex::setComplexNumber( double rp, double ip ) {
 realPart = rp;
 imaginaryPart = ip;
} 

http://blog.csdn.net/tiantang46800/article/details/6938762//パラメータなしコンストラクション関数//クラスを作成してコンストラクション関数を書かないと、デフォルトの非パラメトリックコンストラクション関数が自動的に生成され、関数は空で、何もしません//次のコンストラクション関数を書いている限り、システムはこのようなデフォルトのコンストラクション関数を自動的に生成しません.このような非パラメトリックコンストラクション関数を望んでいる場合は、自分で表示して書く必要があります.//一般コンストラクタ(リロードコンストラクタとも呼ばれる)/一般コンストラクタには様々なパラメータ形式があり、1つのクラスに複数の一般コンストラクタがあり、パラメータの個数やタイプが異なること(c++ベースのリロード関数の原理)/たとえば、Complex(int num)のコンストラクション関数を書くこともできます//オブジェクトを作成するときに、入力したパラメータに応じて異なるコンストラクション関数を呼び出すこともできます