rational.h
来自「开放源码的编译器open watcom 1.6.0版的源代码」· C头文件 代码 · 共 420 行 · 第 1/2 页
H
420 行
// operator== -- Overload the equality operator for the rational class
// Input: Reference to rational object
// Output: TRUE/FALSE
inline Boolean CoolRational::operator== (const CoolRational& r) const {
return (this->num == r.num && this->den == r.den);
}
// operator!= -- Overload the inequality operator for the Rational class
// Input: Reference to a constant rational object
// Ouput: TRUE/FALSE
inline Boolean CoolRational::operator!= (const CoolRational& r) const {
return !(*this == r);
}
// operator< -- Overload operator less than for the rational class
// Input: Reference to rational number
// Output: TRUE/FALSE
inline Boolean CoolRational::operator< (const CoolRational& r) const {
if (this->den == r.den) // If same denominator
return (this->num < r.num); // Return less than sense
else // Else calculate common denom.
return ((this->num * r.den) < (this->den * r.num)); // And return state
}
// operator> -- Overload operator greater than for the rational class
// Input: Reference to rational number
// Output: TRUE/FALSE
inline Boolean CoolRational::operator> (const CoolRational& r) const {
if (this->den == r.den) // If same denominator
return (this->num > r.num); // Return greater than sense
else // Else calculate common denom.
return ((this->num * r.den) > (this->den * r.num)); // And return state
}
// operator<= -- Overload the less than or equal to operator for rational
// Input: Reference to a constant rational object
// Output: TRUE/FALSE
inline Boolean CoolRational::operator<= (const CoolRational& r) const {
return !(*this > r);
}
// operator>= -- Overload the greater than or equal to operator for rational
// Input: Reference to a constant rational object
// Output: TRUE/FALSE
inline Boolean CoolRational::operator>= (const CoolRational& r) const {
return !(*this < r);
}
// operator<< -- Overload the output operator for a reference to a rational
// Input: Reference to an ostream
// Output: Reference to an ostream
inline ostream& operator<< (ostream& os, const CoolRational& r) {
os << r.num << "/" << r.den; // Output rational number
return os; // Return ostream reference
}
// operator<< -- Overload the output operator for a pointer to a rational
// Input: Ostream reference, pointer to a rational object
// Output: Ostream reference
inline ostream& operator<< (ostream& os, const CoolRational* r) {
return operator<< (os, *r);
}
// operator- -- Overload the unary minus operator for the Rational class
// Input: None
// Output: Negated rational value
inline CoolRational CoolRational::operator- () const {
CoolRational r (-this->num, this->den); // Get negative value
return r; // Return deferenced pointer
}
// operator! -- Overload the negation operator for the Rational class
// Input: None
// Output: TRUE/FALSE
inline Boolean CoolRational::operator! () const {
return ((this->num == 0) ? TRUE : FALSE); // Return logical state
}
// operator++ -- Overload the increment operator for the Rational class
// Input: None
// Output: Reference to updated rational object
inline CoolRational& CoolRational::operator++ () {
this->num += this->den; // Increment numerator
return *this; // Return updated object
}
// operator-- -- Overload the decrement operator for the Rational class
// Input: None
// Output: Reference to updated rational object
inline CoolRational& CoolRational::operator-- () {
this->num -= this->den; // Decrement numerator
return *this; // Return updated object
}
// truncate -- Converts rational value by truncating towards zero
// Input: None
// Output: long truncated value
inline long CoolRational::truncate () const {
return (long) (this->num / this->den); // Return truncated rational
}
// floor -- Converts rational value by truncating towards negative infinity
// Input: None
// Output: long value of truncated rational
inline long CoolRational::floor () const {
long temp = this->truncate (); // Get truncated value
return ((this->num < 0.0 && (-(this->num%this->den) > 0.5*this->den)) ?
(temp - 1) : temp);
}
// ceiling -- Converts rational value by truncating towards positive infinity
// Input: None
// Output: long value of truncated rational
inline long CoolRational::ceiling () const {
long temp = this->truncate (); // Get truncated value
return ((this->num > 0.0 && ((this->num%this->den) > 0.5*this->den)) ?
(temp + 1) : temp);
}
// operator+ -- Overload the addition operator for rational
// Input: Reference to two rational numbers
// Output: A new rational number
inline CoolRational operator+ (const CoolRational& r1, const CoolRational& r2) {
CoolRational result(r1);
result += r2;
return result;
}
// operator- -- Overload the subtraction operator for rational
// Input: Reference to two rational numbers
// Output: A new rational number
inline CoolRational operator- (const CoolRational& r1, const CoolRational& r2) {
CoolRational result(r1);
result -= r2;
return result;
}
// operator* -- Overload the multiplication operator for rational
// Input: Reference to two rational numbers
// Output: A new rational number
inline CoolRational operator* (const CoolRational& r1, const CoolRational& r2) {
CoolRational result(r1);
result *= r2;
return result;
}
// operator/ -- Overload the division operator for rational
// Input: Reference to two rational numbers
// Output: A new rational number
inline CoolRational operator/ (const CoolRational& r1, const CoolRational& r2) {
CoolRational result(r1);
result /= r2;
return result;
}
// operator% -- Overload the remainder operator for rational
// Input: Reference to two rational numbers
// Output: A new rational number
inline CoolRational operator% (const CoolRational& r1, const CoolRational& r2) {
CoolRational result(r1);
result %= r2;
return result;
}
// operator double -- Implicit conversion operator from Rational to a double
// Input: None
// Output: Double value equivalent to rational
inline CoolRational::operator double () {
return ((double)this->num)/((double)this->den); // Double answer
}
#endif
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?