Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //// CFRACTION.H ////
- #ifndef CFRACTION_H
- #define CFRACTION_H
- class CFraction {
- public:
- CFraction(int nCount = 0, int nDenom = 1);
- CFraction(double nVal, int nMaxDenom = 100);
- CFraction(const CFraction &rOrig);
- ~CFraction();
- void Add(const CFraction &rOther);
- void Sub(const CFraction &rOther);
- void Mul(const CFraction &rOther);
- void Div(const CFraction &rOther);
- void Exp(int nExpand);
- void Red(int nReduce = 0);
- double Double() const;
- CFraction operator+(const CFraction &rOther) const;
- CFraction operator-(const CFraction &rOther) const;
- CFraction operator*(const CFraction &rOther) const;
- CFraction operator/(const CFraction &rOther) const;
- CFraction &operator =(const CFraction &rOrig);
- friend std::ostream &operator <<(std::ostream &os, const CFraction &frac);
- friend std::istream &operator >>(std::istream &is, CFraction &frac);
- private:
- static int gcd(int a, int b);
- int m_nCount;
- int m_nDenom;
- };
- std::ostream &operator <<(std::ostream &os, const CFraction &frac);
- std::istream &operator >>(std::istream &is, CFraction &frac);
- #endif /* CFRACTION_H */
- //// CFRACTION.CPP ////
- #include <iostream>
- #include <stdlib.h>
- #include "cfraction.h"
- using std::cout;
- using std::endl;
- class CPseudoFraction {
- public:
- CPseudoFraction(int count, int denom) :
- m_nCount(count), m_nDenom(denom) {
- }
- CPseudoFraction(const CPseudoFraction &low, const CPseudoFraction &high) :
- m_nCount(low.m_nCount + high.m_nCount), m_nDenom(low.m_nDenom + high.m_nDenom) {
- }
- double compare(double f) {
- if (m_nDenom == 0) {
- return -1;
- }
- else {
- return f - ((double)m_nCount / (double)m_nDenom);
- }
- }
- int m_nCount;
- int m_nDenom;
- };
- CFraction::CFraction(int nCount, int nDenom) :
- m_nCount(nCount), m_nDenom(nDenom) {
- cout << "CFraction::CFraction(int nCount, int nDenom)" << endl;
- Red();
- }
- CFraction::CFraction(double fVal, int nMaxDenom) :
- m_nCount(0), m_nDenom(1) {
- cout << "CFraction::CFraction(double fVal, int nMaxDenom)" << endl;
- CPseudoFraction low(0, 1);
- CPseudoFraction high(1, 0);
- CPseudoFraction middle(low, high);
- while (middle.m_nDenom < nMaxDenom) {
- if (middle.compare(fVal) < 0) {
- high = middle;
- }
- else {
- low = middle;
- }
- middle = CPseudoFraction(low, high);
- }
- if (middle.compare(fVal) < 0) {
- m_nCount = low.m_nCount;
- m_nDenom = low.m_nDenom;
- }
- else {
- m_nCount = high.m_nCount;
- m_nDenom = high.m_nDenom;
- }
- }
- CFraction::CFraction(const CFraction &rOrig) :
- m_nCount(rOrig.m_nCount), m_nDenom(rOrig.m_nDenom) {
- cout << "CFraction::CFraction(const CFraction &rOrig)" << endl;
- }
- CFraction::~CFraction() {
- cout << "CFraction::~CFraction()" << endl;
- }
- //void CFraction::Add(const CFraction &rOther);
- //void CFraction::Sub(const CFraction &rOther);
- //void CFraction::Mul(const CFraction &rOther);
- //void CFraction::Div(const CFraction &rOther);
- void CFraction::Exp(int nExpand)
- {
- m_nCount *= nExpand;
- m_nDenom *= nExpand;
- }
- void CFraction::Red(int nReduce)
- {
- if (nReduce == 0) {
- nReduce = gcd(m_nCount, m_nDenom);
- }
- m_nCount /= nReduce;
- m_nDenom /= nReduce;
- if (m_nDenom < 0) {
- m_nCount *= -1;
- m_nDenom *= -1;
- }
- }
- double CFraction::Double() const
- {
- return (double)m_nCount / (double)m_nDenom;
- }
- CFraction CFraction::operator+(const CFraction &rOther) const
- {
- CFraction result(m_nCount*rOther.m_nDenom + rOther.m_nCount*m_nDenom,
- m_nDenom*rOther.m_nDenom);
- return result;
- }
- //CFraction CFraction::operator-(const CFraction &rOther) const;
- //CFraction CFraction::operator*(const CFraction &rOther) const;
- //CFraction CFraction::operator/(const CFraction &rOther) const;
- CFraction &CFraction::operator =(const CFraction &rOrig)
- {
- m_nCount = rOrig.m_nCount;
- m_nDenom = rOrig.m_nDenom;
- return *this;
- }
- //CFraction::operator double() const
- //
- //{
- // return Double();
- //}
- //
- //CFraction::operator bool() const
- //
- //{
- // return m_nCount != 0;
- //}
- int CFraction::gcd(int a, int b)
- {
- a = abs(a);
- b = abs(b);
- while (b != 0) {
- int r = a % b;
- a = b;
- b = r;
- }
- return a;
- }
- std::ostream &operator <<(std::ostream &os, const CFraction &frac)
- {
- os << frac.m_nCount << "/" << frac.m_nDenom;
- return os;
- }
- std::istream &operator >> (std::istream &is, CFraction &frac);
- //// MAIN.CPP ////
- #include <cstdlib>
- #include <iostream>
- #include "CFraction.h"
- CFraction getFractionValue();
- /*
- *
- */
- int main(int argc, char** argv) {
- CFraction pi(3.1415926, 100);
- std::cout << pi << " " << pi.Double() << std::endl;
- CFraction x = getFractionValue();
- std::cout << x << std::endl;
- system("PAUSE");
- return 0;
- }
- CFraction getFractionValue()
- {
- CFraction result(12, 6);
- return result + CFraction(20);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement