Please Use C++
I tried to calculate complex number by using *= and operator /=
but I got an incorrect result compared with the result of complex number calculator
For example,
When I calculate ( (c5 *= c4) *= c4) by using my operator function, the result was 1.08288e+06+1.11262e+07i on output,
However, when using a complex calculator, the result was = −253987.448 − 355181.112i, so I got the wrong answer
There is my code below. It compiles well, but my result is incorrect.
Please fix my operator function(*=, /=) code(I highlighted) in order to derive the same answer as the calculator one.
Thank you.
complexDriver.cpp file
#include <iostream>
#include "complex.h"
using namespace std;
int main( ) {
complex c1, c2( 1.2, 4.9 ), c3( 2.2, 1.0 ), c4( -7.0, 9.6 ),
c5(8.1, -4.3),
c6(0.0, -7.1), c7(6.4), c8(0.0, 1.0), c9(0.0, 4.1), c10(0.0, -1.0),
c11;
cout << "(c5 += c2) += c3 is " << ( (c5 += c2) += c3
) << endl;
cout << "(c5 -= c1) -= c2 is " << ( (c5 -= c1) -= c2 )
<< endl;
cout << "(c5 *= 22) *= 13 is " << ( (c5 *= 22) *= 13 )
<< endl;
cout << "(c5 *= c4) *= c4 is " << ( (c5 *= c4) *= c4)
<< endl;
cout << "(c3 /= 2)/c3 is " << (c3/=2)/c3 <<
endl;
cout << "c4 is " << c4 << endl;
cout << "(c4 /= c1) / c1 is " << ( (c4 /= c1 ) / c1 )
<< endl;
cout << "c4 is " << c4 << endl;
}
complex.h file
#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
using std::istream;
using std::ostream;
class complex
{
// Stream I/O
friend ostream& operator<<(ostream &, const complex
&);
friend istream& operator>>(istream&,
complex&);
public:
float getReal() const; // getter
void setReal(float x); // setter
float getImaginary() const; // getter
void setImaginary(float y); // setter
// Default constructor
complex(float x = 0.0, float y = 0.0);
// Math operators +, -, *, /
complex operator+(const complex &) const;
complex operator-(const complex &) const;
complex operator*(const complex &)const;
complex operator/(const complex &)const;
// Assignment +=, -=, *= and /=
complex &operator+=(const complex &);
complex &operator-=(const complex &);
complex &operator*=(const complex &);
complex &operator/=(const complex &);
private:
float Real;
float Imaginary;
};
#endif // COMPLEX_H
complex.cpp file
#include <iostream>
#include "complex.h"
using std::cout;
using std::endl;
float complex::getReal() const { // getter
return Real;
}
void complex::setReal(float x) { // setter
Real = x;
}
float complex::getImaginary() const { // getter
return Imaginary;
}
void complex::setImaginary(float y) { // setter
Imaginary = y;
}
// Default constructor
complex::complex(float x, float y) {
setReal(x);
setImaginary(y);
}
// Math operators +
complex complex::operator+(const complex &c) const {
complex sum(getReal() + c.getReal(),
getImaginary() + c.getImaginary());
return sum;
}
// Math operators -
complex complex::operator-(const complex &c) const {
complex sub(getReal() - c.getReal(),
getImaginary() - c.getImaginary());
return sub;
}
// Math operators *
complex complex::operator*(const complex &c) const{
complex mult;
mult.Real = (Real*c.Real) - (Imaginary*c.Imaginary);
mult.Imaginary = (Real* c.Imaginary) + (Imaginary*c.Real);
return mult;
}
// Math operators /
complex complex::operator/(const complex &c) const{
complex divi;
if((c.Real*c.Real+c.Imaginary*c.Imaginary)==0){
cout<< "can't devide by ";
}else{
divi.Real=((Real*c.Real)+(Imaginary*c.Imaginary))/(c.Real*c.Real+c.Imaginary*c.Imaginary);
divi.Imaginary=((Imaginary*c.Real)-(Real*c.Imaginary))/(c.Real*c.Real+c.Imaginary*c.Imaginary);
return divi;
}
}
// Assignment +=
complex &complex::operator+=(const complex &c) {
Real +=c.Real;
Imaginary += c.Imaginary;
return *this;
}
// Assignment -=
complex &complex::operator-=(const complex &c) {
Real -=c.Real;
Imaginary -=c.Imaginary;
return *this;
}
// Assignment *=
complex &complex::operator*=(const complex &c) {
Real = (Real*c.Real) - (Imaginary*c.Imaginary);
Imaginary = (Real*c.Imaginary) + (c.Real*Imaginary);
return *this;
}
// Assignment /=
// (Divide by zero error should be handled in /=
method.)
complex& complex::operator/=(const complex& c) {
if((c.Real*c.Real + c.Imaginary*c.Imaginary)==0){
cout<< "can't devide by ";
}else{
Real = (Real*c.Real + Imaginary*c.Imaginary)/(c.Real*c.Real +
c.Imaginary*c.Imaginary);
Imaginary = (c.Real*Imaginary - Real*c.Imaginary)/(c.Real*c.Real +
c.Imaginary*c.Imaginary);
return *this;
}
}
// Stream I/O
ostream& operator<<(ostream &out, const complex &c) {
if(c.Real==0.0&&c.Imaginary==0.0){
out<<0;
}else{
out<<c.Real<<"+"<<c.Imaginary<<"i";
}
return out;
}
istream& operator >> (istream& input, complex&
c) {
input >> c.Real >> c.Imaginary;
return input;
}
// Assignment *=
complex &complex::operator*=(const complex &c) {
Real = (Real*c.Real) - (Real*c.Imaginary);
Imaginary = (Imaginary*c.Real) + (Imaginary*c.Imaginary);
return *this;
}
// Assignment /=
// (Divide by zero error should be handled in /=
method.)
complex& complex::operator/=(const complex& c)
{
temp=*this;
if((c.Real*c.Real + c.Imaginary*c.Imaginary)==0){
cout<< "can't divide by ";
}else{
this->Real = ((temp.Real*c.Real )+(
temp.Imaginary*c.Imaginary))/((pow(c.Real,2.0)) +
(pow(c.Imaginary,2.0));
this->Imaginary = ((c.Real*temp.Imaginary) -
(temp.Real*c.Imaginary))/((pow(c.Real,2.0))
+(pow(c.Imaginary,2.0)));
return *this;
}
}
Pls check your code by implementing the above given step to obtain the output.
Get Answers For Free
Most questions answered within 1 hours.