Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

[c++]Having issues with errors in my program that is supposed to impletment complex numbers //complex.h #pragma once #ifndef __COMPLEX__ #define __COMPLEX__ class complex { public:

[c++]Having issues with errors in my program that is supposed to impletment complex numbers

//complex.h

#pragma once

#ifndef __COMPLEX__

#define __COMPLEX__

class complex {

public:

complex() { //dis b the default constructor

re = 0;

im = 0;

}

// the above can be written complex()=default;

complex(const double a) { //dis b the complex2double converta

double re;

double im;

}

complex(const double a, const double b) { //cart coord rep constructa

re = a;

im = b;

}

//could combine all of the above in one syntactic construct

//***copy construct begins//

complex(const complex& c) {

re = c.re;

im = c.im;

}

////can called with 0,1, or 2 parameters e.g. complex z1(4); complex z2;

//copy construct end//

//complex(const double a=0.0, const double b = 0.0) {

// re = a;

// im = b;

//}

//complex(const double a = 0.0, const double b = 0.0) : re{ a }, im{ b }

//{

//

//}

//assignment OP begin//

complex& complex::operator=(const complex& rhs) {

this->re = rhs.re;

this->im = rhs.im;

return *this;

}

//Class complex member arithmetic and additional assignment operators +=, -=, *=, /=////////////////

//

complex& complex::operator+=(const complex& rhs) {

this->re = this->re + rhs.re;

this->im = this->im + rhs.im;

return *this;

}

complex& complex::operator-=(const complex&rhs) {

this->re = this->re - rhs.re;

this->im = this->im - rhs.im;

return *this;

}

complex& complex::complex::operator*=(const complex& rhs) {

complex t;

t.re = (re*rhs.re) - (im*rhs.im);

t.im = (im * rhs.re) + (re * rhs.im);

this->re = t.re;

this->im = t.im;

return *this;

}

complex& complex::operator/=(const complex&rhs) {

complex t;

t.re = ((this->re *rhs.re) + (this->im *rhs.im)) / ((rhs.re *rhs.re) + (rhs.im * rhs.im));

t.im = ((this->im *rhs.re) + (this->re *rhs.im)) / ((rhs.re *rhs.re) + (rhs.im * rhs.im));

this->re = t.re;

this->im = t.im;

return *this;

}

//endmath//start dubs

double real() const {

return re;

}

double imag() const {

return im;

}

double magnitude() const {

return sqrt((re*re) + (im*im));

}

private:

double re;

double im;

};

complex operator+(const complex& z1, const complex & z2);

complex operator+(const complex&z1, const double x);

complex operator+(const double x, const complex&z);

complex operator-(const complex& z1, const complex & z2);

complex operator-(const complex&z1, const double x);

complex operator-(const double x, const complex&z);

complex operator*(const complex& z1, const complex & z2);

complex operator*(const complex&z1, const double x);

complex operator*(const double x, const complex&z);

complex operator/(const complex& z1, const complex & z2);

complex operator/(const complex&z1, const double x);

complex operator/(const double x, const complex&z);

complex operator+(const complex&z);

complex operator-(const complex &z);

bool operator == (const complex&z1, const complex&z2);

bool operator != (const complex &z1, const complex &z2);

ostream& operator<<(ostream&, complex&z);

istream & operator >> (istream&, complex& z);

double magnitude(const complex&z);

double real(const complex&z);

double imag(const complex&z);

complex cmplx(const double re, const double im);

complex polar(const double r, const double theta);

complex polar(const double r);

complex conj(const complex&z);

double norm(const complex&z);

double arg(const complex&z);

#endif

-------------------------------------------------------------------------

//complex.cpp

#include

#include "complex.h"

#include

using namespace std;

complex& complex::operator=(const complex& rhs) {

}

complex& complex::operator+=(const complex& rhs) {

}

complex& complex::operator-=(const complex&rhs) {

}

complex operator+(const complex& lhs, const complex& rhs)

{

complex z{ lhs };

return z += rhs;

}

inline bool operator == (const complex& z1, const complex& z2) {

return ((z1.real() == z2.real()) && (z1.imag() == z2.imag()));

}

inline bool operator != (const complex& z1, const complex& z2) {

return !(z1 == z2);

}

ostream& operator<<(ostream& out, complex& z) {

out << '(' << z.real() << ',' << z.imag() << ')';

return out;

}

istream & operator >> (istream& in, complex& z) {

in >> z.real >> z.imag;

return in;

}

double magnitude(const complex& z) {//also known as absolute value

return sqrt((z.real() * z.real()) + (z.imag() * z.imag()));

}

double real(const complex& z) {

return z.real();

}

double imag(const complex& z) {

return z.imag();

}

complex cmplx(const double real, const double imag) {

complex com;

com.real = real;

com.imag = imag;

return(com);

}

complex polar(const double r, const double theta) {

return(r*cmplx(cos(theta), sin(theta)));

}// Constructs a complex via polar coords.

complex polar(const double r) {}

complex conj(const complex& z) {

return complex{ z.real(), -z.imag() };

}// Returns the conjugate of complex number

double norm(const complex& z) {

return pow(z.magnitude(), 2);

}

// Returns squared magnitude (absolute) value of z

double arg(const complex& z){

return(z == 0 ? 0 : atan2(z.imag, z.real));

}

// Returns arg (theta) value of complex number, z. Must be calculated from internal

//representation.Undefined for z = 0.

//x=im, y=re

//

-----------------------------------------------------------------------------

//maincomponentdriver.cpp

#include

#include "complex.h"

using namespace std;

inline bool approx_value(double x, double y) {

return (y - .0001<=x) && x<=(y+.0001);

}

int main()

{

complex z1;

complex z2{ 3 };

complex z3{ 3.4,4.0 };

complex z4{ z3 };

complex z5 = z4;

complex z6(z5);

z2.operator+=(z3);

(z1 == z3);

z2 += z4;

z1 = z2 + z5;

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems Uropean Conference Adbis 2020 Lyon France August 25 27 2020 Proceedings Lncs 12245

Authors: Jerome Darmont ,Boris Novikov ,Robert Wrembel

1st Edition

3030548317, 978-3030548315

More Books

Students also viewed these Databases questions