C++ 复数C+的加法、乘法、除法和减法问题+;-运算符重载
我写了一个处理复数的程序——写,读,做一些简单的操作。 我的老师说我必须定义许多运算符(十八个)。我和一个接线员有问题C++ 复数C+的加法、乘法、除法和减法问题+;-运算符重载,c++,oop,operator-overloading,operators,complex-numbers,C++,Oop,Operator Overloading,Operators,Complex Numbers,我写了一个处理复数的程序——写,读,做一些简单的操作。 我的老师说我必须定义许多运算符(十八个)。我和一个接线员有问题 Comp operator=(const Comp x) { Comp temp; temp.real = re(); temp.imag = im(); return temp; } 当我在代码中使用这个操作符时,加法、乘法、除法和减法都不起作用。 我的输出中有 (1.10,2.00)
Comp operator=(const Comp x)
{
Comp temp;
temp.real = re();
temp.imag = im();
return temp;
}
当我在代码中使用这个操作符时,加法、乘法、除法和减法都不起作用。
我的输出中有
(1.10,2.00)
(1.70,3.14)
2.28
3.57
1.07
1.07
(1.10,-2.00)
(1.70,-3.14)
(0.00,0.00)
(0.00,0.00)
(0.00,0.00)
(0.00,0.00)
而不是
(1.10,2.00)
(1.70,3.14)
2.28
3.57
1.07
1.07
(1.10,-2.00)
(1.70,-3.14)
(2.80,5.14)
(-0.60,-1.14)
(-4.41,6.85)
(0.64,-0.00)
这是我的全部密码
#include <fstream>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cmath>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
using namespace std;
namespace ComplexNumbers
{
class Comp {
double real, imag;
public:
Comp(){
real=0;
imag=0;
}
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;
temp.real = re();
temp.imag = -im();
return temp;
}
~Comp(){}
const Comp operator+();
const Comp operator-();
bool operator!(void);
const Comp& operator++()
{
return *this;
}
const Comp operator++(int)
{
Comp temp(*this);
operator++();
return temp;
}
const Comp& operator--()
{
return *this;
}
const Comp operator--(int)
{
Comp temp(*this);
operator--();
return temp;
}
Comp operator=(const Comp x)
{
Comp temp;
temp.real = re();
temp.imag = im();
return temp;
}
Comp& operator-=(const Comp& x)
{
int value = 0;
value -= x.real;
value -= x.imag;
return *this;
}
Comp& operator+=(const Comp& x)
{
int value = 0;
value += x.real;
value += x.imag;
return *this;
}
Comp& operator*=(const Comp& x)
{
int value = 0;
value *= x.real;
value *= x.imag;
return *this;
}
Comp& operator/=(const Comp& x)
{
int value = 0;
value /= x.real;
value /= x.imag;
return *this;
}
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 - x.imag * y.imag);
temp.imag = (x.real * y.imag + x.imag * y.real);
return temp;
}
friend const Comp operator/(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = ((x.real * y.real) + (x.imag * y.imag))/(y.real*y.real + y.imag*y.imag);
temp.imag = ((x.imag * y.real) - (x.real * y.imag))/(y.real*y.real + y.imag*y.imag);
return temp;
}
friend bool operator==(const Comp& x, const Comp& y)
{
if (x.real == y.real && x.imag == y.imag)
return 1;
else
return 0;
}
friend bool operator!=(const Comp& x, const Comp& y)
{
if (x.real != y.real || x.imag != y.imag)
return 1;
else
return 0;
}
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 = '0';
return wart2>>c>>b.real>>c>>b.imag>>c;
}
};
}
using namespace ComplexNumbers;
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;
}
#包括
#包括
#包括
#包括
#包括
#伊夫德夫·穆皮
#定义M_PI 3.14159265358979323846
#恩迪夫
使用名称空间std;
名称空间复数
{
班级薪酬{
双实数,imag;
公众:
Comp(){
实数=0;
imag=0;
}
双重(空)常数
{
回归真实;
}
双im(无效)常数
{
返回图像;
}
双模(空)常数
{
返回sqrt(re()*re()+im()*im());
}
双参数(无效)常数
{
双faza;
如果(im()>=0)
faza=acos(re()/mod());
其他的
faza=2*M_PI-acos(re()/mod());
返回法扎;
}
常量Comp conj(void)常量
{
补偿温度;
temp.real=re();
temp.imag=-im();
返回温度;
}
~Comp(){}
常量Comp运算符+();
常量Comp运算符-();
布尔运算符!(无效);
常量Comp&运算符++()
{
归还*这个;
}
常量Comp运算符++(int)
{
补偿温度(*本);
运算符++();
返回温度;
}
常量组件和运算符--()
{
归还*这个;
}
常量Comp运算符--(int)
{
补偿温度(*本);
运算符——();
返回温度;
}
复合运算符=(常数复合x)
{
补偿温度;
temp.real=re();
temp.imag=im();
返回温度;
}
组件和运算符-=(常量组件和x)
{
int值=0;
值-=x.real;
值-=x.imag;
归还*这个;
}
组件和运算符+=(常数组件和x)
{
int值=0;
值+=x.real;
值+=x.imag;
归还*这个;
}
组件和运算符*=(常数组件和x)
{
int值=0;
值*=x.real;
值*=x.imag;
归还*这个;
}
Comp&operator/=(常数Comp&x)
{
int值=0;
值/=x.real;
值/=x.imag;
归还*这个;
}
友元常量Comp运算符+(常量Comp&x、常量Comp&y)
{
补偿温度;
temp.real=x.real+y.real;
温度imag=x.imag+y.imag;
返回温度;
}
friend const Comp操作符-(const Comp&x,const Comp&y)
{
补偿温度;
temp.real=x.real-y.real;
温度imag=x.imag-y.imag;
返回温度;
}
friend const Comp operator*(const Comp&x、const Comp&y)
{
补偿温度;
temp.real=(x.real*y.real-x.imag*y.imag);
temp.imag=(x.real*y.imag+x.imag*y.real);
返回温度;
}
友元常量Comp运算符/(常量Comp&x,常量Comp&y)
{
补偿温度;
temp.real=((x.real*y.real)+(x.imag*y.imag))/(y.real*y.real+y.imag*y.imag);
temp.imag=((x.imag*y.real)-(x.real*y.imag))/(y.real*y.real+y.imag*y.imag);
返回温度;
}
友元布尔运算符==(常数Comp&x,常数Comp&y)
{
if(x.real==y.real&&x.imag==y.imag)
返回1;
其他的
返回0;
}
友元布尔运算符!=(常数Comp&x,常数Comp&y)
{
if(x.real!=y.real | | x.imag!=y.imag)
返回1;
其他的
返回0;
}
friend std::奥斯特雷姆和运营商;
}
};
}
使用名称空间复数;
int main(int argc,char*argv[])
{
ifstream读取(argv[1]);
如果(!读取)
{cerr您的复制赋值操作符应该赋值给对象本身,如下所示:
Comp &operator=(const Comp x)
{
real = x.real;
imag = x.imag;
return *this;
}
还请注意函数签名的更改
许多其他操作符(operator-=
、operator+=
、operator*=
和operator/=
)也遇到了同样的问题,您还应该提供一个复制构造函数(“规则3”)。您的复制分配操作符应该分配给对象本身,如下所示:
Comp &operator=(const Comp x)
{
real = x.real;
imag = x.imag;
return *this;
}
还请注意函数签名的更改
您的许多其他运算符(运算符-=
、运算符+=
、运算符*=
和运算符/=
)也遇到同样的问题,您还应该提供一个副本构造函数(“规则3”).非常感谢。你是天才!@Mark Harmond。我算是这个网站的新手。非常感谢。你是天才!@Mark Harmond。我算是这个网站的新手。