Advertisement
Guest User

Untitled

a guest
May 31st, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.74 KB | None | 0 0
  1. //// CFRACTION.H ////
  2.  
  3. #ifndef CFRACTION_H
  4. #define CFRACTION_H
  5.  
  6. class CFraction {
  7. public:
  8.     CFraction(int nCount = 0, int nDenom = 1);
  9.     CFraction(double nVal, int nMaxDenom = 100);
  10.     CFraction(const CFraction &rOrig);
  11.     ~CFraction();
  12.     void Add(const CFraction &rOther);
  13.     void Sub(const CFraction &rOther);
  14.     void Mul(const CFraction &rOther);
  15.     void Div(const CFraction &rOther);
  16.     void Exp(int nExpand);
  17.     void Red(int nReduce = 0);
  18.     double Double() const;
  19.     CFraction operator+(const CFraction &rOther) const;
  20.     CFraction operator-(const CFraction &rOther) const;
  21.     CFraction operator*(const CFraction &rOther) const;
  22.     CFraction operator/(const CFraction &rOther) const;
  23.     CFraction &operator =(const CFraction &rOrig);
  24.     friend std::ostream &operator <<(std::ostream &os, const CFraction &frac);
  25.     friend std::istream &operator >>(std::istream &is, CFraction &frac);
  26. private:
  27.     static int gcd(int a, int b);
  28.     int m_nCount;
  29.     int m_nDenom;
  30. };
  31.  
  32. std::ostream &operator <<(std::ostream &os, const CFraction &frac);
  33. std::istream &operator >>(std::istream &is, CFraction &frac);
  34.  
  35. #endif  /* CFRACTION_H */
  36.  
  37. //// CFRACTION.CPP ////
  38.  
  39. #include <iostream>
  40. #include <stdlib.h>
  41.  
  42. #include "cfraction.h"
  43.  
  44. using std::cout;
  45. using std::endl;
  46.  
  47. class CPseudoFraction {
  48. public:
  49.     CPseudoFraction(int count, int denom) :
  50.         m_nCount(count), m_nDenom(denom) {
  51.     }
  52.     CPseudoFraction(const CPseudoFraction &low, const CPseudoFraction &high) :
  53.         m_nCount(low.m_nCount + high.m_nCount), m_nDenom(low.m_nDenom + high.m_nDenom) {
  54.     }
  55.     double compare(double f) {
  56.         if (m_nDenom == 0) {
  57.             return -1;
  58.         }
  59.         else {
  60.             return f - ((double)m_nCount / (double)m_nDenom);
  61.         }
  62.     }
  63.     int m_nCount;
  64.     int m_nDenom;
  65. };
  66.  
  67. CFraction::CFraction(int nCount, int nDenom) :
  68.     m_nCount(nCount), m_nDenom(nDenom) {
  69.     cout << "CFraction::CFraction(int nCount, int nDenom)" << endl;
  70.     Red();
  71. }
  72.  
  73. CFraction::CFraction(double fVal, int nMaxDenom) :
  74.     m_nCount(0), m_nDenom(1) {
  75.     cout << "CFraction::CFraction(double fVal, int nMaxDenom)" << endl;
  76.     CPseudoFraction low(0, 1);
  77.     CPseudoFraction high(1, 0);
  78.     CPseudoFraction middle(low, high);
  79.  
  80.     while (middle.m_nDenom < nMaxDenom) {
  81.         if (middle.compare(fVal) < 0) {
  82.             high = middle;
  83.         }
  84.         else {
  85.             low = middle;
  86.         }
  87.         middle = CPseudoFraction(low, high);
  88.     }
  89.     if (middle.compare(fVal) < 0) {
  90.         m_nCount = low.m_nCount;
  91.         m_nDenom = low.m_nDenom;
  92.     }
  93.     else {
  94.         m_nCount = high.m_nCount;
  95.         m_nDenom = high.m_nDenom;
  96.     }
  97. }
  98.  
  99. CFraction::CFraction(const CFraction &rOrig) :
  100.     m_nCount(rOrig.m_nCount), m_nDenom(rOrig.m_nDenom) {
  101.     cout << "CFraction::CFraction(const CFraction &rOrig)" << endl;
  102. }
  103.  
  104. CFraction::~CFraction() {
  105.     cout << "CFraction::~CFraction()" << endl;
  106. }
  107.  
  108. //void CFraction::Add(const CFraction &rOther);
  109. //void CFraction::Sub(const CFraction &rOther);
  110. //void CFraction::Mul(const CFraction &rOther);
  111. //void CFraction::Div(const CFraction &rOther);
  112.  
  113. void CFraction::Exp(int nExpand)
  114.  
  115. {
  116.     m_nCount *= nExpand;
  117.     m_nDenom *= nExpand;
  118. }
  119.  
  120. void CFraction::Red(int nReduce)
  121.  
  122. {
  123.     if (nReduce == 0) {
  124.         nReduce = gcd(m_nCount, m_nDenom);
  125.     }
  126.     m_nCount /= nReduce;
  127.     m_nDenom /= nReduce;
  128.     if (m_nDenom < 0) {
  129.         m_nCount *= -1;
  130.         m_nDenom *= -1;
  131.     }
  132. }
  133.  
  134. double CFraction::Double() const
  135.  
  136. {
  137.     return (double)m_nCount / (double)m_nDenom;
  138. }
  139.  
  140. CFraction CFraction::operator+(const CFraction &rOther) const
  141.  
  142. {
  143.     CFraction result(m_nCount*rOther.m_nDenom + rOther.m_nCount*m_nDenom,
  144.         m_nDenom*rOther.m_nDenom);
  145.     return result;
  146. }
  147.  
  148. //CFraction CFraction::operator-(const CFraction &rOther) const;
  149. //CFraction CFraction::operator*(const CFraction &rOther) const;
  150. //CFraction CFraction::operator/(const CFraction &rOther) const;
  151.  
  152. CFraction &CFraction::operator =(const CFraction &rOrig)
  153.  
  154. {
  155.     m_nCount = rOrig.m_nCount;
  156.     m_nDenom = rOrig.m_nDenom;
  157.     return *this;
  158.  
  159. }
  160.  
  161. //CFraction::operator double() const
  162. //
  163. //{
  164. //   return Double();
  165. //}
  166. //
  167. //CFraction::operator bool() const
  168. //
  169. //{
  170. //    return m_nCount != 0;
  171. //}
  172.  
  173. int CFraction::gcd(int a, int b)
  174.  
  175. {
  176.     a = abs(a);
  177.     b = abs(b);
  178.  
  179.     while (b != 0) {
  180.         int r = a % b;
  181.         a = b;
  182.         b = r;
  183.     }
  184.  
  185.     return a;
  186. }
  187.  
  188. std::ostream &operator <<(std::ostream &os, const CFraction &frac)
  189.  
  190. {
  191.     os << frac.m_nCount << "/" << frac.m_nDenom;
  192.     return os;
  193. }
  194.  
  195. std::istream &operator >> (std::istream &is, CFraction &frac);
  196.  
  197.  
  198. //// MAIN.CPP ////
  199.  
  200. #include <cstdlib>
  201. #include <iostream>
  202.  
  203. #include "CFraction.h"
  204.  
  205. CFraction getFractionValue();
  206. /*
  207. *
  208. */
  209. int main(int argc, char** argv) {
  210.     CFraction pi(3.1415926, 100);
  211.  
  212.     std::cout << pi << " " << pi.Double() << std::endl;
  213.  
  214.     CFraction x = getFractionValue();
  215.     std::cout << x << std::endl;
  216.     system("PAUSE");
  217.     return 0;
  218. }
  219.  
  220. CFraction getFractionValue()
  221.  
  222. {
  223.     CFraction result(12, 6);
  224.     return result + CFraction(20);
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement