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
- 풀스택프레임워크
- 자바
- 프로그래머스
- 개인프로젝트
- 빅데이터
- java
- 모의 주식
- 코딩테스트
- 백준
- 자바정리
- 주가예측
- java정리
- 코테
- Reflex
- Project
- 사물인터넷
- SNS
- 코딩
- 개발자
- frontend
- 이웃한 칸
- 코딩독학
- 프로그래밍
- C++
- 프로그램
- 자바독학
- Python
- 개발
- backend
- 코스닥
Archives
- Today
- Total
Creative Code
main.cpp(Rational클래스 라이브러리) 본문
※main.cpp파일
#include <iostream>
#include "rational.h"
int main() {
Rational r1(3,5);
Rational r2(2,7);
std::cout << r1+r2 << std::endl;
std::cout << r1-r2 << std::endl;
std::cout << r1*r2 << std::endl;
std::cout << r1/r2 << std::endl;
std::cout << r1++ << std::endl;
std::cout << ++r1 << std::endl;
std::cout << (r1>r2) << std::endl;
std::cout << r1+5 << std::endl;
std::cout << r1*100 << std::endl;
return 0;
}
※rational.h파일
#ifndef RATIONAL_H
#define RATIONAL_H
#include <iostream>
class Rational {
friend std::ostream& operator<<(std::ostream& s,const Rational& r);
private:
long num;
long den;
long gcd(long, long);
public:
Rational(long num=0, long den=1); //생성자
Rational(const Rational &rhs); // 복사생성자
Rational& operator=(const Rational& rhs); //치환연산자
Rational& operator=(long rhs);
long numerator() const {return num;} //get num함수
long denominator() const {return den;} //get den함수
Rational operator+() const {return *this;} // +r1
Rational operator-() const {return Rational(-num,den);} // -r1
Rational invert() const {return Rational(den,num);} // 역수
const Rational& operator+=(const Rational &rhs); // +=연산
const Rational& operator-=(const Rational &rhs); // -=연산
const Rational& operator*=(const Rational &rhs); // *=연산
const Rational& operator/=(const Rational &rhs); // /=연산
const Rational& operator+=(long rhs);
const Rational& operator-=(long rhs);
const Rational& operator*=(long rhs);
const Rational& operator/=(long rhs);
const Rational& operator++();
const Rational operator++(int);
const Rational& operator--();
const Rational operator--(int);
const Rational operator+(const Rational& r);
const Rational operator-(const Rational& r);
const Rational operator*(const Rational& r);
const Rational operator/(const Rational& r);
bool operator==(const Rational& rhs);
bool operator!=(const Rational& rhs);
bool operator<=(const Rational& rhs);
bool operator>=(const Rational& rhs);
bool operator<(const Rational& rhs);
bool operator>(const Rational& rhs);
};
#endif
※rational.cpp파일
#include "rational.h"
#include <iostream>
std::ostream& operator<<(std::ostream& s, const Rational& r) {
if (r.denominator() == 1L) s<<r.numerator();
else {
s<<r.numerator();
s<<"/";
s<<r.denominator();
}
return s;
}
inline Rational& Rational::operator=(long rhs) {
num = rhs;
den = 1;
return *this;
}
inline double toDouble(const Rational& r) {
return double(r.numerator())/r.denominator();
}
inline long trunc(const Rational& r) {
return r.numerator() / r.denominator();
}
inline long floor(const Rational& r) {
long q = r.numerator()/r.denominator();
return (r.numerator() < 0 && r.denominator() != 1) ? --q : q;
}
inline long ceil(const Rational& r) {
long q = r.numerator() / r.denominator();
return (r.numerator() >=0 && r.denominator() != 1) ? ++q : q;
}
Rational::Rational(long n, long d) {
if (d == 0L) {
std::cerr<<"Division by Zero" << std::endl;
exit(1);
}
if (d<0L) {n = -n; d = -d;}
if (n == 0L) {
num = 0L; den = 1L;
} else {
long g = gcd(n,d);
num = n/g;
den = d/g;
}
}
Rational::Rational(const Rational& rhs) {
num = rhs.num;
den = rhs.den;
}
long Rational::gcd(long u, long v) {
long a = labs(u), b = labs(v);
long tmp;
if (b>a) {
tmp = a;
a = b;
b = tmp;
}
for (;;) {
if (b == 0L) return a;
else if (b == 1L) return b;
else {
tmp =b; b = a%b; a = tmp;
}
}
}
const Rational& Rational::operator+=(const Rational& rhs) {
long g1 = gcd(den, rhs.den);
if (g1 == 1L) {
num = num*rhs.den + den*rhs.num;
den = den*rhs.den;
} else {
long t = num * (rhs.den/g1) + (den/g1)*rhs.num;
long g2 = gcd(t,g1);
num = t/g2;
den = (den/g1) * (rhs.den/g2);
}
return *this;
}
const Rational& Rational::operator-=(const Rational& rhs) {
long g1 = gcd(den, rhs.den);
if (g1 == 1L) {
num = num*rhs.den - den*rhs.num;
den = den*rhs.den;
} else {
long t = num*(rhs.den/g1) - (den/g1) * rhs.num;
long g2 = gcd(t,g1);
num = t/g2;
den = (den/g1) * (rhs.den/g2);
}
return *this;
}
const Rational& Rational::operator*=(const Rational& rhs) {
long g1 = gcd(num,rhs.den);
long g2 = gcd(den,rhs.num);
num = (num/g1) * (rhs.num/g2);
den = (den/g2) * (rhs.den/g1);
return *this;
}
const Rational& Rational::operator/=(const Rational& rhs) {
if (rhs.num == 0L) {
std::cerr << "Division by Zero" << std::endl;
exit(1);
}
long g = gcd(rhs.num,rhs.den);
num *= rhs.den/g;
den *= rhs.num/g;
if (den < 0L) {
num = -num;
den = -den;
}
return *this;
}
const Rational& Rational::operator+=(long rhs) {
num = num + den*rhs;
return *this;
}
const Rational& Rational::operator-=(long rhs) {
num = num - den*rhs;
return *this;
}
const Rational& Rational::operator*=(long rhs) {
long g = gcd(den,rhs);
num *= rhs/g;
den /= g;
return *this;
}
const Rational Rational::operator+(const Rational &r){
int num1 = num * r.den + den * r.num;
int den1 = den * r.den;
return Rational(num1,den1);
}
const Rational Rational::operator-(const Rational &r){
int num1 = num * r.den - den * r.num;
int den1 = den * r.den;
return Rational(num1,den1);
}
const Rational Rational::operator*(const Rational &r) {
int num1 = num * r.num;
int den1 = den * r.den;
return Rational(num1,den1);
}
const Rational Rational::operator/(const Rational &r) {
int num1 = num * r.den;
int den1 = den * r.num;
return Rational(num1,den1);
}
const Rational& Rational::operator++() {
return (*this +=1);
}
const Rational& Rational:: operator--() {
return (*this -=1);
}
const Rational Rational::operator++(int ) {
num /=gcd(num,den);
den /=gcd(num,den);
Rational result(*this);
num += den;
return result;
}
const Rational Rational::operator--(int ) {
num/=gcd(num,den);
den/=gcd(num,den);
Rational result(*this);
num -= den;
return result;
}
bool Rational::operator==(const Rational& rhs) {
return (this->num == rhs.numerator() && this->den == rhs.denominator());
}
bool Rational::operator!=(const Rational& rhs) {
return (this->num != rhs.numerator() || this->den != rhs.denominator());
}
bool Rational::operator<(const Rational& rhs) {
return (toDouble(*this) < toDouble(rhs));
}
bool Rational::operator>(const Rational& rhs) {
return (toDouble(*this) > toDouble(rhs));
}
bool Rational::operator<=(const Rational& rhs) {
return ((*this<rhs) || (*this==rhs));
}
bool Rational::operator>=(const Rational& rhs) {
return ((*this>rhs)||(*this==rhs));
}
'C++ Programming' 카테고리의 다른 글
string3(string 클래스 개선) (0) | 2023.09.05 |
---|---|
add.cpp(cin으로 입력받기) (0) | 2023.09.05 |
main.cpp(타입캐스팅) (0) | 2023.09.04 |
main.cpp(rational클래스 증감연산자) (0) | 2023.09.04 |
main.cpp(complex클래스 증감연산자) (0) | 2023.09.01 |