Guest User

Untitled

a guest
Jan 23rd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. //Solution to homework 7
  2. //CSCE 2050, Fall 2011
  3. //Sebastian Spaink
  4. //Dr.Xiaohui Yuan
  5.  
  6. #include <iostream>
  7. #include <iomanip>
  8. using namespace std;
  9.  
  10. class complex
  11. {
  12. friend ostream & operator<<( ostream &, const complex &);
  13. friend istream & operator>>( istream & , complex &);
  14. friend bool operator==(const complex &, const complex &);
  15. friend bool operator!=(const complex &, const complex &);
  16.  
  17. public:
  18. complex( double = 0.0, double = 0.0 );
  19. complex operator+( const complex & ) const;
  20. complex operator-( const complex & ) const;
  21. complex operator*( const complex & ) const;
  22. private:
  23. double real;
  24. double imaginary;
  25. };
  26.  
  27. complex::complex( double realPart, double imaginaryPart ) : real( realPart ), imaginary( imaginaryPart )
  28. {
  29.  
  30. }
  31.  
  32. complex complex::operator+( const complex &operand2 ) const
  33. {
  34. return complex( real + operand2.real, imaginary + operand2.imaginary );
  35. }
  36.  
  37. complex complex::operator-( const complex &operand2 ) const
  38. {
  39. return complex( real - operand2.real, imaginary - operand2.imaginary );
  40. }
  41.  
  42. complex complex::operator*( const complex &operand2 ) const
  43. {
  44. double temp;
  45. temp = operand2.real * imaginary + real * operand2.imaginary + imaginary * operand2.imaginary;
  46.  
  47. return complex( real * operand2.real, temp );
  48. }
  49.  
  50. ostream & operator<<( ostream & output, const complex &operand2 )
  51. {
  52.  
  53. output << "Real: " << operand2.real << " Imaginary: " << operand2.imaginary << endl;
  54.  
  55. return output;
  56. }
  57.  
  58. istream & operator>>( istream & input, complex &operand2 )
  59. {
  60. input >> operand2.real;
  61. input.ignore(1);
  62. input >> operand2.imaginary;
  63.  
  64. return input;
  65. }
  66.  
  67. bool operator==( const complex &operand1, const complex &operand2 )
  68. {
  69. if( operand1.real == operand2.real && operand1.imaginary == operand2.imaginary )
  70. return true;
  71. else
  72. return false;
  73. }
  74.  
  75. bool operator!=( const complex &operand1, const complex &operand2 )
  76. {
  77. if( !(operand1 == operand2) )
  78. return true;
  79. else
  80. return false;
  81. }
  82.  
  83. int main()
  84. {
  85. complex x;
  86. complex y;
  87. complex temp;
  88.  
  89. cout << "Enter a real part and imaginary part for complex number X: ";
  90. cout << endl<< "(seperated by a space)" << endl;
  91. cin >> x;
  92.  
  93. cout << "Enter a real part and imaginary part for complex number Y: ";
  94. cout << endl << "(seperated by a space)" << endl;
  95. cin >> y;
  96.  
  97. cout << x;
  98. cout << y;
  99.  
  100. temp = x * y;
  101.  
  102. cout << "Multiplying complex X and complex Y together you get: "<< endl;
  103. cout << temp;
  104.  
  105. if( x == y )
  106. cout << "X and Y are equal" << endl;
  107. else
  108. cout << "X and Y are NOT equal" << endl;
  109.  
  110. if( x != y )
  111. cout << "X and Y are NOT equal" << endl;
  112. else
  113. cout << "X and Y are equal" << endl;
  114.  
  115.  
  116. return 0;
  117. }
Add Comment
Please, Sign In to add comment