The part I need help with is to fill in the lines of this code: complex.h #ifnde
ID: 654870 • Letter: T
Question
The part I need help with is to fill in the lines of this code:
complex.h
#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
using namespace std;
class Complex
{
// overloaded input and output operators
/* Write friend declarations for the stream insertion
and extraction operators */
public:
Complex( void ); // constructor
private:
/* Write declarations for data members real and imaginary */
}; // end class Complex
#endif
complex.cpp
#include <iostream>
#include <iomanip>
#include "Complex.h"
using namespace std;
// default constructor
Complex::Complex(void) :
real(0),
imaginary(0)
{
// empty body
} // end Complex constructor
// overloaded output (<<) operator
ostream &operator<<(ostream &output, const Complex &c)
{
output << c.real << showpos << c.imaginary << "i " << showpos;
return output;// return ostream reference
} // end overloaded output (<<) operator
// overloaded input (>>) operator
istream &operator>>(istream &input, Complex &c)
{
int number;
int multiplier;
char temp; // temporary variable used to store input
input >> number; // get input
// test if character is a space
if ( /* Write a call to the peek member function to
test if the next character is a space ' ' */) // case a + bi
{
c.real = number;
input >> temp;
multiplier = (temp == '+') ? 1 : -1;
// set failbit if character not a space
if (input.peek() != ' ')
/* Write a call to the clear member function with
ios::failbit as the argument to set input's fail bit */
else
{
// set imaginary part if data is valid
if (input.peek() == ' ')
{
input >> c.imaginary;
c.imaginary *= multiplier;
input >> temp;
if ( /* Write a call to member function peek to test if the next
character is not a newline */) // character not a newline
input.clear(ios::failbit); // set bad bit
} // end if
else
input.clear(ios::failbit); // set bad bit
} // end else
} // end if
else if ( /* Write a call to member function peek to test if
the next character is 'i' */) // test for i of imaginary number
{
input >> temp;
// test for newline character entered
if (input.peek() == ' ')
{
c.real = 0;
c.imaginary = number;
} // end if
else
input.clear(ios::failbit); // set bad bit
} // end else if
else if (input.peek() == ' ') // set real number if it is valid
{
c.real = number;
c.imaginary = 0;
} // end else if
else
input.clear(ios::failbit); // set bad bit
return input;
} // end overloaded input (>>) operator
Explanation / Answer
// Complex.h
#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
using std:stream;
using std::istream;
class Complex
{
friend ostream &operator<<(ostream &output, const Complex &c);
friend istream &operator>>(istream &input, Complex &c);
public:
Complex(void);
private:
int real;
int imaginary;
};
// Complex.cpp
#include <iostream>
#include <iomanip>
using std::ios;
using std::istream;
using std:stream;
using std::showpos;
#include "Complex.h"
Complex::Complex(void):
real(0),
imaginary(0)
{
}
ostream &operator<<(ostream &output, const Complex &c)
{
output << c.real << showpos << c.imaginary << "i " << showpos;
return output;
}
istream &operator>>(istream &input, Complex &c)
{
int number;
int multiplier;
char temp;
input >> number;
if (input.peek() == ' ')
{
c.real = number;
input >> temp;
multiplier = ( temp == '+' ) ? 1 : -1;
if (input.peek() != ' ')
input.clear( ios::failbit );
else
{
if (input.peek() == ' ')
{
input >> c.imaginary;
c.imaginary *= multiplier;
input >> temp;
if ( input.peek() == ' ' )
input.clear(ios::failbit);
}
else
input.clear(ios::failbit);
}
}
else if (input.peek() == 'i')
{
input >> temp;
if (input.peek() == ' ')
{
c.real = 0;
c.imaginary = number;
}
else
input.clear(ios::failbit );
}
else if (input.peek() == ' ')
{
c.real = number;
c.imaginary = 0;
}
else
input.clear(ios::failbit);
return input;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.