Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. .h
  2. struct fraction {
  3. public:
  4. fraction();
  5. fraction(signed int a, signed int b);
  6. fraction(signed int a);
  7. fraction(std::string s);
  8. double frac_eval();
  9. signed int numerator();
  10. signed int denominator();
  11. std::string str();
  12. std::string tex();
  13. fraction& operator +(fraction &a1);
  14. fraction& operator -(fraction &a1);
  15. fraction& operator *(fraction &a1);
  16. fraction& operator /(fraction &a1);
  17. fraction& operator +(int a1);
  18. fraction& operator -(int a1);
  19. fraction& operator *(int a1);
  20. fraction& operator /(int a1);
  21. private:
  22. signed int top;
  23. signed int bot;
  24. };
  25.  
  26.  
  27.  
  28.  
  29.  
  30. ######################################################################################################################
  31. .cpp
  32. std::string fraction::str() {
  33. if (top == 0)
  34. return "0";
  35. else if (bot == 1)
  36. return std::to_string(top);
  37. else
  38. return boost::str(boost::format("%1%/%2%") % std::to_string(top) % std::to_string(bot));
  39. }
  40. std::string fraction::tex() {
  41. if (top == 0)
  42. return "0";
  43. else if (bot == 1)
  44. return std::to_string(top);
  45. else
  46. return boost::str(boost::format("\\frac{%1%}{%2%}") % std::to_string(top) % std::to_string(bot));
  47. }
  48. fraction::fraction(signed int a, signed int b) :top(a), bot(b) {}
  49. fraction::fraction(signed int a) : top(a), bot(1) {}
  50. fraction::fraction(std::string s) {
  51. size_t pos = s.find_first_of("/");
  52. top = std::stoi(s.substr(0, pos));
  53. if (pos == std::string::npos) bot = 1;
  54. else bot = std::stoi(s.substr(pos + 1, s.length()));
  55. }
  56. fraction::fraction() {}
  57. double fraction::frac_eval() { return (double)top / (double)bot; }
  58. signed int fraction::numerator() { return top; }
  59. signed int fraction::denominator() { return bot; }
  60. fraction& fraction::operator+(fraction &a1) {
  61. top = a1.numerator()*bot + a1.denominator()*top;
  62. bot = a1.denominator()*bot;
  63. for (int i = abs(top*bot); i > 1; i--) {
  64. if ((top % i == 0) && (bot % i == 0)) {
  65. bot /= i;
  66. top /= i;
  67. }
  68. }
  69. return *this;
  70. }
  71. fraction& fraction::operator-(fraction &a1) {
  72. top = -a1.numerator()*bot + a1.denominator()*top;
  73. bot = a1.denominator()*bot;
  74. for (int i = abs(top*bot); i > 1; i--) {
  75. if ((top % i == 0) && (bot % i == 0)) {
  76. bot /= i;
  77. top /= i;
  78. }
  79. }
  80. return *this;
  81. }
  82. fraction& fraction::operator*(fraction &a1) {
  83. top = a1.numerator()*top;
  84. bot = a1.denominator()*bot;
  85. for (int i = abs(top*bot); i > 1; i--) {
  86. if ((top % i == 0) && (bot % i == 0)) {
  87. bot /= i;
  88. top /= i;
  89. }
  90. }
  91. return *this;
  92. }
  93. fraction& fraction::operator/(fraction &a1) {
  94. bot = a1.numerator()*bot;
  95. top = a1.denominator()*top;
  96. for (int i = abs(top*bot); i > 1; i--) {
  97. if ((top % i == 0) && (bot % i == 0)) {
  98. bot /= i;
  99. top /= i;
  100. }
  101. }
  102. return *this;
  103. }
  104.  
  105. fraction & fraction::operator+(int a1)
  106. {
  107. top = a1*bot +top;
  108.  
  109. for (int i = abs(top*bot); i > 1; i--) {
  110. if ((top % i == 0) && (bot % i == 0)) {
  111. bot /= i;
  112. top /= i;
  113. }
  114. }
  115. return *this;
  116. }
  117.  
  118. fraction & fraction::operator-(int a1)
  119. {
  120. top = -a1*bot + top;
  121.  
  122. for (int i = abs(top*bot); i > 1; i--) {
  123. if ((top % i == 0) && (bot % i == 0)) {
  124. bot /= i;
  125. top /= i;
  126. }
  127. }
  128. return *this;
  129. }
  130.  
  131. fraction & fraction::operator*(int a1)
  132. {
  133. top = a1*top;
  134.  
  135. for (int i = abs(top*bot); i > 1; i--) {
  136. if ((top % i == 0) && (bot % i == 0)) {
  137. bot /= i;
  138. top /= i;
  139. }
  140. }
  141. return *this;
  142. }
  143.  
  144. fraction & fraction::operator/(int a1)
  145. {
  146. bot = a1*bot;
  147.  
  148. for (int i = abs(top*bot); i > 1; i--) {
  149. if ((top % i == 0) && (bot % i == 0)) {
  150. bot /= i;
  151. top /= i;
  152. }
  153. }
  154. return *this;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement