計程8-15
#include <iostream>
using std::ostream;
using std::istream;
using std::cout;
using std::cin;
class complex{
friend ostream &operator<<(ostream &, const complex &);
friend istream &operator>>(istream &, complex &);
public:
complex(double=0.0, double=0.0);
complex operator+(const complex &)const;
complex operator-(const complex &)const;
complex operator*(const complex &)const;
bool operator==(const complex &)const;
bool operator!=(const complex &right)const
{ return !(*this==right); }
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);
}
complex complex::operator*(const complex &operand2) const
{
return complex((real* operand2.real) - (imaginary* operand2.imaginary) ,
(imaginary* operand2.real) + (real* operand2.imaginary));
}
bool complex::operator==(const complex &A)const
{
if((*this).real==A.real && (*this).imaginary==A.imaginary)
return true;
return false;
}
const complex& complex::operator=(const complex &right)
{
real = right.real;
imaginary = right.imaginary;
return *this;
}
ostream &operator<<(ostream &output, const complex &A)
{
output<<"(" <<A.real <<"," <<A.imaginary <<")" ;
return output;
}
istream &operator>>(istream &input, complex &A)
{
input.ignore();
input>>A.real ;
input.ignore();
input>>A.imaginary ;
input.ignore();
return input;
}
int main()
{
cout<<"Key in TWO complex numbers, in the form (a,b)(c,d) \n";
complex c1,c2;
complex x, y, z, w ;
cin>> c1;
cin>> c2;
cout<<"c1=";
cout<< c1 <<"\n";
cout<<"c2=";
cout<< c2 <<"\n";
x = c1 + c2;
y = c1 - c2;
z = c1 * c2;
w = c1;
cout<<"x=" <<x <<"\n";
cout<<"y=" <<y <<"\n";
cout<<"z=" <<z <<"\n";
cout<<"w=" <<w <<"\n";
if(w==c1)
cout<<"w==c1"<<"\n";
return 0;
}
--
天下最難的事,就是享受最簡單平凡的日子
而最簡單平凡的日子,往往是天下最單純的幸福
--
※ 發信站: 批踢踢實業坊(ptt.csie.ntu.edu.tw)
◆ From: 61.226.112.68
推
推 61.224.56.186 01/01, , 1F
推 61.224.56.186 01/01, 1F
推
推 61.226.112.68 01/01, , 2F
推 61.226.112.68 01/01, 2F