// Lab 2: Complex.h
#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
using std::ostream;
using std::istream;
class Complex
{
// overloaded input and output operators
friend ostream &operator<<( ostream &output, const Complex &c );
friend istream &operator>>( istream &input, Complex &c );
/* Write friend declarations for the stream insertion
and extraction operators */
public:
Complex( void ); // constructor
private:
int real;
int imaginary;
/* Write declarations for data members real and imaginary */
}; // end class Complex
#endif
// Lab 2: Complex.cpp
// Member-function definition of class Complex.
#include <iostream>
using std::ios;
using std::istream;
using std::ostream;
#include <iomanip>
using std::showpos;
#include "Complex.h"
// 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\n" << 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 ( input.peek() == ' ' /* 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 */
input.clear( ios::failbit );
else
{
// set imaginary part if data is valid
if ( input.peek() == ' ' )
{
input >> c.imaginary;
c.imaginary *= multiplier;
input >> temp;
if ( input.peek() == '\n' /* Write a call to member function peek to test if the next
character is a newline \n */ ) // 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
elseif ( input.peek() == 'i' /* 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() == '\n' )
{
c.real = 0;
c.imaginary = number;
} // end if
else
input.clear( ios::failbit ); // set bad bit
} // end else if
elseif ( input.peek() == '\n' ) // 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
// Lab 2: ComplexInput.cpp
// Complex test program.
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include "Complex.h"
int main()
{
Complex complex; // create Complex object
// ask user to enter complex number
cout << "Input a complex number in the form A + Bi:\n";
cin >> complex; // store complex number
if ( /* Write a call to member funciton fail to determine if the
stream operation failed, then negate it to test if input
was valid */ ) // display complex number entered by user if valid
cout << "Complex number entered was:\n" << complex << endl;
else
cout << "Invalid Data Entered\n";
return 0;
} // end main
Ok i think i got this pretty much done but i got confused in the ComplexInput.cpp where it says
/* Write a call to member funciton fail to determine if the
stream operation failed, then negate it to test if input
was valid */ ) // display complex number entered by user if
Im not sure what this means, or how to write this.... can anyone help? Im pretty sure its an easy question, and i think the rest of the code is right (i didnt get any errors). Also if anyone can explain what the purpose of this is and how it works that would be great!! Thanks in advance!