printf("ho_tari\n");

Complex6 (inline 함수) 본문

C++

Complex6 (inline 함수)

호타리 2023. 9. 11. 12:02

<main.cpp>

#include <iostream>

#include "complex.h"



int main()

{

	Complex c1(3.0, 4.0);       // (3.0, 4.0i)

	Complex c2(3.0);            // (3.0, 0.0i)

	//Complex c2 = 3.0;         // implicit conversion good!

	Complex c3;                 // (0.0, 0.0i)

	const Complex c4 = c1;

	

	c2 = c1;

	c2.real(c1.real());

	c2.imag(c1.imag());

	

	std::cout << "c1 : (" << c1.real() << ", " << c1.imag() << "i)" << std::endl;

	

	c3 = c1 + c2;     // c1.operator+(c2); or operator(c1, c2);

	c3 = c1 - c2;

	

	c1 += c2;

	c1 -= c2;

	

	c1 > c2;

	c1 < c2;

	c1 >= c2;

	c1 <= c2;

	

	if (c1 == c2)

	{

		std::cout << "c1 and c2 are equal" << std::endl;

	}

	else

	{

		std::cout << "c1 and c2 are not equal" << std::endl;

	}

	

	c1 != c2;

	

	std::cin >> c3;

	

	std::cout << "c1 : " << c1 << std::endl;

	std::cout << "c2 : " << c2 << std::endl;

	std::cout << "c3 : " << c3 << std::endl;

	std::cout << "c4 : " << c4 << std::endl;

	

	return 0;

}

<complex.h>

#ifndef COMPLEX_H

#define COMPLEX_H

#include <iostream>



class Complex {

friend std::istream& operator>>(std::istream& in, Complex& rhs);

friend std::ostream& operator<<(std::ostream& out, const Complex& rhs);



private:

	double re_;

	double im_;

	

public:

	//Complex *operator&() { return *this; }

	//const Complex *operator&() const { return *this; }

	

	//Complex();

	//Complex(double re);       /* explicit X */

	Complex(double re = 0.0, double im = 0.0);

	//Complex(const Complex& rhs) { /*memberwise copy*/ }

	//~Complex() {}

	

	//Complex& operator=(const Complex& rhs) { /*memberwise*/ return *this; }

	

	Complex& operator+=(const Complex& rhs);

	Complex& operator-=(const Complex& rhs);

	//Complex& operator*=(const Complex& rhs);     // is not implemented

	//Complex& operator/=(const Complex& rhs);     // is not implemented

	

	const Complex operator+(const Complex& rhs) const;

	const Complex operator-(const Complex& rhs) const;

	//const Complex operator*(const Complex& rhs) const;     // is not implemented

	//const Complex operator/(const Complex& rhs) const;     // is not implemented

	

	bool operator==(const Complex& rhs) const;

	bool operator!=(const Complex& rhs) const;

	bool operator>(const Complex& rhs) const;

	bool operator<(const Complex& rhs) const;

	bool operator>=(const Complex& rhs) const;

	bool operator<=(const Complex& rhs) const;

	

	double real() const;

	double imag() const;

	

	void real(double re);

	void imag(double im);

	

};



inline Complex::Complex(double re, double im)

: re_(re), im_(im)

{

	

}



inline double Complex::real() const

{

	return re_;

}



inline double Complex::imag() const

{

	return im_;

}



inline void Complex::real(double re)

{

	re_ = re;

}



inline void Complex::imag(double im)

{

	im_ = im;

}



#endif

<complex.cpp>

#include "complex.h"



std::istream& operator>>(std::istream& in, Complex& rhs)

{

	double re = 0.0, im = 0.0;

	

	char c = 0;

	in >> c;

	

	if (c == '(')

	{

		in >> re >> c;

		

		if (c == ',') in >> im >> c;

		if (c == 'i') in >> c;

		if (c != ')') in.clear(std::ios::failbit);

	}

	else

	{

		in.putback(c);

		in >> re;

	}

	

	if (in )

	{

		rhs = Complex(re, im);

	}

	

	return in;

}



std::ostream& operator<<(std::ostream& out, const Complex& rhs)

{

	out << "(" << rhs.re_ << ", " << rhs.im_ << "i)";

	

	return out;

}



/*

Complex::Complex()

: re_(0.0), im_(0.0)

{



}



Complex::Complex(double re)

: re_(re), im_(0.0)

{



}

*/



Complex& Complex::operator+=(const Complex& rhs)

{

	return *this = *this + rhs;

}



Complex& Complex::operator-=(const Complex& rhs)

{

	return *this = *this - rhs;

}



const Complex Complex::operator+(const Complex& rhs) const

{

	Complex result(re_ + rhs.re_, im_ + rhs.im_);

	

	return result;

}



const Complex Complex::operator-(const Complex& rhs) const

{

	Complex result(re_ +- rhs.re_, im_ - rhs.im_);

	

	return result;

}



bool Complex::operator==(const Complex& rhs) const

{

	return re_ == rhs.re_ && im_ == rhs.im_;

}



bool Complex::operator!=(const Complex& rhs) const

{

	//return re_ != rhs.re_ || im_ != rhs.im_;

	//return !this->operator==(rhs);

	return !(*this == rhs);

}



bool Complex::operator>(const Complex& rhs) const

{

	//return sqrt(re_ * re_ + im_ * im_) > sqrt(rhs.re_ * rhs.re_ + rhs.im_ * rhs.im_);

	return re_ * re_ + im_ * im_ > rhs.re_ * rhs.re_ + rhs.im_ * rhs.im_;

}



bool Complex::operator<(const Complex& rhs) const

{

	return re_ * re_ + im_ * im_ < rhs.re_ * rhs.re_ + rhs.im_ * rhs.im_;

}



bool Complex::operator>=(const Complex& rhs) const

{

	return !this->operator<(rhs);

}



bool Complex::operator<=(const Complex& rhs) const

{

	return !this->operator>(rhs);

}

 

<compile 결과>

'C++' 카테고리의 다른 글

Rational3 (inline 함수, namespace)  (0) 2023.09.11
Complex6 (inline 함수, namespace)  (0) 2023.09.11
String4 (const_cast)  (0) 2023.09.08
Shape (dynamic_cast)  (0) 2023.09.08
Pointer3 (reinterpret_cast)  (0) 2023.09.08