In this exercise, you will define a complex data type complx , to manipulate com
ID: 664051 • Letter: I
Question
In this exercise, you will define a complex data type complx, to manipulate complex numbers: a+bi. In complx classwhere you can declare complex data, input/output complex data and check if two complex numbers equal each other or not using the same operators used for other built-in data types.
You need to partially implement a few functions (in red) in class complx
// Complx.h
#include
#include
using namespace std;
class complx
{
double real,
imag;
public:
complx( double real = 0., double imag = 0.); // constructor
(StartRed)~complx(); // destrcutor
friend int operator== (const complx&, const complx&);(EndRed)
//Sets private data members.
void Set(double new_real, double new_imaginary) {
real = new_real;
imag = new_imaginary;
}
//Returns the real part of the complex number.
double Real() {
return real;
}
//Returns the imaginary part of the complex number.
double Imaginary() {
return imag;
}
};
extern ifstream &operator >> ( ifstream &in_file, complx &number );
(StartRed)extern istream &operator >> ( istream &in_file, complx &number );
extern ostream &operator << ( ostream &out_file, complx &number );(EndRed)
----------------------------------------------------------------------------------------------------------------------------------------------------------------
The following code implements two functions in the class. You need to implement the rest of the operators (in red …). You may refer to the sample program on class complx in week 5 lectures Objects and Classes and the solutions to question 5 in Quiz 3 on how to implement functions/operators related to class complx.
// Complx.cpp
#include "complx.h"
complx::~complx() {
(RED)…(RED)
}
ostream &operator<< ( ostream &out_file, complx &number )
{
(RED)…(RED)
}
extern ifstream &operator >> ( ifstream &in_file, complx &number )
{
double re, is;
char ch;
if (in_file >> ch && ch == '('&& in_file >> re >> ch && ch == ','
&& in_file >> is >> ch && ch == ')')
number.Set(re,is);
else cerr << "Finish the input"<
return in_file;
}
extern istream &operator >> ( istream &in_file, complx &number )
{
(RED)…(RED)
}
// define constructor
complx::complx( double r, double i )
{
real = r; imag = i;
}
int operator== (const complx& a, const complx& b){
(RED)…(RED)
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Explanation / Answer
I have implemented the header file with necessay functions for complex class
complex::complex(double rr, double ii) : r(rr), i(ii) {
}
complex operator+ (const complex &a, const complex &b) {
complex result;
result.r = a.r + b.r;
result.i = a.i + b.i;
return result;
}
complex operator- (const complex &a, const complex &b) {
complex result;
result.r = a.r - b.r;
result.i = a.i - b.i;
return result;
}
complex operator* (const complex &a, const complex &b) {
complex result;
result.r = (a.r * b.r - a.i * b.i);
result.i = (a.r * b.i + a.i * b.r);
return result;
}
complex operator/ (const complex &a, const complex &b) {
complex 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 complex &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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.