Question

Please Use C++ I tried to calculate complex number by using *= and operator /= but...

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;
}

Homework Answers

Answer #1

// 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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
Something is either messed up in my operator overload <<, covertopostfix function, or my main output....
Something is either messed up in my operator overload <<, covertopostfix function, or my main output. Cannot figure it out. please help. Please comment your changes too. Program below is supposed to be outputting like this: InFix is:   A+B-C Post fix is:   A B + C - InFix is:   A+C Post fix is:   A C + InFix is:   x*(y+z)-(w+t) Post fix is:   x y z + * w t + - InFix is:   A+B*(C+D)-E/F+G+H Post fix is:   A B C...
- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h"...
- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h" template StackLinked::StackLinked (int maxNumber) { } template StackLinked::StackLinked(const StackLinked& other) { } template StackLinked& StackLinked::operator=(const StackLinked& other) { } template StackLinked::~StackLinked() {    clear(); } template void StackLinked::push(const DataType& newDataItem) throw (logic_error) {    } template DataType StackLinked::pop() throw (logic_error) { } template void StackLinked::clear() {    StackNode* t;    while ( top != NULL)    {        t = top;       ...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private data member for the triplet is a generic array with three elements. The triplet ADT has the following functions:  default constructor  explicit constructor: initialize the data member using parameters  three accessors (three get functions) which will return the value of each individual element of the array data member  one mutator (set function) which will assign values to the data member...
Data Structures using C++ Consider the classes QueueADT and ArrayQueueType: QueueADT: #ifndef QUEUEADT_H #define QUEUEADT_H template...
Data Structures using C++ Consider the classes QueueADT and ArrayQueueType: QueueADT: #ifndef QUEUEADT_H #define QUEUEADT_H template <class ItemType> class QueueADT { public:        // Action responsibilities        virtual void resetQueue() = 0;           // Reset the queue to an empty queue.           // Post: Queue is empty.        virtual void add(const ItemType& newItem) = 0;           // Function to add newItem to the queue.           // Pre: The queue exists and is not full.          ...
IN C++ - most of this is done it's just missing the bolded part... Write a...
IN C++ - most of this is done it's just missing the bolded part... Write a program that creates a class hierarchy for simple geometry. Start with a Point class to hold x and y values of a point. Overload the << operator to print point values, and the + and – operators to add and subtract point coordinates (Hint: keep x and y separate in the calculation). Create a pure abstract base class Shape, which will form the basis...
Data Structures using C++ Consider the following class #ifndef LINKEDQUEUETYPE_H #define LINKEDQUEUETYPE_H #include <iostream> #include <new>...
Data Structures using C++ Consider the following class #ifndef LINKEDQUEUETYPE_H #define LINKEDQUEUETYPE_H #include <iostream> #include <new>    #include <cstdlib> #include "QueueADT.h" using namespace std; // Definition of the node template <class ItemType> struct NodeType {        ItemType info;        NodeType<ItemType> *next; }; template <class ItemType> class LinkedQueueType: public QueueADT<ItemType> { public:        // Constructor        LinkedQueueType();           // Default constructor.           // Post: An empty queue has been created. queueFront = NULL;           //       queueBack = NULL;...
IntList Lab Specifications You are required to come up with a single header file (IntList.h) that...
IntList Lab Specifications You are required to come up with a single header file (IntList.h) that declares and implements the IntNode class (just copy it exactly as it is below) as well as declares the IntList Class interface only. You are also required to come up with a separate implementation file (IntList.cpp) that implements the member functions of the IntList class. While developing your IntList class you must write your own test harness (within a file named main.cpp). Never implement...
The decimal values of the Roman numerals are: M D C L X V I 1000...
The decimal values of the Roman numerals are: M D C L X V I 1000 500 100 50 10 5 1 Remember, a larger numeral preceding a smaller numeral means addition, so LX is 60. A smaller numeral preceding a larger numeral means subtraction, so XL is 40. Assignment: Begin by creating a new project in the IDE of your choice. Your project should contain the following: Write a class romanType. An object of romanType should have the following...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT