Write a program that accomplishes each of the following:
a) Create a user-defined class Complex that contains the private integer data members real and imaginary and declares stream insertion and stream extraction overloaded operator functions as friends of the class.
b) Define the stream insertion and stream extraction operator functions. The stream extraction operator function should determine whether the data entered is valid, and, if not, it should set failbit to indicate improper input. The input should be of the form 3 + 8i
c) The values can be negative or positive, and it is possible that one of the two values is not provided, in which case the appropriate data member should be set to 0. The stream insertion operator should not be able to display the point if an input error occurred. For negative imaginary values, a minus sign should be printed rather than a plus sign.
d) Write a main function that tests input and output of user-defined class Complex, using the overloaded stream extraction and stream insertion operators.
```
#ifndef COMPLEX_H
#define COMPLEX_H
#include
using namespace std;
class Complex
{
// overloaded input and output operators
friend ostream &operator<<( ostream&, const Complex& );
friend istream &operator>>( istream&, Complex& );
public:
Complex( void ); // constructor
private:
int real; // hold real part of complex number
int imaginary; // hold imaginary part of complex number
}; // end class Complex
#endif
```
```
// Member-function definition of class Complex.
#include
#include
#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\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() == ' ' ) // case a + bi
{
c.real = number;
input >> temp;
multiplier = ( temp == '+' ) ? 1 : -1;
// set failbit if character not a space
if ( input.peek() != ' ' )
input.clear( ios::failbit ); // set bad bit
else
{
// set imaginary part if data is valid
if ( input.peek() == ' ' )
{
input >> c.imaginary;
c.imaginary *= multiplier;
input >> temp;
if ( input.peek() != '\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
else if ( input.peek() == '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
else if ( 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
```
```
// Complex test program.
#include
#include "Complex.h"
using namespace std;
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 ( !cin.fail() ) // display complex number entered by user if valid
cout << "Complex number entered was:\n" << complex << endl;
else
cout <<
```
Input a complex number in the form A + Bi:
7 - 777i
Complex number entered was:
7-777i
Input a complex number in the form A + Bi:
hello
Invalid Data Entered
You might also like to view...
The ______ effect changes the audio amplitude such that the highest is the amplitude you specify.
A. amplify B. envelope C. noise reduction D. normalize E. reverb
What is a loop?
What will be an ideal response?
Domain local groups can include users, computers, and groups from any domain in the forest, but cannot be employed to grant permissions to any resource in the forest
Indicate whether the statement is true or false
The mysql_connect() function returns a 1 if it connects to the database successfully or 0 if it doesn't.
Answer the following statement true (T) or false (F)