Advertisement
Guest User

Untitled

a guest
Sep 21st, 2014
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. using namespace std;
  2.  
  3. class fraction
  4. {
  5. long num;
  6. long den;
  7.  
  8. public:
  9. fraction(long,long);
  10. fraction();
  11. ~fraction();
  12. void setNum ( long );
  13. void setDen ( long );
  14. long getNum ( void );
  15. long getDen ( void );
  16.  
  17. void print (void);
  18.  
  19. void add (fraction, fraction);
  20. void sub (fraction, fraction);
  21. void mult (fraction, fraction);
  22. void div (fraction, fraction);
  23. void inc (fraction);
  24.  
  25. } ; // end of class fraction
  26.  
  27. long gcd (long x, long y);
  28.  
  29. fraction::fraction(long l_num, long l_den)
  30. {
  31.  
  32. num = l_num;
  33. den = l_den;
  34. }
  35. fraction::fraction()
  36. {
  37.  
  38. }
  39. fraction::~fraction()
  40. {
  41.  
  42.  
  43. }
  44.  
  45. void fraction::setNum (long l_num )
  46. {
  47. num = l_num ;
  48. }
  49.  
  50.  
  51. void fraction::setDen (long l_den )
  52. {
  53. den = l_den ;
  54. }
  55.  
  56. long fraction::getDen ( )
  57. {
  58. return den ;
  59. }
  60.  
  61. long fraction::getNum ( )
  62. {
  63. return num ;
  64. }
  65.  
  66. void fraction:: print (void)
  67. {
  68. cout<<num/gcd(num,den)<<"/"<<den/gcd(num,den) <<endl;
  69. }
  70.  
  71. void fraction::add (fraction f1, fraction f2)
  72. {
  73. num = (f1.getNum ( ) * f2.getDen ( )) + ( f1.getDen ( ) * f2.getNum ( ) );
  74. den = (f1.getDen ( ) * f2.getDen ( ));
  75. }
  76.  
  77. void fraction::sub (fraction f1, fraction f2)
  78. {
  79. num = (f1.getNum ( ) * f2.getDen ( )) - ( f1.getDen ( ) * f2.getNum ( ) );
  80. den = (f1.getDen ( ) * f2.getDen ( ));
  81. }
  82.  
  83. void fraction::mult (fraction f1, fraction f2)
  84. {
  85. num = (f1.getNum ( ) * f2.getNum ( ));
  86. den = (f1.getDen ( ) * f2.getDen ( ));
  87.  
  88. }
  89.  
  90. void fraction::div(fraction f1, fraction f2)
  91. {
  92.  
  93. num = (f1.getNum ( ) * f2.getDen ( ));
  94. den = (f1.getDen ( ) * f2.getNum ( ));
  95. }
  96.  
  97. void fraction::inc (fraction f1)
  98. {
  99. num = (f1.getNum ( )) + ( f1.getDen ( ) );
  100. den = (f1.getDen ( ) );
  101.  
  102. }
  103.  
  104.  
  105. long gcd (long x, long y)
  106. {
  107. return (x == 0) ? y : gcd (y%x, x);
  108. }
  109.  
  110. int main ( )
  111. {
  112. // define seven instances of the class fraction
  113. fraction f1(1L,2L),f2(3L,4L),f3, f4,f5,f6, f7;
  114.  
  115. //set values for the numerator and denominator to f1 and print them
  116. //f1.setDen( 2L);
  117. //f1.setNum( 0L);
  118. f1.print();
  119.  
  120. //set values for the numerator and denominator to f2 and print them
  121. //f2.setDen( 4L);
  122. //f2.setNum( 3L);
  123. f2.print();
  124.  
  125. f3.add( f1, f2);
  126. f3.print();
  127.  
  128. f4.sub( f1, f2);
  129. f4.print();
  130.  
  131. f5.mult( f1, f2);
  132. f5.print();
  133.  
  134. f6.div( f1, f2);
  135. f6.print();
  136.  
  137. f7.inc(f1);
  138. f7.print();
  139.  
  140. return 0;
  141.  
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement