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