如何实现complx类和实现算术函数

时间:2013-11-16 20:07:19

标签: c++ math

我现在正在完成我的任务,它要求我实现complx类,并且还要输入缺少的算术运算符,他给了我们一个加法器,我确定我有减法和乘法的权利但是我搞砸了运算符+双部分,也不确定与运营商==。非常新的编程,所以如果你看到其他任何错误或可以改进,请说出来。这是标题

// Complx.h
#include <iostream>
#include <fstream>
using namespace std;

class complx
{     

public:
double real,
imag;       
complx( double real = 0., double imag = 0.); // constructor
complx operator+(complx); // operator+()

complx operator+(double); // operator+()with double
complx operator- (complx); // operator-()
complx operator* (complx); // operator*()

bool operator== (complx); // operator==()


//Sets private data members.
void Set(double new_real, double new_imaginary) {
real = new_real;
imag = new_imaginary;
}

//Returns the real part of the complex number.
double Real() {
return real;
}

//Returns the imaginary part of the complex number.
double Imaginary() {
return imag;
}
};

ostream &operator << ( ostream &out_file, complx number );

extern istream &operator >> ( istream &in_file, complx &number );

extern ifstream &operator >> ( ifstream &in_file, complx &number );

complx &operator + (double, complx);
complx &operator - (double, complx);
complx &operator * (double, complx);

我的complex.cpp文件

// Complx.cpp
#include "complx.h"
#include <iostream>

extern ifstream &operator >> ( ifstream &in_file, complx &number )
{
double re, is;
char ch;
if (in_file >> ch && ch == '('&& in_file >> re >> ch && ch == ','
&& in_file >> is >> ch && ch == ')')
number.Set(re,is);
else cerr << "Finish the input"<<endl;
return in_file;
}

ostream &operator<< ( ostream &out_file, complx number )
{
out_file << '(' << number.Real() << ',' << number.Imaginary() << ')';
return out_file;
}

// define constructor
complx::complx( double r, double i )
{
real = r; imag = i;
}

// define overloaded + (plus) operator
complx complx::operator+ (complx c)
{
complx result;
result.real = (this->real + c.real);
result.imag = (this->imag + c.imag);
return result;
}
//define overloaded double + operator
complx operator+(const double&, const complx& c)
{
complx operator+(const double&, const complx& c)
{
complx result;
result.real = (double real + c.real);
result.imag = (this->imag + c.imag);
return result;
}
// define overloaded - operator
complx complx::operator- (complx c)
{
complx result;
result.real = (this->real - c.real);
result.imag = (this->imag - c.imag);
return result;
}
//define overloaded * operator
complx complx::operator* (complx c)
{
complx result;
result.real = (this->real * c.real);
result.imag = (this->imag * c.imag);
return result;
}

和我的call_complx.cpp

//call_complx.cpp
#include "complx.h"
ifstream infile ("in.dat");
int main()
{
          int i=0;
          complx in[7];
          double d = 4.5; 
          cout<< "The input numbers are: " << endl;

          while (infile  >> in[i]){
              cout << in[i] << endl;
              i++;
          }        
          complx s1 =  in[0] + in[1]; // calls complx::operator+()
          complx s2 =  d + in[2]; // overload operator+()
          complx s3 =  in[3] + d; // overload operator+()
          complx a = in[4] - in[5];
          complx mm=in[3]*in[4];
          complx dm=d*in[4] ;
          complx b=d-in[0] ;

          cout << "The sum is a complex number " << s1 <<endl;
          cout << "The sum is a complex number " << s2 <<endl;
          cout << "The sum is a complex number " << s3 <<endl;
          cout << "The subtract is a complex number " << a <<endl;
          cout << "The product is a complex number " << mm <<endl;          
          cout << "The subtract is a complex number " << b <<endl;
          cout << "The product is a complex number " << dm <<endl;
          if (in[4] == in[5]) cout << "in[4] and in[5] are the same " << endl;
          system("pause");
          return 0;   //successful termination
}

这也是我第一次在这里发帖,所以如果有任何规则,并且我不遵守,请原谅并启发我的无知请。

1 个答案:

答案 0 :(得分:1)

我在您的代码中发现了operatr+ (double, complx)的一些重复声明,这看似不对。也就是说,您复制了运算符的声明。删除一份副本:

complx operator+(const double&, const complx& c)
{
complx operator+(const double&, const complx& c) // <--- needs to go
{                                                // <--- needs to go

忽略这些边缘细节,这里有几点说明:

  1. 通常,您不希望将二进制算术运算符实现为成员。相反,您希望使用e.g.operator+=()operator-=()等实现变异运算符,然后根据这些来实现运算符,例如,

    complx& complx::operator+= (complx other) {
        this->real += other.real;
        this->imag += other.imag;
    } 
    complx operator+ (complx c0, complx c1) {
        return c0 += c1;
    }
    
  2. 你的乘法肯定是错误的:根据你的implmenetation i 2 == i 但它应该是 -1 。对于乘法(和除法),进行成员运算肯定是不够的。相反,结果是(a + bi)(c + di)==(ac - bd)+(ad + bc)i 当然,正常分布适用于观察 i 2 == -1
  3. 将非成员二元运算符与支持隐式转换的类一起使用的简洁方面是,您根本不需要对混合类型版本感到困扰:隐式转换与这些运算符很好地配合。当然,您仍然可以实现混合类型运算符,但您可能最好只实现一次逻辑并委托给已经实现的运算符,例如:

    complx operator+ (double d, complx c) {
        return complx(d) + c;
        // or: return complx(d) += c;
    }
    

    顺便说一句,你从不希望通过const&传递内置类型!按价值传递它们会更有效率。