printf("ho_tari\n");

Complex (istream 입력 함수) 본문

C++

Complex (istream 입력 함수)

호타리 2023. 9. 5. 10:16

<main.cpp>

#include <cstdio>

#include "complex.h"

#include <iostream>



int main()

{

	Complex c1;

	Complex c2 = 3.0;                // Complex c2(3.0)

	Complex c3(3.0, 4.0);

	const Complex c4 = c3;           // const Complex c4(c3)

	

	c1.real(c3.real());

	c1.imag(c3.imag());

	

	c1 = c3;

	if (c1 == c3)

	{

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

	}

	else

	{

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

	}

	

	c1 != c3;

	

	c2 = c1 + c3;

	c2 = c1 - c3;

	

	++c1;     // c1.operator++()

	c1++;     // c1.operator++(int )

	

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

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

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

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

	

	Complex c5;

	std::cin >> c5;

	

	std::cout << "c5 : (" << c5 << 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() { } // is not generated

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

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

	

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

	

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

	//~Complex () { }

	

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

	// +=, -=, *=, /=

	

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

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

	// >, <, >=, <=

	

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

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

	// *, /

	

	const Complex& operator++();

	const Complex operator++(int );

	

	double real() const;

	double imag() const;

	

	void real(double re);

	void imag(double 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.real() << ", " << rhs.imag() << "i)";

	

	return out;

}



Complex::Complex(double re, double im)

{

	re_ = re;     // (this->) 생략 가능 : this->re_ = re_

	im_ = im;

}



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

{

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

}



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

{

	return !this->operator==(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;

}



const Complex& Complex::operator++()

{

	re_ += 1.0;

	

	return *this;

}



const Complex Complex::operator++(int )

{

	Complex result = *this;

	re_ += 1.0;

	

	return result;

}



double Complex::real() const

{

	return re_;

}



double Complex::imag() const

{

	return im_;

}



void Complex::real(double re)

{

	re_ = re;

}



void Complex::imag(double im)

{

	im_ = im;

}

 

<compile 결과>

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

String (클래스 개선)  (0) 2023.09.05
StringRep (reference counting 참조 계수)  (0) 2023.09.05
Rational3 (istream 입력 함수)  (0) 2023.09.05
add  (0) 2023.09.05
Rational (라이브러리)  (0) 2023.09.05