34 RFCharge::RFCharge(
const RFCharge& charge){
35 int num = charge.GetNumerator();
36 int den = charge.GetDenominator();
50 m_ChargeNum = std::abs(num);
51 m_ChargeDen = std::abs(den);
57 RFCharge::RFCharge(
int charge){
63 m_ChargeNum = std::abs(charge);
67 RFCharge::RFCharge(
int num,
int den){
80 m_ChargeNum = std::abs(num);
81 m_ChargeDen = std::abs(den);
87 RFCharge::~RFCharge() {}
89 int RFCharge::GetNumerator()
const {
96 int RFCharge::GetDenominator()
const {
100 void RFCharge::Simplify(){
101 int f = gcd(m_ChargeNum, m_ChargeDen);
108 bool RFCharge::operator==(
const RFCharge& val)
const {
110 return (m_ChargeNum == val.GetNumerator())
111 && (m_ChargeDen == val.GetDenominator());
113 return (-m_ChargeNum == val.GetNumerator())
114 && (m_ChargeDen == val.GetDenominator());
117 bool RFCharge::operator==(
int val)
const {
122 return (m_ChargeNum == val);
124 return (-m_ChargeNum == val);
127 bool RFCharge::operator!=(
const RFCharge& val)
const {
129 return (m_ChargeNum != val.GetNumerator())
130 || (m_ChargeDen != val.GetDenominator());
132 return (-m_ChargeNum != val.GetNumerator())
133 || (m_ChargeDen != val.GetDenominator());
136 bool RFCharge::operator!=(
int val)
const{
141 return (m_ChargeNum != val);
143 return (-m_ChargeNum != val);
146 void RFCharge::operator=(
const RFCharge& val){
147 int num = val.GetNumerator();
154 m_ChargeNum = std::abs(num);
155 m_ChargeDen = val.GetDenominator();
158 void RFCharge::operator=(
int val){
164 m_ChargeNum = std::abs(val);
168 RFCharge RFCharge::operator+(
const RFCharge& val)
const {
169 int den = val.GetDenominator();
171 return RFCharge(GetNumerator()*den+val.GetNumerator()*m_ChargeDen,
175 RFCharge RFCharge::operator+(
int val)
const {
176 return RFCharge(GetNumerator()+val*m_ChargeDen, m_ChargeDen);
179 RFCharge RFCharge::operator-(
const RFCharge& val)
const {
180 int den = val.GetDenominator();
182 return RFCharge(GetNumerator()*den-val.GetNumerator()*m_ChargeDen,
186 RFCharge RFCharge::operator-(
int val)
const {
187 return RFCharge(GetNumerator()-val*m_ChargeDen, m_ChargeDen);
190 RFCharge RFCharge::operator*(
const RFCharge& val)
const {
191 return RFCharge(GetNumerator()*val.GetNumerator(),
192 val.GetDenominator()*m_ChargeDen);
195 RFCharge RFCharge::operator*(
int val)
const {
196 return RFCharge(GetNumerator()*val, m_ChargeDen);
199 RFCharge RFCharge::operator/(
const RFCharge& val)
const {
200 return RFCharge(GetNumerator()*val.GetDenominator(),
201 val.GetNumerator()*m_ChargeDen);
204 RFCharge RFCharge::operator/(
int val)
const {
205 return RFCharge(GetNumerator(), m_ChargeDen*val);
208 RFCharge& RFCharge::operator+=(
const RFCharge& val){
209 int den = val.GetDenominator();
210 int mnum = GetNumerator()*den + val.GetNumerator()*m_ChargeDen;
217 m_ChargeNum = std::abs(mnum);
226 RFCharge& RFCharge::operator+=(
int val){
227 int mnum = GetNumerator() + val*m_ChargeDen;
234 m_ChargeNum = std::abs(mnum);
242 RFCharge& RFCharge::operator-=(
const RFCharge& val){
243 int den = val.GetDenominator();
244 int mnum = GetNumerator()*den - val.GetNumerator()*m_ChargeDen;
251 m_ChargeNum = std::abs(mnum);
260 RFCharge& RFCharge::operator-=(
int val){
261 int mnum = GetNumerator() - val*m_ChargeDen;
268 m_ChargeNum = std::abs(mnum);
276 RFCharge& RFCharge::operator*=(
const RFCharge& val){
277 int mnum = GetNumerator()*val.GetNumerator();
291 m_ChargeNum = std::abs(mnum);
292 m_ChargeDen *= val.GetDenominator();
300 RFCharge& RFCharge::operator*=(
int val){
301 int mnum = GetNumerator()*val;
315 m_ChargeNum = std::abs(mnum);
323 RFCharge& RFCharge::operator/=(
const RFCharge& val){
324 int num = val.GetNumerator();
336 m_ChargeDen *= std::abs(num);
337 m_ChargeNum *= val.GetDenominator();
345 RFCharge& RFCharge::operator/=(
int val){
356 m_ChargeDen *= std::abs(val);
364 RFCharge RFCharge::operator-() {
368 RFCharge::operator double()
const {
369 return double(GetNumerator())/
373 RFCharge operator*(
int val1,
const RFCharge& val2){
377 RFCharge operator/(
int val1,
const RFCharge& val2){
378 return RFCharge(val1)/val2;
381 int gcd(
int x,
int y){