Advertisement
HellFinger

Untitled

May 27th, 2020
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int gcd(int a,int b)
  5. {
  6. if (b==0) return a;
  7. else return gcd(b,a%b);
  8. }
  9.  
  10.  
  11. class fraction
  12. {
  13. private:
  14. int divident;
  15. int divider;
  16.  
  17. public:
  18. fraction() {}
  19. fraction(int up, int down){
  20. divident = up;
  21. divider = down;
  22. }
  23. void setDivident(int d){
  24. divident = d;
  25. }
  26.  
  27. void setDivider(int d){
  28. divider = d;
  29. }
  30.  
  31. int getDivident(){
  32. return divident;
  33. }
  34.  
  35. int getDivider(){
  36. return divider;
  37. }
  38.  
  39. fraction operator+ (fraction s){
  40. fraction result;
  41. if (this->getDivider() == s.getDivider()){
  42. result.setDivident(s.getDivident() + this->getDivident());
  43. result.setDivider(this->getDivider());
  44. return result;
  45. }
  46.  
  47. int nod = gcd(this->getDivider(), s.getDivider());
  48.  
  49. if (nod != 1) {
  50. int side_a = s.getDivider()/nod;
  51. int side_b = this->getDivider()/nod;
  52.  
  53. result.setDivident(side_a*this->getDivident() + side_b*s.getDivident());
  54. result.setDivider(side_a*side_b*nod);
  55. }
  56. else{
  57. result.setDivident(s.getDivider()*this->getDivident() + this->getDivider()*s.getDivident());
  58. result.setDivider(s.getDivider()*this->getDivider());
  59. }
  60.  
  61.  
  62. return result;
  63. }
  64.  
  65. void operator=(fraction r){
  66. this->setDivident(r.getDivident());
  67. this->setDivider(r.getDivider());
  68.  
  69. }
  70.  
  71. void print(){
  72. cout << divident << "/" << divider << endl;
  73. }
  74.  
  75. };
  76.  
  77.  
  78. int main()
  79. {
  80.  
  81. fraction a(1,2);
  82. fraction b(3,4);
  83. fraction c(1,3);
  84.  
  85. fraction d = c+ a;
  86.  
  87. d.print();
  88.  
  89. return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement