a program about fractions

Reply

Join Date: Apr 2004
Posts: 40
Reputation: cybergirl is an unknown quantity at this point 
Solved Threads: 0
cybergirl's Avatar
cybergirl cybergirl is offline Offline
Light Poster

a program about fractions

 
1
  #1
May 27th, 2004
Hey guys,
I was wondering if you could help me fix my program. I'm witing a program in C++ that adds, subtracts, multiplies and divides fractions. I'm suppose to put in the cosntructor that the denominator cannot be 0 or a negative number and i also I have to reduce the fraction. I have no problem adding, subtracting , etc. When i run it saids that there's no appropriate default constructor available for x, a, and b. Well take a look at my code so, you can see what i mean.

  1. #include <iostream.h>
  2. #include <math.h>
  3. class RationalNumbers
  4. {
  5. public:
  6. RationalNumbers(double, double);
  7. RationalNumbers operator+(const RationalNumbers &) const;
  8. RationalNumbers operator-(const RationalNumbers &) const;
  9. RationalNumbers operator*(const RationalNumbers &) const;
  10. RationalNumbers operator/(const RationalNumbers &) const;
  11.  
  12. friend ostream &operator<<(ostream&, const RationalNumbers &);
  13. friend istream &operator>>(istream&, RationalNumbers & );
  14. private:
  15. double numerator;
  16. double denominator;
  17. };
  18.  
  19. // Constructor
  20. RationalNumbers::RationalNumbers( double num, double den )
  21. {
  22. num=numerator;
  23. if (den>0)
  24. {den=denominator;}
  25.  
  26.  
  27.  
  28. }
  29. //void RationalNumbers::reduce()
  30. //{}
  31. // addition
  32. RationalNumbers RationalNumbers::operator+( const RationalNumbers &part2 ) const
  33. {
  34. return RationalNumbers( numerator*part2.denominator+part2.numerator*denominator,
  35. denominator*part2.denominator );
  36. }
  37. // subtraction
  38. RationalNumbers RationalNumbers::operator-( const RationalNumbers &part2 ) const
  39. {
  40. return RationalNumbers( numerator*part2.denominator-part2.numerator*denominator,
  41. denominator*part2.denominator );
  42. }
  43. //multiplication
  44. RationalNumbers RationalNumbers::operator *( const RationalNumbers &part2 ) const
  45. {
  46. return RationalNumbers(numerator*part2.numerator, denominator*part2.denominator);
  47. }
  48. //Division
  49. RationalNumbers RationalNumbers::operator /(const RationalNumbers &part2) const
  50. {
  51. return RationalNumbers(numerator*part2.denominator, denominator*part2.numerator);
  52. }
  53.  
  54.  
  55. istream &operator>>(istream &in, RationalNumbers &part2)
  56. {
  57. in>>part2.numerator;
  58. in.ignore();
  59. in>>part2.denominator ;
  60.  
  61. return in;
  62. }
  63. // show form: (a, b)
  64. ostream &operator <<(ostream &out, const RationalNumbers &part2)
  65. {out<<part2.numerator<<"/"<<part2.denominator <<endl;
  66. return out;
  67. }
  68.  
  69. int main()
  70. {
  71. RationalNumbers x;
  72. RationalNumbers a;
  73. RationalNumbers b;
  74.  
  75. cout<<"Enter 2 integers for a:\n";
  76. cin>>a;
  77.  
  78. cout<<"\nEnter 2 integers for b:\n";
  79. cin>>b;
  80.  
  81.  
  82. cout<<"x = a + b: ";
  83. x=a+b;
  84. cout<<x;
  85.  
  86. cout<<"x = a - b: ";
  87. x=a-b;
  88. cout<<x;
  89. cout<<"x = a*b: ";
  90. x=a*b;
  91. cout<<x;
  92.  
  93. cout<<"x=a/b: ";
  94. x=a/b;
  95. cout<<x;
  96.  
  97.  
  98.  
  99. return 0;
  100. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,316
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 229
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: a program about fractions

 
0
  #2
May 28th, 2004
Your only constructor takes two parameters.
  1. // Constructor
  2. RationalNumbers::RationalNumbers( double num, double den )
  3. {
  4. num=numerator;
  5. if (den>0)
  6. {den=denominator;}
  7. }
But you instantiate some objects without supplying parameters.
  1. RationalNumbers x;
  2. RationalNumbers a;
  3. RationalNumbers b;
1. You need to define a constructor that takes no parameters.
  1. // Constructor
  2. RationalNumbers::RationalNumbers( )
  3. : numerator(0.0), denominator(1.0) {}
2. Or supply defaults for your constructor that takes two parameters.
  1. // Constructor
  2. RationalNumbers::RationalNumbers( double num = 0.0, double den = 1.0 )
  3. : numerator(num), denominator(den) {}
3. Or instantiate your objects using your existing constructor.
  1. RationalNumbers x(0.0,1.0);
  2. RationalNumbers a(0.0,1.0);
  3. RationalNumbers b(0.0,1.0);
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 40
Reputation: cybergirl is an unknown quantity at this point 
Solved Threads: 0
cybergirl's Avatar
cybergirl cybergirl is offline Offline
Light Poster

Re: a program about fractions

 
0
  #3
May 28th, 2004
Thank you, The Other Dave.

I fixed my problem. Now is running perfectly. Now my program has a constructor that prevents a 0 denominator in a fraction and reduces fractions that are not in reduce form and avoids negative numbers. I overloaded the addition, multiplication, subtraction and division operators and the relational and equality operators too. Here is how it looks now.
Feel free to give any comments.

  1. #include <iostream.h>
  2. #include <math.h>
  3. class RationalNumbers
  4. {
  5. public:
  6. RationalNumbers(double=0.0, double=1.0);
  7. RationalNumbers operator+(const RationalNumbers &) const;
  8. RationalNumbers operator-(const RationalNumbers &) const;
  9. RationalNumbers operator*(const RationalNumbers &) const;
  10. RationalNumbers operator/(const RationalNumbers &) const;
  11. bool operator<(const RationalNumbers &) const;
  12. bool operator>(const RationalNumbers &) const;
  13. bool operator<=(const RationalNumbers &) const;
  14. bool operator>=(const RationalNumbers &) const;
  15. bool operator==(const RationalNumbers &) const;
  16. bool operator!=(const RationalNumbers &) const;
  17. friend ostream &operator<<(ostream&, const RationalNumbers &);
  18. friend istream &operator>>(istream&, RationalNumbers & );
  19.  
  20. private:
  21. double numerator;
  22. double denominator;
  23. };
  24.  
  25. // Constructor
  26. RationalNumbers::RationalNumbers( double num, double den )
  27. : numerator( num ), denominator(den)
  28.  
  29. { num=numerator;
  30. if (denominator>0)
  31. den=denominator;
  32. else cout<<"Error Try again"<<endl;
  33. int i;
  34. for(i=2; i<=numerator; ++i) {
  35. if( ((numerator/i) == ((int)(numerator/i))) && ((denominator/i) == ((int)(denominator/i))) ) {
  36. numerator /= i;
  37. denominator /= i;
  38. --i;
  39. }
  40. }
  41. }
  42.  
  43.  
  44. // addition
  45. RationalNumbers RationalNumbers::operator+( const RationalNumbers &part2 ) const
  46. {
  47. return RationalNumbers( numerator*part2.denominator+part2.numerator*denominator,
  48. denominator*part2.denominator );
  49. }
  50. // subtraction
  51. RationalNumbers RationalNumbers::operator-( const RationalNumbers &part2 ) const
  52. {
  53. return RationalNumbers( numerator*part2.denominator-part2.numerator*denominator,
  54. denominator*part2.denominator );
  55. }
  56. //multiplication
  57. RationalNumbers RationalNumbers::operator *( const RationalNumbers &part2 ) const
  58. {
  59. return RationalNumbers(numerator*part2.numerator, denominator*part2.denominator);
  60. }
  61. //Division
  62. RationalNumbers RationalNumbers::operator /(const RationalNumbers &part2) const
  63. {
  64. return RationalNumbers(numerator*part2.denominator, denominator*part2.numerator);
  65. }
  66. //a is less than b
  67. bool RationalNumbers::operator <(const RationalNumbers &part2) const
  68. {double temp, temp2;
  69. temp=numerator*part2.denominator;
  70. temp2=part2.numerator*denominator;
  71. if (temp < temp2)
  72. return true;
  73. else
  74. return false;
  75. }
  76. //a is greater than b
  77. bool RationalNumbers::operator >(const RationalNumbers &part2) const
  78. {double temp, temp2;
  79. temp=numerator*part2.denominator;
  80. temp2=part2.numerator*denominator;
  81. if(temp > temp2)
  82. return true;
  83. else
  84. return false;
  85. }
  86. //a is less or equal to b
  87. bool RationalNumbers::operator <=(const RationalNumbers &part2) const
  88. {double temp, temp2;
  89. temp=numerator*part2.denominator;
  90. temp2=part2.numerator*denominator;
  91. if(temp <= temp2)
  92. return true;
  93. else
  94. return false;
  95. }
  96. bool RationalNumbers::operator >=(const RationalNumbers &part2) const
  97. {double temp, temp2;
  98. temp=numerator*part2.denominator;
  99. temp2=part2.numerator*denominator;
  100. if(temp >= temp2)
  101. return true;
  102. else
  103. return false;
  104. }
  105. // a is equal to b
  106. bool RationalNumbers::operator ==(const RationalNumbers &part2) const
  107. {
  108. if(numerator == part2.numerator && denominator==part2.denominator)
  109. return true;
  110. else
  111. return false;
  112. }
  113. //a not equal to b
  114. bool RationalNumbers::operator !=(const RationalNumbers &part2) const
  115. {
  116. if(numerator == part2.numerator && denominator==part2.denominator)
  117. return false;
  118. else
  119. return true;
  120. }
  121. istream &operator>>(istream &in, RationalNumbers &part2)
  122. {
  123. in>>part2.numerator;
  124. in.ignore();
  125. in>>part2.denominator ;
  126.  
  127. return in;
  128. }
  129. // show form: (a, b)
  130. ostream &operator <<(ostream &out, const RationalNumbers &part2)
  131. {out<<part2.numerator<<"/"<<part2.denominator <<endl;
  132. return out;
  133. }
  134.  
  135. int main()
  136. {
  137.  
  138. RationalNumbers x;
  139. RationalNumbers a;
  140. RationalNumbers b;
  141.  
  142. cout<<"Enter a fraction for a:\n";
  143. cin>>a;
  144.  
  145. cout<<"\nEnter a fraction for b:\n";
  146. cin>>b;
  147.  
  148. cout<<"x = a + b: ";
  149. x=a+b;
  150. cout<<x;
  151.  
  152. cout<<"x = a - b: ";
  153. x=a-b;
  154. cout<<x;
  155. cout<<"x = a*b: ";
  156. x=a*b;
  157. cout<<x;
  158.  
  159. cout<<"x=a/b: ";
  160. x=a/b;
  161. cout<<x;
  162.  
  163. a<b;
  164. if (a<b)
  165. cout<<"a<b"<<endl;
  166. a>b;
  167. if(a>b)
  168. cout<<"a>b"<<endl;
  169.  
  170. a<=b;
  171. if(a<=b)
  172. cout<<"a<=b"<<endl;
  173. a>=b;
  174. if(a>=b)
  175. cout<<"a>=b"<<endl;
  176. a==b;
  177. if(a==b)
  178. cout<<"a equals b"<<endl;
  179. a!=b;
  180. if (a!=b)
  181. cout<<"a not equal to b"<<endl<<endl;
  182. return 0;
  183. }

I wanted to put a while loop so the program keeps asking the user to enter another fraction but i didn't know how to stop the loop. I wanted to do something like this:

  1. int main()
  2. {
  3.  
  4. RationalNumbers x;
  5. RationalNumbers a;
  6. RationalNumbers b;
  7. while(1){ // loop I don't know how to stop it
  8. cout<<"Enter a fraction for a:\n";
  9. cin>>a;
  10.  
  11. cout<<"\nEnter a fraction for b:\n";
  12. cin>>b;
  13. if(?) exit(0); // I don't know what to put between the parentesis
  14. cout<<"x = a + b: ";
  15. x=a+b;
  16. cout<<x;
  17. blah, blah, blah...
  18. ........
  19. a==b;
  20. if(a==b)
  21. cout<<"a equals b"<<endl;
  22. a!=b;
  23. if (a!=b)
  24. cout<<"a not equal to b"<<endl<<endl;
  25. }
  26. return 0;
  27. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,316
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 229
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: a program about fractions

 
0
  #4
May 28th, 2004
>I wanted to put a while loop so the program keeps asking the user to enter another fraction but i didn't know how to stop the loop.

Here is one way.
  1. int main()
  2. {
  3. char c;
  4. do
  5. {
  6. /* ... */
  7. cout << "Continue? (y/n) ";
  8. cin >> c;
  9. } while ( c == 'y' );
  10. return 0;
  11. }
Something you should know about checking for floating point equality:
http://www.eskimo.com/~scs/C-faq/q14.5.html

You're not using anything from <math.h>

The linter also tells me this.
  1. [Info 1739] Binary operator 'RationalNumbers::operator+(const RationalNumbers &) const' should be non-member function
  2. [Info 1754] Expected symbol 'operator+=' to be declared for class 'RationalNumbers'
  3. [Info 1739] Binary operator 'RationalNumbers::operator-(const RationalNumbers &) const' should be non-member function
  4. [Info 1754] Expected symbol 'operator-=' to be declared for class 'RationalNumbers'
  5. [Info 1739] Binary operator 'RationalNumbers::operator*(const RationalNumbers &) const' should be non-member function
  6. [Info 1754] Expected symbol 'operator*=' to be declared for class 'RationalNumbers'
  7. [Info 1739] Binary operator 'RationalNumbers::operator/(const RationalNumbers &) const' should be non-member function
  8. [Info 1754] Expected symbol 'operator/=' to be declared for class 'RationalNumbers'
  9. [Info 1739] Binary operator 'RationalNumbers::operator<(const RationalNumbers &) const' should be non-member function
  10. [Info 1739] Binary operator 'RationalNumbers::operator>(const RationalNumbers &) const' should be non-member function
  11. [Info 1739] Binary operator 'RationalNumbers::operator<=(const RationalNumbers &) const' should be non-member function
  12. [Info 1739] Binary operator 'RationalNumbers::operator>=(const RationalNumbers &) const' should be non-member function
  13. [Info 1739] Binary operator 'RationalNumbers::operator==(const RationalNumbers &) const' should be non-member function
  14. [Info 1739] Binary operator 'RationalNumbers::operator!=(const RationalNumbers &) const' should be non-member function
I'm not exactly sure what it is trying to tell me, though. Here are the explanations of the diagnostic messages.
1739 Binary operator 'Symbol' should be non-member function -- The indicated function was declared as a member function. There were a number of indicators to suggest that it should have been a non-member function. The class, X of which it was a member has a constructor that could be used to convert numeric values to X. The parameter to the operator was X or its equivalent. For this reason the operator would behave unsymmetrically. A numeric value on the right hand side would be promoted but not a value on the left hand side. For example, X op 27 would work but 27 op X would not. [12, Item 19].

1754 Expected symbol 'Symbol' to be declared for class 'Symbol' -- The first Symbol is of the form: operator op= where op is a binary operator. A binary operator op was declared for type X where X is identified by the second Symbol. For example, the appearance of:

X operator+( const X &, const X & );

somewhere in the program would suggest that a += version appear as a member function of class X. This is not only to fulfill reasonable expectations on the part of the programmer but also because operator+= is likely to be more efficient than operator+ and because operator+ can be written in terms of operator+=. [23, Item 22]

The message is also given for member binary operators. In all cases the message is not given unless the return value matches the first argument (this is the implicit argument in the case of a member function).
Perhaps this may mean more to you.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,316
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 229
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: a program about fractions

 
0
  #5
May 28th, 2004
I've gotten to like a particular style, and I'm not pushing it here. Style can make code easier to read and thus easier to debug. Just for the sake of comparison, here is much the same code done with shorter names and more whitespace between operators.
  1. #include <iostream>
  2. using std::cin;
  3. using std::cout;
  4. using std::endl;
  5. using std::istream;
  6. using std::ostream;
  7.  
  8. class rational
  9. {
  10. double num;
  11. double den;
  12. public:
  13. // Constructor
  14. rational::rational( double n = 0.0, double d = 1.0 ) : num( n ), den( d )
  15. {
  16. if ( den > 0 )
  17. {
  18. d = den;
  19. }
  20. else
  21. {
  22. cout << "Error Try again" << endl;
  23. }
  24. for ( int i = 2; i <= num; ++i )
  25. {
  26. if ( ((num/i) == ((int)(num/i))) && ((den/i) == ((int)(den/i))) )
  27. {
  28. num /= i;
  29. den /= i;
  30. --i;
  31. }
  32. }
  33. }
  34. // addition
  35. rational rational::operator+ ( const rational &rhs )
  36. {
  37. return rational( num * rhs.den + rhs.num * den, den * rhs.den );
  38. }
  39. // subtraction
  40. rational rational::operator- ( const rational &rhs ) const
  41. {
  42. return rational( num * rhs.den - rhs.num * den, den * rhs.den );
  43. }
  44. // multiplication
  45. rational rational::operator* ( const rational &rhs ) const
  46. {
  47. return rational(num * rhs.num, den * rhs.den);
  48. }
  49. // division
  50. rational rational::operator/ ( const rational &rhs ) const
  51. {
  52. return rational(num * rhs.den, den * rhs.num);
  53. }
  54. // less than
  55. bool rational::operator< ( const rational &rhs ) const
  56. {
  57. return( num * rhs.den < rhs.num * den );
  58. }
  59. // greater than
  60. bool rational::operator> ( const rational &rhs ) const
  61. {
  62. return( num * rhs.den > rhs.num * den );
  63. }
  64. // less or equal to
  65. bool rational::operator<= ( const rational &rhs ) const
  66. {
  67. return( num * rhs.den <= rhs.num * den );
  68. }
  69. // greater than or equal to
  70. bool rational::operator>= ( const rational &rhs ) const
  71. {
  72. return( num * rhs.den >= rhs.num * den );
  73. }
  74. // equality
  75. bool rational::operator== ( const rational &rhs ) const
  76. {
  77. return num == rhs.num && den==rhs.den;
  78. }
  79. // inequality
  80. bool rational::operator!= ( const rational &rhs ) const
  81. {
  82. return !( num == rhs.num && den==rhs.den );
  83. }
  84. // extraction
  85. friend ostream &operator<< ( ostream&, const rational& );
  86. // insertion
  87. friend istream &operator>> ( istream&, rational& );
  88. };
  89.  
  90. istream &operator>> ( istream &in, rational &rhs )
  91. {
  92. return in >> rhs.num >> rhs.den;
  93. }
  94.  
  95. // show form: (a, b)
  96. ostream &operator<< ( ostream &out, const rational &rhs )
  97. {
  98. return out << rhs.num << "/" << rhs.den;
  99. }
  100.  
  101. int main()
  102. {
  103. rational x, a, b;
  104. char c;
  105. do
  106. {
  107. cout << "Enter a fraction for a: ";
  108. cin >> a;
  109.  
  110. cout << "Enter a fraction for b: ";
  111. cin >> b;
  112.  
  113. x = a + b; cout << "x = a + b: " << x << endl;
  114. x = a - b; cout << "x = a - b: " << x << endl;
  115. x = a * b; cout << "x = a * b: " << x << endl;
  116. x = a / b; cout << "x = a / b: " << x << endl;
  117.  
  118. cout << "Continue? (y/n) ";
  119. cin >> c;
  120. } while ( c == 'y' );
  121. return 0;
  122. }
  123.  
  124. /* my output
  125. Enter a fraction for a: 1 2
  126. Enter a fraction for b: 1 3
  127. x = a + b: 5/6
  128. x = a - b: 1/6
  129. x = a * b: 1/6
  130. x = a / b: 3/2
  131. Continue? (y/n) y
  132. Enter a fraction for a: 1 4
  133. Enter a fraction for b: 1 5
  134. x = a + b: 9/20
  135. x = a - b: 1/20
  136. x = a * b: 1/20
  137. x = a / b: 5/4
  138. Continue? (y/n) n
  139. */
I also shortened a few of the functions. And I moved the endl out of the extraction operator.

But notice in your constructor how you are using initializer syntax to assign to the denominator, then in the body you check for greater than zero to see whether to assign it -- it may have already been assigned. Also, how do you "Try again" on a constructor?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 40
Reputation: cybergirl is an unknown quantity at this point 
Solved Threads: 0
cybergirl's Avatar
cybergirl cybergirl is offline Offline
Light Poster

Re: a program about fractions

 
0
  #6
Jun 1st, 2004
oops, I forgot to erase num( n ), den( d ) when i made the restrictions for the denominator. I didn't notice since it didn't give me a error message. Thanks for pointing that out.


The Other Dave, your program doesn't read a fraction, it only reads two intergers.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,316
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 229
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: a program about fractions

 
0
  #7
Jun 1st, 2004
>The Other Dave, your program doesn't read a fraction, it only reads two intergers.

Sure it does. I just chose to use whitespace as the delimiter instead of a '/'. Add in more code to ignore the '/' if you wish.
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



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC