那你幫我想想作業ㄅ.....
我已經寫到一半了....
#include <iostream.h>
class complex {
friend ostream &operator<<(ostream &, complex &);
friend istream &operator>>(istream &, complex &);
public:
complex( double = 0.0, double = 0.0 );
complex operator+( const complex & ) const;
complex operator-( const complex & ) const;
const complex &operator=( const complex & );
private:
double real;
double imaginary;
};
complex::complex( double r, double i )
: real( r ), imaginary( i ) { }
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 );
}
const complex& complex::operator=( const complex &right )
{
real = right.real;
imaginary = right.imaginary;
return *this;
}
ostream &operator<<(ostream &output, complex &com)
{
output << "( "<< com.real << " , " << com.imaginary << " )";
return output;
}
istream &operator>>(istream &input, complex &com)
{
input>>com.real>>com.imaginary;
return input;
}
int main()
{
complex x, y, z;
cout << "enter the complex y in the form"
<< " ( 1.2 , 3.4 ):\n";
cin >> y;
cout << "enter the complex z in the form"
<< " ( 1.2 , 3.4 ):\n";
cin >> z;
cout << "x: "<<x;
cout << "\ny: "<<y;
cout << "\nz: "<<z;
x = y + z;
cout << "\n\nx = y + z:\n"<<x;
cout << " = "<<y;
cout << " + "<<z;
x = y - z;
cout << "\n\nx = y - z:\n"<<x;
cout << " = "<<y;
cout<< " - "<<z;
return 0;
}
之後要(b) overload the multiplication operator to enable multiplication of
two complex numbers as in algbbra
(c)overload the ==and != operators to allow comparisons of complex
numbers
b&c聽我同學說比較簡單...不過現在沒時間想...
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.116.101.126