Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 | 31 |
Tags
- backend
- 이웃한 칸
- 개발
- java
- 모의 주식
- 코스닥
- 코딩독학
- 주가예측
- 개발자
- 사물인터넷
- Project
- C++
- 풀스택프레임워크
- 자바정리
- Reflex
- 백준
- 프로그래머스
- 자바
- 프로그램
- SNS
- 코딩테스트
- 자바독학
- frontend
- 빅데이터
- java정리
- 코딩
- 프로그래밍
- 개인프로젝트
- Python
- 코테
Archives
- Today
- Total
Creative Code
main.cpp(complex 연산) 본문
※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 + 0i
Complex c3; // 0 + 0i
//c3.real(c1.real());
//c3.imag(c1.imag());
c3 = c1;
c2 = 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;
}
std::cout << "(" << c1.real() << ", " << c1.imag() << "i)" << std::endl;
std::cout << "(" << c2.real() << ", " << c2.imag() << "i)" << std::endl;
std::cout << "(" << c3.real() << ", " << c3.imag() << "i)" << std::endl;
return 0;
}
※complex.h파일
#ifndef COMPLEX_H
#define COMPLEX_H
class Complex{
private:
double re; // real part
double im; // imaginary part
public:
Complex(double re, double im);
Complex(double re);
Complex();
~Complex();
double real();
double imag();
void real(double re);
void imag(double im);
void operator=(const Complex &rhs);
bool operator==(const Complex &rhs);
Complex operator+(const Complex &rhs);
Complex operator-(const Complex &rhs);
};
//Complex operator+(Complex lhs, Complex rhs);
//Complex operator+(const Complex *pc1, const Complex *pc2);
//Complex operator+(Complex &r1, Complex &r2);
#endif
※complex.cpp파일
#include "complex.h"
//member function
/*Complex operator+(Complex lhs, Complex rhs){
Complex result(lhs.real() + rhs.real(),lhs.imag() + rhs.imag());
return result;
}*/
/*Complex operator+(const Complex *pc1, const Complex *pc2){
Complex result(pc1->real() + pc2 ->real(), pc1->imag() + pc2->imag());
return result;
}*/
/*Complex operator+(Complex &r1, Complex &r2){
Complex result(r1.real()+r2.real(),r1.imag()+r2.imag());
return result;
}*/
Complex::Complex()
{
this->re = 0.0;
this->im = 0.0;
}
Complex::Complex(double re)
{
this->re = re;
this->im = 0.0;
}
Complex::Complex(double re, double im)
{
this->re = re;
this->im = im;
}
Complex::~Complex()
{
}
void Complex::operator=(const Complex& rhs) {
this->re = rhs.re;
this->im = rhs.im;
}
bool Complex::operator==(const Complex &rhs) {
/*if (this->re == rhs.re && this->im == rhs.im){
return true;
} else {
return false;
}*/
return (this->re == rhs.re && this->im == rhs.im);
}
double Complex::real()
{
return this->re;
}
double Complex::imag()
{
return this->im;
}
void Complex::real(double re)
{
this->re = re;
}
void Complex::imag(double im)
{
this->im = im;
}
Complex Complex::operator+(const Complex &rhs) {
Complex result(this->re + rhs.re, this->im + rhs.im);
return result;
}
Complex Complex::operator-(const Complex &rhs){
Complex result(this->real()-rhs.re, this->imag()-rhs.im);
return result;
}
'C++ Programming' 카테고리의 다른 글
main.cpp(complex클래스 여러 연산자) (0) | 2023.08.31 |
---|---|
main.cpp(Rational 사칙연산) (0) | 2023.08.30 |
main.cpp(complex 클래스) (0) | 2023.08.30 |
main.cpp(linked list) (0) | 2023.08.29 |
main.cpp(queue) (0) | 2023.08.29 |