Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'd like to get some help with this C++ assignment. A complex number , c , is an ordered pair of real numbers ( doubles

I'd like to get some help with this C++ assignment.

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 therealandimaginary 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. Thezero-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.

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

#include #include #include #include #include

using namespace std;

class Complex { private: double real; double imag;

public: static const double ZERO_VAL; Complex(double real, double imag); bool setReal(double realVal); bool setImag(double imagVal); double getReal() const; double getImag() const; double modulus() const; Complex reciprocal() const;

string toString() const;

class DivByZeroException {};

//--operator overloads Complex& operator=(const Complex& complexRHS); bool operator==(const Complex& complexRHS) const;

friend ostream & operator<<(ostream& ostrm, const Complex& complexObj); friend Complex operator+(const Complex& c1, const Complex& c2); friend Complex operator-(const Complex& c1, const Complex& c2); friend Complex operator* (const Complex& c1, const Complex& c2); friend Complex operator/(const Complex& c1, const Complex& c2); friend bool operator<(const Complex& c1, const Complex& c2);

};

// Complex Class Definitions //---static declarations--- const double Complex::ZERO_VAL = 0.0000000001;

//--public member functions---- Complex::Complex(double real = 0, double imag = 0) { setReal(real); setImag(imag); }

bool Complex::setReal(double realVal) { real = realVal; return true; }

bool Complex::setImag(double imagVal) { imag = imagVal; return true; }

double Complex::getReal() const { return real; }

double Complex::getImag() const { return imag; }

double Complex::modulus() const { return sqrt((real*real) + (imag*imag)); }

Complex Complex::reciprocal() const { if (modulus() <= ZERO_VAL) throw DivByZeroException();

Complex temp; temp.real = real / (modulus() * modulus()); temp.imag = (-1 * imag) / (modulus() * modulus());

return temp; }

string Complex::toString() const { string retString, realStr, imagStr; ostringstream cnvrtReal, cnvrtImag;

//converting real, imaginary parts to string cnvrtReal << real; cnvrtImag << imag; realStr = cnvrtReal.str(); imagStr = cnvrtImag.str(); retString = "(" + realStr + ", " + imagStr + ")";

return retString; }

//---operator overloads---- Complex & Complex::operator=(const Complex& complexRHS) { if (this != &complexRHS) { this->real = complexRHS.real; this->imag = complexRHS.imag; }

return *this; }

ostream & operator<<(ostream& ostrm, const Complex& complexObj) { ostrm << complexObj.toString(); return ostrm; }

//Operators +, -, *, and / Complex operator+(const Complex& c1, const Complex& c2) { Complex temp; temp.real = c1.real + c2.real; temp.imag = c1.imag + c2.imag;

return temp;

}

Complex operator-(const Complex& c1, const Complex& c2) { Complex temp; temp.real = c1.real - c2.real; temp.imag = c1.imag - c2.imag;

return temp; }

Complex operator*(const Complex& c1, const Complex& c2) { Complex temp; temp.setReal((c1.real*c2.real) - (c1.imag*c2.imag)); temp.setImag((c1.real*c2.imag) + (c1.imag*c2.real));

return temp; }

Complex operator/(const Complex& c1, const Complex& c2) { Complex temp = c1 * c2.reciprocal(); return temp; }

bool Complex::operator==(const Complex& complexRHS) const { if ((this->real == complexRHS.real) && (this->imag == complexRHS.imag)) return true; return false; }

bool operator<(const Complex& c1, const Complex& c2) { if (c1.modulus() < c2.modulus()) return true; return false; }

//----END Complex CLASS DEFINITIONS-------

int main() { double double1 = 3.66, double2 = -0.317; Complex i(0, -1); Complex complex1(3, -3.2223), complex2(-0.004), complex3(-2.3, 19), complex4; Complex quotient;

//Displaying complex objects with toString cout << "--Complex objects, doubles: " << "i: " << i.toString() << endl << "double1, double2: " << double1 << ", " << double2 << endl << "complex1: " << complex1.toString() << endl << "complex2: " << complex2.toString() << endl << "complex3: " << complex3.toString() << endl << "complex4: " << complex4.toString() << endl << endl;

//Testing mutators, accessors functionality cout << "---Mutator/accessor tests: "; if (complex1.setReal(100)) cout << "Successfully set real of complex1 "; else cout << "Failed to set real part of complex1 ";

if (complex1.setImag(-0.569)) cout << "Successfully set imag of complex1 "; else cout << "Failed to set imag part of complex1 ";

if (complex2.setReal(0) && complex2.setImag(0)) cout << "Successfully set complex2 to (0,0) "; else cout << "Failed to set complex2 ";

cout << "complex1 real: " << complex1.getReal() << endl << "complex1 imag: " << complex1.getImag() << endl << endl;

//Throwing exceptions, test overload of << operator cout << "--Exception throw tests. Also overload of << operator: "; try { cout << complex2.reciprocal(); } catch (...) { cout << " Tried to show 0 reciprocal! (expected)"; }

try { quotient = complex1 / complex2; cout << quotient << endl; } catch (...) { cout << " !!!divide by zero exception(expected)!!! "; quotient = Complex(0); }

//Testing arithmetic with overloaded operators and doubles cout << "---Testing overloaded operators with arithmetic and doubles: ";

cout << "complex1 / double1: " << complex1 / double1 << endl << "complex1 + complex2: " << complex1 + complex2 << endl << "complex2 - double2: " << complex2 - double2 << endl << "complex3 - complex1: " << complex3 - complex1 << endl << "complex3 / complex1: " << complex3 - complex1 << endl << "complex3 * double2: " << complex3 * double2 << endl << "complex3 * complex1" << complex3 * complex1 << endl << "(c3*c1)/(c1*c3): " << (complex3*complex1) / (complex1*complex3) << endl << "complex1 - complex1: " << complex1 - complex1 << endl << endl;

//Testing boolean operators: cout << "---Testing boolean operators: "; if (complex2 < complex1) cout << "complex2 < complex1 (expected) "; else cout << "complex2 > complex1 ";

if (complex2 == complex1) cout << "complex2 equal to complex1 "; else cout << "complex2 not equal to complex1 (expected) ";

if (complex2 == complex4) cout << "complex2=complex4 (expected) "; else cout << "complex2 != complex4 ";

//Testing assignment: cout << "---Testing assignment: "; cout << "Setting c1 = c2, c4=c3" << endl; complex1 = complex2; complex4 = complex3; cout << "complex1, complex2: " << complex1 << ", " << complex2 << endl << "complex3, complex4: " << complex3 << " , " << complex4 << endl;

int foo; cin >> foo; 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

Step: 3

blur-text-image

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

The World Wide Web And Databases International Workshop Webdb 98 Valencia Spain March 27 28 1998 Selected Papers Lncs 1590

Authors: Paolo Atzeni ,Alberto Mendelzon ,Giansalvatore Mecca

1st Edition

3540658904, 978-3540658900

More Books

Students also viewed these Databases questions

Question

Give the general syntax of the nslookup command

Answered: 1 week ago

Question

Solve the integral:

Answered: 1 week ago

Question

What is meant by Non-programmed decision?

Answered: 1 week ago

Question

What are the different techniques used in decision making?

Answered: 1 week ago

Question

Describe the seven standard parts of a letter.

Answered: 1 week ago

Question

Explain how to develop effective Internet-based messages.

Answered: 1 week ago

Question

Identify the advantages and disadvantages of written messages.

Answered: 1 week ago