Need help with a Reduce Fraction as part of a Rational Class

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2006
Posts: 10
Reputation: lanario is an unknown quantity at this point 
Solved Threads: 0
lanario lanario is offline Offline
Newbie Poster

Need help with a Reduce Fraction as part of a Rational Class

 
0
  #1
Apr 18th, 2006
I'm lost based on the code provided, how to reduce a fraction down. I've looked on the internet and yes most are using global variables; I however, can not for my class. It must be within the function. And there needs to be two calls outside of each of the operators to the reduce() function. Where am I going wrong?





  1. #include <iostream>
  2. using namespace std;
  3. #include "Rational.h"
  4.  
  5. // the setNumerator function sets the numerator value for the object.
  6.  
  7. void Rational::setNumerator (int numer)
  8. {
  9. numeratorValue = numer;
  10. }
  11.  
  12. // setDenominator is responsible for changing the denominatorValue data
  13. // member. It does some basic error checking to make sure we don.t try
  14. // to set a 0 as the denominator. Our solution to this is to set the
  15. // denom to 1 after outputting an error message
  16.  
  17. void Rational::setDenominator (int denom)
  18. {
  19.  
  20. if (denom != 0)
  21. {
  22. denominatorValue = denom;
  23. }
  24. else
  25. {
  26. cerr << "Illegal denominator. Using 1 instead" << endl;
  27. denominatorValue = 1;
  28. }
  29.  
  30. return;
  31. }
  32.  
  33. // specific constructor, called when one or two arguments are given
  34. // in object instantiation. Sets rational to appropriate value
  35.  
  36. Rational::Rational (int numer, int denom)
  37. {
  38. setNumerator(numer);
  39. setDenominator(denom);
  40. reduce();
  41. }
  42.  
  43. // getNumerator and getDenominator are simple, private inspector
  44. // member functions that return the values of the numerator and
  45. // denominator for our object.
  46.  
  47. int Rational::getNumerator() const
  48. {
  49. return numeratorValue;
  50. }
  51.  
  52.  
  53. int Rational::getDenominator() const
  54. {
  55. return denominatorValue;
  56. }
  57.  
  58. // add() performs addition of two Rational objects. One object
  59. // is doing the addition, the other object (the argument) is being
  60. // added. This function creates a very (very!) temporary third
  61. // Rational object as it.s setting itself up to return a value. It
  62. // returns the newly created Rational object. This is one of
  63. // the few times you see a constructor called explicitly.
  64.  
  65. Rational Rational::add (const Rational &r) const
  66. {
  67. int a = getNumerator();
  68. int b = getDenominator();
  69. int c = r.getNumerator();
  70. int d = r.getDenominator();
  71. Rational result(a*d + b*c, b*d);
  72. result.reduce();
  73. return result;
  74. }
  75.  
  76. // multiply() performs multiplying two Rational objects. One object
  77. // is doing the multiplying, the other object (the argument) is being
  78. // multiplied. This function creates a very (very!) temporary third
  79. // Rational object as it.s setting itself up to return a value. It
  80. // returns the newly created Rational object. This is one of
  81. // the few times you see a constructor called explicitly.
  82.  
  83. Rational Rational::multiply (const Rational &r) const
  84. {
  85. int a = getNumerator();
  86. int b = getDenominator();
  87. int c = r.getNumerator();
  88. int d = r.getDenominator();
  89. Rational result(a*c, b*d);
  90. result.reduce();
  91. return result;
  92. }
  93.  
  94. // insert() takes an ostream as the only argument. An ostream
  95. // is an output stream, like cout. by making this an argument, you
  96. // could output Rational numbers to files or other devices or streams.
  97.  
  98. void Rational::insert (ostream &streamout) const
  99. {
  100. streamout << getNumerator() << "/" << getDenominator();
  101. return;
  102. }
  103.  
  104. // extract() takes an istream as the only argument. An istream
  105. // is an input stream, like cin. by making this an argument, you
  106. // could input Rational numbers from files or other devices or
  107. // streams.
  108.  
  109. void Rational::extract (istream &streamin)
  110. {
  111. int numer;
  112. int denom;
  113. char slash;
  114. streamin >> numer >> slash >> denom;
  115. setNumerator(numer);
  116. setDenominator(denom);
  117. return;
  118. }
  119.  
  120. // Here we implement the + operator. all this "function" does is end
  121. // up calling the add() behavior of the rational number class.
  122. // by overloading the operator, we are providing the .natural
  123. // interface. for the object that we are looking for to make working
  124. // with Rational objects intuitive.
  125.  
  126. Rational operator+ (const Rational &left, const Rational &right)
  127. {
  128. return left.add(right);
  129. }
  130.  
  131. // and here is the implementation for the * operator. We simply end up
  132. // calling the multiply() behavior of a rational object.
  133.  
  134. Rational operator* (const Rational &left, const Rational &right)
  135. {
  136. return left.multiply(right);
  137. }
  138.  
  139. // here is the implementation for the << operator. We simply end
  140. // up calling the insert() behavior of a rational object. Because
  141. // of the "nature of output" in C++ (to be explained in more detail in 1620)
  142. // we need to return the ostream out of the function. This is
  143. // a critical step in overloading the << operator.
  144.  
  145. ostream& operator<< (ostream &output, const Rational &rat)
  146. {
  147. rat.insert(output);
  148. return output;
  149. }
  150.  
  151. istream& operator>> (istream &input, Rational &rat)
  152. {
  153. rat.extract(input);
  154. return input;
  155. }
  156.  
  157.  
  158.  
  159. Rational Rational::subtract (const Rational &r) const
  160. {
  161. int a = getNumerator();
  162. int b = getDenominator();
  163. int c = r.getNumerator();
  164. int d = r.getDenominator();
  165. Rational result(a*d - b*c, b*d);
  166. result.reduce();
  167. return result;
  168. }
  169.  
  170. Rational operator- (const Rational &left, const Rational &right)
  171. {
  172. return left.subtract(right);
  173. }
  174.  
  175. Rational Rational::divide (const Rational &r) const
  176. {
  177. int a = getNumerator();
  178. int b = getDenominator();
  179. int c = r.getNumerator();
  180. int d = r.getDenominator();
  181. Rational result(a*d, b*c);
  182. result.reduce();
  183. return result;
  184. }
  185.  
  186. Rational operator/ (const Rational &left, const Rational &right)
  187. {
  188. return left.divide(right);
  189. }
  190.  
  191.  
  192. bool Rational::lessThan (const Rational &r) const
  193. {
  194. int a = getNumerator();
  195. int b = getDenominator();
  196. int c = r.getNumerator();
  197. int d = r.getDenominator();
  198. if (a*d < c*b)
  199. return true;
  200. else
  201. return false;
  202.  
  203. }
  204.  
  205. bool Rational::lessThanEqual (const Rational &r) const
  206. {
  207. int a = getNumerator();
  208. int b = getDenominator();
  209. int c = r.getNumerator();
  210. int d = r.getDenominator();
  211. if ( a*d <= c*b)
  212. return true;
  213. else
  214. return false;
  215. }
  216.  
  217. bool Rational::greaterThan (const Rational &r) const
  218. {
  219. int a = getNumerator();
  220. int b = getDenominator();
  221. int c = r.getNumerator();
  222. int d = r.getDenominator();
  223. if (a*d > c*b)
  224. return true;
  225. else
  226. return false;
  227. }
  228.  
  229. bool Rational::greaterThanEqual (const Rational &r) const
  230. {
  231. int a = getNumerator();
  232. int b = getDenominator();
  233. int c = r.getNumerator();
  234. int d = r.getDenominator();
  235. if (a*d >= c*b)
  236. return true;
  237. else
  238. return false;
  239. }
  240.  
  241. bool Rational::equalTo (const Rational &r) const
  242. {
  243. int a = getNumerator();
  244. int b = getDenominator();
  245. int c = r.getNumerator();
  246. int d = r.getDenominator();
  247. if ((a==c) && (b==d))
  248. return true;
  249. else
  250. return false;
  251. }
  252.  
  253. bool Rational::notEqualTo (const Rational &r) const
  254. {
  255. int a = getNumerator();
  256. int b = getDenominator();
  257. int c = r.getNumerator();
  258. int d = r.getDenominator();
  259. if ((a!=c) || (b!=d))
  260. return true;
  261. else
  262. return false;
  263. }
  264. void Rational::reduce()
  265. {
  266. int x = getNumerator();
  267. int a;
  268. x = a;
  269. int y = getDenominator();
  270. int b;
  271. y = b;
  272.  
  273.  
  274.  
  275. int i;
  276. while (i = (a % b))
  277. {
  278. a = b;
  279. b = i;
  280. }
  281. x /= b;
  282. y /= b;
  283.  
  284. }
  285.  
  286. bool operator< (const Rational &left, const Rational &right)
  287. {
  288. return left.lessThan(right);
  289. }
  290.  
  291. bool operator<= (const Rational &left, const Rational &right)
  292. {
  293. return left.lessThanEqual(right);
  294. }
  295.  
  296. bool operator> (const Rational &left, const Rational &right)
  297. {
  298. return left.greaterThan(right);
  299. }
  300.  
  301. bool operator>= (const Rational &left, const Rational &right)
  302. {
  303. return left.greaterThanEqual(right);
  304. }
  305.  
  306. bool operator== (const Rational &left, const Rational &right)
  307. {
  308. return left.equalTo(right);
  309. }
  310.  
  311. bool operator!= (const Rational &left, const Rational &right)
  312. {
  313. return left.notEqualTo(right);
  314. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,052
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Need help with a Reduce Fraction as part of a Rational Class

 
3
  #2
Apr 18th, 2006
  1. void Rational::reduce()
  2. {
  3. int x = getNumerator();
  4. int a;
  5. x = a;
What is the value of x supposed to be right now?
  1. int y = getDenominator();
  2. int b;
  3. y = b;
What is the value of y supposed to be right now?
  1. int i;
  2. while (i = (a % b))
Why are you doing arithmetic with a and b, if they are uninitialised?
  1. {
  2. a = b;
  3. b = i;
  4. }
  5. x /= b;
  6. y /= b;
  7. }
How are the numerator and denominator supposed to change if you don't actually set them to the newly calculated values?
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 3
Reputation: Xantox is an unknown quantity at this point 
Solved Threads: 0
Xantox Xantox is offline Offline
Newbie Poster

Re: Need help with a Reduce Fraction as part of a Rational Class

 
-1
  #3
Jul 14th, 2009
I just wrote this for my class:
//written by Ben Hinrichs 2009
#include <iostream.h>

class RationalNumber
{
public:
	RationalNumber()
	{
		doTheMath();	
	}
	
void doTheMath()
	{
		cout << "Enter an int: ";
		cin >> a;
		cout << "Enter a denominator: ";
		cin >> b;
		if (b <= 0)
			cout << "Enter a valid denominator\n";
		else
		{
			c = a;
			d = b;

			cout << c << "/" << d << " reduces to ";

			if(b % a == 0)
			{
				gcf = a;
				c = a / gcf;
				d = b / gcf;
				cout << c << "/" << d << endl;
			}
			else
			{
				gcf = 0;
				do
				{
					modul = b % a;
					gcf = a;
					b = a;
					a = modul;
				} while(modul != 0);
				c = c / gcf;
				d = d / gcf;
				cout << c << "/" << d << endl;
			}
			
		}
	}
private:
        int a, b, c, d, modul, gcf;
};



int main()
{	
	RationalNumber Ponies;

	return 0;
}
Last edited by Xantox; Jul 14th, 2009 at 12:53 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC