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.
So far I have only added the 2 functions and i know that I want to use sscanf to parse complex number (like 2+4i) and that I might need 3 seperate sscanf for the 3 seperate equations. I am not sure how to implement them though
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
