Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Solution to homework 7
- //CSCE 2050, Fall 2011
- //Sebastian Spaink
- //Dr.Xiaohui Yuan
- #include <iostream>
- #include <iomanip>
- using namespace std;
- class complex
- {
- friend ostream & operator<<( ostream &, const complex &);
- friend istream & operator>>( istream & , complex &);
- friend bool operator==(const complex &, const complex &);
- friend bool operator!=(const complex &, const complex &);
- public:
- complex( double = 0.0, double = 0.0 );
- complex operator+( const complex & ) const;
- complex operator-( const complex & ) const;
- complex operator*( const complex & ) const;
- private:
- double real;
- double imaginary;
- };
- complex::complex( double realPart, double imaginaryPart ) : real( realPart ), imaginary( imaginaryPart )
- {
- }
- complex complex::operator+( const complex &operand2 ) const
- {
- return complex( real + operand2.real, imaginary + operand2.imaginary );
- }
- complex complex::operator-( const complex &operand2 ) const
- {
- return complex( real - operand2.real, imaginary - operand2.imaginary );
- }
- complex complex::operator*( const complex &operand2 ) const
- {
- double temp;
- temp = operand2.real * imaginary + real * operand2.imaginary + imaginary * operand2.imaginary;
- return complex( real * operand2.real, temp );
- }
- ostream & operator<<( ostream & output, const complex &operand2 )
- {
- output << "Real: " << operand2.real << " Imaginary: " << operand2.imaginary << endl;
- return output;
- }
- istream & operator>>( istream & input, complex &operand2 )
- {
- input >> operand2.real;
- input.ignore(1);
- input >> operand2.imaginary;
- return input;
- }
- bool operator==( const complex &operand1, const complex &operand2 )
- {
- if( operand1.real == operand2.real && operand1.imaginary == operand2.imaginary )
- return true;
- else
- return false;
- }
- bool operator!=( const complex &operand1, const complex &operand2 )
- {
- if( !(operand1 == operand2) )
- return true;
- else
- return false;
- }
- int main()
- {
- complex x;
- complex y;
- complex temp;
- cout << "Enter a real part and imaginary part for complex number X: ";
- cout << endl<< "(seperated by a space)" << endl;
- cin >> x;
- cout << "Enter a real part and imaginary part for complex number Y: ";
- cout << endl << "(seperated by a space)" << endl;
- cin >> y;
- cout << x;
- cout << y;
- temp = x * y;
- cout << "Multiplying complex X and complex Y together you get: "<< endl;
- cout << temp;
- if( x == y )
- cout << "X and Y are equal" << endl;
- else
- cout << "X and Y are NOT equal" << endl;
- if( x != y )
- cout << "X and Y are NOT equal" << endl;
- else
- cout << "X and Y are equal" << endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment