Question: Complete the C++ class specification and implementations, complexNumber.h and complexNumber.cpp by adding a new constructor which allows initialization by a string literal (e.g., ComplexNumber x

Complete the C++ class specification and implementations, complexNumber.h and complexNumber.cpp by adding a new constructor which allows initialization by a string literal (e.g., ComplexNumber x = 3+4i;) and by adding a new input operator >> which lets you assign values to complex numbers using cin. Program must be executable under linux.

3 files were given to help complete the assignment.

complexNumber.h:

#ifndef COMPLEX_NUMBER #define COMPLEX_NUMBER

#include using namespace std;

class ComplexNumber { public: ComplexNumber(double real = 0.0, double imag = 0.0);

friend ComplexNumber operator+ (const ComplexNumber &a, const ComplexNumber &b); friend ComplexNumber operator- (const ComplexNumber &a, const ComplexNumber &b); friend ComplexNumber operator* (const ComplexNumber &a, const ComplexNumber &b); friend ComplexNumber operator/ (const ComplexNumber &a, const ComplexNumber &b);

friend ostream& operator<<(ostream &out, const ComplexNumber &c);

private: double r; double i; };

#endif // COMPLEX_NUMBER

complexNumber.cpp:

#include "complexNumber.h"

ComplexNumber::ComplexNumber(double rr, double ii) : r(rr), i(ii) { }

ComplexNumber operator+ (const ComplexNumber &a, const ComplexNumber &b) { ComplexNumber result;

result.r = a.r + b.r; result.i = a.i + b.i;

return result; }

ComplexNumber operator- (const ComplexNumber &a, const ComplexNumber &b) { ComplexNumber result;

result.r = a.r - b.r; result.i = a.i - b.i;

return result; }

ComplexNumber operator* (const ComplexNumber &a, const ComplexNumber &b) { ComplexNumber result;

result.r = (a.r * b.r - a.i * b.i); result.i = (a.r * b.i + a.i * b.r);

return result; }

ComplexNumber operator/ (const ComplexNumber &a, const ComplexNumber &b) { ComplexNumber result;

result.r = (a.r * b.r + a.i * b.i) / (b.r * b.r + b.i * b.i); result.i = (a.i * b.r - a.r * b.i) / (b.r * b.r + b.i * b.i);

return result; }

ostream& operator<< (ostream &out, const ComplexNumber &c) { // If you would like a more capable version of the output function, please // see the HW instructions for a link to such a version. out << c.r << "+" << c.i << "i"; return out; }

and testComplexNumber.cpp:

#include #include "complexNumber.h"

using namespace std;

int main () { ComplexNumber x, y;

// test assignment via string (note, you may also want to test with // other values in addition to the one below) x = "2+4i"; cout << "x = " << x << endl;

// test multiple inputs with a single cin object cout << "Enter two complex numbers: "; cin >> x >> y; cout << "You entered x = " << x << " and y = " << y << endl;

// test assignment via input operator (try several values to ensure // correctness of your implementation) cout << "Please enter a complex number to test (type to exit): "; cin >> x; while (!cin.eof()) { cout << "The complex number you entered is: " << x << endl; cin >> x; }

return 0; }

You are to edit complexNumber.h and complexNumber.cpp to:

Add a new constructor and

Add an input operator (the function prototypes are listed below).

DO NOT put the implementation of the functions in complexNumber.h; rather put the implementation in complexNumber.cpp. Also, DO NOT insert a main() function into either complexNumber.h or complexNumber.cpp. I have provided a main program with which you can test your full implementation in a separate source file called testComplexNumber.cpp. Details: The functions you must add to complete the implementation are as follows:

a. ComplexNumber(const char *str) This constructor initializes a ComplexNumber object using a C-string with one of the following forms:

i. [+|-]realNumber ii. [+|-][realNumber]i iii. [+|-]realNumber(+|-)[realNumber]i For full credit you must be able to read complex numbers matching any of the three forms. For simplicity in the implementation, your solution is NOT required to reject ill-formed complex numbers. In the specifications above:

| stands for OR, [] stands for OPTIONALLY, () stands for EITHER, realNumber stands for any valid real number literal, including numbers in signed or unsigned scientific notation.

b. istream& operator>>(istream &in, ComplexNumber &c); This friend function allows input into ComplexNumber using cin and the extraction operator (i.e., >>).

These are the ONLY two functions you are REQUIRED to write. However, you may write as many helper functions for the class as you deem necessary.

This is what I have

For .h

#ifndef COMPLEX_NUMBER #define COMPLEX_NUMBER

#include using namespace std;

class ComplexNumber { public: ComplexNumber(double real = 0.0, double imag = 0.0); ComplexNumber(const char*str);

friend ComplexNumber operator+ (const ComplexNumber &a, const ComplexNumber &b); friend ComplexNumber operator- (const ComplexNumber &a, const ComplexNumber &b); friend ComplexNumber operator* (const ComplexNumber &a, const ComplexNumber &b); friend ComplexNumber operator/ (const ComplexNumber &a, const ComplexNumber &b);

friend ostream& operator<<(ostream &out, const ComplexNumber &c); friend istream& operator>>(istream &in, ComplexNumber &c); private: double r; double i; };

#endif // COMPLEX_NUMBER

and for .cpp

// Author: Matthew Kline // Date: 2/7/17 // Purpose: HW2

#include "complexNumber.h" #include #include

ComplexNumber::ComplexNumber(double rr, double ii) : r(rr), i(ii) { }

ComplexNumber::ComplexNumber(const char *str) { //init to 0 to start r = 0; //assume i is one until proven otherwise i = 1; //find if complex number int totalIn = sscanf(str, &r, &i); if(totalIn < 2 && strchr(str, '+') == 0 && strchr(str, '-') == 0) {

if(strchr(str, 'i') != 0){ i = 1; r = 0; sscanf(str, &i); } else { i = 0; sscanf(str, &r); } } else if (totalIn == 1 && i == 1 && r != 0 && strchr(str, 'i') == 0) { i = 0; } if(strstr(str, "-i") != 0) { i = -i; } } ComplexNumber operator+ (const ComplexNumber &a, const ComplexNumber &b) { ComplexNumber result;

result.r = a.r + b.r; result.i = a.i + b.i;

return result; }

ComplexNumber operator- (const ComplexNumber &a, const ComplexNumber &b) { ComplexNumber result;

result.r = a.r - b.r; result.i = a.i - b.i;

return result; }

ComplexNumber operator* (const ComplexNumber &a, const ComplexNumber &b) { ComplexNumber result;

result.r = (a.r * b.r - a.i * b.i); result.i = (a.r * b.i + a.i * b.r);

return result; }

ComplexNumber operator/ (const ComplexNumber &a, const ComplexNumber &b) { ComplexNumber result;

result.r = (a.r * b.r + a.i * b.i) / (b.r * b.r + b.i * b.i); result.i = (a.i * b.r - a.r * b.i) / (b.r * b.r + b.i * b.i);

return result; }

ostream& operator<< (ostream &out, const ComplexNumber &b) { bool rPrinted = false; if (b.r != 0 || (b.r == 0 && b.i == 0)) { out << b.r; rPrinted = true; } if (b.i > 0) { if (rPrinted) { out << "+"; } if (b.i != 1) { out << b.i; } out << "i"; }else if (b.i < 0) { if (b.i == -1) { out << "-"; } else { out << b.i; } out << "i"; } return out; }

istream& operator>> (istream &in, ComplexNumber &c) { string s; in >> s; c = s.c_str(); return in; }

test remained the same.

whever I try to compile it I get this error

complexNumber.cpp: In constructor ComplexNumber::ComplexNumber(const char*) complexNumber.cpp:18:36: error: cannot convert double* to const char* for(const char*, const char*, ...) int totalIn = sscanf(str, &r, &i); ^ complexNumber.cpp:24:18: error: cannot convert double* to const char* for(const char*, const char*, ...) sscanf(str, &i); ^ complexNumber.cpp:27:18: error: cannot convert double* to const char* for(const char*, const char*, ...) sscanf(str, &r); ^ > error: cannot convert double* to const char* for argument 2 const char*, ...)

I have tried changing them to float as well as on the .h file but that did not work either.

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!