簡體   English   中英

C ++運算符重載用於復數運算

[英]C++ operator overloading for complex number operations

我有一個C ++作業,我很難入門。 目標是“為復數設計一個使用以下重載運算符的類:>> << - - * /”

我的問題不是關於這個的語法,而是關於邏輯的更多問題。 我可以用一些幫助頭腦風暴。

輸入樣本:
2.5 -2.2
1.0 1.0

OutPut示例:
A =(2.5)+(-2.2)i
B =(1.0)+(1.0)i

A + B =(3.5)+( - 1.2)i
A - B = ..............
A * B = ..............
A / B = ..............

那么我該如何開始呢? “Complex”類重載了這些運算符,這是否意味着我只能在類中使用這些運算符(即公共函數內部)? 如果是這樣,我想這樣做嗎? 或者我想在我的客戶端/驅動程序代碼中執行此操作?

第二,它只是將i添加到每行的第二個值? 這似乎太容易了。 任何方向都會非常感激。 (僅供記錄,我不是要找任何人為我做功課......可以只使用一些輸入)

在我看來,重點是演示類操作重載,所以我認為這個想法是你創建一個類Complex,它包含有關實數和虛數的信息(我的意思是它的想象)。 在您自己完成的操作員覆蓋中處理復雜數字之間的各種操作。

一旦你有了它並且你看到它工作(做一個靜態測試方法,它做各種操作並將結果打印到屏幕上),然后擔心使用該類來處理輸入,因為解析輸入本身就是另一個任務。 有時將問題划分為較小的問題比嘗試同時進行兩者更簡單。

希望有所幫助。 祝好運!

他們喜歡成對的價值觀:

A = N1 + I1i
B = N2 + I2i


A + B = (N1 + I1i) + (N2 + I2i)
      = N1 + I1i + N2 + I2i
      = (N1 + N2) + (I1i + I2i)
      = (N1 + N2) + (I1 + I2)i
A - B = (N1 + I1i) - (N2 + I2i)
      = N1 + I1i - N2 - I2i
      = (N1 - N2) + (I1i - I2i)
      = (N1 - N2) + (I1 - I2)i

// N1, N2, I1, I2 are all just normal numbers.
// You can multiply them like normal. You just have to keep track of the `i`
// Also not that i = sqrt(-1)
// Therefore  i * i = sqrt(-1) * sqrt(-1)
//                  = sqrt(-1)^2
//                  = -1
A * B = (N1 + I1i) * (N2 + I2i)
      = (N1 * N2) + (N1 * I2i) + (I1i * N2) + (I1i * I2i)
      = (N1 * N2) + (N1 * I2)i + (N2 * I1)i + (i * i * I1 * I2)
      = (N1 * N2) + i((N1 * I2) + (N2 * I1)) + (-1 * I1 * I2)

      // Simplest form
      = ((N1 * N2) - (I1 * I2)) + ((N1 * I2) + (N2 * I1))i


A / B = Repeat as above.

您需要設計一個名為Complex的類,至少包括:

  • 一個構造函數,允許您從實部和虛部組件值構造一個Complex對象,例如Complex(1,5)

  • 覆蓋+運算符,以便添加兩個Complex對象,返回一個新的Complex對象,例如Complex(1,5)+ Complex(3,7)是Complex(4,12)

  • 其他運營商同樣如此

但首先,您需要了解復數背后的基本數學,以便您可以編寫運算符重載方法。

要完成此任務,您需要執行以下操作:

定義一個類(例如Complex),它可以保存復數的實部和虛部的數據。

超載相應的運營商(例如):

class Complex
{
public:
    // other declarations here
    Complex operator+ (const Complex& rhs) const;
    // other stuff here
};

實現相應的運算符以實際執行數學運算(例如):

Complex Complex::operator+ (const Complex& rhs) const
{
    Complex result = *this;
    result.Real += rhs.Real;
    result.Imaginary += rhs.Imaginary;
    return result;
}

希望你現在完成家庭作業:)如果有人還需要幫助,這是我的解決方案。

#include <string>
#include <sstream>
#include <iostream>

using namespace std;


class Complex {
    float real_, imaginary_;
  public:
    Complex (float, float);
    Complex operator= (const Complex& rhs);
    Complex operator+ (const Complex& rhs) const;
    Complex operator- (const Complex& rhs) const;
    Complex operator* (const Complex& rhs) const;
    string toString() const;
};

Complex::Complex (float r, float i){
  real_ = r;
  imaginary_ = i;
}

Complex Complex::operator= (const Complex& rhs){
    real_ = rhs.real_;
    imaginary_ = rhs.imaginary_;
    return *this;
}

Complex Complex::operator+ (const Complex& rhs) const{
    Complex result = *this;
    result.real_ += rhs.real_;
    result.imaginary_ += rhs.imaginary_;
    return result;
}

Complex Complex::operator- (const Complex& rhs) const{
    Complex result = *this;
    result.real_ -= rhs.real_;
    result.imaginary_ -= rhs.imaginary_;
    return result;
}

Complex Complex::operator* (const Complex& rhs) const{
    Complex result = *this; // this-> == *this == (*this)
    result.real_ = real_ * rhs.real_ - imaginary_ * rhs.imaginary_;
    //cout << result.real_ << "R " << result.imaginary_ << "I "<< "|" << rhs.real_ << "R " << rhs.imaginary_ << "I\n";
    result.imaginary_ = (real_ * rhs.imaginary_) + (rhs.real_ * imaginary_);
    //cout << result.real_ << "R " << result.imaginary_ << "I "<< "|" << rhs.real_ << "R " << rhs.imaginary_ << "I\n";
    return result;
}

string Complex::toString() const {
  stringstream ss;
  if (imaginary_ > 0){
    ss << real_ << " + " << imaginary_ << "i";
  }
  else {
    ss << real_ << " " << imaginary_ << "i";
  }
  return ss.str();
}

int main () {
  Complex a(5, 6);
  Complex b(1, 4);

  Complex sum = a + b;
  Complex dif = a - b;
  Complex pro = a * b;

  cout << "sum: " << sum.toString() << "\n";
  cout << "difference: " << dif.toString() << "\n";
  cout << "product: " << pro.toString() << "\n";

  return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM