Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. /*
  2. a) Adding two Rational numbers. The result should be stored in reduced form.
  3. b) Subtracting two Rational numbers. The result should be stored in reduced form.
  4. c) Multiplying two Rational numbers. The result should be stored in reduced form.
  5. d) Dividing two Rational numbers. The result should be stored in reduced form.
  6. e) Printing Rational numbers in the form a/b where a is the numerator and b is the denominator.
  7. f) Printing Rational numbers in floating-point format.
  8. */
  9.  
  10. #ifndef FRAZIONE_H_
  11. #define FRAZIONE_H_
  12. #include <cassert>
  13. #include <iostream>
  14. using namespace std;
  15.  
  16. namespace pius {
  17.  
  18. class frazione {
  19.  
  20. friend ostream& operator <<(ostream& out, const frazione& f){out << f.num << "/" << f.den; return out;}
  21. public:
  22. frazione(const int& initial_num = 1, const int& initial_den = 1) {
  23. assert(initial_den != 0);
  24. num = initial_num;
  25. den = initial_den;
  26. reduce_fraction();
  27. }
  28. void reduce_fraction ();
  29. void operator =(const frazione& f){num = f.get_num(); den = f.get_den();}
  30. int get_num()const{return num;}
  31. int get_den()const{return den;}
  32. float stampa(){float x; x = (float)num/(float)den; return x;}
  33. bool operator ==(const frazione& x){return (this->get_num()*x.den==x.num*this->get_den());}
  34. bool operator !=(const frazione& x){return !(this->get_num()*x.den==x.num*this->get_den());}
  35. bool operator <=(const frazione& x){return (this->get_num()*x.den<=x.num*this->get_den());}
  36. bool operator >=(const frazione& x){return (this->get_num()*x.den>=x.num*this->get_den());}
  37. bool operator >(const frazione& x){return (this->get_num()*x.den>x.num*this->get_den());}
  38. bool operator <(const frazione& x){return (this->get_num()*x.den<x.num*this->get_den());}
  39.  
  40.  
  41. private:
  42. int num, den;
  43. };
  44. //toolkit
  45. frazione operator +(const frazione& fattore1, const frazione& fattore2);
  46. frazione operator -(const frazione& fattore1, const frazione& fattore2);
  47. frazione operator *(const frazione& fattore1, const frazione& fattore2);
  48. frazione operator /(const frazione& fattore1, const frazione& fattore2);
  49. } /* namespace pius */
  50.  
  51. #endif /* FRAZIONE_H_ */
  52.  
  53.  
  54.  
  55. ////////////////////////////////////////////////////////////////////////////////////////////
  56.  
  57.  
  58. /*
  59. * frazione.cpp
  60. *
  61. * Created on: 04 gen 2017
  62. * Author: valyfox
  63. */
  64.  
  65. #include "frazione.h"
  66.  
  67. namespace pius {
  68. void frazione::reduce_fraction ()
  69. {
  70. for (int i = den * num; i > 1; i--) {
  71. if ((den % i == 0) && (num % i == 0)) {
  72. den /= i;
  73. num /= i;
  74. }
  75. }
  76. }
  77.  
  78. frazione operator +(const frazione& fattore1, const frazione& fattore2){
  79. int num1, den1, num2, den2, numeratore, denominatore;
  80. num1 = fattore1.get_num();
  81. den1 = fattore1.get_den();
  82. num2 = fattore2.get_num();
  83. den2 = fattore2.get_den();
  84.  
  85. denominatore = den1 * den2;
  86. numeratore = (den2 * num1) + (den1 * num2);
  87.  
  88. frazione f(numeratore, denominatore);
  89. return f;
  90. }
  91. frazione operator -(const frazione& fattore1, const frazione& fattore2){
  92. int num1, den1, num2, den2, numeratore, denominatore;
  93. num1 = fattore1.get_num();
  94. den1 = fattore1.get_den();
  95. num2 = fattore2.get_num();
  96. den2 = fattore2.get_den();
  97.  
  98. denominatore = den1 * den2;
  99. numeratore = (den2 * num1) - (den1 * num2);
  100. frazione f(numeratore, denominatore);
  101. return f;
  102. }
  103. frazione operator *(const frazione& fattore1, const frazione& fattore2){
  104. int num1, den1, num2, den2, numeratore, denominatore;
  105. num1 = fattore1.get_num();
  106. den1 = fattore1.get_den();
  107. num2 = fattore2.get_num();
  108. den2 = fattore2.get_den();
  109. numeratore = num1 * num2;
  110. denominatore = den1 * den2;
  111. frazione f(numeratore, denominatore);
  112. return f;
  113. }
  114. frazione operator /(const frazione& fattore1, const frazione& fattore2){
  115. int num1, den1, num2, den2, numeratore, denominatore;
  116. num1 = fattore1.get_num();
  117. den1 = fattore1.get_den();
  118. num2 = fattore2.get_num();
  119. den2 = fattore2.get_den();
  120. numeratore = num1 * den2;
  121. denominatore = den1 * num2;
  122. frazione f(numeratore, denominatore);
  123. return f;
  124. }
  125. }
  126. /* namespace pius */
  127.  
  128.  
  129. ///////////////////////////////////////////////////////////////////////////////////////////////////
  130.  
  131. #include "frazione.h"
  132. using namespace pius;
  133.  
  134. int main(){
  135. frazione f1(7, 3);
  136. frazione f4(8, 4);
  137. frazione f2(1, 3);
  138.  
  139. cout << f1 << "\n" << f2 << endl;
  140. frazione f3;
  141. f3 = f1 + f2;
  142. cout << f3 << endl;
  143. f3 = f1 - f2;
  144. cout << f3 << endl;
  145. f3 = f1 * f2;
  146. cout << f3 << endl;
  147. f3 = f1 / f2;
  148. cout << f3 << endl;
  149. cout << f1.stampa() << endl;
  150.  
  151. if (f1==f1)
  152. cout<<"sono uguali";
  153.  
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement