Dear Chegg Experts, I am a C++ programming student that is struggling with an op
ID: 3664382 • Letter: D
Question
Dear Chegg Experts,
I am a C++ programming student that is struggling with an operators themed assignment. I would be grateful for any insight offered.
I will post the specifications and my code below to show where I am thus far.
Thank you for your time and efforts.
SPEC START
Operators
A complex number, c, is an ordered pair of real numbers (doubles). For example, for any two real numbers, s and t, we can form the complex number:
c = (s,t)
This is only part of what makes a complex number complex. Another important aspect is the definition of special rules for adding, multiplying, dividing, etc. these ordered pairs. Complex numbers are more than simply x-y coordinates because of these operations. Examples of complex numbers in this format are (3, 3), (-1, -5), (1.034, 77.5) and (99.9, -108.5). We will build a class of complex numbers called Complex.
One important property of every complex number, c, is its length, or modulus, defined as follows. If c = (s, t) then:
|c| = modulus(c) = (s^2 + t^2)
|(s,t)| = modulus(c) = (s^2 + t^2)
Create a class of Complex numbers.
Private Data
double real, double imag - These two doubles define the Complex number and are called therealand imaginary parts. Think of each Complex number as an ordered pair, (real, imag) of doubles.
Public Instance Methods
Constructors - Allow Complex objects to be constructed with zero, one or two double arguments. The zero-parameter constructor initializes the complex number to (0, 0). The one-parameter constructor should set the real member to the argument passed and set the imag member to 0. The two-parameter constructor should set both real and imag according to the parameters. DO ALL THREE VERSIONS IN A SINGLE CONSTRUCTOR USING DEFAULT PARAMETERS.
Accessors/Mutators - The usual -- two mutators, two accessors.
double modulus() - This will return the double |c|, i.e., the modulus, of the complex number. If the Complex object, c, represents the ordered pair (s, t), then the formula above will give the double value to return.
Complex reciprocal() - This defined in the division operator definition, below.
toString() - This will return a string like "(-23.4, 8.9)" to the client. Do not provide a show() or display() method. You will do this using yourinsertion operator as shown below.
Operators - We will describe several operators that you must implement for the class. Some will be defined externally (which I will call friend operators. However, you may, or may not really need to declare them to be friends - all you need to know is that if I say "friend" I mean implement the operator as a non-member. Otherwise, implement it as a member.
Exception Class -Division By Zero
Create your own DivByZeroException as a nested class, just like we did when we learned exceptions. Make sure that both your reciprocal() andoperator/() functions throw this exception (you can do this by only throwing it for reciprocal() if you do it correctly). Test it out in your client with a try/catch block, attempting both a normal, and a fatal division.
To test for division by zero, look at the reciprocal() and test for that being zero. However, don't test for == 0.0 exactly, because of inaccuracies in computer storage of doubles. Instead, pick a literal like .00000001 and proclaim the a complex object to be zero if its modulus (or modulus squared) is less than that literal value.
Description of the Operators
Operators +, -, * and /
Provide four overloaded operators, +, -, * and /. Implement these operators as friend methods of the class, so that Complex objects can be combined using these four operations. Also, allow a mixed mode operation containing a double and a Complex (which would return a Complex), by treating a double x, as if it were the complex number (x,0). This should come about naturally as a result of the Complex/Complex operators and the proper constructor definitions, not by creating 3 overloads for each operator.
The rules for adding complex numbers are:
(r,i) + (s,j) = (r + s , i + j)
The rules for subtracting complex numbers are:
(r,i) - (s,j) = (r - s , i - j)
The rules for multiplying complex numbers are:
(r,i) * (s,j) = (r*s - i*j , r*j + s*i)
The rules for dividing complex numbers are:
(r,i) / (s,j) = (r,i) * reciprocal(s,j)
Notice that if you correspond a normal double number x, with its complex counterpart, (x,0), then you can think of the ordinary double numbers as a subset of the complex numbers. Also, note that, under this correspondence, these four operations result in ordinary addition, multiplication, etc. for the double subset. Try adding or dividing (6,0) and (3,0) if you don't believe me.
In summary, you should be able to handle the combinations below:
Complex a(3,-4), b(1.1, 2.1), c;
double x=2, y= -1.7;
c = a + b;
c = x - a;
c = b * y;
// and also:
c = 8 + a;
c = b / 3.2;
To help you confirm your computations, here are some examples:
(1, 2) + (3, 4) = (4, 6)
(1, 2) - (3, 4) = (-2, -2)
(1, 2) * (3, 4) = (-5, 10)
(1, 2) / (3, 4) = (0.44, 0.08)
(1, 2) + 10 = (11, 2)
10 / (3, 4) = (1.2, -1.6)
Operators << and =
Overload the insertion and assignment operators in the expected ways.
Operators < and ==
a < b should mean |a| < |b|, that is, a.modulus() < b.modulus(). a == b should mean (a.real == b.real) && (a.imag == b.imag). Define these two operators to make this so. (Note: < is not standard in math; there is no natural ordering mathematically for complex numbers. However, we'll allow it to be defined this way in the problem.)
Pass all Complex parameters as const & and return all values of functions that sensibly return complex numbers (like operator+(), e.g.) as Complex values (not & parameters).
Here is my code to show where I am so far.
//
// main.cpp
// Assignment_5_Operators
//
// Created by Macintosh User on 1/30/16.
// Copyright © 2016 Macintosh User. All rights reserved.
//
#include <iostream>
using namespace std;
class Complex
{
private:
double real;
double imag;
//double complex number (real,imag) of doubles
public:
// Complex(); //Necessary?
Complex(double real,double imag);
double getRealDouble();
bool setRealDouble();
double getImagDouble();
bool setImagDouble();
Complex reciprocal();
double modulus();
string toString();
Complex operator+(Complex n);
Complex operator-(Complex n);
Complex operator*(Complex n);
Complex operator/(Complex n);
Complex operator<<(Complex n);
Complex operator=(Complex n);
Complex operator<(Complex n);
Complex operator==(Complex n);
};
int main()
{
/*
Complex a(3,-4), b(1.1, 2.1), c;
double x=2, y= -1.7;
c = a + b;
c = x - a;
c = b * y;
// and also:
c = 8 + a;
c = b / 3.2;
(1, 2) + (3, 4) = (4, 6)
(1, 2) - (3, 4) = (-2, -2)
(1, 2) * (3, 4) = (-5, 10)
(1, 2) / (3, 4) = (0.44, 0.08)
(1, 2) + 10 = (11, 2)
10 / (3, 4) = (1.2, -1.6)
*/
;
return 0;
}
// Function Definitions
/*
Complex() //Necessary?
{
;
}
*/
Complex(real,imag)
{
;
}
double getRealDouble()
{
return real;
}
double getImagDouble()
{
return imag;
}
bool setRealDouble()
{
;
}
bool setImagDouble()
{
;
}
Complex reciprocal()
//c.reciprocal() should return the complex number = ( r / (r*r + i*i), -i / (r*r + i*i) ),
//if (r*r + i*i) is not zero.
//If (r*r + i*i) is zero, then reciprocal() throws an exception.
{
;
}
Complex modulus()
{
;
}
string toString()
{
;
}
Complex operator+(Complex n)
{
//(r,i) + (s,j) = (r + s, i + j).
Complex num;
num.setRealDouble(n.getRealDouble() + this->real);
num.setImagDouble(n.getImagDouble() + this->imag);
return num;
}
Complex operator-(Complex n)
{
//(r,i) - (s,j) = (r - s, i - j).
Complex num;
num.setRealDouble(n.getRealDouble() - this->real);
num.setImagDouble(n.getImagDouble() - this->imag);
return num;
}
Complex operator*(Complex n)
//(a+bi)(c+di) = ac + adi + bci - bd (i*i = -1) = (ac-bd) + i[ad+bc]
//(a+bi)(a-bi) = a^2 + abi - abi - b^2(-1) (i*i = -1) = (a^2-b^2)*(-1) = a^2+b^2 real numbers
//a-bi = a+(-bi)
//(r,i) * (s,j) = (r*s - i*j, r*j + s*i).
{
Complex num;
//num.setRealDouble(n.getRealDouble() * this->real);
//num.setImagDouble(n.getImagDouble() * this->imag);
return num;
}
Complex operator/(Complex n) //How would I implement this operation method?
//(a + bi / c + di) * (c - di/c - di) = (ac - adi + bci + bd/c^2 + d^2) = ([ac + bd] + i[bc - ad] / c^2+d^2)
//(r,i) / (s,j) = (r,i) * reciprocal(s,j)
{
Complex num;
//num.setRealDouble(n.getRealDouble() / this->real);
//num.setImagDouble(n.getImagDouble() / this->imag);
return num;
}
Complex operator<<(Complex n)
{
Complex num;
//num.setRealDouble(n.getRealDouble() / this->real);
//cout << num << "real part: " << n.real << " ";
//cout << num << "imag part: " << n.imag << " ";
//num.setImagDouble(n.getImagDouble() / this->imag);
return num;
}
Complex operator=(Complex n)
{
Complex num;
//num.setRealDouble(n.getRealDouble() = this->real);
//num.setImagDouble(n.getImagDouble() = this->imag);
return num;
}
Complex operator<(Complex n)
{
Complex num;
//num.setRealDouble(n.getRealDouble() / this->real);??
//num.setImagDouble(n.getImagDouble() / this->imag);??
return num;
}
Complex operator==(Complex n)
{
Complex num;
//num.setRealDouble(n.getRealDouble() == this->real);
//num.setImagDouble(n.getImagDouble() == this->imag);
return num;
}
Explanation / Answer
#include <iostream>
#include <string>
#include <cmath>
#include <sstream>
using namespace std;
class Complex
{
friend Complex operator+ (const Complex& number1, const Complex& number2);
friend Complex operator- (const Complex& number1, const Complex& number2);
friend Complex operator* (const Complex& number1, const Complex& number2);
friend Complex operator/ (const Complex& number1, const Complex& number2);
friend ostream & operator<< (ostream& strm, Complex& number);
private:
double real, imag;
public:
static const double DEFAULT_VALUE;
static const double ZERO_VALUE;
Complex ( double real = DEFAULT_VALUE, double imag = DEFAULT_VALUE);
bool setReal (double real);
bool setImag (double imag);
double getReal ();
double getImag ();
double modulus() const;
Complex recriprocal() const;
string toString();
bool operator< (const Complex& number1);
bool operator==(const Complex& number1);
Complex & operator= (const Complex& number1);
class DivByZeroException { };
};
const double Complex::DEFAULT_VALUE = 0;
const double Complex::ZERO_VALUE = 0.00000001;
int main()
{
Complex testNumber;
Complex number1(1,2), number2(3,4);
double testDouble = 10;
cout << "Number 1 is " << number1 << " & Number 2 is " << number2 << endl;
cout << endl << "----- Operator Testing -----" << endl;
testNumber = number1 + number2;
cout << number1 << " + " << number2 << " = " << testNumber << endl;
testNumber = number1 - number2;
cout << number1 << " - " << number2 << " = " << testNumber << endl;
testNumber = number1 * number2;
cout << number1 << " * " << number2 << " = " << testNumber << endl;
testNumber = number1 / number2;
cout << number1 << " / " << number2 << " = " << testNumber << endl;
testNumber = number1 + testDouble;
cout << number1 << " + " << testDouble << " = " << testNumber << endl;
testNumber = number1 - testDouble;
cout << number1 << " - " << testDouble << " = " << testNumber << endl;
testNumber = number1 * testDouble;
cout << number1 << " * " << testDouble << " = " << testNumber << endl;
testNumber = testDouble / number2;
cout << testDouble << " / " << number2 << " = " << testNumber << endl;
if (number2 < number1)
cout << endl << "Number 2 is less than number 1" << endl;
else
cout << endl << "Number 2 is not less than number 1" << endl;
if (number2 == number1)
cout << endl << "Number 2 is the same as number 1" << endl;
else
cout << endl << "Number 2 is different from number 1" << endl;
cout << endl << "If Number 1 = Number 2, we will get: " << endl;
number1 = number2;
cout << "Number 1 is " << number1 << endl;
cout << "Number 2 is " << number2 << endl;
if (number2 == number1)
cout << endl << "Number 2 is the same as number 1" << endl;
else
cout << endl << "Number 2 is different from number 1" << endl;
cout << endl << "----- Exception Testing -----" << endl;
try
{
testNumber = number1 / testDouble;
cout << "If we divide " << number1 << " by " << testDouble
<< ", we get: " << endl << testNumber << endl << endl;
testNumber.setReal (0.000000000000000000001);
testNumber.setImag (0.000000000000000000001);
cout << "If we divide " << number1 << " by "
<< testNumber << ", we get: " << endl;
testNumber = number1 / testNumber;
}
catch (Complex::DivByZeroException)
{
cout << "*** Likely div. by 0 exception ***" << endl;
testNumber = 0;
}
cout << endl << "----- Mutator Test -----" << endl;
number2.setImag(20);
cout << number2 << endl << endl;
cout << "----- Accesor Test -----" << endl;
number1.getReal();
cout << number1 << endl << endl;
return 0;
}
Complex::Complex (double real, double imag)
{
setReal(real);
setImag(imag);
}
bool Complex::setReal (double real)
{
this->real = real;
return true;
}
bool Complex::setImag (double imag)
{
this->imag = imag;
return true;
}
double Complex::getReal ()
{
return real;
}
double Complex::getImag ()
{
return imag;
}
double Complex::modulus() const
{
double modulus, squareSum;
squareSum = real*real + imag*imag;
modulus = sqrt(squareSum);
return modulus;
}
Complex Complex::recriprocal() const
{
double modulusSquare, realRecriprocal, imagReciprocal;
modulusSquare = modulus()*modulus();
if(modulusSquare <= ZERO_VALUE)
throw DivByZeroException();
else
{
realRecriprocal = real/modulusSquare;
imagReciprocal = -1*imag/modulusSquare;
Complex recriprocal(realRecriprocal, imagReciprocal);
return recriprocal;
}
}
string Complex::toString()
{
string printString, realString, imagString;
ostringstream cnvrt1;
cnvrt1 << real;
realString = cnvrt1.str();
ostringstream cnvrt2;
cnvrt2 << imag;
imagString = cnvrt2.str();
if (real <= ZERO_VALUE && real >= -1*ZERO_VALUE)
realString = "0";
if (imag <= ZERO_VALUE && imag >= -1*ZERO_VALUE)
imagString = "0";
printString = "( " + realString + ", " + imagString + " )";
return printString;
}
bool Complex::operator< (const Complex& number1)
{
if(modulus() < number1.modulus())
return true;
return false;
}
bool Complex::operator== (const Complex& number1)
{
if(real == number1.real && imag == number1.imag)
return true;
return false;
}
Complex & Complex::operator= (const Complex& number1)
{
if (this != &number1)
{
this->real = number1.real;
this->imag = number1.imag;
}
return *this;
}
Complex operator+ (const Complex& number1, const Complex& number2)
{
Complex temp;
temp.real = number1.real + number2.real;
temp.imag = number1.imag + number2.imag;
return temp;
}
Complex operator- (const Complex& number1, const Complex& number2)
{
Complex temp;
temp.real = number1.real - number2.real;
temp.imag = number1.imag - number2.imag;
return temp;
}
Complex operator* (const Complex& number1, const Complex& number2)
{
Complex temp;
temp.real = number1.real * number2.real - number1.imag * number2.imag;
temp.imag = number1.real * number2.imag + number2.real * number1.imag;
return temp;
}
Complex operator/ (const Complex& number1, const Complex& number2)
{
Complex temp;
temp = number1 * number2.recriprocal ();
return temp;
}
ostream & operator<< (ostream& strm, Complex& number)
{
strm << number.toString();
return strm;
}
/* -------- paste of run --------
Number 1 is ( 1, 2 ) & Number 2 is ( 3, 4 )
----- Operator Testing -----
( 1, 2 ) + ( 3, 4 ) = ( 4, 6 )
( 1, 2 ) - ( 3, 4 ) = ( -2, -2 )
( 1, 2 ) * ( 3, 4 ) = ( -5, 10 )
( 1, 2 ) / ( 3, 4 ) = ( 0.44, 0.08 )
( 1, 2 ) + 10 = ( 11, 2 )
( 1, 2 ) - 10 = ( -9, 2 )
( 1, 2 ) * 10 = ( 10, 20 )
10 / ( 3, 4 ) = ( 1.2, -1.6 )
Number 2 is not less than number 1
Number 2 is different from number 1
If Number 1 = Number 2, we will get:
Number 1 is ( 3, 4 )
Number 2 is ( 3, 4 )
Number 2 is the same as number 1
----- Exception Testing -----
If we divide ( 3, 4 ) by 10, we get:
( 0.3, 0.4 )
If we divide ( 3, 4 ) by ( 0, 0 ), we get:
*** Likely div. by 0 exception ***
----- Mutator Test -----
( 3, 20 )
----- Accesor Test -----
( 3, 4 )
Press any key to continue . . .
---------------------------------*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.