【问题标题】:C++ operator overloading for complex number operations用于复数运算的 C++ 运算符重载
【发布时间】:2010-12-03 17:14:56
【问题描述】:

我有一个 C++ 作业,但我无法开始。目标是“设计一个对复数使用以下重载运算符的类:>>

我的问题不在于它的语法,而在于逻辑。我可以使用一些帮助头脑风暴。

输入样本:
2.5 -2.2
1.0 1.0

输出示例:
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 添加到每行的第二个值?这似乎太容易了。任何方向将不胜感激。 (只是为了记录,我不是在寻找任何人为我做作业......可以使用一些输入)

【问题讨论】:

    标签: c++ operator-overloading complex-numbers


    【解决方案1】:

    在我看来,重点是演示类操作重载,所以我认为这个想法是让您创建一个类 Complex 来保存有关实数和虚数的信息(i 表示它是虚数)。在您自己执行的运算符覆盖中处理复数之间的各种运算。

    一旦你有了它并且你看到它可以工作(创建一个静态测试方法来执行各种操作并将结果打印到屏幕上),然后担心使用该类来处理输入,因为解析输入将是另一个任务本身。有时,将问题分成更小的问题比尝试同时解决这两个问题更简单。

    希望对您有所帮助。祝你好运!

    【讨论】:

      【解决方案2】:

      您需要设计一个名为 Complex 的类,其中至少包括:

      • 一个构造函数,允许您从实部和虚部值构造一个 Complex 对象,例如复杂(1, 5)

      • 覆盖 + 运算符,以便您可以添加两个 Complex 对象,返回一个新的 Complex 对象,例如复数(1, 5) + 复数(3, 7) 是复数(4, 12)

      • 对于其他运算符也是如此

      但首先您需要了解复数背后的基本数学,以便您可以编写运算符重载方法。

      【讨论】:

        【解决方案3】:

        他们喜欢成对的价值观:

        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.
        

        【讨论】:

          【解决方案4】:

          要完成这项任务,您必须做几件事:

          定义一个可以保存复数实部和虚部数据的类(例如 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;
          }
          

          【讨论】:

            【解决方案5】:

            希望你现在已经完成了家庭作业 :) 如果有人仍然需要帮助,这是我的解决方案。

            #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;
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2012-12-22
              • 2016-09-26
              • 2012-04-21
              • 1970-01-01
              • 1970-01-01
              • 2012-11-26
              • 2016-02-19
              相关资源
              最近更新 更多