重载运算符 C++,复数?
Overloading operators C++, complex numbers?
我有这么大的问题。我写了一个关于复数的程序。该程序读写复数,将它们相加等。
他说我应该读 Why should I overload a C++ operator as a global function (STL does) and what are the caveats? 和
1) 我必须创建 5 个运算符,它们是 class 的成员函数并且有一个参数:+、−、!、++、−−。那么
2) 我必须创建 5 个运算符,它们是 class 的成员函数,并且有两个参数:=、+=、−=、*=、/=;那么
3) 我必须创建 8 个运算符,它们是全局友元函数 +、-、*、/、==、!=、<<、>> 并带有两个参数。最后一个没问题:
friend const Comp operator+(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real + y.real;
temp.imag = x.imag + y.imag;
return temp;
}
friend const Comp operator-(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real - y.real;
temp.imag = x.imag - y.imag;
return temp;
}
friend const Comp operator*(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real * y.real;
temp.imag = x.imag * y.imag;
return temp;
}
friend const Comp operator/(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real / y.real;
temp.imag = x.imag / y.imag;
return temp;
}
除了这个??????我这里应该return什么?????我比较的时候应该是bool呀????
friend const Comp operator==(const Comp& x, const Comp& y)
{
}
friend const Comp operator!=(const Comp& x, const Comp& y)
{
}
我想我找到了解决方案
friend bool operator==(const Comp& x, const Comp& y)
{
return (x.real == y.real && x.imag == y.imag);
}
friend bool operator!=(const Comp& x, const Comp& y)
{
return (x.real != y.real && x.imag != y.imag);
}
这是我的全部代码
#include <fstream>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cmath>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
using namespace std;
class Comp {
double real, imag;
public:
Comp(){
real;
imag;
}
double re(void) const
{
return real;
}
double im(void) const
{
return imag;
}
double mod(void) const
{
return sqrt(re()*re() + im()*im());
}
double arg(void) const
{
double faza;
if (im() >= 0)
faza = acos(re()/mod());
else
faza = 2*M_PI - acos(re()/mod());
return faza;
}
const Comp conj(void) const
{
Comp temp;
-im();
return temp;
}
~Comp(){}
/*
Comp operator+(const Comp& x);
Comp operator-(const Comp& x);
bool operator!(void);
const Comp& operator++();
const Comp operator++(int);
const Comp& operator--();
const Comp operator--(int);
Comp operator=(const Comp x);
Comp operator-=(const Comp& x);
Comp operator+=(const Comp& x);
Comp operator*=(const Comp& x);
Comp operator/=(const Comp& x);
*/
friend const Comp operator+(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real + y.real;
temp.imag = x.imag + y.imag;
return temp;
}
friend const Comp operator-(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real - y.real;
temp.imag = x.imag - y.imag;
return temp;
}
friend const Comp operator*(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real * y.real;
temp.imag = x.imag * y.imag;
return temp;
}
friend const Comp operator/(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real / y.real;
temp.imag = x.imag / y.imag;
return temp;
}
friend const Comp operator==(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real + y.real;
temp.imag = x.imag + y.imag;
return temp;
}
friend const Comp operator!=(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real + y.real;
temp.imag = x.imag + y.imag;
return temp;
}
friend std::ostream& operator<<(std::ostream& wart1, const Comp& a)
{
return wart1 <<fixed << setprecision(2) << '(' << a.re() << "," << a.im() << ')' << ' ' << endl;
}
friend std::istream& operator>>(std::istream& wart2, Comp& b){
char c;
return wart2>>c>>b.real>>c>>b.imag>>c;
}
};
int main(int argc, char* argv[])
{
ifstream read(argv[1]);
if (!read)
{ cerr << "Open error: " << argv[1] << endl; exit(1);}
ofstream write(argv[2]);
if(!write) { cerr << "Open error: " << argv[2] << endl; exit(2);}
read.clear();
read.seekg(0);
Comp x1;
read >> x1;
write << x1;
cout << x1;
Comp x2;
read >> x2;
write << x2;
cout << x2;
cout << x1.mod() << endl;
cout << x2.mod() << endl;
cout << x1.arg() << endl;
cout << x2.arg() << endl;
cout << x1.conj();
cout << x2.conj();
write << x2;
write << x1.mod() << endl;
write << x2.mod() << endl;
write << x1.arg() << endl;
write << x2.arg() << endl;
write << x1.conj();
write << x2.conj();
Comp sum;
sum = x1 + x2;
cout << sum;
write << sum;
Comp sub;
sub = x1 - x2;
cout << sub;
write << sub;
Comp mult;
mult = x1 * x2;
cout << mult;
write << mult;
Comp div;
div = x1 / x2;
cout << div;
write << div;
return 0;
}
如果您正确地实现了 operator==
(您在标题为 "I think that I found solution" 的块中所做的),那么您可以将其用于 operator!=
:
friend bool operator!=(const Comp& x, const Comp& y)
{
return !(x == y);
}
您的版本不正确,因为如果它们具有相同的实部和不同的虚部,它会报告它们不是不相等的。
此外,在第 (1) 部分中,它指的是一元 + 和 -(不是我们在第 3 部分中得到的二进制版本)。所以你在 commented-out 块中的前两个声明是不正确的,它们应该是:
Comp operator+();
Comp operator-();
我有这么大的问题。我写了一个关于复数的程序。该程序读写复数,将它们相加等。 他说我应该读 Why should I overload a C++ operator as a global function (STL does) and what are the caveats? 和
1) 我必须创建 5 个运算符,它们是 class 的成员函数并且有一个参数:+、−、!、++、−−。那么
2) 我必须创建 5 个运算符,它们是 class 的成员函数,并且有两个参数:=、+=、−=、*=、/=;那么
3) 我必须创建 8 个运算符,它们是全局友元函数 +、-、*、/、==、!=、<<、>> 并带有两个参数。最后一个没问题:
friend const Comp operator+(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real + y.real;
temp.imag = x.imag + y.imag;
return temp;
}
friend const Comp operator-(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real - y.real;
temp.imag = x.imag - y.imag;
return temp;
}
friend const Comp operator*(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real * y.real;
temp.imag = x.imag * y.imag;
return temp;
}
friend const Comp operator/(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real / y.real;
temp.imag = x.imag / y.imag;
return temp;
}
除了这个??????我这里应该return什么?????我比较的时候应该是bool呀????
friend const Comp operator==(const Comp& x, const Comp& y)
{
}
friend const Comp operator!=(const Comp& x, const Comp& y)
{
}
我想我找到了解决方案
friend bool operator==(const Comp& x, const Comp& y)
{
return (x.real == y.real && x.imag == y.imag);
}
friend bool operator!=(const Comp& x, const Comp& y)
{
return (x.real != y.real && x.imag != y.imag);
}
这是我的全部代码
#include <fstream>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cmath>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
using namespace std;
class Comp {
double real, imag;
public:
Comp(){
real;
imag;
}
double re(void) const
{
return real;
}
double im(void) const
{
return imag;
}
double mod(void) const
{
return sqrt(re()*re() + im()*im());
}
double arg(void) const
{
double faza;
if (im() >= 0)
faza = acos(re()/mod());
else
faza = 2*M_PI - acos(re()/mod());
return faza;
}
const Comp conj(void) const
{
Comp temp;
-im();
return temp;
}
~Comp(){}
/*
Comp operator+(const Comp& x);
Comp operator-(const Comp& x);
bool operator!(void);
const Comp& operator++();
const Comp operator++(int);
const Comp& operator--();
const Comp operator--(int);
Comp operator=(const Comp x);
Comp operator-=(const Comp& x);
Comp operator+=(const Comp& x);
Comp operator*=(const Comp& x);
Comp operator/=(const Comp& x);
*/
friend const Comp operator+(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real + y.real;
temp.imag = x.imag + y.imag;
return temp;
}
friend const Comp operator-(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real - y.real;
temp.imag = x.imag - y.imag;
return temp;
}
friend const Comp operator*(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real * y.real;
temp.imag = x.imag * y.imag;
return temp;
}
friend const Comp operator/(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real / y.real;
temp.imag = x.imag / y.imag;
return temp;
}
friend const Comp operator==(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real + y.real;
temp.imag = x.imag + y.imag;
return temp;
}
friend const Comp operator!=(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real + y.real;
temp.imag = x.imag + y.imag;
return temp;
}
friend std::ostream& operator<<(std::ostream& wart1, const Comp& a)
{
return wart1 <<fixed << setprecision(2) << '(' << a.re() << "," << a.im() << ')' << ' ' << endl;
}
friend std::istream& operator>>(std::istream& wart2, Comp& b){
char c;
return wart2>>c>>b.real>>c>>b.imag>>c;
}
};
int main(int argc, char* argv[])
{
ifstream read(argv[1]);
if (!read)
{ cerr << "Open error: " << argv[1] << endl; exit(1);}
ofstream write(argv[2]);
if(!write) { cerr << "Open error: " << argv[2] << endl; exit(2);}
read.clear();
read.seekg(0);
Comp x1;
read >> x1;
write << x1;
cout << x1;
Comp x2;
read >> x2;
write << x2;
cout << x2;
cout << x1.mod() << endl;
cout << x2.mod() << endl;
cout << x1.arg() << endl;
cout << x2.arg() << endl;
cout << x1.conj();
cout << x2.conj();
write << x2;
write << x1.mod() << endl;
write << x2.mod() << endl;
write << x1.arg() << endl;
write << x2.arg() << endl;
write << x1.conj();
write << x2.conj();
Comp sum;
sum = x1 + x2;
cout << sum;
write << sum;
Comp sub;
sub = x1 - x2;
cout << sub;
write << sub;
Comp mult;
mult = x1 * x2;
cout << mult;
write << mult;
Comp div;
div = x1 / x2;
cout << div;
write << div;
return 0;
}
如果您正确地实现了 operator==
(您在标题为 "I think that I found solution" 的块中所做的),那么您可以将其用于 operator!=
:
friend bool operator!=(const Comp& x, const Comp& y)
{
return !(x == y);
}
您的版本不正确,因为如果它们具有相同的实部和不同的虚部,它会报告它们不是不相等的。
此外,在第 (1) 部分中,它指的是一元 + 和 -(不是我们在第 3 部分中得到的二进制版本)。所以你在 commented-out 块中的前两个声明是不正确的,它们应该是:
Comp operator+();
Comp operator-();