gpt4 book ai didi

c++ - 复数模板类

转载 作者:行者123 更新时间:2023-11-28 02:14:41 25 4
gpt4 key购买 nike

#ifndef COMPLEXNUMBER_H
#define COMPLEXNUMBER_H

#include <iostream>

using namespace std;

template <class T>
class ComplexNumber
{

friend ostream& operator<<(ostream&, const ComplexNumber&);
friend istream& operator>>(istream&, ComplexNumber&);
public:

void setComplexNumber(const T&, const T&);
T getComplexNumber(T&, T&) const;
ComplexNumber();
ComplexNumber(T real , T imaginary);
T operator+(const ComplexNumber&)const;
T operator-(const ComplexNumber&)const;
T operator*(const ComplexNumber&)const;
T operator/(const ComplexNumber&)const;
T& operator=(const ComplexNumber&);
bool operator==(const ComplexNumber&) const;
bool operator!=(const ComplexNumber&) const;

private:

T realnumber;
T imaginarynumber;
};

template<class T>
ostream& operator<<(ostream& out, const ComplexNumber<T>& complex)
{

out<<complex.realnumber;
out<<"+";
out<<complex.imaginarynumber;
out<<"i";
return out;
}

template <class T>
istream& operator>>(istream& in, ComplexNumber<T>& complex)
{
char ch;
in>>complex.realnumber;
in>>complex.imaginarynumber;
in>>ch;
return in;
}

template <class T>
T& ComplexNumber<T>::operator=(const ComplexNumber& other)
{
if (this != &other)
{
realnumber = other.realnumber;
imaginarynumber = other.imaginarynumber;
}

return *this;
}

template <class T>
bool ComplexNumber<T>::operator ==(const ComplexNumber& other) const
{
return (realnumber == other.realnumber && imaginarynumber == other.imaginarynumber);
}

template<class T>
bool ComplexNumber<T>::operator !=(const ComplexNumber& other) const
{
return (realnumber != other.realnumber && imaginarynumber != other.imaginarynumber);
}


//Default Constructor
template<class T>
ComplexNumber<T>::ComplexNumber()
{
realnumber = 0.0;
imaginarynumber = 0.0;
}


//Constructor which takes in two arguments
template<class T>
ComplexNumber<T>::ComplexNumber(const T real,const T imaginary)
{
realnumber = real;
imaginarynumber = imaginary;
}

template<class T>
void ComplexNumber<T>::setComplexNumber(const T& real, const T& imaginary)
{
realnumber = real;
imaginarynumber = imaginary;
}

//Overloaded Addition Operator
template<class T>
T ComplexNumber<T>::operator +(const ComplexNumber& other) const
{
ComplexNumber<T>temp;
temp.realnumber = realnumber + other.realnumber;
temp.imaginarynumber = imaginarynumber + other.imaginarynumber;
return ComplexNumber;
}

//Overloaded subtraction operator
template<class T>
T ComplexNumber<T>::operator -(const ComplexNumber& other) const
{
ComplexNumber<T> temp;
temp.realnumber = realnumber - other.realnumber;
temp.imaginarynumber = imaginarynumber - other.imaginarynumber;
return temp;
}

//Overloaded multiplication operator
template<class T>
T ComplexNumber<T> ::operator *(const ComplexNumber& other)const
{
ComplexNumber<T> temp;
temp.realnumber = (realnumber * other.realnumber) - (imaginarynumber * other.imaginarynumber);
temp.imaginarynumber = (realnumber * other.imaginarynumber) + (imaginarynumber * other.realnumber);
return temp;
}

//Overloaded division operator
template<class T>
T ComplexNumber<T> ::operator/(const ComplexNumber& other)const
{
ComplexNumber<T> temp;
temp.realnumber = ((realnumber * other.realnumber) + (imaginarynumber * other.imaginarynumber))/((pow(other.realnumber,2.0))+(pow(other.imaginarynumber,2.0)));
temp.imaginarynumber = ((other.realnumber * imaginarynumber) - (realnumber * other.imaginarynumber))/((pow(other.realnumber,2.0))+(pow(other.imaginarynumber,2.0)));
return temp;
}


#endif

我遇到一个问题,我的加法、减法、乘法和除法运算符无法从 ComplexNumber 转换为 double 或我想要的任何其他数据类型。请让我知道我做错了什么。谢谢。

最佳答案

//Overloaded Addition Operator
template<class T>
T ComplexNumber<T>::operator +(const ComplexNumber& other) const
{
ComplexNumber<T>temp;
temp.realnumber = realnumber + other.realnumber;
temp.imaginarynumber = imaginarynumber + other.imaginarynumber;
return ComplexNumber;
}

我需要

return temp;

在这里编译。这就是你要问的吗?

此外,在这种情况下,您可能希望该方法返回 ComplexNumber<T> , 不是 T .这可能就是您遇到的转换问题。它正在尝试转换 ComplexNumber<T>T回来。和 T我猜在您运行的任何示例中都是双倍的。

其他一些问题。 friend 声明需要是一个模板。还有一些其他的东西。但这是一个功能版本。至少它对我有用。它需要重新添加其他运算符。

#include <iostream>

using namespace std;

template <class T>
class ComplexNumber
{
public:
ComplexNumber();
ComplexNumber(T real , T imaginary);
ComplexNumber<T> operator+(const ComplexNumber<T>&)const;

template <class U>
friend ostream& operator<<(ostream&, const ComplexNumber<U>&);

private:
T realnumber;
T imaginarynumber;
};

//Default Constructor
template<class T>
ComplexNumber<T>::ComplexNumber()
{
realnumber = 0.0;
imaginarynumber = 0.0;
}


//Constructor which takes in two arguments
template<class T>
ComplexNumber<T>::ComplexNumber(const T real,const T imaginary)
{
realnumber = real;
imaginarynumber = imaginary;
}

//Overloaded Addition Operator
template<class T>
ComplexNumber<T> ComplexNumber<T>::operator +(const ComplexNumber<T>& other) const
{
ComplexNumber<T> temp;
temp.realnumber = realnumber + other.realnumber;
temp.imaginarynumber = imaginarynumber + other.imaginarynumber;
return temp;
}

template<class T>
ostream& operator<<(ostream& out, const ComplexNumber<T>& complex)
{
out<<complex.realnumber;
out<<"+";
out<<complex.imaginarynumber;
out<<"i";
return out;
}

int main(){
ComplexNumber<double> cn1(1,1);
ComplexNumber<double> cn2(1,1);
ComplexNumber<double> cn3 = cn1+cn2;
cout<<cn3<<endl;
}

关于c++ - 复数模板类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34366371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com