2、重载复数类的输出运算符<<和输入运算符>>
输出格式为:a+bi,如:3+4i,4-5i (程序取名hw15_02.cpp)
/*写的比较豪放,因为要求少,自己加了很多*/
#include <iostream>
#include <cmath>
#include <string>
#include <sstream>
using namespace std;
template<typename T>
class Complex
{
public:
Complex():real(0),imag(0){}
Complex(T real, T imag){this->real=real;this->imag=imag;}
template<typename U>
friend Complex<U> operator+(const Complex<U>& ,const Complex<U>& );
template<typename U>
friend Complex<U> operator*(const Complex<U>& ,const Complex<U>& );
template<typename U>
friend Complex<U> operator/(const Complex<U>& ,const Complex<U>& );
template<typename U>
friend ostream & operator<<(ostream &, const Complex<U> &c);
template<typename U>
friend istream & operator>>(istream &, Complex<U> &c);
double Magnitude()const;
Complex conjugate()const;
double Mode() const{
return (*this*(*this).conjugate()).real;
}
double conMagnitude()const;
operator double()const {
if (imag==0) return real;
else return Magnitude();
}
operator string()const;
private:
T real, imag;
};
template<typename T>
Complex<T> operator+(const Complex<T> &c,const Complex<T> &a) { return Complex<T>(a.real + c.real, a.imag + c.imag); }
template<typename T>
Complex<T> operator*(const Complex<T> &c,const Complex<T> &a) { return Complex<T>(a.real * c.real-a.imag * c.imag, a.real * c.imag + a.imag * c.real); }
template<typename T>
double Complex<T>::Magnitude()const {
return sqrt(real*real + imag*imag);
}
template<typename T>
Complex<T> Complex<T>::conjugate()const {
return Complex<T>(real,-imag);
}
template<typename T>
double Complex<T>::conMagnitude()const {
return sqrt(((*this)*conjugate()));
}
template<typename T>
Complex<T>::operator string() const {
stringstream ss;
if (real == 0) {
if (imag == 1) {
ss << "i";
}
else if (imag == -1) {
ss << "-i";
}
else {
ss << imag << "i";
}
}
else {
ss << real;
if (imag > 0) {
ss << "+";
if (imag == 1) {
ss << "i";
}
else if (imag == -1) {
ss << "-i";
}
else {
ss << imag << "i";
}
}
else if (imag < 0) {
if (imag == 1) {
ss << "i";
}
else if (imag == -1) {
ss << "-i";
}
else {
ss << imag << "i";
}
}
}
return ss.str();
}
template<typename T>
Complex<T> operator/(const Complex<T> &c,const Complex<T> &a) {
Complex<T> S=c*a.conjugate();
return Complex<T>(S.real/a.Mode(),S.imag/a.Mode());
}
template<typename T>
ostream & operator<<(ostream &out, const Complex<T> & c) {
if (c.real == 0) {
if (c.imag== 1) {
out << "i";
}
else if (c.imag == -1) {
out << "-i";
}
else {
out << c.imag << "i";
}
}
else {
if (c.imag > 0) {
out << c.real << "+" ;
if (c.imag== 1) {
out << "i";
}
else if (c.imag == -1) {
out << "-i";
}
else {
out << c.imag << "i";
}
}
else if (c.imag < 0) {
out << c.real ;
if (c.imag== 1) {
out << "i";
}
else if (c.imag == -1) {
out << "-i";
}
else {
out << c.imag << "i";
}
}
else {
out << c.real;
}
}
return out;
}
template<typename T>
istream & operator>>(istream &in,Complex<T> & c) {
cout << "Enter real: ";
in >> c.real;
cout << "Enter imag: ";
in >>c.imag;
return in;
}
template<class T>
void info(T z3) {
cout<<z3<<" info:"<<endl;
cout<<'|'<<z3<<"|"<<"=sqt("<<z3<<"*"<<z3.conjugate()<<")"<<"=sqt("<<z3*z3.conjugate()<<")="<<z3.conMagnitude()<<endl;
}
template<class T>
void test(T z1, T z2) {
info(z1);
info(z2);
T z3=z1+z2;
cout<<z1<<"+"<<z2<<"="<<z1+z2<<endl;
info(z3);
cout<<z1<<"*"<<z2<<"="<<z1*z2<<endl;
cout<<z1<<"/"<<z2<<"="<<z1<<"*"<<z2.conjugate()<<"/"<<z2<<"*"<<z2.conjugate()<<"="<<z1*z2.conjugate()<<"/"<<z2*z2.conjugate()<<"="<<z1/z2<<endl;
}
int main()
{
Complex<double> z1, z2;
cin >> z1 >> z2;
test(z1, z2);
return 0;
}
Post Views: 8