Question: New: In addition to the above methods, you must create and update the following portions of the public interface: ToComplex: This static method shall accept

New: In addition to the above methods, you must create and update the following portions of the public interface:

ToComplex: This static method shall accept a string of the type described in ToString and return a Complex class instance. In this version of the method, you must detect and assert the validity of the string representation. A viable representation is, in order:

An open parenthesis (

0 or more space characters (no \t or characters)

A possibly signed () integer or floating point value

0 or more space characters

Exactly one of

(a) A + or character, signifying an imaginary number is present, followed by i. 0 or more space characters and

ii. Exactly one of An unsigned integer or floating point value, followed by

A. 0 or more space characters and

B. The character i The character i

(b) The character i, signifying that no real is present, or (c) A close parenthesis character ), signifying no imaginary number given, the Complex is found,

and the method is done.

0 or more space characters

A close parenthesis

CSCE 240 Homework 2.5 Page 2 of 2

LT and operator<: These comparison methods should implement comparison via the complex of a modulus number. For a given complex v = (+i), the modulus or v is defined as v = 2 + 2. You will need to determine a means to compare Complex instances to ints and doubles using this logic.

operator>>: This function (friend or not) shall accept an istream object and Complex object. It shall extract a string of the type described in ToString from the stream and return a complex number. As in ToComplex, the method should assert the validity of string representation. The function should correctly accept and return the istream object so that it behaves as cin.

IsComplex: these static methods should examine the contents of their respective string and stream instances to ensure that a Complex can be extracted from the contained representation. Note that the istream must be maintainedthat is after you have evaluated its contents, you must place those contents back on the stream. You may want to read the istream::putback documentation. It will require a modicum of creativity to get this correct for the overloaded stream method.

My .h File:

#ifndef _YOUR_USERNAME_HERE_HW_2_5_COMPLEX_H_ // NOLINT #define _YOUR_USERNAME_HERE_HW_2_5_COMPLEX_H_ // NOLINT

#include #include #include #include #include using std::cout; using std::cin; using std::endl; using std::string; using std::istream; using std::ostream; using std::to_string;

typedef std::stringstream StrStream;

class Complex {

public: /* * TODO: assert the correctness of the string representation of the Complex * instance in str_complex when converting. */ static const Complex ToComplex(const string& str_complex);

/* * TODO: examine the contents of the parameters to make sure a valid * representation of a Complex instance is stored. */ static bool IsComplex(const string& in_string); static bool IsComplex(istream& in_stream);

string ToString(void);

/* TODO: Implement the LT and operator< methods and functions */ bool LT(const Complex& rhs) const; bool LT(double rhs) const; bool LT(int rhs) const; bool operator<(const Complex& rhs) const; bool operator<(double rhs) const; bool operator<(int rhs) const; friend bool operator<(double lhs, const Complex& rhs); friend bool operator<(int lhs, const Complex& rhs);

/* TODO: assert the correctness of the Complex string in lhs */ friend istream& operator>>(istream& lhs, Complex& rhs);

/* * Keep these from your previous submission or implement them so that I may * unit test your class. */ Complex(); Complex(double imag); Complex(double real, double imag);

double real() const; double imag() const;

void real(double real); void imag(double imag);

private:

double real_; double imag_; };

#endif // NOLINT

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!