C++复数运算的运算符重载

发布于 2024-10-06 02:07:27 字数 562 浏览 4 评论 0原文

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

I have an assignment in C++ and I'm having trouble getting started. The goal is to "design a class that uses the following overloaded operators for complex numbers: >> << + - * / "

My question isn't about the syntax of this, but more about the logic. I could use some help brain storming.

Input Sample:
2.5 -2.2
1.0 1.0

OutPut Sample:
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 = ..............

So how do I start this? The class "Complex" overloads these operators, so does that mean that I can only use these operators in the class (i.e. inside public functions)? If so would I want to do it this way? Or would I want to do it in my client/driver code?

Second, is it just adding i to the second value of each line? That seems too easy. Any direction would be much appreciated. (Just for the record, I'm not looking for anybody to do my homework for me... could just use some input)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

万水千山粽是情ミ 2024-10-13 02:07:27

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

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

It seems to me that the point is to demonstrate class operation overloading, so I think the idea is for you to make a class Complex which holds information about the real number and the imaginary number (the i means it's imaginary). Handle various operations between complex numbers in operator overrides you do yourself.

Once you have that and you see that it works (make a static test method which does various operations and prints the results to the screen), then worry about using that class to work with input since parsing input will be another task in of itself. Sometimes it's just simpler to divide problems into smaller problems than to attempt to do both at the same time.

红ご颜醉 2024-10-13 02:07:27

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

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

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

  • < p>对于其他运算符类似

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

You need to design a class called Complex that includes at least:

  • a constructor allowing you to construct a Complex object from real and imaginary component values e.g. Complex(1, 5)

  • overrides the + operator so that you can add two Complex objects, returning a new Complex object e.g. Complex(1, 5) + Complex(3, 7) is Complex(4, 12)

  • similarly for other operators

But first you need to understand the basic math behind complex numbers so that you can write the operator overload methods.

万劫不复 2024-10-13 02:07:27

他们喜欢成对的值:

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.

They like pairs of values:

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.
始终不够 2024-10-13 02:07:27

要完成此任务,您必须做一些事情:

定义一个类(例如 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;
}

There are a few things you have to do to accomplish this task:

Define a class (e.g. Complex) that can hold the data for the real and imaginary part of a complex number.

Overload the respective operators (e.g.):

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

Implement the respective operators to actually perform the mathematical operation (e.g.):

Complex Complex::operator+ (const Complex& rhs) const
{
    Complex result = *this;
    result.Real += rhs.Real;
    result.Imaginary += rhs.Imaginary;
    return result;
}
帅气称霸 2024-10-13 02:07:27

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

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

Hope you are done with homework now :) Here is my solution if anyone still needs help.

#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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文