2020-10-16 고급프로그래밍

Hyeonu_Chun·2021년 6월 22일
0

Absolute C++ 6th ed./Savitch Chap.8 Programming Project. 3

  1. 문제 기술
    필자는 복소수의 기초연산을 할 수 있는 프로그램의 설계를 요한다.

  2. 설계 계획
    클래스 내부에 문제에서 요하는 데이터 멤버와 멤버 함수를 작성하고 연산자 오버로딩을 통해 클래스와 클래스의 연산을 가능하게 만든다. 입출력의 형태도 오버로딩을 통해 형태에 따라 입력되고 출력되게 만든다.

  3. 데이터 처리 과정
    먼저 Complex class type의 (a, b), (c, d)를 파라메타로 가지는 객체를 생성한다. 이라는 객체를 생성한다. 따라서 Complex class의 오버로디드 생성자가 호출되어 데이터 멤버를 파라메타에 해당하는 객체 생성을 완료한다. 기존 main 함수에 입력되어 있는 연산문에 따라 Complex x와 y는 오버로디드 된 연산자 *를 호출하여 복소수의 곱셈연산을 수행하게 된다. 이후 출력 연산자를 호출하여 사용자가 원하는 출력의 형태로 출력하여 곱셈연산을 대조 비교한다.

  4. 실행 결과 및 분석

#include <iostream>
using namespace std;

class Complex {
private:
	double real;
	double imaginary;
public:
	Complex(double real = 0, double imaginary = 0);
	Complex(double realpart);
	~Complex() {}
	Complex operator+(const Complex &a) const;
	Complex operator-(const Complex &a) const;
	Complex operator-() const;
	Complex operator*(const Complex &a) const;
	Complex operator/ (const Complex & a) const;
	bool operator==(const Complex& a);
	bool operator!=(const Complex& a);
	friend ostream &operator<<(ostream &os, Complex &a);
	friend istream &operator>>(istream &is, Complex &a);
	double displayReal() { return real; }
	double displayImaginary() { return imaginary; }
};

Complex::Complex(double real, double imaginary) {
	this->real = real;
	this->imaginary = imaginary;
}

Complex::Complex(double realpart) {
	this->real = realpart;
	this->imaginary = 0;
}

ostream &operator<<(ostream &os, Complex &a) {
	if (a.real != 0) os << a.real;
	if (a.imaginary != 0) {
		cout.setf(ios::showpos);
		os << a.imaginary << "i";
		cout.unsetf(ios::showpos);
	}
	if (a.real == 0 && a.imaginary == 0) os << 0;
	return os;
}

istream &operator>>(istream &is, Complex &a) {
	is >> a.real >> a.imaginary;
	return is;
}

Complex Complex::operator+(const Complex& a) const {
	Complex temp(this->real + a.real, this->imaginary + a.imaginary);
	return temp;
}

Complex Complex::operator-(const Complex& a) const {
	Complex temp(this->real - a.real, this->imaginary - a.imaginary);
	return temp;
}

Complex Complex::operator-() const {
	Complex temp(-this->real, -this->imaginary);
	return temp;
}

Complex Complex::operator*(const Complex& a) const {
	double x, y;
	x = this->real * a.real - this->imaginary * a.imaginary;
	y = this->real * a.imaginary + this->imaginary * a.real;
	Complex temp(x, y);
	return temp;
}

Complex Complex::operator/ (const Complex& a) const {
	double x, y, z;
	z = a.real * a.real + a.imaginary * a.imaginary;
	x = (this->real * a.real + this->imaginary * a.imaginary) / z;
	y = (this->imaginary * a.real + this->real * a.imaginary) / z;
	Complex temp(x, y);
	return temp;
}

bool Complex::operator==(const Complex& a) {
	if (this->real == a.real && this->imaginary == a.imaginary) {
		return true;
	}
	else return false;
}

bool Complex::operator!=(const Complex& a) {
	if (this->real != a.real && this->imaginary != a.imaginary) {
		return true;
	}
	else return false;
}

int main() {
	double a, b, c, d;
	cout << "Please enter four values to calculate : ";
	cin >> a >> b >> c >> d;
	Complex x(a,b), y(c,d);
	const Complex i(0, 1);

	Complex z = x * y;

	cout << x << " * " << y << " = " << z << " (=(" << a << "*" << c << " - " << b << "*" << d << ") + (" << a << "*" << d << " + " << b << "*" << c << "))" <<endl;
}


문제가 요한대로 각 객체를 생성하여 곱셈 뿐만 아니라 다양한 형태의 연산을 시도하였을 때 정상적으로 작동하였다. 복소수 i와의 연산에서도 문제가 없었지만 오버로디드 연산자를 사용하여 객체 3개 이상의 연산을 할 시 문제가 발생 되었다. 입출력 연산의 경우 함수 호출 과정을 줄이기 위해 Friend 함수로 입력했지만 이 점은 피드백 과정 이후에 개선한 것으로 기존 연산자 함수들은 유지하였는데, 나머지 연산자 함수들도 개선을 통해 3개 이상의 피연산 계산이 가능하도록 해야 한다는 것을 인지하였다.

profile
Stay hungry, stay foolish

0개의 댓글