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
- 개인프로젝트
- 코딩
- 모의 주식
- SNS
- 프로그램
- 코딩테스트
- 풀스택프레임워크
- frontend
- 프로그래머스
- 프로그래밍
- Project
- java정리
- Reflex
- 개발
- 개발자
- 자바
- 이웃한 칸
- 코테
- 자바정리
- java
- 주가예측
- 빅데이터
- 백준
- 자바독학
- 코스닥
- C++
- Python
- backend
- 사물인터넷
- 코딩독학
Archives
- Today
- Total
Creative Code
main.cpp(rational클래스 여러 연산자) 본문
※main.cpp파일
#include <iostream>
#include "rational.h"
int main() {
Rational r1; // 0
Rational r2(3); // 3/1
Rational r3(3,4); // 3/4
std::cout << "r1 : " << r1 << std::endl;
std::cout << "r1 + r2 : " << r1+r2 <<std::endl;
std::cout << "r2 * r3 : " << r2*r3 << std::endl;
std::cout << "r2 == r3 : " << (r2==r3) << std::endl;
std::cout << "r2 > r3 : " << (r2>r3) << std::endl;
std::cout << "r2 / r3 : " << (r2/r3) << std::endl;
}
※rational.h파일
#ifndef RATIONAL_H
#define RATIONAL_H
#include <iostream>
class Rational {
friend std::ostream& operator<<(std::ostream& out, const Rational& rhs);
private:
int num;
int den;
public:
Rational(int num = 0, int den = 1);
Rational(const Rational &rhs);
~Rational();
Rational operator=(const Rational& rhs);
Rational& operator+=(const Rational& rhs);
bool operator>(const Rational& rhs);
bool operator==(const Rational& rhs);
bool operator!=(const Rational& rhs);
Rational operator+(const Rational& rhs);
Rational operator-(const Rational& rhs);
Rational operator*(const Rational& rhs);
Rational operator/(const Rational& rhs);
};
#endif
※rational.cpp파일
#include "rational.h"
#include <cassert>
int GCD(int a, int b){
if (b == 0) return a;
else return GCD(b,a%b);
}
std::ostream& operator<<(std::ostream& out, const Rational& rhs) {
out << "(" << rhs.num << "/" << rhs.den << ")";
return out;
}
Rational::Rational(int num, int den) {
this->num = num;
this->den = den;
}
Rational::Rational(const Rational&rhs) {
this->num = rhs.num;
this->den = rhs.den;
}
Rational::~Rational() {
}
Rational Rational::operator=(const Rational &rhs) {
this->num = rhs.num;
this->den = rhs.den;
return *this;
}
Rational& Rational::operator+=(const Rational &rhs) {
this->num = this->num * rhs.den + this->den*rhs.num;
this->den = this->den * rhs.den;
int gcd = GCD(this->num,this->den);
this->num /= gcd;
this->den /= gcd;
return *this;
}
bool Rational::operator>(const Rational &rhs) {
double result1 = (double)(this->num)/(double)(this->den);
double result2 = (double)(rhs.num)/(double)(rhs.den);
return result1>result2;
}
bool Rational::operator==(const Rational &rhs) {
int gcd1 = GCD(this->num,this->den);
int gcd2 = GCD(rhs.num,rhs.den);
return (this->num/gcd1 == rhs.num/gcd2 && this->den/gcd1 == rhs.den/gcd2);
}
bool Rational::operator!=(const Rational &rhs) {
return !(*this==rhs);
}
Rational Rational::operator+(const Rational &rhs) {
int num1 = this->num * rhs.den + this->den * rhs.num;
int den1 = this->den * rhs.den;
int gcd = GCD(num1,den1);
return Rational(num1/gcd,den1/gcd);
}
Rational Rational::operator-(const Rational &rhs) {
int num1 = this->num * rhs.den - this->den * rhs.num;
int den1 = this->den * rhs.den;
int gcd = GCD(num1,den1);
return Rational(num1/gcd,den1/gcd);
}
Rational Rational::operator*(const Rational &rhs) {
int num1 = this->num * rhs.num;
int den1 = this->den * rhs.den;
int gcd = GCD(num1,den1);
return Rational(num1/gcd,den1/gcd);
}
Rational Rational::operator/(const Rational &rhs) {
assert(rhs.num!=0);
int num1 = this->num * rhs.den;
int den1 = this->den * rhs.num;
int gcd = GCD(num1,den1);
return Rational(num1/gcd,den1/gcd);
}
'C++ Programming' 카테고리의 다른 글
main.cpp(string2) (0) | 2023.09.01 |
---|---|
main.cpp(string) (0) | 2023.08.31 |
main.cpp(complex클래스 여러 연산자) (0) | 2023.08.31 |
main.cpp(Rational 사칙연산) (0) | 2023.08.30 |
main.cpp(complex 연산) (0) | 2023.08.30 |