C++;复数运算的运算符重载 我在C++中有一个任务,而且我开始有困难。我们的目标是“设计一个使用以下重载运算符处理复数的类:>>
在我看来,重点是演示类操作重载,所以我认为我们的想法是让一个类变得复杂,它包含实数和虚数的信息(I表示它是虚数).在运算符重写中处理复数之间的各种操作 一旦你有了它,并且你看到它工作了(做一个静态测试方法,进行各种操作并将结果打印到屏幕上),然后担心使用该类处理输入,因为解析输入本身就是另一项任务。有时,将问题划分为更小的问题要比同时尝试两者更简单C++;复数运算的运算符重载 我在C++中有一个任务,而且我开始有困难。我们的目标是“设计一个使用以下重载运算符处理复数的类:>>,c++,operator-overloading,complex-numbers,C++,Operator Overloading,Complex Numbers,在我看来,重点是演示类操作重载,所以我认为我们的想法是让一个类变得复杂,它包含实数和虚数的信息(I表示它是虚数).在运算符重写中处理复数之间的各种操作 一旦你有了它,并且你看到它工作了(做一个静态测试方法,进行各种操作并将结果打印到屏幕上),然后担心使用该类处理输入,因为解析输入本身就是另一项任务。有时,将问题划分为更小的问题要比同时尝试两者更简单 希望有帮助。祝你好运!他们喜欢成对的价值观: A = N1 + I1i B = N2 + I2i A + B = (N1 + I1i) + (N
希望有帮助。祝你好运!他们喜欢成对的价值观:
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的类,该类至少包括:
- 一种构造函数,允许您从实部和虚部数值构造复杂对象,例如复数(1,5)
- 重写+运算符,以便可以添加两个复杂对象,并返回一个新的复杂对象,例如复杂(1,5)+复杂(3,7)是复杂(4,12)
- 其他运营商也是如此
但首先,您需要了解复数背后的基本数学,以便编写运算符重载方法。要完成此任务,您必须做几件事: 定义可以保存复数实部和虚部数据的类(例如复数) 使各操作员过载(例如): 执行相应的运算符以实际执行数学运算(例如):
希望你现在完成作业:)如果还有人需要帮助,这是我的解决方案
#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;
}
#包括
#包括
#包括
使用名称空间std;
阶级情结{
浮点实数,虚数;
公众:
复杂(浮动,浮动);
复数运算符=(常数复数和rhs);
复数运算符+(常数复数和rhs)常数;
复算子-(const Complex&rhs)const;
复数运算符*(常数复数和rhs)常数;
字符串toString()常量;
};
复数::复数(浮点r,浮点i){
真实=r;
假想的ui=i;
}
复数::运算符=(常数复数和rhs){
real_uu=rhs.real;
假想的=rhs.假想的;
归还*这个;
}
复数::运算符+(常数复数和rhs)常数{
复杂结果=*这;
result.real_u+=rhs.real_u+;
result.virtual u+=rhs.virtual u+;
返回结果;
}
复数复数::运算符-(常数复数和rhs)常数{
复杂结果=*这;
result.real-=rhs.real-;
result.virtual-=rhs.virtual;
返回结果;
}
复数复数::运算符*(常数复数和rhs)常数{
复杂结果=*this;//this->==*this==(*this)
result.real u=real*rhs.real uu-virtual*rhs.virtual u;
//库特
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;
}